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 all 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
76 changes: 57 additions & 19 deletions fury/actor.py
Expand Up @@ -22,7 +22,8 @@
PolyDataNormals, Assembly, LODActor, VTK_UNSIGNED_CHAR,
PolyDataMapper2D, ScalarBarActor, PolyVertex, CellArray,
UnstructuredGrid, DataSetMapper, ConeSource, ArrowSource,
SphereSource, CylinderSource, DiskSource, TexturedSphereSource,
SphereSource, CylinderSource, DiskSource,
TexturedSphereSource,
Texture, FloatArray, VTK_TEXT_LEFT, VTK_TEXT_RIGHT,
VTK_TEXT_BOTTOM, VTK_TEXT_TOP, VTK_TEXT_CENTERED,
TexturedActor2D, TextureMapToPlane, TextActor3D,
Expand All @@ -32,7 +33,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_polydata_primitives_count)


def slicer(data, affine=None, value_range=None, opacity=1.,
Expand Down Expand Up @@ -587,6 +589,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 +727,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 @@ -1028,9 +1038,6 @@ def _color_fa(fa, evecs):
return np.abs(evecs[..., 0]) * np.clip(fa, 0, 1)[..., None]





def tensor_slicer(evals, evecs, affine=None, mask=None, sphere=None, scale=2.2,
norm=True, opacity=1., scalar_colors=None):
"""Slice many tensors as ellipsoids in native or world coordinates.
Expand Down Expand Up @@ -1480,6 +1487,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 +1614,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 +1784,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 +1865,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 +1956,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 +2036,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 +2079,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 +2121,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 +2165,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 +2208,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 +2249,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 +2311,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 +2356,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 +3009,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 +3090,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
49 changes: 48 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, primitives_count_from_actor
from fury.testing import assert_greater, assert_greater_equal
from fury.primitive import prim_sphere

Expand Down Expand Up @@ -1492,3 +1492,50 @@ 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]])
lines = np.array([[[0, 0, 0], [1, 1, 1]], [[1, 1, 1], [2, 2, 2]]])

args_1 = {'centers': centers}
args_2 = {**args_1, 'colors': colors}
args_3 = {**args_2, 'directions': directions}

cen_c = len(centers)
lin_c = len(lines)

actors_test_cases = [
[actor.box, args_1, cen_c],
[actor.rectangle, args_1, cen_c],
[actor.square, args_1, cen_c],
[actor.cube, args_1, cen_c],
[actor.sphere, {**args_2, 'use_primitive': True}, cen_c],
[actor.sphere, {**args_2, 'use_primitive': False}, cen_c],
[actor.sdf, args_1, cen_c],
[actor.billboard, args_1, cen_c],
[actor.superquadric, args_1, cen_c],
[actor.markers, args_1, cen_c],
[actor.octagonalprism, args_1, cen_c],
[actor.frustum, args_1, cen_c],
[actor.pentagonalprism, args_1, cen_c],
[actor.triangularprism, args_1, cen_c],
[actor.rhombicuboctahedron, args_1, cen_c],
[actor.cylinder, args_3, cen_c],
[actor.disk, args_3, cen_c],
[actor.cone, {**args_3, 'use_primitive': False}, cen_c],
[actor.cone, {**args_3, 'use_primitive': True}, cen_c],
[actor.arrow, {**args_3, 'repeat_primitive': False}, cen_c],
[actor.arrow, {**args_3, 'repeat_primitive': True}, cen_c],
[actor.dot, {'points': centers}, cen_c],
[actor.point, {'points': centers, 'colors': colors}, cen_c],
[actor.line, {'lines': lines}, lin_c],
[actor.streamtube, {'lines': lines}, lin_c],
]
for test_case in actors_test_cases:
act_func = test_case[0]
args = test_case[1]
primitives_count = test_case[2]
act = act_func(**args)
npt.assert_equal(primitives_count_from_actor(act), primitives_count)
59 changes: 56 additions & 3 deletions fury/tests/test_utils.py
Expand Up @@ -16,16 +16,21 @@
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,
primitives_count_to_actor, primitives_count_from_actor,
set_polydata_primitives_count,
get_polydata_primitives_count)
from fury import actor, window, utils
from fury.lib import (numpy_support, PolyData, PolyDataMapper2D, Points,
CellArray, Polygon, Actor2D, DoubleArray,
CellArray, Polygon, Actor2D, DoubleArray, VTK_INT,
UnsignedCharArray, TextActor3D, VTK_DOUBLE, VTK_FLOAT)

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 +819,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_primitives_count_to_actor():
act = actor.axes()
primitives_count_to_actor(act, 1)
polydata = act.GetMapper().GetInput()
prim_count = get_polydata_field(polydata, 'prim_count')[0]
npt.assert_equal(prim_count, 1)


def test_primitives_count_from_actor():
act = actor.axes()
polydata = act.GetMapper().GetInput()
add_polydata_numeric_field(polydata, "prim_count", 1, array_type=VTK_INT)
prim_count = primitives_count_from_actor(act)
npt.assert_equal(prim_count, 1)


def test_primitives_count():
# testing on actor
act = actor.axes()
primitives_count_to_actor(act, 3)
prim_count = primitives_count_from_actor(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)