def points_actor(self): poly = vtk.vtkPolyData() poly.SetPoints(self.points) mapper = vtk.vtkPointGaussianMapper() mapper.SetInputData(poly) mapper.EmissiveOff() mapper.SetScaleFactor(0.0) actor = vtk.vtkActor() actor.GetProperty().SetRepresentationToPoints() # 没有明显帮助 actor.SetMapper(mapper) actor.GetProperty().SetPointSize(1) actor.GetProperty().SetColor((1, 0, 0)) return actor
def showPointCloud(modelNodeID): model = slicer.mrmlScene.GetNodeByID(modelNodeID) polydata = model.GetPolyData() # Reuse the locator locator = vtk.vtkStaticPointLocator() locator.SetDataSet(polydata) locator.BuildLocator() # Remove isolated points removal = vtk.vtkRadiusOutlierRemoval() removal.SetInputData(polydata) removal.SetLocator(locator) removal.SetRadius(10) removal.SetNumberOfNeighbors(2) removal.GenerateOutliersOn() # Time execution timer = vtk.vtkTimerLog() timer.StartTimer() removal.Update() timer.StopTimer() time = timer.GetElapsedTime() print("Time to remove points: {0}".format(time)) print(" Number removed: {0}".format(removal.GetNumberOfPointsRemoved()), " (out of: {}".format(polydata.GetNumberOfPoints())) # First output are the non-outliers remMapper = vtk.vtkPointGaussianMapper() remMapper.SetInputConnection(removal.GetOutputPort()) remMapper.EmissiveOff() remMapper.SetScaleFactor(0.0) remActor = vtk.vtkActor() remActor.SetMapper(remMapper) lm = slicer.app.layoutManager() tdWidget = lm.threeDWidget(0) tdView = tdWidget.threeDView() rWin = tdView.renderWindow() rCollection = rWin.GetRenderers() renderer = rCollection.GetItemAsObject(0) # Add the actors to the renderer, set the background and size # renderer.AddActor(remActor)
print(" Time to generate curvature: {0}".format(time)) # Break out the curvature into three separate arrays assign = vtk.vtkAssignAttribute() assign.SetInputConnection(curv.GetOutputPort()) assign.Assign("PCACurvature", "VECTORS", "POINT_DATA") extract = vtk.vtkExtractVectorComponents() extract.SetInputConnection(assign.GetOutputPort()) extract.Update() print(extract.GetOutput(0).GetScalarRange()) print(extract.GetOutput(1).GetScalarRange()) print(extract.GetOutput(2).GetScalarRange()) # Three different outputs for different curvatures subMapper = vtk.vtkPointGaussianMapper() subMapper.SetInputConnection(extract.GetOutputPort(0)) subMapper.EmissiveOff() subMapper.SetScaleFactor(0.0) subActor = vtk.vtkActor() subActor.SetMapper(subMapper) subActor.AddPosition(0, 2.25, 0) sub1Mapper = vtk.vtkPointGaussianMapper() sub1Mapper.SetInputConnection(extract.GetOutputPort(1)) sub1Mapper.EmissiveOff() sub1Mapper.SetScaleFactor(0.0) sub1Actor = vtk.vtkActor() sub1Actor.SetMapper(sub1Mapper)
norms.FlipNormalsOn() norms.SetNormalOrientationToPoint() norms.SetOrientationPoint(0,0,0) # Time execution timer = vtk.vtkTimerLog() timer.StartTimer() norms.Update() timer.StopTimer() time = timer.GetElapsedTime() print("Points processed: {0}".format(NPts)) print(" Time to generate normals: {0}".format(time)) #print(hBin) #print(hBin.GetOutput()) subMapper = vtk.vtkPointGaussianMapper() subMapper.SetInputConnection(norms.GetOutputPort()) subMapper.EmissiveOff() subMapper.SetScaleFactor(0.0) subActor = vtk.vtkActor() subActor.SetMapper(subMapper) # Draw the normals mask = vtk.vtkMaskPoints() mask.SetInputConnection(norms.GetOutputPort()) mask.SetRandomModeType(1) mask.SetMaximumNumberOfPoints(250) hhog = vtk.vtkHedgeHog() hhog.SetInputConnection(mask.GetOutputPort())
pc.SetInputConnection(0,renSource.GetOutputPort()) pc.SetInputConnection(1,renSource1.GetOutputPort()) pc.SetCamera(ren0.GetActiveCamera()) pc.CullNearPointsOn() pc.CullFarPointsOn() pc.ProduceVertexCellArrayOff() print(pc) timer = vtk.vtkTimerLog() timer.StartTimer() pc.Update() timer.StopTimer() time = timer.GetElapsedTime() print("Generate point cloud: {0}".format(time)) pcMapper = vtk.vtkPointGaussianMapper() pcMapper.SetInputConnection(pc.GetOutputPort()) pcMapper.EmissiveOff() pcMapper.SetScaleFactor(0.0) pcActor = vtk.vtkActor() pcActor.SetMapper(pcMapper) ren1.AddActor(pcActor) ren1.SetBackground(0,0,0) cam = ren1.GetActiveCamera() cam.SetFocalPoint(0,0,0) cam.SetPosition(1,1,1) ren1.ResetCamera() renWin.Render()
cpf.SetInputData(polydata) cpf.SetExtractionModeToAllRegions() cpf.SetRadius(0.25) print(cpf) # Time execution timer = vtk.vtkTimerLog() timer.StartTimer() cpf.Update() timer.StopTimer() time = timer.GetElapsedTime() print("Number of regions extracted: {0}".format( cpf.GetNumberOfExtractedRegions())) print(" Time to extract all regions: {0}".format(time)) segMapper = vtk.vtkPointGaussianMapper() segMapper.SetInputConnection(cpf.GetOutputPort()) segMapper.EmissiveOff() segMapper.SetScaleFactor(0.0) segMapper.SetScalarRange(cpf.GetOutput().GetScalarRange()) segActor = vtk.vtkActor() segActor.SetMapper(segMapper) # Create an outline outline = vtk.vtkOutlineFilter() outline.SetInputData(polydata) outlineMapper = vtk.vtkPolyDataMapper() outlineMapper.SetInputConnection(outline.GetOutputPort())
# Time execution timer = vtk.vtkTimerLog() timer.StartTimer() removal.Update() timer.StopTimer() time = timer.GetElapsedTime() print("Number of points processed: {0}".format(NPts)) print(" Time to remove outliers: {0}".format(time)) print(" Number removed: {0}".format(removal.GetNumberOfPointsRemoved())) print(" Computed mean: {0}".format(removal.GetComputedMean())) print(" Computed standard deviation: {0}".format( removal.GetComputedStandardDeviation())) # First output are the non-outliers remMapper = vtk.vtkPointGaussianMapper() remMapper.SetInputConnection(removal.GetOutputPort()) remMapper.EmissiveOff() remMapper.SetScaleFactor(0.0) remActor = vtk.vtkActor() remActor.SetMapper(remMapper) # Create an outline outline = vtk.vtkOutlineFilter() outline.SetInputData(polydata) outlineMapper = vtk.vtkPolyDataMapper() outlineMapper.SetInputConnection(outline.GetOutputPort()) outlineActor = vtk.vtkActor()
# create some scalars based on implicit function # Create a cylinder cyl = vtk.vtkCylinder() cyl.SetCenter(0,0,0) cyl.SetRadius(0.1) # Generate scalars and vector sample = vtk.vtkSampleImplicitFunctionFilter() sample.SetInputConnection(points.GetOutputPort()) sample.SetImplicitFunction(cyl) sample.Update() print(sample.GetOutput().GetScalarRange()) # Draw the points sampleMapper = vtk.vtkPointGaussianMapper() sampleMapper.SetInputConnection(sample.GetOutputPort(0)) sampleMapper.EmissiveOff() sampleMapper.SetScaleFactor(0.0) sampleMapper.SetScalarRange(0,20) sampleActor = vtk.vtkActor() sampleActor.SetMapper(sampleMapper) # Create an outline outline = vtk.vtkOutlineFilter() outline.SetInputConnection(sample.GetOutputPort()) outlineMapper = vtk.vtkPolyDataMapper() outlineMapper.SetInputConnection(outline.GetOutputPort())
iren.SetRenderWindow(renWin) # create some points and display them # math = vtk.vtkMath() math.RandomSeed(31415) points = vtk.vtkPoints() i = 0 while i < NPts: points.InsertPoint(i,math.Random(0,1),math.Random(0,1),0.0) i = i + 1 profile = vtk.vtkPolyData() profile.SetPoints(points) ptMapper = vtk.vtkPointGaussianMapper() ptMapper.SetInputData(profile) ptMapper.EmissiveOff() ptMapper.SetScaleFactor(0.0) ptActor = vtk.vtkActor() ptActor.SetMapper(ptMapper) ptActor.GetProperty().SetColor(0,0,0) ptActor.GetProperty().SetPointSize(2) # Tessellate them # voronoi = vtk.vtkVoronoi2D() voronoi.SetInputData(profile) voronoi.SetGenerateScalarsToNone() voronoi.SetGenerateScalarsToPointIds()
subsample = vtk.vtkVoxelGrid() subsample.SetInputConnection(points.GetOutputPort()) subsample.SetConfigurationStyleToAutomatic() # Time execution timer = vtk.vtkTimerLog() timer.StartTimer() subsample.Update() timer.StopTimer() time = timer.GetElapsedTime() print("Time to subsample: {0}".format(time)) print(" Original number of points: {0}".format(NPts)) print(" Final number of points: {0}".format(subsample.GetOutput().GetNumberOfPoints())) # Output the original points subMapper = vtk.vtkPointGaussianMapper() subMapper.SetInputConnection(points.GetOutputPort()) subMapper.EmissiveOff() subMapper.SetScaleFactor(0.0) subActor = vtk.vtkActor() subActor.SetMapper(subMapper) # Create an outline outline = vtk.vtkOutlineFilter() outline.SetInputConnection(points.GetOutputPort()) outlineMapper = vtk.vtkPolyDataMapper() outlineMapper.SetInputConnection(outline.GetOutputPort()) outlineActor = vtk.vtkActor()
print(" Time to generate curvature: {0}".format(time)) # Break out the curvature into thress separate arrays assign = vtk.vtkAssignAttribute() assign.SetInputConnection(curv.GetOutputPort()) assign.Assign("PCACurvature", "VECTORS", "POINT_DATA") extract = vtk.vtkExtractVectorComponents() extract.SetInputConnection(assign.GetOutputPort()) extract.Update() print(extract.GetOutput(0).GetScalarRange()) print(extract.GetOutput(1).GetScalarRange()) print(extract.GetOutput(2).GetScalarRange()) # Three different outputs for different curvatures subMapper = vtk.vtkPointGaussianMapper() subMapper.SetInputConnection(extract.GetOutputPort(0)) subMapper.EmissiveOff() subMapper.SetScaleFactor(0.0) subActor = vtk.vtkActor() subActor.SetMapper(subMapper) subActor.AddPosition(0,2.25,0) sub1Mapper = vtk.vtkPointGaussianMapper() sub1Mapper.SetInputConnection(extract.GetOutputPort(1)) sub1Mapper.EmissiveOff() sub1Mapper.SetScaleFactor(0.0) sub1Actor = vtk.vtkActor() sub1Actor.SetMapper(sub1Mapper)
removal.GenerateOutliersOn() # Time execution timer = vtk.vtkTimerLog() timer.StartTimer() removal.Update() timer.StopTimer() time = timer.GetElapsedTime() print("Number of points processed: {0}".format(NPts)) print(" Time to remove outliers: {0}".format(time)) print(" Number removed: {0}".format(removal.GetNumberOfPointsRemoved())) print(" Computed mean: {0}".format(removal.GetComputedMean())) print(" Computed standard deviation: {0}".format(removal.GetComputedStandardDeviation())) # First output are the non-outliers remMapper = vtk.vtkPointGaussianMapper() remMapper.SetInputConnection(removal.GetOutputPort()) remMapper.EmissiveOff() remMapper.SetScaleFactor(0.0) remActor = vtk.vtkActor() remActor.SetMapper(remMapper) # Create an outline outline = vtk.vtkOutlineFilter() outline.SetInputData(polydata) outlineMapper = vtk.vtkPolyDataMapper() outlineMapper.SetInputConnection(outline.GetOutputPort()) outlineActor = vtk.vtkActor()
def main(): points_fn, probe_fn = get_program_parameters() colors = vtk.vtkNamedColors() points_reader = vtk.vtkDelimitedTextReader() points_reader.SetFileName(points_fn) points_reader.DetectNumericColumnsOn() points_reader.SetFieldDelimiterCharacters('\t') points_reader.SetHaveHeaders(True) table_points = vtk.vtkTableToPolyData() table_points.SetInputConnection(points_reader.GetOutputPort()) table_points.SetXColumn('x') table_points.SetYColumn('y') table_points.SetZColumn('z') table_points.Update() points = table_points.GetOutput() points.GetPointData().SetActiveScalars('val') range = points.GetPointData().GetScalars().GetRange() # Read a probe surface stl_reader = vtk.vtkSTLReader() stl_reader.SetFileName(probe_fn) stl_reader.Update() surface = stl_reader.GetOutput() bounds = np.array(surface.GetBounds()) dims = np.array([101, 101, 101]) box = vtk.vtkImageData() box.SetDimensions(dims) box.SetSpacing((bounds[1::2] - bounds[:-1:2]) / (dims - 1)) box.SetOrigin(bounds[::2]) # Gaussian kernel gaussian_kernel = vtk.vtkGaussianKernel() gaussian_kernel.SetSharpness(2) gaussian_kernel.SetRadius(12) interpolator = vtk.vtkPointInterpolator() interpolator.SetInputData(box) interpolator.SetSourceData(points) interpolator.SetKernel(gaussian_kernel) resample = vtk.vtkResampleWithDataSet() resample.SetInputData(surface) resample.SetSourceConnection(interpolator.GetOutputPort()) mapper = vtk.vtkPolyDataMapper() mapper.SetInputConnection(resample.GetOutputPort()) mapper.SetScalarRange(range) actor = vtk.vtkActor() actor.SetMapper(mapper) point_mapper = vtk.vtkPointGaussianMapper() point_mapper.SetInputData(points) point_mapper.SetScalarRange(range) point_mapper.SetScaleFactor(0.6) point_mapper.EmissiveOff() point_mapper.SetSplatShaderCode( "//VTK::Color::Impl\n" "float dist = dot(offsetVCVSOutput.xy,offsetVCVSOutput.xy);\n" "if (dist > 1.0) {\n" " discard;\n" "} else {\n" " float scale = (1.0 - dist);\n" " ambientColor *= scale;\n" " diffuseColor *= scale;\n" "}\n") point_actor = vtk.vtkActor() point_actor.SetMapper(point_mapper) renderer = vtk.vtkRenderer() renWin = vtk.vtkRenderWindow() renWin.AddRenderer(renderer) iren = vtk.vtkRenderWindowInteractor() iren.SetRenderWindow(renWin) renderer.AddActor(actor) renderer.AddActor(point_actor) renderer.SetBackground(colors.GetColor3d('SlateGray')) renWin.SetSize(640, 480) renWin.SetWindowName('PointInterpolator') renderer.ResetCamera() renderer.GetActiveCamera().Elevation(-45) iren.Initialize() renWin.Render() iren.Start()
cpf = vtk.vtkConnectedPointsFilter() cpf.SetInputData(polydata) cpf.SetExtractionModeToAllRegions(); cpf.SetRadius(0.25); print(cpf) # Time execution timer = vtk.vtkTimerLog() timer.StartTimer() cpf.Update() timer.StopTimer() time = timer.GetElapsedTime() print("Number of regions extracted: {0}".format(cpf.GetNumberOfExtractedRegions())) print(" Time to extract all regions: {0}".format(time)) segMapper = vtk.vtkPointGaussianMapper() segMapper.SetInputConnection(cpf.GetOutputPort()) segMapper.EmissiveOff() segMapper.SetScaleFactor(0.0) segMapper.SetScalarRange(cpf.GetOutput().GetScalarRange()) segActor = vtk.vtkActor() segActor.SetMapper(segMapper) # Create an outline outline = vtk.vtkOutlineFilter() outline.SetInputData(polydata) outlineMapper = vtk.vtkPolyDataMapper() outlineMapper.SetInputConnection(outline.GetOutputPort())
# create some scalars based on implicit function # Create a cylinder cyl = vtk.vtkCylinder() cyl.SetCenter(0, 0, 0) cyl.SetRadius(0.1) # Generate scalars and vector sample = vtk.vtkSampleImplicitFunctionFilter() sample.SetInputConnection(points.GetOutputPort()) sample.SetImplicitFunction(cyl) sample.Update() print(sample.GetOutput().GetScalarRange()) # Draw the points sampleMapper = vtk.vtkPointGaussianMapper() sampleMapper.SetInputConnection(sample.GetOutputPort(0)) sampleMapper.EmissiveOff() sampleMapper.SetScaleFactor(0.0) sampleMapper.SetScalarRange(0, 20) sampleActor = vtk.vtkActor() sampleActor.SetMapper(sampleMapper) # Create an outline outline = vtk.vtkOutlineFilter() outline.SetInputConnection(sample.GetOutputPort()) outlineMapper = vtk.vtkPolyDataMapper() outlineMapper.SetInputConnection(outline.GetOutputPort())