Python VTK 给点添加颜色

  • Post author:
  • Post category:python




目录


前言:


效果:


code:


前言:

有时间我们需要给点加上颜色

效果:

code:

 
import vtk
from numpy import random
import os

class VtkPointCloud:

    def __init__(self, yMin=-10.0, yMax=10.0, maxNumPoints=1e6):
        self.maxNumPoints = maxNumPoints
        self.vtkPolyData = vtk.vtkPolyData()
        self.clearPoints()
        mapper = vtk.vtkPolyDataMapper()
        mapper.SetInputData(self.vtkPolyData)
        mapper.SetColorModeToDefault()
        mapper.SetScalarRange(yMin, yMax)
        mapper.SetScalarVisibility(1)
        self.vtkActor = vtk.vtkActor()
        self.vtkActor.GetProperty().SetPointSize(3)
        self.vtkActor.SetMapper(mapper)

    def addPoint(self, point):

        pointId = self.vtkPoints.InsertNextPoint(point[:])
        self.vtkDepth.InsertNextValue(point[1])
        self.vtkCells.InsertNextCell(1)
        self.vtkCells.InsertCellPoint(pointId)


    def clearPoints(self):
        self.vtkPoints = vtk.vtkPoints()
        self.vtkCells = vtk.vtkCellArray()
        self.vtkDepth = vtk.vtkDoubleArray()
        self.vtkDepth.SetName('DepthArray')
        self.vtkPolyData.SetPoints(self.vtkPoints)
        self.vtkPolyData.SetVerts(self.vtkCells)
        self.vtkPolyData.GetPointData().SetScalars(self.vtkDepth)
        self.vtkPolyData.GetPointData().SetActiveScalars('DepthArray')


reader = vtk.vtkSTLReader()
reader.SetFileName("lung.stl")
reader.Update()

bounds = reader.GetOutput().GetBounds()
pointCloud = VtkPointCloud(bounds[2], bounds[3])

total = reader.GetOutput().GetNumberOfPoints()
for number in range(total):
    point = reader.GetOutput().GetPoints().GetPoint(number)
    pointCloud.addPoint(point)

# Renderer
renderer = vtk.vtkRenderer()
renderer.AddActor(pointCloud.vtkActor)
renderer.SetBackground(.2, .3, .4)
renderer.ResetCamera()

# Render Window
renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(renderer)

# Interactor
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)

# Begin Interaction
renderWindow.Render()
renderWindowInteractor.Start()



版权声明:本文为q610098308原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。