def vertices_triangles(a, b, c): edge_list = [] vert_list = [[0, 0, c], [0, 0, c], [0, 0, c], [0, 0, c]] my_polydata = vtk.vtkPolyData() #Initialize the polydata for v in v_range: z_new = c * np.cos(np.deg2rad(v)) n = len(vert_list) vert_list.append([a * np.sin(np.deg2rad(v)), 0.0, z_new]) vert_list.append([0.0, b * np.sin(np.deg2rad(v)), z_new]) vert_list.append([-1 * a * np.sin(np.deg2rad(v)), 0.0, z_new]) vert_list.append([0.0, -1 * b * np.sin(np.deg2rad(v)), z_new]) edge_list.append([n - 4, n, n - 3]) edge_list.append([n, n + 1, n - 3]) edge_list.append([n - 3, n + 1, n - 2]) edge_list.append([n + 1, n + 2, n - 2]) edge_list.append([n - 2, n + 2, n - 1]) edge_list.append([n + 2, n + 3, n - 1]) edge_list.append([n - 1, n + 3, n - 4]) edge_list.append([n + 3, n, n - 4]) #Converting the list to an array my_vertices = np.array(vert_list) my_triangles = np.array(edge_list) #Converting the vertices and faces to a Polydata ut_vtk.set_polydata_vertices(my_polydata, my_vertices) ut_vtk.set_polydata_triangles(my_polydata, my_triangles.astype('i8')) # Setting colors for the sphere sphere_vertices = ut_vtk.get_polydata_vertices(my_polydata) colors = sphere_vertices * 255 ut_vtk.set_polydata_colors(my_polydata, colors) sphere_actor = ut_vtk.get_actor_from_polydata(my_polydata) return sphere_actor
Create a cube with vertices and triangles as numpy arrays """ my_vertices = np.array([[0.0, 0.0, 0.0], [0.0, 0.0, 1.0], [0.0, 1.0, 0.0], [0.0, 1.0, 1.0], [1.0, 0.0, 0.0], [1.0, 0.0, 1.0], [1.0, 1.0, 0.0], [1.0, 1.0, 1.0]]) # the data type for vtk is needed to mention here, numpy.int64 my_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='i8') """ Set vertices and triangles in the ``vtkPolyData`` """ ut_vtk.set_polydata_vertices(my_polydata, my_vertices) ut_vtk.set_polydata_triangles(my_polydata, my_triangles) """ Save the ``vtkPolyData`` """ file_name = "my_cube.vtk" io_vtk.save_polydata(my_polydata, file_name) print("Surface saved in " + file_name) """ Load the ``vtkPolyData`` """ cube_polydata = io_vtk.load_polydata(file_name) """ add color based on vertices position
[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='i8') """ Set vertices and triangles in the ``vtkPolyData`` """ ut_vtk.set_polydata_vertices(my_polydata, my_vertices) ut_vtk.set_polydata_triangles(my_polydata, my_triangles) """ Save the ``vtkPolyData`` """ file_name = "my_cube.vtk" io_vtk.save_polydata(my_polydata, file_name) print("Surface saved in " + file_name) """ Load the ``vtkPolyData`` """ cube_polydata = io_vtk.load_polydata(file_name)
def sphere(centers, colors, radii=1., theta=16, phi=16, vertices=None, faces=None): """ Visualize one or many spheres with different colors and radii Parameters ---------- centers : ndarray, shape (N, 3) colors : ndarray (N,3) or (N, 4) or tuple (3,) or tuple (4,) RGB or RGBA (for opacity) R, G, B and A should be at the range [0, 1] radii : float or ndarray, shape (N,) theta : int phi : int vertices : ndarray, shape (N, 3) faces : ndarray, shape (M, 3) If faces is None then a sphere is created based on theta and phi angles If not then a sphere is created with the provided vertices and faces. Returns ------- vtkActor Examples -------- >>> from dipy.viz import window, actor >>> ren = window.Renderer() >>> centers = np.random.rand(5, 3) >>> sphere_actor = actor.sphere(centers, window.colors.coral) >>> ren.add(sphere_actor) >>> # window.show(ren) """ if np.array(colors).ndim == 1: colors = np.tile(colors, (len(centers), 1)) if isinstance(radii, (float, int)): radii = radii * np.ones(len(centers), dtype='f8') pts = numpy_to_vtk_points(np.ascontiguousarray(centers)) cols = numpy_to_vtk_colors(255 * np.ascontiguousarray(colors)) cols.SetName('colors') radii_fa = numpy_support.numpy_to_vtk( np.ascontiguousarray(radii.astype('f8')), deep=0) radii_fa.SetName('rad') polydata_centers = vtk.vtkPolyData() polydata_sphere = vtk.vtkPolyData() if faces is None: src = vtk.vtkSphereSource() src.SetRadius(1) src.SetThetaResolution(theta) src.SetPhiResolution(phi) else: ut_vtk.set_polydata_vertices(polydata_sphere, vertices) ut_vtk.set_polydata_triangles(polydata_sphere, faces) polydata_centers.SetPoints(pts) polydata_centers.GetPointData().AddArray(radii_fa) polydata_centers.GetPointData().SetActiveScalars('rad') polydata_centers.GetPointData().AddArray(cols) glyph = vtk.vtkGlyph3D() if faces is None: glyph.SetSourceConnection(src.GetOutputPort()) else: if major_version <= 5: glyph.SetSource(polydata_sphere) else: glyph.SetSourceData(polydata_sphere) if major_version <= 5: glyph.SetInput(polydata_centers) else: glyph.SetInputData(polydata_centers) glyph.Update() mapper = vtk.vtkPolyDataMapper() if major_version <= 5: mapper.SetInput(glyph.GetOutput()) else: mapper.SetInputData(glyph.GetOutput()) mapper.SetScalarModeToUsePointFieldData() mapper.SelectColorArray('colors') actor = vtk.vtkActor() actor.SetMapper(mapper) return actor