def applyFilters(self, state): surface = state.inputModelNode.GetPolyData() if state.decimation: triangle = vtk.vtkTriangleFilter() triangle.SetInput(surface) decimation = vtk.vtkDecimatePro() decimation.SetInput(triangle.GetOutput()) decimation.SetTargetReduction(state.reduction) decimation.SetBoundaryVertexDeletion(state.boundaryDeletion) decimation.PreserveTopologyOn() decimation.Update() surface = decimation.GetOutput() if state.smoothing: if state.smoothingMethod == "Laplace": smoothing = vtk.vtkSmoothPolyDataFilter() smoothing.SetInput(surface) smoothing.SetBoundarySmoothing(state.boundarySmoothing) smoothing.SetNumberOfIterations(state.laplaceIterations) smoothing.SetRelaxationFactor(state.laplaceRelaxation) smoothing.Update() surface = smoothing.GetOutput() elif state.smoothingMethod == "Taubin": smoothing = vtk.vtkWindowedSincPolyDataFilter() smoothing.SetInput(surface) smoothing.SetBoundarySmoothing(state.boundarySmoothing) smoothing.SetNumberOfIterations(state.taubinIterations) smoothing.SetPassBand(state.taubinPassBand) smoothing.Update() surface = smoothing.GetOutput() if state.normals: normals = vtk.vtkPolyDataNormals() normals.SetInput(surface) normals.AutoOrientNormalsOn() normals.SetFlipNormals(state.flipNormals) normals.SetSplitting(state.splitting) normals.SetFeatureAngle(state.featureAngle) normals.ConsistencyOn() normals.Update() surface = normals.GetOutput() if state.cleaner: cleaner = vtk.vtkCleanPolyData() cleaner.SetInput(surface) cleaner.Update() surface = cleaner.GetOutput() if state.connectivity: connectivity = vtk.vtkPolyDataConnectivityFilter() connectivity.SetInput(surface) connectivity.SetExtractionModeToLargestRegion() connectivity.Update() surface = connectivity.GetOutput() state.outputModelNode.SetAndObservePolyData(surface) return True
def Smooth_Surface(self, surface): # Take a vtk surface and run it through the smoothing pipeline boneNormals = vtk.vtkPolyDataNormals() try: boneNormals.SetInputData(surface) except: boneNormals.SetInputConnection(surface.GetOutputPort()) boneNormals.Update() # Clean the polydata so that the edges are shared! cleanPolyData = vtk.vtkCleanPolyData() cleanPolyData.SetInputConnection(boneNormals.GetOutputPort()) cleanPolyData.Update() polydata = cleanPolyData.GetOutput() if self.smoothing_iterations > 0: # Apply laplacian smoothing to the surface smoothingFilter = vtk.vtkSmoothPolyDataFilter() smoothingFilter.SetInputData(polydata) smoothingFilter.SetNumberOfIterations( int(self.smoothing_iterations)) smoothingFilter.SetRelaxationFactor(self.relaxation_factor) smoothingFilter.Update() polydata = smoothingFilter.GetOutput() if self.decimate_surface > 0 and self.decimate_surface < 1: # We want to preserve topology (not let any cracks form). This may # limit the total reduction possible, which we have specified at 80%. deci = vtk.vtkDecimatePro() deci.SetInputData(polydata) deci.SetTargetReduction(self.decimate_surface) deci.PreserveTopologyOn() deci.Update() polydata = deci.GetOutput() # Clean the polydata so that the edges are shared! cleanPolyData = vtk.vtkCleanPolyData() cleanPolyData.SetInputData(polydata) cleanPolyData.Update() polydata = cleanPolyData.GetOutput() # Generate surface normals to give a better visualization normals = vtk.vtkPolyDataNormals() normals.SetInputData(polydata) normals.Update() polydata = normals.GetOutput() return polydata
def decimateSurface( self, polyData ): ''' ''' decimationFilter = vtk.vtkDecimatePro() decimationFilter.SetInputData( polyData ) decimationFilter.SetTargetReduction( 0.99 ) decimationFilter.SetBoundaryVertexDeletion( 0 ) decimationFilter.PreserveTopologyOn() decimationFilter.Update() cleaner = vtk.vtkCleanPolyData() cleaner.SetInputData( decimationFilter.GetOutput() ) cleaner.Update() triangleFilter = vtk.vtkTriangleFilter() triangleFilter.SetInputData( cleaner.GetOutput() ) triangleFilter.Update() outPolyData = vtk.vtkPolyData() outPolyData.DeepCopy( triangleFilter.GetOutput() ) return outPolyData
def vtk_to_obj_converter(self, node, radius=0.1, number_of_sides=3): qt.QApplication.setOverrideCursor(qt.Qt.WaitCursor) polydata = node.GetPolyData() tuber = vtk.vtkTubeFilter() tuber.SetNumberOfSides(int(number_of_sides)) tuber.SetRadius(radius) tuber.SetInputData(polydata) tuber.Update() tubes = tuber.GetOutputDataObject(0) # scalars = tubes.GetPointData().GetArray(0) # scalars.SetName("scalars") triangles = vtk.vtkTriangleFilter() triangles.SetInputData(tubes) triangles.Update() tripolydata = vtk.vtkPolyData() tripolydata.ShallowCopy(triangles.GetOutput()) # Decrease the number of triangle of 30% to reduce Blender loading costs decimate = vtk.vtkDecimatePro() decimate.SetInputData(tripolydata) decimate.SetTargetReduction(.30) decimate.Update() mapper = vtk.vtkPolyDataMapper() mapper.SetInputConnection(decimate.GetOutputPort()) r = random.randrange(0, 256, 1) g = random.randrange(0, 256, 1) b = random.randrange(0, 256, 1) actor = vtk.vtkActor() actor.SetMapper(mapper) # actor.GetProperty().SetColor(0, 0, 0) # Ka: ambient color of the material (r, g, b) actor.GetProperty().SetDiffuseColor( r, g, b) # Kd: diffuse color of the material (r, g, b) # actor.GetProperty().SetSpecularColor(0, 0, 0) # Ks: specular color of the material (r, g, b) renderer = vtk.vtkRenderer() renderer.AddActor(actor) window = vtk.vtkRenderWindow() window.AddRenderer(renderer) # Get output path storageNode = node.GetStorageNode() filepath = storageNode.GetFullNameFromFileName() filename = filepath.rsplit('.', 1) writer = vtk.vtkOBJExporter() writer.SetFilePrefix(filename[0]) writer.SetInput(window) writer.Write() qt.QApplication.restoreOverrideCursor() if writer.Write() == 0: qt.QMessageBox.critical(None, "Conversion", "Conversion failed") else: qt.QMessageBox.information(None, "Conversion", "Conversion done")