Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding box actor #958

Open
wants to merge 6 commits into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions fury/actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,89 @@ def sphere(
obj.local.position = centers[0]
obj.prim_count = prim_count
return obj


def box(
centers,
*,
directions=(1, 0, 0),
colors=(1, 0, 0),
scales=(1, 1, 1),
opacity=None,
material="phong",
enable_picking=True,
):
"""Visualize one or many boxes with different features.

Parameters
----------
centers : ndarray, shape (N, 3)
Box positions.
directions : ndarray, shape (N, 3), optional
The orientation vector of the box.
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 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
>>> scales = np.random.rand(5, 3)
>>> box_actor = actor.box(centers=centers, scales=scales)
>>> scene.add(box_actor)
>>> show_manager = window.ShowManager(scene=scene, size=(600, 600))
>>> show_manager.start()
"""
vertices, faces = fp.prim_box()
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
89 changes: 63 additions & 26 deletions fury/primitive.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,41 +262,78 @@ def prim_box():
Returns
-------
vertices: ndarray
8 vertices coords that composed our box
24 vertices coords that composed our box
triangles: ndarray
12 triangles that composed our box
normals: ndarray
24 normals for each vertex of the box

"""
vertices = np.array(
[
[-0.5, -0.5, -0.5],
[-0.5, -0.5, 0.5],
[-0.5, 0.5, -0.5],
[-0.5, 0.5, 0.5],
[0.5, -0.5, -0.5],
[0.5, -0.5, 0.5],
[0.5, 0.5, -0.5],
[0.5, 0.5, 0.5],
]

vertices = (
np.array(
[
# Back
[-1, -1, -1],
[1, -1, -1],
[1, 1, -1],
[-1, 1, -1],
# Front
[-1, -1, 1],
[1, -1, 1],
[1, 1, 1],
[-1, 1, 1],
# Left
[-1, -1, -1],
[-1, 1, -1],
[-1, 1, 1],
[-1, -1, 1],
# Right
[1, -1, -1],
[1, 1, -1],
[1, 1, 1],
[1, -1, 1],
# Top
[-1, 1, -1],
[1, 1, -1],
[1, 1, 1],
[-1, 1, 1],
# Bottom
[-1, -1, -1],
[1, -1, -1],
[1, -1, 1],
[-1, -1, 1],
],
dtype=np.float32,
)
* 0.5
)
triangles = np.array(

faces = np.array(
[
[0, 6, 4],
[0, 2, 6],
[0, 3, 2],
[0, 1, 3],
[2, 7, 6],
[2, 3, 7],
# Back
[2, 1, 0],
[3, 2, 0],
# Front
[4, 5, 6],
[4, 6, 7],
[4, 7, 5],
[0, 4, 5],
[0, 5, 1],
[1, 5, 7],
[1, 7, 3],
# Left
[8, 10, 9],
[11, 10, 8],
# Right
[12, 13, 14],
[12, 14, 15],
# Top
[16, 17, 18],
[16, 18, 19],
# Bottom
[20, 21, 22],
[20, 22, 23],
],
dtype="i8",
dtype=np.uint32,
)
return vertices, triangles

return vertices, faces


@warn_on_args_to_kwargs()
Expand Down
19 changes: 19 additions & 0 deletions fury/tests/test_actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,22 @@ def test_sphere():
assert len(vertices) == len(colors)

npt.assert_array_almost_equal(len(faces), (2 * phi * (theta - 2)))


def test_box():
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)

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

mean_vertex = np.mean(box_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)
Loading