Skip to content

Commit

Permalink
added actor for cylinder
Browse files Browse the repository at this point in the history
  • Loading branch information
ManishReddyR committed Jan 18, 2025
1 parent cb4637e commit f183974
Showing 1 changed file with 104 additions and 0 deletions.
104 changes: 104 additions & 0 deletions fury/actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,107 @@ def box(
obj.prim_count = prim_count

return obj


def cylinder(
centers,
colors,
*,
height= 1,
sectors= 36,
radii= 0.5,
scales=(1, 1, 1),
directions=(0, 1, 0),
capped= True,
opacity= None,
material= "phong",
enable_picking= True,
):
"""Visualize one or many cylinders with different features.
Parameters
----------
centers : ndarray, shape (N, 3)
Box 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 cylinder. Default is 1.
sectors: int, optional
The number of divisions around the cylinder's circumference (like longitudinal slices).
Higher values produce smoother cylinders. Default is 36.
radii : float or ndarray (N,) or tuple, optional
The radius of the base of the cylinders. A single value applies to all cylinders,
while an array specifies a radius for each cylinder individually. Default is 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).
directions : ndarray, shape (N, 3), optional
The orientation vector of the box.
capped : bool, optional
Whether to add caps (circular ends) to the cylinders. Default is True.
scales : int or ndarray (N,3) or tuple (3,), optional
The size of the box in each dimension. If a single value is provided,
the same size will be used for all boxes.
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 boxes. Options are 'phong' and 'basic'.
enable_picking : bool, optional
Whether the boxes should be pickable in a 3D scene.
Returns
-------
mesh_actor : Actor
A mesh actor containing the generated boxes, 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)
>>> cylinder_actor = actor.cylinder(centers=centers, colors=colors)
>>> scene.add(cylinder_actor)
>>> show_manager = window.ShowManager(scene=scene, size=(600, 600))
>>> show_manager.start()
"""

vertices, faces = fp.prim_cylinder(radius= radii, height= height, sectors= sectors, capped= capped)
res = fp.repeat_primitive(
vertices,
faces,
directions=directions,
centers=centers,
colors=colors,
scales=scales,
)
big_vertices, big_faces, big_colors, _ = res
prim_count = len(centers)
big_colors = big_colors / 255.0

if isinstance(opacity, (int, float)):
if big_colors.shape[1] == 3:
big_colors = np.hstack(
(big_colors, np.full((big_colors.shape[0], 1), opacity))
)
else:
big_colors[:, 3] *= opacity


geo = buffer_to_geometry(
indices=big_faces.astype("int32"),
positions=big_vertices.astype("float32"),
texcoords=big_vertices.astype("float32"),
colors=big_colors.astype("float32"),
)
mat = _create_mesh_material(material=material, enable_picking=enable_picking)
obj = create_mesh(geometry=geo, material=mat)
obj.local.position = centers[0]

obj.prim_count = prim_count

return obj

0 comments on commit f183974

Please sign in to comment.