def make_mesh_actor(mesh, color=(0, 1, 0), opacity=0.1, vertex_scalars=None, lut=None, calc_normals=True): mesh_poly = trimesh_to_vtk(mesh.vertices, mesh.faces, mesh.graph_edges) if vertex_scalars is not None: mesh_poly.GetPointData().SetScalars(numpy_to_vtk(vertex_scalars)) mesh_mapper = vtk.vtkPolyDataMapper() if calc_normals and mesh.graph_edges is None: norms = vtk.vtkTriangleMeshPointNormals() norms.SetInputData(mesh_poly) mesh_mapper.SetInputConnection(norms.GetOutputPort()) else: mesh_mapper.SetInputData(mesh_poly) mesh_actor = vtk.vtkActor() if lut is not None: mesh_mapper.SetLookupTable(lut) mesh_mapper.ScalarVisibilityOn() mesh_actor.SetMapper(mesh_mapper) mesh_actor.GetProperty().SetColor(*color) mesh_actor.GetProperty().SetOpacity(opacity) return mesh_actor
def __init__(self, data_dir, Mreader, Vreader): # inv,inc,iniv,inxi,inxc Volume # in9,in99,inim,ins Actors planes = vtk.vtkPlane() #planes.SetOrigin(0,-1,0) #planes.SetNormal(1,0,0) planeC = vtk.vtkPlaneCollection() planeC.AddItem(planes) ###################################################### # Strip, Clip, Mapper, Actor # Stripper for getting smooth poly outlines, # gives circle and not random triangles stripper = vtk.vtkStripper() stripper.SetInputConnection(Mreader.GetOutputPort()) # Clipper for PLANE 1 Stripper 1 clipper = vtk.vtkClipClosedSurface() clipper.SetInputConnection(stripper.GetOutputPort()) #clip.Update() clipperOutline = vtk.vtkClipClosedSurface() clipperOutline.SetInputConnection(stripper.GetOutputPort()) clipperOutline.SetClippingPlanes(planeC) clipperOutline.GenerateFacesOff() clipperOutline.GenerateOutlineOn() innerNorms = vtk.vtkTriangleMeshPointNormals() innerNorms.SetInputConnection(Mreader.GetOutputPort()) innerMapper = vtk.vtkOpenGLPolyDataMapper() innerMapper.SetInputConnection(innerNorms.GetOutputPort()) innerActor = vtk.vtkActor() innerActor.SetMapper(innerMapper) clipMapper = vtk.vtkImageFlip() clipMapper = vtk.vtkPolyDataMapper() clipMapper.SetInputConnection(clipper.GetOutputPort()) clipActor = vtk.vtkActor() clipActor.SetMapper(clipMapper) clipActor.GetProperty().SetColor(1, 0.5, 0.5) clipActor.GetProperty().SetOpacity(0.5) clipActor.SetPosition(0.001, 0.001, 0.001) clipperOutlineMapper = vtk.vtkPolyDataMapper() clipperOutlineMapper.SetInputConnection(clipperOutline.GetOutputPort()) clipOutlineActor = vtk.vtkActor() clipOutlineActor.SetMapper(clipperOutlineMapper) clipOutlineActor.GetProperty().SetColor(0, 1, 0) clipOutlineActor.SetPosition(0.001, 0.001, 0.001) tfun = vtk.vtkPiecewiseFunction() tfun.AddPoint(0, 0) tfun.AddPoint(1600, 0.05) tfun.AddPoint(2500, 0.15) tfun.AddPoint(2400, 0.0) tfun.AddPoint(2540, 0.97) volumeMapper = vtk.vtkGPUVolumeRayCastMapper() volumeMapper.SetInputConnection(Vreader.GetOutputPort()) volumeMapper.SetBlendModeToComposite() volumeProperty = vtk.vtkVolumeProperty() volumeProperty.SetScalarOpacity(tfun) volumeProperty.SetInterpolationTypeToLinear() volumeProperty.ShadeOn() newvol = vtk.vtkVolume() newvol.SetMapper(volumeMapper) newvol.SetProperty(volumeProperty) ####################### planeWidget = vtk.vtkImagePlaneWidget() planeWidget.RestrictPlaneToVolumeOn() planeWidget.SetInputConnection(Mreader.GetOutputPort()) planeWidget.RestrictPlaneToVolumeOn() planeWidget.GetResliceOutput() rsx = planeWidget.GetResliceAxes() #planeWidget.SetSliceIndex(52) #planeWidget.SetOrigin(1,-1,-1) planeWidget.SetResliceInterpolateToLinear() planeWidget.PlaceWidget() #planeWidget.SetInteractor(iren) px = planeWidget.GetSlicePosition() #Cut(px) #clipActor.VisibilityOn() def UpdateMesh(obj, event): #global planes, clipActor obj.GetNormal() planes.SetNormal(obj.GetNormal()) planes.SetOrigin(obj.GetOrigin()) clipper.SetClippingPlanes(planeC) clipActor.VisibilityOn() planesS = vtk.vtkPlanes() def ClipVolumeRender(obj, event): #global planes, volumeMapper planes.SetOrigin(obj.GetOrigin()) x2 = obj.GetOrigin()[0] y2 = obj.GetOrigin()[1] z2 = obj.GetOrigin()[2] x3 = obj.GetPoint1()[0] y3 = obj.GetPoint1()[1] z3 = obj.GetPoint1()[2] x1 = obj.GetPoint2()[0] y1 = obj.GetPoint2()[1] z1 = obj.GetPoint2()[2] a1 = x2 - x1 b1 = y2 - y1 c1 = z2 - z1 a2 = x3 - x1 b2 = y3 - y1 c2 = z3 - z1 a = b1 * c2 - b2 * c1 b = a2 * c1 - a1 * c2 c = a1 * b2 - b1 * a2 d = (-a * x1 - b * y1 - c * z1) #level1 = list([x,y,z,0,x,y,z,0,x,y,z,0,x,y,z,0,x,y,z,0,x,y,z,0]) level1 = list([ -a, -b, -c, -d, -a, -b, -c, -d, -a, -b, -c, -d, -a, -b, -c, -d, -a, -b, -c, -d, -a, -b, -c, -d ]) print(a, b, c, d) planesS.SetFrustumPlanes(level1) #level2 = list([xMin,xMax, yMin,yMax,zMin,zMax]) #planesS.SetBounds(level2) #planesS.SetNormals(obj.GetNormal()) #planesS.GetPlane(0,planes) volumeMapper.SetClippingPlanes(planesS) #print(planesS.GetPoints()) newvol.VisibilityOn() ClipVolumeRender(planeWidget, "STR") UpdateMesh(planeWidget, "STR") #planeWidget.AddObserver("EndInteractionEvent", CutMesh) #planeWidget.AddObserver("StartInteractionEvent", StartInteraction) planeWidget.AddObserver("InteractionEvent", UpdateMesh) #planeWidget.AddObserver("EndInteractionEvent", CutMesh) planeWidget.AddObserver("InteractionEvent", ClipVolumeRender) #planeWidget.AddObserver("EndInteractionEvent", EndInteraction) self.planeWidget = planeWidget #self.reader = reader self.clipActor = clipActor #ren1.AddActor(VActor) self.newvol = newvol self.clipOutlineActor = clipOutlineActor
def mesh_actor(mesh, color=(0, 1, 0), opacity=0.1, vertex_colors=None, face_colors=None, lut=None, calc_normals=True, show_link_edges=False, line_width=3): """ function for producing a vtkActor from a trimesh_io.Mesh Parameters ---------- mesh : trimesh_io.Mesh a mesh to visualize color: various a len 3 iterator of a solid color to label mesh overridden by vertex_colors if passed opacity: float the opacity of the mesh (default .1) vertex_colors: np.array a np.array Nx3 list of explicit colors (where N is len(mesh.vertices)) OR a np.array of len(N) list of values to map through a colormap default (None) will use color to color mesh face_colors: np.array a np.array of Mx3 list of explicit colors (where M is the len(mesh.faces)) OR a np.array of len(M) list of values to map through a colormap (default None will use color for mesh) lut: np.array not implemented calc_normals: bool whether to calculate normals on the mesh. Default (True) will take more time, but will render a smoother mesh not compatible with sbow_link_edges. default True show_link_edges: bool whether to show the link_edges as lines. Will prevent calc_normals. default False line_width: int how thick to show lines (default 3) Returns ------- vtk.vtkActor vtkActor representing the mesh (to be passed to render_actors) """ if show_link_edges: mesh_poly = trimesh_to_vtk(mesh.vertices, mesh.faces, mesh.link_edges) else: mesh_poly = trimesh_to_vtk(mesh.vertices, mesh.faces, None) if vertex_colors is not None: vertex_color, map_vertex_color = process_colors( vertex_colors, mesh.vertices) vtk_vert_colors = numpy_to_vtk(vertex_color) vtk_vert_colors.SetName('colors') mesh_poly.GetPointData().SetScalars(vtk_vert_colors) if face_colors is not None: face_color, map_face_colors = process_colors(face_colors, mesh.faces) vtk_face_colors = numpy_to_vtk(face_color) vtk_face_colors.SetName('colors') mesh_poly.GetCellData().SetScalars(vtk_face_colors) mesh_mapper = vtk.vtkPolyDataMapper() if calc_normals and (not show_link_edges): norms = vtk.vtkTriangleMeshPointNormals() norms.SetInputData(mesh_poly) mesh_mapper.SetInputConnection(norms.GetOutputPort()) else: mesh_mapper.SetInputData(mesh_poly) mesh_actor = vtk.vtkActor() if lut is not None: mesh_mapper.SetLookupTable(lut) if face_colors is not None: if map_face_colors: mesh_mapper.SelectColorArray('colors') mesh_mapper.ScalarVisibilityOn() mesh_actor.SetMapper(mesh_mapper) mesh_actor.GetProperty().SetLineWidth(line_width) mesh_actor.GetProperty().SetColor(*color) mesh_actor.GetProperty().SetOpacity(opacity) return mesh_actor
program.SetUniform3f("diffuseColorUniform", diffuseColor) renWin = vtk.vtkRenderWindow() renWin.SetSize(400, 400) iren = vtk.vtkRenderWindowInteractor() iren.SetRenderWindow(renWin) ren = vtk.vtkRenderer() ren.SetBackground(0.0, 0.0, 0.0) ren.GradientBackgroundOn() renWin.AddRenderer(ren) actor = vtk.vtkActor() ren.AddActor(actor) reader = vtk.vtkPLYReader() reader.SetFileName("" + str(vtkGetDataRoot()) + "/Data/dragon.ply") norms = vtk.vtkTriangleMeshPointNormals() norms.SetInputConnection(reader.GetOutputPort()) mapper = vtk.vtkOpenGLPolyDataMapper() mapper.SetInputConnection(norms.GetOutputPort()) actor.SetMapper(mapper) actor.GetProperty().SetAmbientColor(0.2, 0.2, 1.0) actor.GetProperty().SetDiffuseColor(1.0, 0.65, 0.7) actor.GetProperty().SetSpecularColor(1.0, 1.0, 1.0) actor.GetProperty().SetSpecular(0.5) actor.GetProperty().SetDiffuse(0.7) actor.GetProperty().SetAmbient(0.5) actor.GetProperty().SetSpecularPower(20.0) actor.GetProperty().SetOpacity(1.0) mapper.SetVertexShaderCode( "//VTK::System::Dec\n"
program.SetUniform3f("diffuseColorUniform", diffuseColor) renWin = vtk.vtkRenderWindow() renWin.SetSize(400, 400) iren = vtk.vtkRenderWindowInteractor() iren.SetRenderWindow(renWin) ren = vtk.vtkRenderer() ren.SetBackground(0.0, 0.0, 0.0) ren.GradientBackgroundOn() renWin.AddRenderer(ren) actor = vtk.vtkActor() ren.AddActor(actor) reader = vtk.vtkPLYReader() reader.SetFileName("" + str(vtkGetDataRoot()) + "/Data/dragon.ply") norms = vtk.vtkTriangleMeshPointNormals() norms.SetInputConnection(reader.GetOutputPort()) mapper = vtk.vtkOpenGLPolyDataMapper() mapper.SetInputConnection(norms.GetOutputPort()) actor.SetMapper(mapper) actor.GetProperty().SetAmbientColor(0.2, 0.2, 1.0) actor.GetProperty().SetDiffuseColor(1.0, 0.65, 0.7) actor.GetProperty().SetSpecularColor(1.0, 1.0, 1.0) actor.GetProperty().SetSpecular(0.5) actor.GetProperty().SetDiffuse(0.7) actor.GetProperty().SetAmbient(0.5) actor.GetProperty().SetSpecularPower(20.0) actor.GetProperty().SetOpacity(1.0) sp = actor.GetShaderProperty() sp.SetVertexShaderCode(
def __init__(self, data_dir,reader): planes = vtk.vtkPlane() planeC = vtk.vtkPlaneCollection() planeC.AddItem(planes) # Stripper for getting smooth poly outlines, # gives circle and not random triangles stripper = vtk.vtkStripper() stripper.SetInputConnection(reader.GetOutputPort()) # Clipper for PLANE clipper = vtk.vtkClipClosedSurface() clipper.SetInputConnection(stripper.GetOutputPort()) clipper.SetClippingPlanes(planeC) clipMapper = vtk.vtkImageFlip() clipMapper = vtk.vtkPolyDataMapper() clipMapper.SetInputConnection(clipper.GetOutputPort()) clipActor = vtk.vtkActor() clipActor.SetMapper(clipMapper) clipActor.GetProperty().SetColor(0.5,0.5,0.5) clipActor.GetProperty().SetOpacity(0.5) clipActor.SetPosition(0.001,0.001,0.001) clipperOutline = vtk.vtkClipClosedSurface() clipperOutline.SetInputConnection(stripper.GetOutputPort()) clipperOutline.SetClippingPlanes(planeC) clipperOutline.GenerateFacesOff() clipperOutline.GenerateOutlineOn() innerNorms = vtk.vtkTriangleMeshPointNormals() innerNorms.SetInputConnection(reader.GetOutputPort()) innerMapper = vtk.vtkOpenGLPolyDataMapper() innerMapper.SetInputConnection(innerNorms.GetOutputPort()) innerActor = vtk.vtkActor() innerActor.SetMapper(innerMapper) clipperOutlineMapper = vtk.vtkPolyDataMapper() clipperOutlineMapper.SetInputConnection(clipperOutline.GetOutputPort()) clipOutlineActor = vtk.vtkActor() clipOutlineActor.SetMapper(clipperOutlineMapper) clipOutlineActor.GetProperty().SetColor(0,1,0) clipOutlineActor.SetPosition(0.001,0.001,0.001) planeWidget = vtk.vtkImagePlaneWidget() planeWidget.SetInputConnection(reader.GetOutputPort()) planeWidget.RestrictPlaneToVolumeOn() planeWidget.GetResliceOutput() rsx = planeWidget.GetResliceAxes() #planeWidget.SetSliceIndex(52) #planeWidget.SetOrigin(1,-1,-1) planeWidget.SetResliceInterpolateToLinear() self.clipActor = clipActor self.planes = planes self.planeWidget = planeWidget self.innerActor = innerActor self.clipOutlineActor = clipOutlineActor px = planeWidget.GetSlicePosition() #Cut(px) #clipActor.VisibilityOn() def UpdateMesh(obj, event): obj.GetNormal() self.planes.SetNormal(obj.GetNormal()) self.planes.SetOrigin(obj.GetOrigin()) self.clipActor.VisibilityOn() #planeWidget.AddObserver("EndInteractionEvent", CutMesh) planeWidget.AddObserver("InteractionEvent", UpdateMesh) self.planeWidget = planeWidget