Skip to content

Commit

Permalink
RF: added both versions of the prim_box; 24 and 8 vertices
Browse files Browse the repository at this point in the history
also fixed tests
  • Loading branch information
m-agour committed Dec 29, 2024
1 parent 29a8763 commit 0eb0442
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 76 deletions.
115 changes: 47 additions & 68 deletions fury/primitive.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,84 +256,63 @@ def prim_square():
return vertices, triangles


def prim_box():
"""Return vertices and triangle for a box geometry.
def prim_box(shared_vertices=False):
"""Return vertices and triangles for a box geometry.
Parameters
----------
shared_vertices : bool, optional
If False, returns 24 vertices (no shared vertices between orthogonal faces).
If True, returns 8 unique vertices.
Returns
-------
vertices: ndarray
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 : ndarray
Array of vertex coordinates.
triangles : ndarray
Array of triangle indices.
"""
if not shared_vertices:
vertices = np.array(
[
[-1, -1, -1], [1, -1, -1], [1, 1, -1], [-1, 1, -1],
[-1, -1, 1], [1, -1, 1], [1, 1, 1], [-1, 1, 1],
[-1, -1, -1], [-1, 1, -1], [-1, 1, 1], [-1, -1, 1],
[1, -1, -1], [1, 1, -1], [1, 1, 1], [1, -1, 1],
[-1, 1, -1], [1, 1, -1], [1, 1, 1], [-1, 1, 1],
[-1, -1, -1], [1, -1, -1], [1, -1, 1], [-1, -1, 1],
], dtype=np.float32
) * 0.5

vertices = (
np.array(
triangles = 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,
[2, 1, 0], [3, 2, 0], [4, 5, 6], [4, 6, 7],
[8, 10, 9], [11, 10, 8], [12, 13, 14], [12, 14, 15],
[16, 17, 18], [16, 18, 19], [20, 21, 22], [20, 22, 23],
], dtype=np.uint32
)
* 0.5
)

faces = np.array(
[
# Back
[2, 1, 0],
[3, 2, 0],
# Front
[4, 5, 6],
[4, 6, 7],
# 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=np.uint32,
)
else:
vertices = np.array(
[
[-1, -1, -1], [-1, -1, 1],
[-1, 1, -1], [-1, 1, 1],
[1, -1, -1], [1, -1, 1],
[1, 1, -1], [1, 1, 1],
], dtype=np.float32
) * 0.5

triangles = np.array(
[
[0, 6, 4], [0, 2, 6], [0, 3, 2], [0, 1, 3],
[2, 7, 6], [2, 3, 7], [4, 6, 7], [4, 7, 5],
[0, 4, 5], [0, 5, 1], [1, 5, 7], [1, 7, 3],
], dtype=np.uint32
)

return vertices, triangles

return vertices, faces


@warn_on_args_to_kwargs()
Expand Down
17 changes: 9 additions & 8 deletions fury/tests/test_primitive.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@
def test_vertices_primitives():
# Tests the default vertices of all the built in primitive shapes.
l_primitives = [
(fp.prim_square, (4, 3), -0.5, 0.5, 0),
(fp.prim_box, (8, 3), -0.5, 0.5, 0),
(fp.prim_tetrahedron, (4, 3), -0.5, 0.5, 0),
(fp.prim_star, (10, 3), -3, 3, -0.0666666666),
(fp.prim_rhombicuboctahedron, (24, 3), -0.5, 0.5, 0),
(fp.prim_frustum, (8, 3), -0.5, 0.5, 0),
(fp.prim_square, (4, 3), -0.5, 0.5, 0, {}),
(fp.prim_box, (24, 3), -0.5, 0.5, 0, {"shared_vertices": False}),
(fp.prim_box, (8, 3), -0.5, 0.5, 0, {"shared_vertices": True}),
(fp.prim_tetrahedron, (4, 3), -0.5, 0.5, 0, {}),
(fp.prim_star, (10, 3), -3, 3, -0.0666666666, {}),
(fp.prim_rhombicuboctahedron, (24, 3), -0.5, 0.5, 0, {}),
(fp.prim_frustum, (8, 3), -0.5, 0.5, 0, {}),
]

for func, shape, e_min, e_max, e_mean in l_primitives:
vertices, _ = func()
for func, shape, e_min, e_max, e_mean, kwargs in l_primitives:
vertices, _ = func(**kwargs)
npt.assert_equal(vertices.shape, shape)
npt.assert_almost_equal(np.mean(vertices), e_mean)
npt.assert_equal(vertices.min(), e_min)
Expand Down

0 comments on commit 0eb0442

Please sign in to comment.