Skip to content

Commit

Permalink
NF: Adding actor for cone, TEST: Adding uni tests for cone.
Browse files Browse the repository at this point in the history
  • Loading branch information
ManishReddyR committed Jan 31, 2025
1 parent 53561ae commit 60bd223
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
76 changes: 76 additions & 0 deletions fury/actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -985,3 +985,79 @@ def superquadric(
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. Default is 1.
sectors: int, optional
The number of divisions around the cones's circumference .
Higher values produce smoother cones. Default is 10.
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. Default:0.5.
scales : int or ndarray (N, 3) or tuple (3,), optional
Scaling factors for the cones in the (x, y, z) dimensions.
Default is uniform scaling (1, 1, 1).
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,
)
52 changes: 52 additions & 0 deletions fury/tests/test_actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,3 +682,55 @@ def test_superquadric():
assert g == 0 and b == 0
assert r == 255
scene.remove(superquadric_actor_2)


def test_cone():
scene = window.Scene()
centers = np.array([[0, 0, 0]])
colors = np.array([[1, 0, 0]])

cone_actor = actor.cone(centers=centers, colors=colors)
scene.add(cone_actor)

npt.assert_array_equal(cone_actor.local.position, centers[0])

assert cone_actor.prim_count == 1

window.snapshot(scene=scene, fname="cone_test_1.png")

img = Image.open("cone_test_1.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

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
scene.remove(cone_actor)

cone_actor_2 = actor.cone(centers=centers, colors=colors, material="basic")
scene.add(cone_actor_2)
window.snapshot(scene=scene, fname="cone_test_2.png")

img = Image.open("cone_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
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(cone_actor_2)

0 comments on commit 60bd223

Please sign in to comment.