def test_spheres_primitives(): l_primitives = [('symmetric362', 362, 720), ('symmetric642', 642, 1280), ('symmetric724', 724, 1444), ('repulsion724', 724, 1444), ('repulsion100', 100, 196), ('repulsion200', 200, 396)] for name, nb_verts, nb_triangles in l_primitives: verts, faces = fp.prim_sphere(name) npt.assert_equal(verts.shape, (nb_verts, 3)) npt.assert_almost_equal(np.mean(verts), 0) npt.assert_equal(len(faces), nb_triangles) npt.assert_equal(list(set(np.concatenate(faces, axis=None))), list(range(len(verts)))) npt.assert_raises(ValueError, fp.prim_sphere, 'sym362') l_primitives = [(10, 10, 82, 160), (20, 20, 362, 720), (10, 12, 102, 200), (22, 20, 398, 792)] for nb_phi, nb_theta, nb_verts, nb_triangles in l_primitives: verts, faces = fp.prim_sphere(phi=nb_phi, theta=nb_theta) npt.assert_equal(verts.shape, (nb_verts, 3)) npt.assert_almost_equal(np.mean(verts), 0) npt.assert_equal(len(faces), nb_triangles) npt.assert_equal(list(set(np.concatenate(faces, axis=None))), list(range(len(verts))))
def test_superquadric_primitives(): # test default, should be like a sphere 362 sq_verts, sq_faces = fp.prim_superquadric() s_verts, s_faces = fp.prim_sphere('symmetric362') npt.assert_equal(sq_verts.shape, s_verts.shape) npt.assert_equal(sq_faces.shape, s_faces.shape) npt.assert_almost_equal(sq_verts, s_verts) # Apply roundness sq_verts, sq_faces = fp.prim_superquadric(roundness=(2, 3)) npt.assert_equal(sq_verts.shape, s_verts.shape) npt.assert_equal(sq_faces.shape, s_faces.shape)
def _sphere(scale=1): vertices, faces = prim_sphere('symmetric362') from fury.utils import set_polydata_vertices, set_polydata_triangles, vtk polydata = vtk.vtkPolyData() set_polydata_vertices(polydata, scale * vertices) set_polydata_triangles(polydata, faces) from fury.utils import set_polydata_normals, normals_from_v_f normals = normals_from_v_f(scale * vertices, faces) set_polydata_normals(polydata, normals) return polydata, scale * vertices, normals
def test_timer(): """Testing add a timer and exit window and app from inside timer.""" xyzr = np.array([[0, 0, 0, 10], [100, 0, 0, 50], [300, 0, 0, 100]]) xyzr2 = np.array([[0, 200, 0, 30], [100, 200, 0, 50], [300, 200, 0, 100]]) colors = np.array([[1, 0, 0, 0.3], [0, 1, 0, 0.4], [0, 0, 1., 0.45]]) scene = window.Scene() sphere_actor = actor.sphere(centers=xyzr[:, :3], colors=colors[:], radii=xyzr[:, 3]) vertices, faces = prim_sphere('repulsion724') sphere_actor2 = actor.sphere(centers=xyzr2[:, :3], colors=colors[:], radii=xyzr2[:, 3], vertices=vertices, faces=faces.astype('i8')) scene.add(sphere_actor) scene.add(sphere_actor2) tb = ui.TextBlock2D() counter = itertools.count() showm = window.ShowManager(scene, size=(1024, 768), reset_camera=False, order_transparent=True) showm.initialize() scene.add(tb) def timer_callback(_obj, _event): cnt = next(counter) tb.message = "Let's count to 10 and exit :" + str(cnt) showm.render() if cnt > 9: showm.exit() # Run every 200 milliseconds showm.add_timer_callback(True, 200, timer_callback) showm.start() arr = window.snapshot(scene, offscreen=True) npt.assert_(np.sum(arr) > 0)
def test_spheres(interactive=False): xyzr = np.array([[0, 0, 0, 10], [100, 0, 0, 25], [200, 0, 0, 50]]) colors = np.array([[1, 0, 0, 0.3], [0, 1, 0, 0.4], [0, 0, 1., 0.99]]) opacity = 0.5 scene = window.Scene() sphere_actor = actor.sphere(centers=xyzr[:, :3], colors=colors[:], radii=xyzr[:, 3], opacity=opacity) scene.add(sphere_actor) if interactive: window.show(scene, order_transparent=True) arr = window.snapshot(scene) report = window.analyze_snapshot(arr, colors=colors) npt.assert_equal(report.objects, 3) npt.assert_equal(sphere_actor.GetProperty().GetOpacity(), opacity) # test with an unique color for all centers scene.clear() sphere_actor = actor.sphere(centers=xyzr[:, :3], colors=np.array([1, 0, 0]), radii=xyzr[:, 3]) scene.add(sphere_actor) arr = window.snapshot(scene) report = window.analyze_snapshot(arr, colors=(1, 0, 0)) npt.assert_equal(report.colors_found, [True]) # test faces and vertices scene.clear() vertices, faces = fp.prim_sphere(name='symmetric362', gen_faces=False) sphere_actor = actor.sphere(centers=xyzr[:, :3], colors=colors[:], radii=xyzr[:, 3], opacity=opacity, vertices=vertices, faces=faces) scene.add(sphere_actor) if interactive: window.show(scene, order_transparent=True) arr = window.snapshot(scene) report = window.analyze_snapshot(arr, colors=colors) npt.assert_equal(report.objects, 3)
def test_tensor_slicer(interactive=False): evals = np.array([1.4, .35, .35]) * 10 ** (-3) evecs = np.eye(3) mevals = np.zeros((3, 2, 4, 3)) mevecs = np.zeros((3, 2, 4, 3, 3)) mevals[..., :] = evals mevecs[..., :, :] = evecs vertices, faces = prim_sphere('symmetric724', True) sphere = Sphere() sphere.vertices = vertices sphere.faces = faces affine = np.eye(4) scene = window.Scene() tensor_actor = actor.tensor_slicer(mevals, mevecs, affine=affine, sphere=sphere, scale=.3, opacity=0.4) _, J, K = mevals.shape[:3] scene.add(tensor_actor) scene.reset_camera() scene.reset_clipping_range() tensor_actor.display_extent(0, 1, 0, J, 0, K) if interactive: window.show(scene, reset_camera=False) tensor_actor.GetProperty().SetOpacity(1.0) if interactive: window.show(scene, reset_camera=False) npt.assert_equal(scene.GetActors().GetNumberOfItems(), 1) # Test extent big_extent = scene.GetActors().GetLastActor().GetBounds() big_extent_x = abs(big_extent[1] - big_extent[0]) tensor_actor.display(x=2) if interactive: window.show(scene, reset_camera=False) small_extent = scene.GetActors().GetLastActor().GetBounds() small_extent_x = abs(small_extent[1] - small_extent[0]) npt.assert_equal(big_extent_x > small_extent_x, True) # Test empty mask empty_actor = actor.tensor_slicer(mevals, mevecs, affine=affine, mask=np.zeros(mevals.shape[:3]), sphere=sphere, scale=.3) npt.assert_equal(empty_actor.GetMapper(), None) # Test error handling of the method when # incompatible dimension of mevals and evecs are passed. mevals = np.zeros((3, 2, 3)) mevecs = np.zeros((3, 2, 4, 3, 3)) with npt.assert_raises(RuntimeError): tensor_actor = actor.tensor_slicer(mevals, mevecs, affine=affine, mask=None, scalar_colors=None, sphere=sphere, scale=.3)
def test_odf_slicer(interactive=False): # TODO: we should change the odf_slicer to work directly # vertices and faces of a sphere rather that needing # a specific type of sphere. We can use prim_sphere # as an alternative to get_sphere. vertices, faces = prim_sphere('repulsion100', True) sphere = Sphere() sphere.vertices = vertices sphere.faces = faces shape = (11, 11, 11, 100) odfs = np.ones(shape) affine = np.array([[2.0, 0.0, 0.0, 3.0], [0.0, 2.0, 0.0, 3.0], [0.0, 0.0, 2.0, 1.0], [0.0, 0.0, 0.0, 1.0]]) mask = np.ones(odfs.shape[:3], bool) mask[:4, :4, :4] = False # Test that affine and mask work odf_actor = actor.odf_slicer(odfs, sphere=sphere, affine=affine, mask=mask, scale=.25, colormap='blues') k = 2 I, J, _ = odfs.shape[:3] odf_actor.display_extent(0, I - 1, 0, J - 1, k, k) scene = window.Scene() scene.add(odf_actor) scene.reset_camera() scene.reset_clipping_range() if interactive: window.show(scene, reset_camera=False) arr = window.snapshot(scene) report = window.analyze_snapshot(arr, find_objects=True) npt.assert_equal(report.objects, 11 * 11 - 16) # Test that global colormap works odf_actor = actor.odf_slicer(odfs, sphere=sphere, mask=mask, scale=.25, colormap='blues', norm=False, global_cm=True) scene.clear() scene.add(odf_actor) scene.reset_camera() scene.reset_clipping_range() if interactive: window.show(scene) # Test that the most basic odf_slicer instanciation works odf_actor = actor.odf_slicer(odfs) scene.clear() scene.add(odf_actor) scene.reset_camera() scene.reset_clipping_range() if interactive: window.show(scene) # Test that odf_slicer.display works properly scene.clear() scene.add(odf_actor) scene.add(actor.axes((11, 11, 11))) for i in range(11): odf_actor.display(i, None, None) if interactive: window.show(scene) for j in range(11): odf_actor.display(None, j, None) if interactive: window.show(scene) # With mask equal to zero everything should be black mask = np.zeros(odfs.shape[:3]) odf_actor = actor.odf_slicer(odfs, sphere=sphere, mask=mask, scale=.25, colormap='blues', norm=False, global_cm=True) scene.clear() scene.add(odf_actor) scene.reset_camera() scene.reset_clipping_range() if interactive: window.show(scene) # global_cm=True with colormap=None should raise an error npt.assert_raises(IOError, actor.odf_slicer, odfs, sphere=None, mask=None, scale=.25, colormap=None, norm=False, global_cm=True) vertices2, faces2 = prim_sphere('repulsion200', True) sphere2 = Sphere() sphere2.vertices = vertices2 sphere2.faces = faces2 # Dimension mismatch between sphere vertices and number # of SF coefficients will raise an error. npt.assert_raises(ValueError, actor.odf_slicer, odfs, mask=None, sphere=sphere2, scale=.25) # colormap=None and global_cm=False results in directionally encoded colors odf_actor = actor.odf_slicer(odfs, sphere=None, mask=None, scale=.25, colormap=None, norm=False, global_cm=False) scene.clear() scene.add(odf_actor) scene.reset_camera() scene.reset_clipping_range() if interactive: window.show(scene) del odf_actor del odfs
import itertools ############################################################################## # Create a sphere actor. Define the center, radius and color of a sphere. # The sphere actor is made of points (vertices) evenly distributing on a # sphere. # Let's create a scene. scene = window.Scene() ############################################################################## # The vertices are connected with triangles in order to specify the direction # of the surface normal. # ``prim_sphere`` provites a sphere with evenly distributed points vertices, triangles = primitive.prim_sphere(name='symmetric362', gen_faces=False) ############################################################################## # To be able to visualize the vertices, let's define a point actor with # green color. point_actor = actor.point(vertices, point_radius=0.01, colors=(0, 1, 0)) ############################################################################## # Normals are the vectors that are perpendicular to the surface at each # vertex. We specify the normals at the vertices to tell the system # whether triangles represent curved surfaces. normals = utils.normals_from_v_f(vertices, triangles) ##############################################################################