简体   繁体   中英

How to insert points extracted from multiple .vtp files into a single polydata

I saved into polydata 10 the points from slice10 and into polydata11 the points from slice11

slice10 = 'Slices\Slice10\Slice10_0_0.vtp'
slice11 = 'Slices\Slice11\Slice11_0_0.vtp'

readerSlice10 = vtk.vtkXMLPolyDataReader()
readerSlice10.SetFileName(slice10)
readerSlice10.Update()

readerSlice11 = vtk.vtkXMLPolyDataReader()
readerSlice11.SetFileName(slice11)
readerSlice11.Update()

polydata10 = vtk.vtkPolyData()
polydata10.SetPoints(readerSlice10.GetOutput().GetPoints())

polydata11 = vtk.vtkPolyData()
polydata11.SetPoints(readerSlice11.GetOutput().GetPoints())

now I wanted to have a single polydata with all the points form slice10 and slice11 together, how can I achieve this?

This may be what you're looking for.

import vtk

# Generate two sets of points for this example.
points0 = vtk.vtkPoints()
points1 = vtk.vtkPoints()

points0.InsertNextPoint(1., 0., 0.)
points0.InsertNextPoint(1., 1., 0.)

points1.InsertNextPoint(1., 0., 1.)
points1.InsertNextPoint(1., 1., 1.)

polydata10 = vtk.vtkPolyData()
polydata11 = vtk.vtkPolyData()
polydata10.SetPoints(points0)
polydata11.SetPoints(points1)
#-------------------------------------

# Create a set of points that joints the two sets of points from polydata10 and polydata11.
pointsJoined = vtk.vtkPoints()
for i in range(polydata10.GetNumberOfPoints()):
    pointsJoined.InsertNextPoint(polydata10.GetPoint(i))

for i in range(polydata11.GetNumberOfPoints()):
    pointsJoined.InsertNextPoint(polydata11.GetPoint(i))

# Initialize a polydata object and set the joint point set.
polydata = vtk.vtkPolyData()
polydata.SetPoints(pointsJoined)

# Create verticies so the points can be visualized
verts = vtk.vtkCellArray()
for i in range(polydata.GetNumberOfPoints()):
    verts.InsertNextCell(1) # Create a 1 dimensional cell
    verts.InsertCellPoint(i) # Append point i to the verts array
polydata.SetVerts(verts)

# Setup and run the visualization
ren = vtk.vtkRenderer() #: vtk.vtkRenderer, The renderer for the visualization
renWin = vtk.vtkRenderWindow() #: vtk.vtkRenderWindow, The render window
renWin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor() #: vtk.vtkRenderWindowInteractor, The render window interactor
iren.SetRenderWindow(renWin)

mapper = vtk.vtkPolyDataMapper()
mapper.SetInputData(polydata)
actor = vtk.vtkActor()
actor.GetProperty().SetPointSize(5) # Increase the size of the points
actor.SetMapper(mapper)

ren.AddActor(actor)
iren.Start()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM