diff --git a/fury/actor.py b/fury/actor.py index 613d0f295..fb5678429 100644 --- a/fury/actor.py +++ b/fury/actor.py @@ -257,20 +257,20 @@ def cylinder( colors : ndarray (N,3) or (N, 4) or tuple (3,) or tuple (4,), optional RGB or RGBA (for opacity) R, G, B and A should be at the range [0, 1]. height: float, optional - The height of the cylinder. Default is 1. + The height of the cylinder. sectors: int, optional The number of divisions around the cylinder's circumference . - Higher values produce smoother cylinders. Default is 36. + Higher values produce smoother cylinders. radii : float or ndarray (N,) or tuple, optional The radius of the base of the cylinders, single value applies to all cylinders, - while an array specifies a radius for each cylinder individually. Default:0.5. - scales : int or ndarray (N, 3) or tuple (3,), optional - Scaling factors for the cylinders in the (x, y, z) dimensions. - Default is uniform scaling (1, 1, 1). + while an array specifies a radius for each cylinder individually. + scales : int or ndarray (N,3) or tuple (3,), optional + The size of the square in each dimension. If a single value is provided, + the same size will be used for all cylinders. directions : ndarray, shape (N, 3), optional The orientation vector of the cylinder. capped : bool, optional - Whether to add caps (circular ends) to the cylinders. Default is True. + Whether to add caps (circular ends) to the cylinders. opacity : float, optional Takes values from 0 (fully transparent) to 1 (opaque). If both `opacity` and RGBA are provided, the final alpha will be: @@ -441,3 +441,689 @@ def frustum( material=material, enable_picking=enable_picking, ) + + +def tetrahedron( + centers, + *, + directions=(0, 0, 0), + colors=(1, 1, 1), + scales=(1, 1, 1), + opacity=None, + material="phong", + enable_picking=True, +): + """Visualize one or many tetrahedrons with different features. + + Parameters + ---------- + centers : ndarray, shape (N, 3) + tetrahedron positions. + directions : ndarray, shape (N, 3), optional + The orientation vector of the tetrahedron. + colors : ndarray (N,3) or (N, 4) or tuple (3,) or tuple (4,), optional + RGB or RGBA (for opacity) R, G, B and A should be at the range [0, 1]. + scales : int or ndarray (N,3) or tuple (3,), optional + The size of the tetrahedron in each dimension. If a single value is provided, + the same size will be used for all tetrahedrons. + opacity : float, optional + Takes values from 0 (fully transparent) to 1 (opaque). + If both `opacity` and RGBA are provided, the final alpha will be: + final_alpha = alpha_in_RGBA * opacity + material : str, optional + The material type for the tetrahedrons. Options are 'phong' and 'basic'. + enable_picking : bool, optional + Whether the tetrahedrons should be pickable in a 3D scene. + + Returns + ------- + mesh_actor : Actor + A mesh actor containing the generated tetrahedrons, with the specified + material and properties. + + Examples + -------- + >>> from fury import window, actor + >>> import numpy as np + >>> scene = window.Scene() + >>> centers = np.random.rand(5, 3) * 10 + >>> colors = np.random.rand(5, 3) + >>> tetrahedron_actor = actor.tetrahedron(centers=centers, colors=colors) + >>> scene.add(tetrahedron_actor) + >>> show_manager = window.ShowManager(scene=scene, size=(600, 600)) + >>> show_manager.start() + """ + vertices, faces = fp.prim_tetrahedron() + return actor_from_primitive( + vertices, + faces, + centers=centers, + colors=colors, + scales=scales, + directions=directions, + opacity=opacity, + material=material, + enable_picking=enable_picking, + ) + + +def icosahedron( + centers, + *, + directions=(0, 0, 0), + colors=(1, 1, 1), + scales=(1, 1, 1), + opacity=None, + material="phong", + enable_picking=True, +): + """Visualize one or many icosahedrons with different features. + + Parameters + ---------- + centers : ndarray, shape (N, 3) + icosahedron positions. + directions : ndarray, shape (N, 3), optional + The orientation vector of the icosahedron. + colors : ndarray (N,3) or (N, 4) or tuple (3,) or tuple (4,), optional + RGB or RGBA (for opacity) R, G, B and A should be at the range [0, 1]. + scales : int or ndarray (N,3) or tuple (3,), optional + The size of the icosahedron in each dimension. If a single value is provided, + the same size will be used for all icosahedrons. + opacity : float, optional + Takes values from 0 (fully transparent) to 1 (opaque). + If both `opacity` and RGBA are provided, the final alpha will be: + final_alpha = alpha_in_RGBA * opacity + material : str, optional + The material type for the icosahedrons. Options are 'phong' and 'basic'. + enable_picking : bool, optional + Whether the icosahedrons should be pickable in a 3D scene. + + Returns + ------- + mesh_actor : Actor + A mesh actor containing the generated icosahedrons, with the specified + material and properties. + + Examples + -------- + >>> from fury import window, actor + >>> import numpy as np + >>> scene = window.Scene() + >>> centers = np.random.rand(5, 3) * 10 + >>> colors = np.random.rand(5, 3) + >>> icosahedron_actor = actor.icosahedron(centers=centers, colors=colors) + >>> scene.add(icosahedron_actor) + >>> show_manager = window.ShowManager(scene=scene, size=(600, 600)) + >>> show_manager.start() + """ + vertices, faces = fp.prim_icosahedron() + return actor_from_primitive( + vertices, + faces, + centers=centers, + colors=colors, + scales=scales, + directions=directions, + opacity=opacity, + material=material, + enable_picking=enable_picking, + ) + + +def rhombicuboctahedron( + centers, + *, + directions=(0, 0, 0), + colors=(1, 1, 1), + scales=(1, 1, 1), + opacity=None, + material="phong", + enable_picking=True, +): + """Visualize one or many rhombicuboctahedrons with different features. + + Parameters + ---------- + centers : ndarray, shape (N, 3) + rhombicuboctahedron positions. + directions : ndarray, shape (N, 3), optional + The orientation vector of the rhombicuboctahedron. + colors : ndarray (N,3) or (N, 4) or tuple (3,) or tuple (4,), optional + RGB or RGBA (for opacity) R, G, B and A should be at the range [0, 1]. + scales : int or ndarray (N,3) or tuple (3,), optional + The size of the rhombicuboctahedro in each dimension. If a single value is + provided, the same size will be used for all rhombicuboctahedrons. + opacity : float, optional + Takes values from 0 (fully transparent) to 1 (opaque). + If both `opacity` and RGBA are provided, the final alpha will be: + final_alpha = alpha_in_RGBA * opacity + material : str, optional + The material type for the rhombicuboctahedrons. Options are 'phong' and 'basic'. + enable_picking : bool, optional + Whether the rhombicuboctahedrons should be pickable in a 3D scene. + + Returns + ------- + mesh_actor : Actor + A mesh actor containing the generated rhombicuboctahedrons, with the specified + material and properties. + + Examples + -------- + >>> from fury import window, actor + >>> import numpy as np + >>> scene = window.Scene() + >>> centers = np.random.rand(5, 3) * 10 + >>> colors = np.random.rand(5, 3) + >>> rhombicuboctahedron_actor = actor.rhombicuboctahedron( + centers=centers, colors=colors + ) + >>> scene.add(rhombicuboctahedron_actor) + >>> show_manager = window.ShowManager(scene=scene, size=(600, 600)) + >>> show_manager.start() + """ + vertices, faces = fp.prim_rhombicuboctahedron() + return actor_from_primitive( + vertices, + faces, + centers=centers, + colors=colors, + scales=scales, + directions=directions, + opacity=opacity, + material=material, + enable_picking=enable_picking, + ) + + +def triangularprism( + centers, + *, + directions=(0, 0, 0), + colors=(1, 1, 1), + scales=(1, 1, 1), + opacity=None, + material="phong", + enable_picking=True, +): + """Visualize one or many triangular prism with different features. + + Parameters + ---------- + centers : ndarray, shape (N, 3) + triangular prism positions. + directions : ndarray, shape (N, 3), optional + The orientation vector of the triangular prism. + colors : ndarray (N,3) or (N, 4) or tuple (3,) or tuple (4,), optional + RGB or RGBA (for opacity) R, G, B and A should be at the range [0, 1]. + scales : int or ndarray (N,3) or tuple (3,), optional + The size of the triangular prism in each dimension. If a single value is + provided, the same size will be used for all triangular prisms. + opacity : float, optional + Takes values from 0 (fully transparent) to 1 (opaque). + If both `opacity` and RGBA are provided, the final alpha will be: + final_alpha = alpha_in_RGBA * opacity + material : str, optional + The material type for the triangular prisms. Options are 'phong' and 'basic'. + enable_picking : bool, optional + Whether the triangular prisms should be pickable in a 3D scene. + + Returns + ------- + mesh_actor : Actor + A mesh actor containing the generated triangular prisms, with the specified + material and properties. + + Examples + -------- + >>> from fury import window, actor + >>> import numpy as np + >>> scene = window.Scene() + >>> centers = np.random.rand(5, 3) * 10 + >>> colors = np.random.rand(5, 3) + >>> triangularprism_actor = actor.triangularprism( + centers=centers, colors=colors) + >>> scene.add(triangularprism_actor) + >>> show_manager = window.ShowManager(scene=scene, size=(600, 600)) + >>> show_manager.start() + """ + vertices, faces = fp.prim_triangularprism() + return actor_from_primitive( + vertices, + faces, + centers=centers, + colors=colors, + scales=scales, + directions=directions, + opacity=opacity, + material=material, + enable_picking=enable_picking, + ) + + +def pentagonalprism( + centers, + *, + directions=(0, 0, 0), + colors=(1, 1, 1), + scales=(1, 1, 1), + opacity=None, + material="phong", + enable_picking=True, +): + """Visualize one or many pentagonal prism with different features. + + Parameters + ---------- + centers : ndarray, shape (N, 3) + pentagonal prism positions. + directions : ndarray, shape (N, 3), optional + The orientation vector of the pentagonal prism. + colors : ndarray (N,3) or (N, 4) or tuple (3,) or tuple (4,), optional + RGB or RGBA (for opacity) R, G, B and A should be at the range [0, 1]. + scales : int or ndarray (N,3) or tuple (3,), optional + The size of the pentagonal prism in each dimension. If a single value is + provided, the same size will be used for all pentagonal prisms. + opacity : float, optional + Takes values from 0 (fully transparent) to 1 (opaque). + If both `opacity` and RGBA are provided, the final alpha will be: + final_alpha = alpha_in_RGBA * opacity + material : str, optional + The material type for the pentagonal prisms. Options are 'phong' and 'basic'. + enable_picking : bool, optional + Whether the pentagonal prisms should be pickable in a 3D scene. + + Returns + ------- + mesh_actor : Actor + A mesh actor containing the generated pentagonal prism, with the specified + material and properties. + + Examples + -------- + >>> from fury import window, actor + >>> import numpy as np + >>> scene = window.Scene() + >>> centers = np.random.rand(5, 3) * 10 + >>> colors = np.random.rand(5, 3) + >>> pentagonalprism_actor = actor.pentagonalprism( + centers=centers, colors=colors) + >>> scene.add(pentagonalprism_actor) + >>> show_manager = window.ShowManager(scene=scene, size=(600, 600)) + >>> show_manager.start() + """ + vertices, faces = fp.prim_pentagonalprism() + return actor_from_primitive( + vertices, + faces, + centers=centers, + colors=colors, + scales=scales, + directions=directions, + opacity=opacity, + material=material, + enable_picking=enable_picking, + ) + + +def octagonalprism( + centers, + *, + directions=(0, 0, 0), + colors=(1, 1, 1), + scales=(1, 1, 1), + opacity=None, + material="phong", + enable_picking=True, +): + """Visualize one or many octagonal prism with different features. + + Parameters + ---------- + centers : ndarray, shape (N, 3) + octagonal prism positions. + directions : ndarray, shape (N, 3), optional + The orientation vector of the octagonal prism. + colors : ndarray (N,3) or (N, 4) or tuple (3,) or tuple (4,), optional + RGB or RGBA (for opacity) R, G, B and A should be at the range [0, 1]. + scales : int or ndarray (N,3) or tuple (3,), optional + The size of the octagonal prism in each dimension. If a single value is + provided, the same size will be used for all octagonal prisms. + opacity : float, optional + Takes values from 0 (fully transparent) to 1 (opaque). + If both `opacity` and RGBA are provided, the final alpha will be: + final_alpha = alpha_in_RGBA * opacity + material : str, optional + The material type for the octagonal prisms. Options are 'phong' and 'basic'. + enable_picking : bool, optional + Whether the octagonal prisms should be pickable in a 3D scene. + + Returns + ------- + mesh_actor : Actor + A mesh actor containing the generated octagonal prisms, with the specified + material and properties. + + Examples + -------- + >>> from fury import window, actor + >>> import numpy as np + >>> scene = window.Scene() + >>> centers = np.random.rand(5, 3) * 10 + >>> colors = np.random.rand(5, 3) + >>> octagonalprism_actor = actor.octagonalprism( + centers=centers, colors=colors) + >>> scene.add(octagonalprism_actor) + >>> show_manager = window.ShowManager(scene=scene, size=(600, 600)) + >>> show_manager.start() + """ + vertices, faces = fp.prim_octagonalprism() + return actor_from_primitive( + vertices, + faces, + centers=centers, + colors=colors, + scales=scales, + directions=directions, + opacity=opacity, + material=material, + enable_picking=enable_picking, + ) + + +def arrow( + centers, + *, + directions=(0, 0, 0), + colors=(1, 1, 1), + height=1.0, + resolution=10, + tip_length=0.35, + tip_radius=0.1, + shaft_radius=0.03, + scales=(1, 1, 1), + opacity=None, + material="phong", + enable_picking=True, +): + """Visualize one or many arrows with different features. + + Parameters + ---------- + centers : ndarray, shape (N, 3) + arrow positions. + directions : ndarray, shape (N, 3), optional + The orientation vector of the arrow. + colors : ndarray (N,3) or (N, 4) or tuple (3,) or tuple (4,), optional + RGB or RGBA (for opacity) R, G, B and A should be at the range [0, 1]. + height : float, optional + The total height of the arrow, including the shaft and tip. + resolution : int, optional + The number of divisions along the arrow's circular cross-sections. + Higher values produce smoother arrows. + tip_length : float, optional + The length of the arrowhead tip relative to the total height. + tip_radius : float, optional + The radius of the arrowhead tip. + shaft_radius : float, optional + The radius of the arrow shaft. + scales : int or ndarray (N,3) or tuple (3,), optional + The size of the arrow in each dimension. If a single value is + provided, the same size will be used for all arrows. + opacity : float, optional + Takes values from 0 (fully transparent) to 1 (opaque). + If both `opacity` and RGBA are provided, the final alpha will be: + final_alpha = alpha_in_RGBA * opacity + material : str, optional + The material type for the arrows. Options are 'phong' and 'basic'. + enable_picking : bool, optional + Whether the arrows should be pickable in a 3D scene. + + Returns + ------- + mesh_actor : Actor + A mesh actor containing the generated arrows, with the specified + material and properties. + + Examples + -------- + >>> from fury import window, actor + >>> import numpy as np + >>> scene = window.Scene() + >>> centers = np.random.rand(5, 3) * 10 + >>> colors = np.random.rand(5, 3) + >>> arrow_actor = actor.arrow(centers=centers, colors=colors) + >>> scene.add(arrow_actor) + >>> show_manager = window.ShowManager(scene=scene, size=(600, 600)) + >>> show_manager.start() + """ + + vertices, faces = fp.prim_arrow( + height=height, + resolution=resolution, + tip_length=tip_length, + tip_radius=tip_radius, + shaft_radius=shaft_radius, + ) + return actor_from_primitive( + vertices, + faces, + centers=centers, + colors=colors, + scales=scales, + directions=directions, + opacity=opacity, + material=material, + enable_picking=enable_picking, + ) + + +def superquadric( + centers, + *, + directions=(0, 0, 0), + roundness=(1, 1), + colors=(1, 1, 1), + scales=(1, 1, 1), + opacity=None, + material="phong", + enable_picking=True, +): + """Visualize one or many superquadric with different features. + + Parameters + ---------- + centers : ndarray, shape (N, 3) + superquadric positions. + directions : ndarray, shape (N, 3), optional + The orientation vector of the superquadric. + roundness : tuple, optional + parameters (Phi and Theta) that control the shape of the superquadric. + colors : ndarray (N,3) or (N, 4) or tuple (3,) or tuple (4,), optional + RGB or RGBA (for opacity) R, G, B and A should be at the range [0, 1]. + scales : int or ndarray (N,3) or tuple (3,), optional + The size of the superquadric in each dimension. If a single value is + provided, the same size will be used for all superquadrics. + opacity : float, optional + Takes values from 0 (fully transparent) to 1 (opaque). + If both `opacity` and RGBA are provided, the final alpha will be: + final_alpha = alpha_in_RGBA * opacity + material : str, optional + The material type for the superquadrics. Options are 'phong' and 'basic'. + enable_picking : bool, optional + Whether the superquadrics should be pickable in a 3D scene. + + Returns + ------- + mesh_actor : Actor + A mesh actor containing the generated superquadrics, with the specified + material and properties. + + Examples + -------- + >>> from fury import window, actor + >>> import numpy as np + >>> scene = window.Scene() + >>> centers = np.random.rand(5, 3) * 10 + >>> colors = np.random.rand(5, 3) + >>> superquadric_actor = actor.superquadric(centers=centers, colors=colors) + >>> scene.add(superquadric_actor) + >>> show_manager = window.ShowManager(scene=scene, size=(600, 600)) + >>> show_manager.start() + """ + vertices, faces = fp.prim_superquadric(roundness=roundness) + return actor_from_primitive( + vertices, + faces, + centers=centers, + colors=colors, + scales=scales, + directions=directions, + opacity=opacity, + material=material, + enable_picking=enable_picking, + ) + + +def cone( + centers, + *, + colors=(1, 1, 1), + height=1, + sectors=10, + radii=0.5, + scales=(1, 1, 1), + directions=(0, 1, 0), + opacity=None, + material="phong", + enable_picking=True, +): + """Visualize one or many cones with different features. + + Parameters + ---------- + centers : ndarray, shape (N, 3) + cone positions. + colors : ndarray (N,3) or (N, 4) or tuple (3,) or tuple (4,), optional + RGB or RGBA (for opacity) R, G, B and A should be at the range [0, 1]. + height: float, optional + The height of the cone. + sectors: int, optional + The number of divisions around the cones's circumference . + Higher values produce smoother cones. + radii : float or ndarray (N,) or tuple, optional + The radius of the base of the cones, single value applies to all cones, + while an array specifies a radius for each cone individually. + scales : int or ndarray (N,3) or tuple (3,), optional + The size of the square in each dimension. If a single value is provided, + the same size will be used for all cones. + directions : ndarray, shape (N, 3), optional + The orientation vector of the cone. + opacity : float, optional + Takes values from 0 (fully transparent) to 1 (opaque). + If both `opacity` and RGBA are provided, the final alpha will be: + final_alpha = alpha_in_RGBA * opacity + material : str, optional + The material type for the cones. Options are 'phong' and 'basic'. + enable_picking : bool, optional + Whether the cones should be pickable in a 3D scene. + + Returns + ------- + mesh_actor : Actor + A mesh actor containing the generated cones, with the specified + material and properties. + + Examples + -------- + >>> from fury import window, actor + >>> import numpy as np + >>> scene = window.Scene() + >>> centers = np.random.rand(5, 3) * 10 + >>> colors = np.random.rand(5, 3) + >>> cone_actor = actor.cone(centers=centers, colors=colors) + >>> scene.add(cone_actor) + >>> show_manager = window.ShowManager(scene=scene, size=(600, 600)) + >>> show_manager.start() + """ + + vertices, faces = fp.prim_cone(radius=radii, height=height, sectors=sectors) + return actor_from_primitive( + vertices, + faces, + centers=centers, + colors=colors, + scales=scales, + directions=directions, + opacity=opacity, + material=material, + enable_picking=enable_picking, + ) + + +def star( + centers, + *, + dim=2, + directions=(0, 0, 0), + colors=(1, 1, 1), + scales=(1, 1, 1), + opacity=None, + material="phong", + enable_picking=True, +): + """Visualize one or many star with different features. + + Parameters + ---------- + centers : ndarray, shape (N, 3) + star positions. + dim : int, optional. + The dimensionality of the star (2D or 3D). + directions : ndarray, shape (N, 3), optional + The orientation vector of the star. + colors : ndarray (N,3) or (N, 4) or tuple (3,) or tuple (4,), optional + RGB or RGBA (for opacity) R, G, B and A should be at the range [0, 1]. + scales : int or ndarray (N,3) or tuple (3,), optional + The size of the star in each dimension. If a single value is + provided, the same size will be used for all stars. + opacity : float, optional + Takes values from 0 (fully transparent) to 1 (opaque). + If both `opacity` and RGBA are provided, the final alpha will be: + final_alpha = alpha_in_RGBA * opacity + material : str, optional + The material type for the stars. Options are 'phong' and 'basic'. + enable_picking : bool, optional + Whether the stars should be pickable in a 3D scene. + + Returns + ------- + mesh_actor : Actor + A mesh actor containing the generated stars, with the specified + material and properties. + + Examples + -------- + >>> from fury import window, actor + >>> import numpy as np + >>> scene = window.Scene() + >>> centers = np.random.rand(5, 3) * 10 + >>> colors = np.random.rand(5, 3) + >>> star_actor = actor.star(centers=centers, colors=colors) + >>> scene.add(star_actor) + >>> show_manager = window.ShowManager(scene=scene, size=(600, 600)) + >>> show_manager.start() + """ + vertices, faces = fp.prim_star(dim=dim) + return actor_from_primitive( + vertices, + faces, + centers=centers, + colors=colors, + scales=scales, + directions=directions, + opacity=opacity, + material=material, + enable_picking=enable_picking, + ) diff --git a/fury/tests/test_actor.py b/fury/tests/test_actor.py index de5c0c77a..ed8057d76 100644 --- a/fury/tests/test_actor.py +++ b/fury/tests/test_actor.py @@ -5,116 +5,22 @@ from fury import actor, window -def test_sphere(): - scene = window.Scene() - centers = np.array([[0, 0, 0]]) - colors = np.array([[1, 0, 0]]) - radii = np.array([1]) - - sphere_actor = actor.sphere(centers=centers, colors=colors, radii=radii) - scene.add(sphere_actor) - - window.snapshot(scene=scene, fname="sphere_test_1.png") - - img = Image.open("sphere_test_1.png") - img_array = np.array(img) - - mean_r, mean_g, mean_b, _ = np.mean( - img_array.reshape(-1, img_array.shape[2]), axis=0 - ) - - assert mean_r > mean_b and mean_r > mean_g - assert 0 <= mean_r <= 255 and 0 <= mean_g <= 255 and 0 <= mean_b <= 255 - - npt.assert_array_equal(sphere_actor.local.position, centers[0]) - - assert sphere_actor.prim_count == 1 - - # center_pixel = img_array[img_array.shape[0] // 2, img_array.shape[1] // 2] - # npt.assert_array_equal(center_pixel[0], colors[0][0] * 255) - - phi, theta = 100, 100 - sphere_actor_2 = actor.sphere( - centers=centers, - colors=colors, - radii=radii, - opacity=1, - material="basic", - phi=phi, - theta=theta, - ) - scene.remove(sphere_actor) - scene.add(sphere_actor_2) - - window.snapshot(scene=scene, fname="sphere_test_2.png") - - img = Image.open("sphere_test_2.png") - img_array = np.array(img) - - mean_r, mean_g, mean_b, mean_a = np.mean( - img_array.reshape(-1, img_array.shape[2]), axis=0 - ) - - assert mean_r > mean_b and mean_r > mean_g - assert 0 <= mean_r <= 255 and 0 <= mean_g <= 255 and 0 <= mean_b <= 255 - assert mean_a == 255.0 - assert mean_b == 0.0 - assert mean_g == 0.0 - - vertices = sphere_actor_2.geometry.positions.view - faces = sphere_actor_2.geometry.indices.view - colors = sphere_actor_2.geometry.colors.view - - vertices_mean = np.mean(vertices, axis=0) - - npt.assert_array_almost_equal(vertices_mean, centers[0]) - - assert len(vertices) == len(colors) - - npt.assert_array_almost_equal(len(faces), (2 * phi * (theta - 2))) - - -def test_box(): +def validate_actors(centers, colors, actor_type="actor_name"): scene = window.Scene() - centers = np.array([[0, 0, 0]]) - colors = np.array([[1, 0, 0]]) - scales = np.array([[1, 1, 7]]) - - box_actor = actor.box(centers=centers, colors=colors, scales=scales) - scene.add(box_actor) + typ_actor = getattr(actor, actor_type) + get_actor = typ_actor(centers=centers, colors=colors) + scene.add(get_actor) - npt.assert_array_equal(box_actor.local.position, centers[0]) + npt.assert_array_equal(get_actor.local.position, centers[0]) - mean_vertex = np.mean(box_actor.geometry.positions.view, axis=0) + mean_vertex = np.round(np.mean(get_actor.geometry.positions.view, axis=0)) npt.assert_array_almost_equal(mean_vertex, centers[0]) - assert box_actor.prim_count == 1 - - scene.remove(box_actor) - - -def test_cylinder(): - scene = window.Scene() - centers = np.array([[0, 0, 0]]) - colors = np.array([[1, 0, 0]]) - sectors = 36 - capped = True - - cylinder_actor = actor.cylinder( - centers=centers, colors=colors, sectors=sectors, capped=capped - ) - scene.add(cylinder_actor) - - npt.assert_array_equal(cylinder_actor.local.position, centers[0]) - - mean_vertex = np.mean(cylinder_actor.geometry.positions.view, axis=0) - npt.assert_array_almost_equal(mean_vertex, centers[0], decimal=2) + assert get_actor.prim_count == 1 + fname = f"{actor_type}_test.png" + window.snapshot(scene=scene, fname=fname) - assert cylinder_actor.prim_count == 1 - - window.snapshot(scene=scene, fname="cylinder_test_1.png") - - img = Image.open("cylinder_test_1.png") + img = Image.open(fname) img_array = np.array(img) mean_r, mean_g, mean_b, mean_a = np.mean( @@ -122,23 +28,19 @@ def test_cylinder(): ) assert mean_r > mean_b and mean_r > mean_g - assert 0 < mean_r < 255 and 0 < mean_g < 255 and 0 <= mean_b < 255 middle_pixel = img_array[img_array.shape[0] // 2, img_array.shape[1] // 2] r, g, b, a = middle_pixel assert r > g and r > b assert g == b - assert r > 0 and g > 0 and b > 0 - - scene.remove(cylinder_actor) - - cylinder_actor_2 = actor.cylinder( - centers=centers, colors=colors, sectors=sectors, capped=capped, material="basic" - ) - scene.add(cylinder_actor_2) - window.snapshot(scene=scene, fname="cylinder_test_2.png") - - img = Image.open("cylinder_test_2.png") + scene.remove(get_actor) + + typ_actor_1 = getattr(actor, actor_type) + get_actor_1 = typ_actor_1(centers=centers, colors=colors, material="basic") + scene.add(get_actor_1) + fname_1 = f"{actor_type}_test_1.png" + window.snapshot(scene=scene, fname=fname_1) + img = Image.open(fname_1) img_array = np.array(img) mean_r, mean_g, mean_b, mean_a = np.mean( @@ -154,78 +56,94 @@ def test_cylinder(): assert r > g and r > b assert g == 0 and b == 0 assert r == 255 - scene.remove(cylinder_actor_2) + scene.remove(get_actor_1) -def test_square(): - scene = window.Scene() +def test_sphere(): centers = np.array([[0, 0, 0]]) colors = np.array([[1, 0, 0]]) + validate_actors(centers=centers, colors=colors, actor_type="sphere") - square_actor = actor.square(centers=centers, colors=colors) - scene.add(square_actor) - npt.assert_array_equal(square_actor.local.position, centers[0]) +def test_box(): + centers = np.array([[0, 0, 0]]) + colors = np.array([[1, 0, 0]]) + validate_actors(centers=centers, colors=colors, actor_type="box") - mean_vertex = np.mean(square_actor.geometry.positions.view, axis=0) - npt.assert_array_almost_equal(mean_vertex, centers[0]) - assert square_actor.prim_count == 1 - scene.remove(square_actor) +def test_cylinder(): + centers = np.array([[0, 0, 0]]) + colors = np.array([[1, 0, 0]]) + validate_actors(centers=centers, colors=colors, actor_type="cylinder") + + +def test_square(): + centers = np.array([[0, 0, 0]]) + colors = np.array([[1, 0, 0]]) + validate_actors(centers=centers, colors=colors, actor_type="square") def test_frustum(): - scene = window.Scene() centers = np.array([[0, 0, 0]]) colors = np.array([[1, 0, 0]]) + validate_actors(centers=centers, colors=colors, actor_type="frustum") - frustum_actor = actor.frustum(centers=centers, colors=colors) - scene.add(frustum_actor) - npt.assert_array_equal(frustum_actor.local.position, centers[0]) +def test_tetrahedron(): + centers = np.array([[0, 0, 0]]) + colors = np.array([[1, 0, 0]]) + validate_actors(centers=centers, colors=colors, actor_type="tetrahedron") - mean_vertex = np.mean(frustum_actor.geometry.positions.view, axis=0) - npt.assert_array_almost_equal(mean_vertex, centers[0]) - assert frustum_actor.prim_count == 1 +def test_icosahedron(): + centers = np.array([[0, 0, 0]]) + colors = np.array([[1, 0, 0]]) + validate_actors(centers=centers, colors=colors, actor_type="icosahedron") - window.snapshot(scene=scene, fname="frustum_test_1.png") - img = Image.open("frustum_test_1.png") - img_array = np.array(img) +def test_rhombicuboctahedron(): + centers = np.array([[0, 0, 0]]) + colors = np.array([[1, 0, 0]]) + validate_actors(centers=centers, colors=colors, actor_type="rhombicuboctahedron") - mean_r, mean_g, mean_b, mean_a = np.mean( - img_array.reshape(-1, img_array.shape[2]), axis=0 - ) - assert mean_r > mean_b and mean_r > mean_g - assert 0 < mean_r < 255 and 0 < mean_g < 255 and 0 <= mean_b < 255 +def test_triangularprism(): + centers = np.array([[0, 0, 0]]) + colors = np.array([[1, 0, 0]]) + validate_actors(centers=centers, colors=colors, actor_type="triangularprism") - middle_pixel = img_array[img_array.shape[0] // 2, img_array.shape[1] // 2] - r, g, b, a = middle_pixel - assert r > g and r > b - assert g == b - assert r > 0 and g > 0 and b > 0 - scene.remove(frustum_actor) - frustum_actor_2 = actor.frustum(centers=centers, colors=colors, material="basic") - scene.add(frustum_actor_2) - window.snapshot(scene=scene, fname="frustum_test_2.png") +def test_pentagonalprism(): + centers = np.array([[0, 0, 0]]) + colors = np.array([[1, 0, 0]]) + validate_actors(centers=centers, colors=colors, actor_type="pentagonalprism") - img = Image.open("frustum_test_2.png") - img_array = np.array(img) - mean_r, mean_g, mean_b, mean_a = np.mean( - img_array.reshape(-1, img_array.shape[2]), axis=0 - ) +def test_octagonalprism(): + centers = np.array([[0, 0, 0]]) + colors = np.array([[1, 0, 0]]) + validate_actors(centers=centers, colors=colors, actor_type="octagonalprism") - assert mean_r > mean_b and mean_r > mean_g - assert 0 < mean_r < 255 - assert mean_g == 0 and mean_b == 0 - middle_pixel = img_array[img_array.shape[0] // 2, img_array.shape[1] // 2] - r, g, b, a = middle_pixel - assert r > g and r > b - assert g == 0 and b == 0 - assert r == 255 - scene.remove(frustum_actor_2) +def test_arrow(): + centers = np.array([[0, 0, 0]]) + colors = np.array([[1, 0, 0]]) + validate_actors(centers=centers, colors=colors, actor_type="arrow") + + +def test_superquadric(): + centers = np.array([[0, 0, 0]]) + colors = np.array([[1, 0, 0]]) + validate_actors(centers=centers, colors=colors, actor_type="superquadric") + + +def test_cone(): + centers = np.array([[0, 0, 0]]) + colors = np.array([[1, 0, 0]]) + validate_actors(centers=centers, colors=colors, actor_type="cone") + + +def test_star(): + centers = np.array([[0, 0, 0]]) + colors = np.array([[1, 0, 0]]) + validate_actors(centers=centers, colors=colors, actor_type="star")