def surface_clip(polydata, type: int): BoundingBox = polydata.GetBounds() center = [0] * 3 normal = [0] * 3 center[0] = (BoundingBox[0] + BoundingBox[1]) / 2 center[1] = (BoundingBox[2] + BoundingBox[3]) / 2 if (type == 0): center[2] = BoundingBox[4] + 120 normal[2] = -1 else: center[2] = BoundingBox[5] - 120 normal[2] = 1 plane1 = vtk.vtkPlane() plane1.SetOrigin(center[0], center[1], center[2]) plane1.SetNormal(normal) planes = vtk.vtkPlaneCollection() planes.AddItem(plane1) clipper = vtk.vtkClipClosedSurface() clipper.SetInputData(polydata) clipper.SetClippingPlanes(planes) clipper.SetScalarModeToColors() clipper.SetActivePlaneId(0) clipper.Update() return clipper.GetOutput()
def getClosedCuttedModel(self, cutPlanes, polyData): clipper = vtk.vtkClipClosedSurface() clipper.SetClippingPlanes(cutPlanes) clipper.SetActivePlaneId(2) clipper.SetInputData(polyData) clipper.Update() cuttedPolyData = clipper.GetOutput() return cuttedPolyData
def opImplement(self): clipper = vtk.vtkClipClosedSurface(); clipper.TriangulationErrorDisplayOn(); clipper.SetInputData(self.data); capPlanes = vtk.vtkPlaneCollection() firstclipPlane = vtk.vtkPlane(); #We should change following 2 lines only #print("Plane normal : {}".format(self.normal)) firstclipPlane.SetNormal(*self.normal); firstclipPlane.SetOrigin(*self.origin); #End possible changes capPlanes.AddItem(firstclipPlane) clipper.SetClippingPlanes(capPlanes); clipper.Update(); resultOne = clipper.GetOutput(); return resultOne;
def Pjauti(self, data): plane1 = vtk.vtkPlane() plane1.SetNormal(1, 0, 0) plane1.SetOrigin(0, 0, 0) plane2 = vtk.vtkPlane() #plane2.SetNormal(-0.5,0.5,0) plane2.SetNormal(0, 1, 0) plane2.SetOrigin(0, 0, 0) coll = vtk.vtkPlaneCollection() coll.AddItem(plane1) coll.AddItem(plane2) clip1 = vtk.vtkClipClosedSurface() clip1.SetClippingPlanes(coll) clip1.SetInputData(data) clip1.Update() return clip1.GetOutput()
def computeVolume(pdata, orientation): bd = pdata.GetBounds() # Define the cutting plane plane = vtk.vtkPlane() if (orientation == 'z'): plane.SetOrigin(0, 0, bd[5] - 0.1) plane.SetNormal(0, 0, -1) elif (orientation == 'x'): plane.SetOrigin(bd[0] + 0.1, 0, 0) plane.SetNormal(1, 0, 0) elif (orientation == 'n'): plane.SetOrigin(0, 0, bd[5] + 100) plane.SetNormal(0, 0, -1) # Need a plane collection for clipping planeCollection = vtk.vtkPlaneCollection() planeCollection.AddItem(plane) # The clipper generates a clipped polygonial model clipper = vtk.vtkClipClosedSurface() clipper.SetClippingPlanes(planeCollection) if (vtk.vtkVersion.GetVTKMajorVersion() < 6): clipper.SetInput(pdata) else: clipper.SetInputData(pdata) clipper.SetGenerateFaces(1) clipper.SetScalarModeToLabels() clipper.Update() # Get volume using mass property massprop = vtk.vtkMassProperties() if (vtk.vtkVersion.GetVTKMajorVersion() < 6): massprop.SetInput(clipper.GetOutput()) else: massprop.SetInputData(clipper.GetOutput()) return massprop.GetVolume()
sphere.SetRadius(1) sphere.SetPhiResolution(10) sphere.SetThetaResolution(10) plane1 = vtk.vtkPlane() plane1.SetOrigin(0.3,0.3,0.3) plane1.SetNormal(-1,-1,-1) plane2 = vtk.vtkPlane() plane2.SetOrigin(0.5,0,0) plane2.SetNormal(-1,0,0) planes = vtk.vtkPlaneCollection() planes.AddItem(plane1) planes.AddItem(plane2) # stripper just increases coverage stripper = vtk.vtkStripper() stripper.SetInputConnection(sphere.GetOutputPort()) clipper = vtk.vtkClipClosedSurface() clipper.SetInputConnection(stripper.GetOutputPort()) clipper.SetClippingPlanes(planes) clipperOutline = vtk.vtkClipClosedSurface() clipperOutline.SetInputConnection(stripper.GetOutputPort()) clipperOutline.SetClippingPlanes(planes) clipperOutline.GenerateFacesOff() clipperOutline.GenerateOutlineOn() sphereMapper = vtk.vtkPolyDataMapper() sphereMapper.SetInputConnection(clipper.GetOutputPort()) clipperOutlineMapper = vtk.vtkPolyDataMapper() clipperOutlineMapper.SetInputConnection(clipperOutline.GetOutputPort()) clipActor = vtk.vtkActor() clipActor.SetMapper(sphereMapper) clipActor.GetProperty().SetColor(0.8,0.05,0.2) clipOutlineActor = vtk.vtkActor()
def ClipClosedSurface(): colors = vtk.vtkNamedColors() # Create a sphere sphereSource = vtk.vtkSphereSource() sphereSource.SetThetaResolution(20) sphereSource.SetPhiResolution(11) sphereSource.Update() polyData = sphereSource.GetOutput() center = polyData.GetCenter() plane1 = vtk.vtkPlane() plane1.SetOrigin(center[0], center[1], center[2]) plane1.SetNormal(0.0, -1.0, 0.0) plane2 = vtk.vtkPlane() plane2.SetOrigin(center[0], center[1], center[2]) plane2.SetNormal(0.0, 0.0, 1.0) plane3 = vtk.vtkPlane() plane3.SetOrigin(center[0], center[1], center[2]) plane3.SetNormal(-1.0, 0.0, 0.0) planes = vtk.vtkPlaneCollection() planes.AddItem(plane1) planes.AddItem(plane2) planes.AddItem(plane3) clipper = vtk.vtkClipClosedSurface() clipper.SetInputData(polyData) clipper.SetClippingPlanes(planes) # clipper.SetActivePlaneId(2) clipper.SetScalarModeToColors() clipper.SetClipColor(colors.GetColor3d("Green")) clipper.SetBaseColor(colors.GetColor3d("Red")) # clipper.SetActivePlaneColor(colors.GetColor3d("SandyBrown")) clipMapper = vtk.vtkDataSetMapper() clipMapper.SetInputConnection(clipper.GetOutputPort()) clipActor = vtk.vtkActor() clipActor.SetMapper(clipMapper) clipActor.GetProperty().SetColor(1.0000, 0.3882, 0.2784) clipActor.GetProperty().SetInterpolationToFlat() # Create graphics stuff ren1 = vtk.vtkRenderer() ren1.SetBackground(colors.GetColor3d("SteelBlue")) renWin = vtk.vtkRenderWindow() renWin.AddRenderer(ren1) renWin.SetSize(512, 512) renWin.SetWindowName("ClipClosedSurface") iren = vtk.vtkRenderWindowInteractor() iren.SetRenderWindow(renWin) # Add the actors to the renderer, set the background and size # ren1.AddActor(clipActor) # Generate an interesting view # ren1.ResetCamera() ren1.GetActiveCamera().Azimuth(120) ren1.GetActiveCamera().Elevation(30) ren1.GetActiveCamera().Dolly(1.0) ren1.ResetCameraClippingRange() renWin.Render() iren.Initialize() iren.Start()
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 LerSTL(self, path): mesh = vtk.vtkSTLReader() mesh.SetFileName(path) mesh.Update() # faz a mapeação dos dados do stl stlMapper = vtk.vtkPolyDataMapper() stlMapper.SetInputConnection(mesh.GetOutputPort()) #represenatação da visualização, neste caso sem ver. stlActor = vtk.vtkLODActor() stlActor.SetMapper(stlMapper) #------------------------------------------------------------------- # construção do plano para corte plane = self.plane = vtk.vtkPlane() # chamar a função plano de corte PlaneCollection = vtk.vtkPlaneCollection( ) # cria uma colecção de planos PlaneCollection.AddItem( plane) # estou a adicionar o plano de corte a essa colecção # faz o corte e fecha logo de imediato o polidata self.clipper = clipper = vtk.vtkClipClosedSurface() clipper.SetInputData(mesh.GetOutput()) clipper.SetClippingPlanes( PlaneCollection) #recebe os meus planos de corte clipper.SetBaseColor(0.0, 0.0, 1.0) clipper.SetClipColor(0.0, 0.0, 1.0) clipper.SetScalarModeToColors() clipper.GenerateFacesOn() #gera a face que fecha o polidata # mapeamento da região da polidata cortada. clipMapper = vtk.vtkPolyDataMapper() clipMapper.SetInputData(clipper.GetOutput()) clipMapper.ScalarVisibilityOn() #------------------------------------------- #--------------da a cor a parte cortada---- backProp = vtk.vtkProperty() backProp.SetDiffuseColor((0.0, 1.0, 0.0)) #--------------------------------------------------- # faz a visualização do polidata cortada-- clipActor = vtk.vtkActor() clipActor.SetMapper(clipMapper) clipActor.GetProperty().SetColor((0.0, 1.0, 0.0)) #-------------------------------------------------------- # esta é a representação do wigdet do plano de corte rep = vtk.vtkImplicitPlaneRepresentation() rep.PlaceWidget(stlActor.GetBounds() ) #é a posição dos limite aonde o plano de corte fica rep.SetPlaceFactor(1.25) # expandir os limite do widget para 25% rep.UseBoundsOn() # mostra a fronteira rep.NormalToZAxisOn() #da a posição de corte, no sentido de z rep.OutlineTranslationOff( ) # nao posso mexer o widget, so posso mexer o plano de corte rep.PlaceWidget(mesh.GetOutput().GetBounds() ) #mostra aonde vai ser posicionado o widget rep.OutsideBoundsOn() rep.VisibilityOn() #............ # chama o proprio widget , para interação para fazer o corte self.planeWidget = vtk.vtkImplicitPlaneWidget2() self.planeWidget.SetInteractor(self.Interactor) self.planeWidget.SetRepresentation(rep) # adiconar a representação self.planeWidget.SetEnabled(0) self.planeWidget.AddObserver( "EndInteractionEvent", self.myCallback) # vai tratar o evento para fazero corte self.planeWidget.On() #--------------------------------------- # adiconar os actores ao renderer para visualização self.renderer.AddActor(stlActor) self.renderer.AddActor(clipActor) #------------------------------------------- #posicionar a camera para poder visualizar a volume todo self.renderer.ResetCamera() #---------------------------------------- #faze a renderização total self.Interactor.Render() #----------------------------- # liga o plano para fazer funcionar o widget- self.VerPlano = True
def __init__(self, runname, input_file, output_file, radius, x, y, z): self.runname = runname reader = vtk.vtkPolyDataReader() reader.SetFileName(input_file) reader.Update() planes = vtk.vtkPlaneCollection() top = vtk.vtkPlane() top.SetNormal(0, 0, -1) top.SetOrigin(x, y, z + radius) planes.AddItem(top) bottom = vtk.vtkPlane() bottom.SetNormal(0, 0, 1) bottom.SetOrigin(x, y, z - radius) planes.AddItem(bottom) front = vtk.vtkPlane() front.SetNormal(0, -1, 0) front.SetOrigin(x, y + radius, z) planes.AddItem(front) back = vtk.vtkPlane() back.SetNormal(0, 1, 0) back.SetOrigin(x, y - radius, z) planes.AddItem(back) right = vtk.vtkPlane() right.SetNormal(1, 0, 0) right.SetOrigin(x - radius, y, z) planes.AddItem(right) left = vtk.vtkPlane() left.SetNormal(-1, 0, 0) left.SetOrigin(x + radius, y, z) planes.AddItem(left) clipper = vtk.vtkClipClosedSurface() clipper.SetClippingPlanes(planes) clipper.SetInputConnection(reader.GetOutputPort()) connectivity = vtk.vtkPolyDataConnectivityFilter() connectivity.SetExtractionModeToAllRegions() connectivity.SetInputConnection(clipper.GetOutputPort()) connectivity.Update() specified = vtk.vtkPolyDataConnectivityFilter() specified.SetExtractionModeToSpecifiedRegions() specified.SetInputConnection(clipper.GetOutputPort()) tester = vtk.vtkPolyDataConnectivityFilter() tester.SetExtractionModeToSpecifiedRegions() tester.SetInputConnection(clipper.GetOutputPort()) dims = {} for i in range(connectivity.GetNumberOfExtractedRegions()): tester.AddSpecifiedRegion(i) tester.Update() bounds = tester.GetOutput().GetBounds() if bounds[0] + radius - delta < x or bounds[1] - radius + delta > x or \ bounds[2] + radius - delta < y or bounds[3] - radius + delta > y or \ bounds[4] + radius - delta < z or bounds[5] - radius + delta > z: mindim = min(bounds[1] - bounds[0], bounds[3] - bounds[2], bounds[5] - bounds[4]) dims[i] = mindim tester.DeleteSpecifiedRegion(i) for i in sorted(dims.iteritems(), key=lambda i: i[1], reverse=True)[0:5]: print i specified.AddSpecifiedRegion(i[0]) #cleaner = vtk.vtkCleanPolyData() #cleaner.SetInputConnection(specified.GetOutputPort()) #cleaner.SetTolerance(1e-5) #orientator = vtk.vtkPolyDataNormals() #orientator.SetInputConnection(clipper.GetOutputPort()) #orientator.ConsistencyOn() #orientator.AutoOrientNormalsOn() writer = vtk.vtkSTLWriter() writer.SetFileName(output_file + 'tmp1.stl') writer.SetInputConnection(clipper.GetOutputPort()) writer.Write() with open("/tmp/test.mlx", "w") as f: f.write(""" <FilterScript> <filter name="Remove Zero Area Faces"/> <filter name="Re-Orient all faces coherentely"/> <filter name="Quadric Edge Collapse Decimation"> <Param type="RichInt" value="30000" name="TargetFaceNum"/> <Param type="RichFloat" value="0" name="TargetPerc"/> <Param type="RichFloat" value="0.3" name="QualityThr"/> <Param type="RichBool" value="false" name="PreserveBoundary"/> <Param type="RichBool" value="false" name="PreserveNormal"/> <Param type="RichBool" value="true" name="PreserveTopology"/> <Param type="RichBool" value="true" name="OptimalPlacement"/> <Param type="RichBool" value="false" name="PlanarQuadric"/> <Param type="RichBool" value="false" name="QualityWeight"/> <Param type="RichBool" value="true" name="AutoClean"/> <Param type="RichBool" value="false" name="Selected"/> </filter> </FilterScript> """) subprocess.call([ "meshlabserver", "-i", output_file + 'tmp1.stl', "-o", output_file + '.off', "-s", "/tmp/test.mlx" ]) #reader = vtk.vtkSTLReader() #reader.SetFileName(output_file + 'tmp2.stl') #reader.Update() writer = vtk.vtkSTLWriter() writer.SetFileName(output_file) writer.SetInputConnection(specified.GetOutputPort()) writer.Write() vtk_writer = vtk.vtkXMLPolyDataWriter() vtk_writer.SetFileName(output_file + ".vtu") vtk_writer.SetInputConnection(specified.GetOutputPort()) vtk_writer.Write()
def create3D(): points = [] ilgis = 0 for x in range(COUNT): ilgis, points = GetForma(ilgis, points) points.append([0, ilgis]) points.append([0, 0]) points.append([R / 2.0, 0]) rodData = "translate([0," + str(R / 4.0) + "," + str( R / 4.0 ) + "])rotate ([-90,0,0]) rotate_extrude($fn=200) polygon( points=" + str( points) + " );\n" rod = open("rod.scad", "w") rod.write(rodData) rod.close() call(["openscad", "-o", "rod.stl", "rod.scad"]) read = vtk.vtkSTLReader() read.SetFileName("rod.stl") read.Update() tran = vtk.vtkTransform() bounds = read.GetOutput().GetBounds() tran.Translate( -read.GetOutput().GetBounds()[0] - (bounds[1] - bounds[0]) / 2.0, -read.GetOutput().GetBounds()[2] - (bounds[3] - bounds[2]) / 2.0, -read.GetOutput().GetBounds()[4] - (bounds[5] - bounds[4]) / 2.0) tfilter = vtk.vtkTransformFilter() tfilter.SetInputData(read.GetOutput()) tfilter.SetTransform(tran) tfilter.Update() w = vtk.vtkSTLWriter() w.SetFileName("rod.stl") w.SetInputData(tfilter.GetOutput()) w.Update() plane1 = vtk.vtkPlane() plane1.SetNormal(1, 0, 0) plane1.SetOrigin(0, 0, 0) plane2 = vtk.vtkPlane() plane2.SetNormal(0, 0, 1) plane2.SetOrigin(0, 0, 0) coll = vtk.vtkPlaneCollection() coll.AddItem(plane1) coll.AddItem(plane2) clip1 = vtk.vtkClipClosedSurface() clip1.SetClippingPlanes(coll) clip1.GenerateFacesOn() clip1.SetInputConnection(tfilter.GetOutputPort()) clip1.Update() beton = vtk.vtkCubeSource() beton.SetBounds(-boxSize, boxSize, -ilgis / 2.0, ilgis / 2.0, -boxSize, boxSize) beton.Update() clip2 = vtk.vtkClipClosedSurface() clip2.SetClippingPlanes(coll) clip2.GenerateFacesOn() clip2.SetInputConnection(beton.GetOutputPort()) clip2.Update() box = vtk.vtkCubeSource() b = clip2.GetOutput().GetBounds() bbbb = clip1.GetOutput().GetBounds() box.SetBounds(b[0], b[1], bbbb[2] - (bbbb[3] - bbbb[2]) * CalcBoxSizeProc / 1000.0, bbbb[3] + (bbbb[3] - bbbb[2]) * CalcBoxSizeProc / 1000.0, b[4], b[5]) box.Update() SaveToSTL(box.GetOutput(), "box.stl") SaveToSTL(clip2.GetOutput(), "beton.stl") SaveToSTL(clip1.GetOutput(), "rod.stl") # create a rendering window and renderer ren = vtk.vtkRenderer() renWin = vtk.vtkRenderWindow() renWin.AddRenderer(ren) # create a renderwindowinteractor iren = vtk.vtkRenderWindowInteractor() iren.SetRenderWindow(renWin) mapper = vtk.vtkPolyDataMapper() mapper.SetInputConnection(clip1.GetOutputPort()) actor = vtk.vtkActor() actor.SetMapper(mapper) ren.AddActor(actor) mapper1 = vtk.vtkPolyDataMapper() mapper1.SetInputConnection(clip2.GetOutputPort()) actor1 = vtk.vtkActor() actor1.SetMapper(mapper1) ren.AddActor(actor1)
volumeMapper1.SetClippingPlanes(clippingPlanes) volume1 = vtk.vtkVolume() volume1.SetMapper(volumeMapper1) volume1.SetProperty(volumeProperty) outline1 = vtk.vtkVolumeOutlineSource() outline1.SetVolumeMapper(volumeMapper1) outline1.GenerateFacesOn() outline1.GenerateScalarsOn() preTrans1 = vtk.vtkTransformPolyDataFilter() preTrans1.SetInputConnection(outline1.GetOutputPort()) preTrans1.SetTransform(userTrans) outlineClip1 = vtk.vtkClipClosedSurface() outlineClip1.SetInputConnection(preTrans1.GetOutputPort()) outlineClip1.SetClippingPlanes(clippingPlanes) outlineClip1.GenerateFacesOff() outlineClip1.GenerateOutlineOn() outlineClip1.SetScalarModeToColors() outlineClip1.SetClipColor(1, 1, 0) outlineClip1.SetActivePlaneId(2) outlineClip1.SetActivePlaneColor(0, 1, 0) postTrans1 = vtk.vtkTransformPolyDataFilter() postTrans1.SetInputConnection(outlineClip1.GetOutputPort()) postTrans1.SetTransform(userTrans.GetInverse()) outlineMapper1 = vtk.vtkDataSetMapper() outlineMapper1.SetInputConnection(postTrans1.GetOutputPort())
sphere.SetRadius(1) sphere.SetPhiResolution(10) sphere.SetThetaResolution(10) plane1 = vtk.vtkPlane() plane1.SetOrigin(0.3, 0.3, 0.3) plane1.SetNormal(-1, -1, -1) plane2 = vtk.vtkPlane() plane2.SetOrigin(0.5, 0, 0) plane2.SetNormal(-1, 0, 0) planes = vtk.vtkPlaneCollection() planes.AddItem(plane1) planes.AddItem(plane2) # stripper just increases coverage stripper = vtk.vtkStripper() stripper.SetInputConnection(sphere.GetOutputPort()) clipper = vtk.vtkClipClosedSurface() clipper.SetInputConnection(stripper.GetOutputPort()) clipper.SetClippingPlanes(planes) clipperOutline = vtk.vtkClipClosedSurface() clipperOutline.SetInputConnection(stripper.GetOutputPort()) clipperOutline.SetClippingPlanes(planes) clipperOutline.GenerateFacesOff() clipperOutline.GenerateOutlineOn() sphereMapper = vtk.vtkPolyDataMapper() sphereMapper.SetInputConnection(clipper.GetOutputPort()) clipperOutlineMapper = vtk.vtkPolyDataMapper() clipperOutlineMapper.SetInputConnection(clipperOutline.GetOutputPort()) clipActor = vtk.vtkActor() clipActor.SetMapper(sphereMapper) clipActor.GetProperty().SetColor(0.8, 0.05, 0.2) clipOutlineActor = vtk.vtkActor()
renWin.SetSize(600, 200) renWin.AddRenderer(ren1) renWin.AddRenderer(ren2) renWin.AddRenderer(ren3) renWin.SetMultiSamples(0) property0 = vtk.vtkProperty() property0.SetDiffuseColor(0.95, 0.90, 0.70) filename = VTK_DATA_ROOT + "/Data/mni-surface-mesh.obj" asciiReader = vtk.vtkMNIObjectReader() property1 = asciiReader.GetProperty() if (asciiReader.CanReadFile(str(filename)) != 0): asciiReader.SetFileName(str(filename)) # this is just to remove the normals, to increase coverage, # i.e. by forcing the writer to generate normals removeNormals = vtk.vtkClipClosedSurface() removeNormals.SetInputConnection(asciiReader.GetOutputPort()) # this is to make triangle strips, also to increase coverage, # because it forces the writer to decompose the strips stripper = vtk.vtkStripper() stripper.SetInputConnection(removeNormals.GetOutputPort()) # test binary writing and reading for polygons binaryWriter = vtk.vtkMNIObjectWriter() binaryWriter.SetInputConnection(stripper.GetOutputPort()) binaryWriter.SetFileName("mni-surface-mesh-binary.obj") binaryWriter.SetProperty(property0) binaryWriter.SetFileTypeToBinary() binaryWriter.Write()
def __init__(self, runname, input_file, output_file, radius, x, y, z): self.runname = runname reader = vtk.vtkPolyDataReader() reader.SetFileName(input_file) reader.Update() planes = vtk.vtkPlaneCollection() top = vtk.vtkPlane() top.SetNormal(0, 0, -1) top.SetOrigin(x, y, z + radius) planes.AddItem(top) bottom = vtk.vtkPlane() bottom.SetNormal(0, 0, 1) bottom.SetOrigin(x, y, z - radius) planes.AddItem(bottom) front = vtk.vtkPlane() front.SetNormal(0, -1, 0) front.SetOrigin(x, y + radius, z) planes.AddItem(front) back = vtk.vtkPlane() back.SetNormal(0, 1, 0) back.SetOrigin(x, y - radius, z) planes.AddItem(back) right = vtk.vtkPlane() right.SetNormal(1, 0, 0) right.SetOrigin(x - radius, y, z) planes.AddItem(right) left = vtk.vtkPlane() left.SetNormal(-1, 0, 0) left.SetOrigin(x + radius, y, z) planes.AddItem(left) clipper = vtk.vtkClipClosedSurface() clipper.SetClippingPlanes(planes) clipper.SetInputConnection(reader.GetOutputPort()) connectivity = vtk.vtkPolyDataConnectivityFilter() connectivity.SetExtractionModeToAllRegions() connectivity.SetInputConnection(clipper.GetOutputPort()) connectivity.Update() specified = vtk.vtkPolyDataConnectivityFilter() specified.SetExtractionModeToSpecifiedRegions() specified.SetInputConnection(clipper.GetOutputPort()) tester = vtk.vtkPolyDataConnectivityFilter() tester.SetExtractionModeToSpecifiedRegions() tester.SetInputConnection(clipper.GetOutputPort()) dims = {} for i in range(connectivity.GetNumberOfExtractedRegions()): tester.AddSpecifiedRegion(i) tester.Update() bounds = tester.GetOutput().GetBounds() if bounds[0] + radius - delta < x or bounds[1] - radius + delta > x or \ bounds[2] + radius - delta < y or bounds[3] - radius + delta > y or \ bounds[4] + radius - delta < z or bounds[5] - radius + delta > z: mindim = min(bounds[1] - bounds[0], bounds[3] - bounds[2], bounds[5] - bounds[4]) dims[i] = mindim tester.DeleteSpecifiedRegion(i) for i in sorted(dims.iteritems(), key=lambda i: i[1], reverse=True)[0:5]: print i specified.AddSpecifiedRegion(i[0]) #cleaner = vtk.vtkCleanPolyData() #cleaner.SetInputConnection(specified.GetOutputPort()) #cleaner.SetTolerance(1e-5) #orientator = vtk.vtkPolyDataNormals() #orientator.SetInputConnection(clipper.GetOutputPort()) #orientator.ConsistencyOn() #orientator.AutoOrientNormalsOn() writer = vtk.vtkSTLWriter() writer.SetFileName(output_file + 'tmp1.stl') writer.SetInputConnection(clipper.GetOutputPort()) writer.Write() with open("/tmp/test.mlx", "w") as f: f.write(""" <FilterScript> <filter name="Remove Zero Area Faces"/> <filter name="Re-Orient all faces coherentely"/> <filter name="Quadric Edge Collapse Decimation"> <Param type="RichInt" value="30000" name="TargetFaceNum"/> <Param type="RichFloat" value="0" name="TargetPerc"/> <Param type="RichFloat" value="0.3" name="QualityThr"/> <Param type="RichBool" value="false" name="PreserveBoundary"/> <Param type="RichBool" value="false" name="PreserveNormal"/> <Param type="RichBool" value="true" name="PreserveTopology"/> <Param type="RichBool" value="true" name="OptimalPlacement"/> <Param type="RichBool" value="false" name="PlanarQuadric"/> <Param type="RichBool" value="false" name="QualityWeight"/> <Param type="RichBool" value="true" name="AutoClean"/> <Param type="RichBool" value="false" name="Selected"/> </filter> </FilterScript> """) subprocess.call(["meshlabserver", "-i", output_file + 'tmp1.stl', "-o", output_file + '.off', "-s", "/tmp/test.mlx"]) #reader = vtk.vtkSTLReader() #reader.SetFileName(output_file + 'tmp2.stl') #reader.Update() writer = vtk.vtkSTLWriter() writer.SetFileName(output_file) writer.SetInputConnection(specified.GetOutputPort()) writer.Write() vtk_writer = vtk.vtkXMLPolyDataWriter() vtk_writer.SetFileName(output_file + ".vtu") vtk_writer.SetInputConnection(specified.GetOutputPort()) vtk_writer.Write()
# The cut plane plane = vtk.vtkPlane() plane.SetOrigin(0, 0, 0) plane.SetNormal(-1, -1, -1) planes = vtk.vtkPlaneCollection() planes.AddItem(plane) # vtkPolyDataPlaneClipper clipper = vtk.vtkPolyDataPlaneClipper() clipper.SetInputConnection(sphere.GetOutputPort()) clipper.SetPlane(plane) clipper.SetBatchSize(10000) clipper.CappingOn() # Compare to vtkClipClosedSurface closedClipper = vtk.vtkClipClosedSurface() closedClipper.SetInputConnection(sphere.GetOutputPort()) closedClipper.SetClippingPlanes(planes) # Compare to vtkClipPolyData oldClipper = vtk.vtkClipPolyData() oldClipper.SetInputConnection(sphere.GetOutputPort()) oldClipper.SetValue(0.0) oldClipper.SetClipFunction(plane) # Time execution timer = vtk.vtkTimerLog() timer.StartTimer() clipper.Update() timer.StopTimer() print("vtkPolyDataPlaneClipper Execution time: ", timer.GetElapsedTime())
iso.SetValue(0,1150) iso.SetInputMemoryLimit(1000) topPlane = vtk.vtkPlane() topPlane.SetNormal(0,0,1) topPlane.SetOrigin(0,0,0.5) botPlane = vtk.vtkPlane() botPlane.SetNormal(0,0,-1) botPlane.SetOrigin(0,0,137.0) sagPlane = vtk.vtkPlane() sagPlane.SetNormal(1,0,0) sagPlane.SetOrigin(100.8,0,0) capPlanes = vtk.vtkPlaneCollection() capPlanes.AddItem(topPlane) capPlanes.AddItem(botPlane) capPlanes.AddItem(sagPlane) clip = vtk.vtkClipClosedSurface() clip.SetClippingPlanes(capPlanes) clip.SetInputConnection(iso.GetOutputPort()) clip.SetBaseColor(0.9804,0.9216,0.8431) clip.SetClipColor(1.0,1.0,1.0) clip.SetActivePlaneColor(1.0,1.0,0.8) clip.SetActivePlaneId(2) clip.SetScalarModeToColors() clip.GenerateOutlineOn() clip.GenerateFacesOn() isoMapper = vtk.vtkPolyDataMapper() isoMapper.SetInputConnection(clip.GetOutputPort()) isoMapper.ScalarVisibilityOn() isoMapper.ImmediateModeRenderingOn() isoActor = vtk.vtkActor() isoActor.SetMapper(isoMapper)
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