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

Added primitives count to the the Actor's polydata #617

Merged
merged 24 commits into from Jul 16, 2022
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
9182972
Added a getter and a setter for the primitives count.
m-agour Jul 1, 2022
e440604
assigning the primitive count while creating the actor
m-agour Jul 1, 2022
d288f23
updated the actors in both repeat_source and repeat_primitives to set…
m-agour Jul 1, 2022
edd9bbf
Added tests
m-agour Jul 1, 2022
f1ec0a5
fixed the description and the datatype
m-agour Jul 4, 2022
d940fe6
Fixed vars names.
m-agour Jul 5, 2022
319a675
Revert "Fixed vars names."
m-agour Jul 5, 2022
2e29d70
Added tests for other actors
m-agour Jul 6, 2022
a80fcea
Updated forgotten actors not using the repeat_source or repeat_primit…
m-agour Jul 6, 2022
0be3bce
Added get/set_polydata_primitives_count and updated tests
m-agour Jul 6, 2022
f120a13
changed the functions names
m-agour Jul 6, 2022
a2f1503
PIP issue
m-agour Jul 6, 2022
998b80d
Using set_polydata_primitives_count instead of set_actor_...
m-agour Jul 6, 2022
428b5a9
Another pep issue
m-agour Jul 6, 2022
1f9b658
Added compound test
m-agour Jul 6, 2022
8d3a283
Added streamtube test
m-agour Jul 6, 2022
75a2e2f
Set primitives count to polydata directly in dot, line and streamtube…
m-agour Jul 6, 2022
15fd506
was comparing to the wrong length
m-agour Jul 6, 2022
95a4f8c
Renamed two functions for the naming consistency
m-agour Jul 7, 2022
95bbac6
Added square test, removed duplicated test and unused imports, solved…
m-agour Jul 7, 2022
8c746b5
added `lines` array at top
m-agour Jul 7, 2022
4907bd2
restructured the test
m-agour Jul 13, 2022
59787dd
Renamed two tests
m-agour Jul 15, 2022
f17f880
Should be `actor.dot`
m-agour Jul 15, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
70 changes: 55 additions & 15 deletions fury/actor.py
Expand Up @@ -32,7 +32,8 @@
set_polydata_vertices, set_polydata_triangles,
shallow_copy, rgb_to_vtk, numpy_to_vtk_matrix,
repeat_sources, get_actor_from_primitive,
fix_winding_order, numpy_to_vtk_colors, color_check)
fix_winding_order, numpy_to_vtk_colors, color_check,
set_actor_primitives_count, set_polydata_primitives_count)
m-agour marked this conversation as resolved.
Show resolved Hide resolved


def slicer(data, affine=None, value_range=None, opacity=1.,
Expand Down Expand Up @@ -587,6 +588,10 @@ def streamtube(lines, colors=None, opacity=1, linewidth=0.1, tube_sides=9,
poly_data, color_is_scalar = lines_to_vtk_polydata(lines, colors)
next_input = poly_data

# set primitives count
prim_count = len(lines)
set_polydata_primitives_count(poly_data, prim_count)

# Set Normals
poly_normals = set_input(PolyDataNormals(), next_input)
poly_normals.ComputeCellNormalsOn()
Expand Down Expand Up @@ -721,6 +726,10 @@ def line(lines, colors=None, opacity=1, linewidth=1,
poly_data, color_is_scalar = lines_to_vtk_polydata(lines, colors)
next_input = poly_data

# set primitives count
prim_count = len(lines)
set_polydata_primitives_count(poly_data, prim_count)

# use spline interpolation
if (spline_subdiv is not None) and (spline_subdiv > 0):
spline_filter = set_input(SplineFilter(), next_input)
Expand Down Expand Up @@ -1480,6 +1489,10 @@ def dot(points, colors=None, opacity=None, dot_size=5):
polydata.SetVerts(vtk_faces)
polydata.GetPointData().SetScalars(color_array)

# set primitives count
prim_count = len(points)
set_polydata_primitives_count(polydata, prim_count)

# Visualize
mapper = PolyDataMapper()
mapper.SetInputData(polydata)
Expand Down Expand Up @@ -1603,8 +1616,9 @@ def sphere(centers, colors, radii=1., phi=16, theta=16,
directions=directions, centers=centers,
colors=colors, scales=scales)
big_verts, big_faces, big_colors, _ = res
prim_count = len(centers)
sphere_actor = get_actor_from_primitive(
big_verts, big_faces, big_colors)
big_verts, big_faces, big_colors, prim_count=prim_count)
sphere_actor.GetProperty().SetOpacity(opacity)
return sphere_actor

Expand Down Expand Up @@ -1772,7 +1786,9 @@ def square(centers, directions=(1, 0, 0), colors=(1, 0, 0), scales=1):
centers=centers, colors=colors, scales=scales)

big_verts, big_faces, big_colors, _ = res
sq_actor = get_actor_from_primitive(big_verts, big_faces, big_colors)
prim_count = len(centers)
sq_actor = get_actor_from_primitive(big_verts, big_faces, big_colors,
prim_count=prim_count)
sq_actor.GetProperty().BackfaceCullingOff()
return sq_actor

Expand Down Expand Up @@ -1851,7 +1867,9 @@ def box(centers, directions=(1, 0, 0), colors=(1, 0, 0), scales=(1, 2, 3)):
centers=centers, colors=colors, scales=scales)

