使用x,y,z坐标创建VTKpolydata

时间:2012-01-31 08:02:50

标签: visual-c++ vtk contour

我是VTK的初学者。我有一个数据集作为x,y,z点和每个点的值。我想使用点创建一个vtkpolydata集,并使用每个点的值创建一个轮廓。 请告诉我如何使用c ++中的给定点集创建vtkPolyData集。

感谢。

2 个答案:

答案 0 :(得分:1)

此示例显示如何执行问题的两个部分: http://www.vtk.org/Wiki/VTK/Examples/Cxx/GeometricObjects/PolyLine

答案 1 :(得分:0)

这是一个改编自this blog的脚本,用于绘制3D xyz数据:

'''
Modified Python 3 VTK script to Display 3D xyz data
Credits to : https://sukhbinder.wordpress.com/2013/09/17/python-vtk-script-to-display-3d-xyz-data/

Script name: xyzviewer.py
'''

import vtk
from numpy import random,genfromtxt,size

class VtkPointCloud:
    def __init__(self, zMin=-10.0, zMax=10.0, maxNumPoints=1e6):
        self.maxNumPoints = maxNumPoints
        self.vtkPolyData = vtk.vtkPolyData()
        self.clearPoints()
        mapper = vtk.vtkPolyDataMapper()
        mapper.SetInputData(self.vtkPolyData)
        mapper.SetColorModeToDefault()
        mapper.SetScalarRange(zMin, zMax)
        mapper.SetScalarVisibility(1)
        self.vtkActor = vtk.vtkActor()
        self.vtkActor.SetMapper(mapper)

    def addPoint(self, point):
        if (self.vtkPoints.GetNumberOfPoints() < self.maxNumPoints):
            pointId = self.vtkPoints.InsertNextPoint(point[:])
            self.vtkDepth.InsertNextValue(point[2])
            self.vtkCells.InsertNextCell(1)
            self.vtkCells.InsertCellPoint(pointId)
        else:
            r = random.randint(0, self.maxNumPoints)
            self.vtkPoints.SetPoint(r, point[:])
        self.vtkCells.Modified()
        self.vtkPoints.Modified()
        self.vtkDepth.Modified()

    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')

def load_data(filename,pointCloud):
    data = genfromtxt(filename,dtype=float,usecols=[0,1,2])

    for k in range(size(data,0)):
        point = data[k] #20*(random.rand(3)-0.5)
        pointCloud.addPoint(point)

    return pointCloud


if __name__ == '__main__':
    import sys


    if (len(sys.argv) < 2):
         print ('Usage: xyzviewer.py itemfile')
         sys.exit()
    pointCloud = VtkPointCloud()
    pointCloud=load_data(sys.argv[1],pointCloud)


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

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

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

# Begin Interaction
    renderWindow.Render()
    renderWindow.SetWindowName("XYZ Data Viewer"+sys.argv[1])
    renderWindowInteractor.Start()

您可以按以下方式运行它:

python xyzviewer.py filename

filename是包含xyz数据的文件。