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