big_verts, big_faces, big_colors, _ = res
box_actor = get_actor_from_primitive(big_verts, big_faces, big_colors)
prim_count = len(centers)
box_actor = get_actor_from_primitive(big_verts, big_faces, big_colors,
prim_count=prim_count)
return box_actor


Expand Down Expand Up @@ -1940,7 +1958,9 @@ def arrow(centers, directions, colors, heights=1., resolution=10,
res = fp.repeat_primitive(vertices, faces, directions=directions, centers=centers,
colors=colors, scales=scales)
big_vertices, big_faces, big_colors, _ = res
arrow_actor = get_actor_from_primitive(big_vertices, big_faces, big_colors)
prim_count = len(centers)
arrow_actor = get_actor_from_primitive(big_vertices, big_faces, big_colors,
prim_count=prim_count)
return arrow_actor

src = ArrowSource() if faces is None else None
Expand Down Expand Up @@ -2018,7 +2038,9 @@ def cone(centers, directions, colors, heights=1., resolution=10,
directions=directions, colors=colors, scales=heights)

big_verts, big_faces, big_colors, _ = res
cone_actor = get_actor_from_primitive(big_verts, big_faces, big_colors)
prim_count = len(centers)
cone_actor = get_actor_from_primitive(big_verts, big_faces, big_colors,
prim_count=prim_count)

return cone_actor

Expand Down Expand Up @@ -2059,7 +2081,9 @@ def triangularprism(centers, directions=(1, 0, 0), colors=(1, 0, 0),
res = fp.repeat_primitive(verts, faces, directions=directions,
centers=centers, colors=colors, scales=scales)
big_verts, big_faces, big_colors, _ = res
tprism_actor = get_actor_from_primitive(big_verts, big_faces, big_colors)
prim_count = len(centers)
tprism_actor = get_actor_from_primitive(big_verts, big_faces, big_colors,
prim_count=prim_count)
return tprism_actor


Expand Down Expand Up @@ -2099,7 +2123,9 @@ def rhombicuboctahedron(centers, directions=(1, 0, 0), colors=(1, 0, 0),
res = fp.repeat_primitive(verts, faces, directions=directions,
centers=centers, colors=colors, scales=scales)
big_verts, big_faces, big_colors, _ = res
rcoh_actor = get_actor_from_primitive(big_verts, big_faces, big_colors)
prim_count = len(centers)
rcoh_actor = get_actor_from_primitive(big_verts, big_faces, big_colors,
prim_count=prim_count)
return rcoh_actor


Expand Down Expand Up @@ -2141,7 +2167,9 @@ def pentagonalprism(centers, directions=(1, 0, 0), colors=(1, 0, 0),
centers=centers, colors=colors, scales=scales)

big_verts, big_faces, big_colors, _ = res
pent_actor = get_actor_from_primitive(big_verts, big_faces, big_colors)
prim_count = len(centers)
pent_actor = get_actor_from_primitive(big_verts, big_faces, big_colors,
prim_count=prim_count)
return pent_actor


Expand Down Expand Up @@ -2182,7 +2210,9 @@ def octagonalprism(centers, directions=(1, 0, 0), colors=(1, 0, 0),
centers=centers, colors=colors, scales=scales)

big_verts, big_faces, big_colors, _ = res
oct_actor = get_actor_from_primitive(big_verts, big_faces, big_colors)
prim_count = len(centers)
oct_actor = get_actor_from_primitive(big_verts, big_faces, big_colors,
prim_count=prim_count)
return oct_actor


Expand Down Expand Up @@ -2221,7 +2251,9 @@ def frustum(centers, directions=(1, 0, 0), colors=(0, 1, 0), scales=1):
centers=centers, colors=colors, scales=scales)

big_verts, big_faces, big_colors, _ = res
frustum_actor = get_actor_from_primitive(big_verts, big_faces, big_colors)
prim_count = len(centers)
frustum_actor = get_actor_from_primitive(big_verts, big_faces, big_colors,
prim_count=prim_count)
return frustum_actor


Expand Down Expand Up @@ -2281,7 +2313,9 @@ def have_2_dimensions(arr):
colors=colors, scales=scales)

big_verts, big_faces, big_colors, _ = res
spq_actor = get_actor_from_primitive(big_verts, big_faces, big_colors)
prim_count = len(centers)
spq_actor = get_actor_from_primitive(big_verts, big_faces, big_colors,
prim_count=prim_count)
return spq_actor


Expand Down Expand Up @@ -2324,7 +2358,9 @@ def billboard(centers, colors=(0, 1, 0), scales=1, vs_dec=None, vs_impl=None,

big_verts, big_faces, big_colors, big_centers = res

bb_actor = get_actor_from_primitive(big_verts, big_faces, big_colors)
prim_count = len(centers)
bb_actor = get_actor_from_primitive(big_verts, big_faces, big_colors,
prim_count=prim_count)
bb_actor.GetMapper().SetVBOShiftScaleMethod(False)
bb_actor.GetProperty().BackfaceCullingOff()
attribute_to_actor(bb_actor, big_centers, 'center')
Expand Down Expand Up @@ -2975,7 +3011,9 @@ def sdf(centers, directions=(1, 0, 0), colors=(1, 0, 0), primitives='torus',
scales=scales)

rep_verts, rep_faces, rep_colors, rep_centers = repeated
box_actor = get_actor_from_primitive(rep_verts, rep_faces, rep_colors)
prim_count = len(centers)
box_actor = get_actor_from_primitive(rep_verts, rep_faces, rep_colors,
prim_count=prim_count)
box_actor.GetMapper().SetVBOShiftScaleMethod(False)

if isinstance(primitives, (list, tuple, np.ndarray)):
Expand Down Expand Up @@ -3054,7 +3092,9 @@ def markers(
scales=scales)

big_verts, big_faces, big_colors, big_centers = res
sq_actor = get_actor_from_primitive(big_verts, big_faces, big_colors)
prim_count = len(centers)
sq_actor = get_actor_from_primitive(big_verts, big_faces, big_colors,
prim_count=prim_count)
sq_actor.GetMapper().SetVBOShiftScaleMethod(False)
sq_actor.GetProperty().BackfaceCullingOff()

Expand Down
78 changes: 77 additions & 1 deletion fury/tests/test_actors.py
Expand Up @@ -12,7 +12,7 @@
from fury import actor, window, primitive as fp
from fury.actor import grid
from fury.decorators import skip_osx, skip_win
from fury.utils import shallow_copy, rotate
from fury.utils import shallow_copy, rotate, get_actor_primitives_count
from fury.testing import assert_greater, assert_greater_equal
from fury.primitive import prim_sphere

Expand Down Expand Up @@ -1492,3 +1492,79 @@ def test_marker_actor(interactive=False):
report = window.analyze_snapshot(arr, colors=colors)
npt.assert_equal(report.objects, 12)


def test_actors_primitives_count():
m-agour marked this conversation as resolved.
Show resolved Hide resolved
centers = np.array([[1, 1, 1], [2, 2, 2]])
directions = np.array([[1, 0, 0], [1, 0, 0]])
colors = np.array([[1, 0, 0], [1, 0, 0]])

box_actor = actor.box(centers)
npt.assert_equal(get_actor_primitives_count(box_actor), len(centers))

sphere_actor = actor.sphere(centers, colors)
npt.assert_equal(get_actor_primitives_count(sphere_actor), len(centers))

prim_sphere_actor = actor.sphere(centers, colors, use_primitive=False)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't use_primitive = False by default?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but I thought this might get changed at any time (use_primitive=True by default). so it's better to explicitly set it to keep the tests valid.

npt.assert_equal(get_actor_primitives_count(prim_sphere_actor), len(centers))
m-agour marked this conversation as resolved.
Show resolved Hide resolved

prim_sphere_actor = actor.sphere(centers, directions, use_primitive=True)
m-agour marked this conversation as resolved.
Show resolved Hide resolved
npt.assert_equal(get_actor_primitives_count(prim_sphere_actor), len(centers))
m-agour marked this conversation as resolved.
Show resolved Hide resolved

sdf_actor = actor.sdf(centers, (1, 0, 0))
npt.assert_equal(get_actor_primitives_count(sdf_actor), len(centers))

billboard_actor = actor.billboard(centers)
npt.assert_equal(get_actor_primitives_count(billboard_actor), len(centers))

superquadric_actor = actor.superquadric(centers)
npt.assert_equal(get_actor_primitives_count(superquadric_actor), len(centers))
m-agour marked this conversation as resolved.
Show resolved Hide resolved

markers_actor = actor.markers(centers)
npt.assert_equal(get_actor_primitives_count(markers_actor), len(centers))

octagonalprism_actor = actor.octagonalprism(centers)
npt.assert_equal(get_actor_primitives_count(octagonalprism_actor), len(centers))
m-agour marked this conversation as resolved.
Show resolved Hide resolved

frustum_actor = actor.frustum(centers)
npt.assert_equal(get_actor_primitives_count(frustum_actor), len(centers))

rhombicuboctahedron_actor = actor.rhombicuboctahedron(centers)
npt.assert_equal(get_actor_primitives_count(rhombicuboctahedron_actor), len(centers))
m-agour marked this conversation as resolved.
Show resolved Hide resolved

pentagonalprism_actor = actor.pentagonalprism(centers)
npt.assert_equal(get_actor_primitives_count(pentagonalprism_actor), len(centers))
m-agour marked this conversation as resolved.
Show resolved Hide resolved

triangularprism_actor = actor.triangularprism(centers)
npt.assert_equal(get_actor_primitives_count(triangularprism_actor), len(centers))
m-agour marked this conversation as resolved.
Show resolved Hide resolved

prim_rhombicuboctahedron = actor.rhombicuboctahedron(centers)
npt.assert_equal(get_actor_primitives_count(prim_rhombicuboctahedron), len(centers))
m-agour marked this conversation as resolved.
Show resolved Hide resolved
m-agour marked this conversation as resolved.
Show resolved Hide resolved

cone_actor = actor.cone(centers, directions, colors, use_primitive=False)
npt.assert_equal(get_actor_primitives_count(cone_actor), len(centers))

prim_cone_actor = actor.cone(centers, directions, colors, use_primitive=True)
m-agour marked this conversation as resolved.
Show resolved Hide resolved
npt.assert_equal(get_actor_primitives_count(prim_cone_actor), len(centers))

arrow_actor = actor.arrow(centers, directions, colors, repeat_primitive=True)
m-agour marked this conversation as resolved.
Show resolved Hide resolved
npt.assert_equal(get_actor_primitives_count(arrow_actor), len(centers))

arrow_actor = actor.arrow(centers, directions, colors, repeat_primitive=False)
m-agour marked this conversation as resolved.
Show resolved Hide resolved
npt.assert_equal(get_actor_primitives_count(arrow_actor), len(centers))

disk_actor = actor.disk(centers, directions, colors)
npt.assert_equal(get_actor_primitives_count(disk_actor), len(centers))

cylinder_actor = actor.cylinder(centers, directions, colors)
npt.assert_equal(get_actor_primitives_count(cylinder_actor), len(centers))

lines = np.array([[[0, 0, 0], [1, 1, 1]], [[1, 1, 1], [2, 2, 2]]])
line_actor = actor.line(np.array(lines))
npt.assert_equal(get_actor_primitives_count(line_actor), len(lines))

streamtube_actor = actor.streamtube(np.array(lines))
npt.assert_equal(get_actor_primitives_count(streamtube_actor), len(lines))

dots_actor = actor.dots(centers)
npt.assert_equal(get_actor_primitives_count(dots_actor), len(centers))

57 changes: 54 additions & 3 deletions fury/tests/test_utils.py
Expand Up @@ -16,16 +16,19 @@
compute_bounds, set_input,
update_actor, get_actor_from_primitive,
get_bounds, update_surface_actor_colors,
apply_affine_to_actor, color_check, is_ui)
apply_affine_to_actor, color_check, is_ui,
set_actor_primitives_count, get_actor_primitives_count,
set_polydata_primitives_count, get_polydata_primitives_count)
m-agour marked this conversation as resolved.
Show resolved Hide resolved
from fury import actor, window, utils
from fury.lib import (numpy_support, PolyData, PolyDataMapper2D, Points,
CellArray, Polygon, Actor2D, DoubleArray,
UnsignedCharArray, TextActor3D, VTK_DOUBLE, VTK_FLOAT)
UnsignedCharArray, TextActor3D, VTK_DOUBLE, VTK_FLOAT, Actor, PolyDataMapper, VTK_INT)
m-agour marked this conversation as resolved.
Show resolved Hide resolved
import fury.primitive as fp

from fury.optpkg import optional_package
dipy, have_dipy, _ = optional_package('dipy')


def test_apply_affine_to_actor(interactive=False):
text_act = actor.text_3d("ALIGN TOP RIGHT", justification='right',
vertical_justification='top')
Expand Down Expand Up @@ -814,12 +817,60 @@ def test_empty_list_to_polydata():
lines = [[]]
_, _ = utils.lines_to_vtk_polydata(lines)


def test_empty_array_to_polydata():
lines = np.array([[]])
_, _ = utils.lines_to_vtk_polydata(lines)


@pytest.mark.skipif(not have_dipy, reason="Requires DIPY")
def test_empty_array_sequence_to_polydata():
from dipy.tracking.streamline import Streamlines
lines = Streamlines()
_, _ = utils.lines_to_vtk_polydata(lines)
_, _ = utils.lines_to_vtk_polydata(lines)


def test_set_polydata_primitives_count():
polydata = PolyData()

set_polydata_primitives_count(polydata, 1)
prim_count = get_polydata_field(polydata, 'prim_count')[0]
npt.assert_equal(prim_count, 1)


def test_get_polydata_primitives_count():
polydata = PolyData()
add_polydata_numeric_field(polydata, "prim_count", 1, array_type=VTK_INT)

prim_count = get_polydata_primitives_count(polydata)
npt.assert_equal(prim_count, 1)


def test_set_actor_primitives_count():
guaje marked this conversation as resolved.
Show resolved Hide resolved
act = actor.axes()
set_actor_primitives_count(act, 1)
polydata = act.GetMapper().GetInput()
prim_count = get_polydata_field(polydata, 'prim_count')[0]
npt.assert_equal(prim_count, 1)


def test_get_actor_primitives_count():
guaje marked this conversation as resolved.
Show resolved Hide resolved
act = actor.axes()
polydata = act.GetMapper().GetInput()
add_polydata_numeric_field(polydata, "prim_count", 1, array_type=VTK_INT)
prim_count = get_actor_primitives_count(act)
npt.assert_equal(prim_count, 1)


def test_primitives_count():
# testing on actor
act = actor.axes()
set_actor_primitives_count(act, 3)
prim_count = get_actor_primitives_count(act)
npt.assert_equal(prim_count, 3)

# testing on polydata
polydata = PolyData()
set_polydata_primitives_count(polydata, 4)
prim_count = get_polydata_primitives_count(polydata)
npt.assert_equal(prim_count, 4)