我用polyline生成polyplane生成隐函数来裁剪一个立方体,从结果来看,这个polyplane不是沿着polyline延展形成的,而是将polyline首尾两个端点连接形成了一个平面进行裁剪的,下面是我的代码
import vtk
points_list = [(0.0, 0.0, 0.0), (1.0, 0.0, 0.0), (1.0, 1.0, 0.0), (0.0, 1.0, 0.0),
(0.0, 0.0, 1.0), (1.0, 0.0, 1.0), (1.0, 1.0, 1.0), (0.0, 1.0, 1.0)]
pts = [(0, 1, 2, 3), (4, 5, 6, 7), (0, 1, 5, 4),
(1, 2, 6, 5), (2, 3, 7, 6), (3, 0, 4, 7)]
cube = vtk.vtkPolyData()
points = vtk.vtkPoints()
for i in range(8):
points.InsertNextPoint(points_list[i])
polys = vtk.vtkCellArray()
for i in range(6):
polys.InsertNextCell(4, pts[i])
cube.SetPoints(points)
cube.SetPolys(polys)
polyline_points = [[0.5, 0, 1.2], [0.5, 0.5, 1.2],[0, 0.5, 1.2]]
polylinedata = vtk.vtkPolyData()
points = vtk.vtkPoints()
for i in range(3):
points.InsertNextPoint(polyline_points[i])
polylinedata.SetPoints(points)
polyline = vtk.vtkPolyLine()
polyline.GetPointIds().SetNumberOfIds(3)
for i in range(3):
polyline.GetPointIds().SetId(i, i)
cell = vtk.vtkCellArray()
cell.InsertNextCell(polyline)
polylinedata.SetLines(cell)
polyLine = vtk.vtkPolyLine().SafeDownCast(polylinedata.GetCell(0))
polyplane = vtk.vtkPolyPlane()
polyplane.SetPolyLine(polyLine)
clipper = vtk.vtkClipDataSet()
clipper.SetInputData(cube)
clipper.SetClipFunction(polyplane)
clipper.Update()
mapper = vtk.vtkDataSetMapper()
# mapper.SetInputData(polylinedata)
mapper1 = vtk.vtkDataSetMapper()
mapper1.SetInputData(clipper.GetOutput())
actor = vtk.vtkActor()
# actor.SetMapper(mapper)
actor1 = vtk.vtkActor()
actor1.SetMapper(mapper1)
print(actor1.GetCenter())
render = vtk.vtkRenderer()
# render.AddActor(actor)
render.AddActor(actor1)
render_window = vtk.vtkRenderWindow()
render_window.AddRenderer(render)
render_window_interactor = vtk.vtkRenderWindowInteractor()
render_window_interactor.SetRenderWindow(render_window)
render_window_interactor.GetInteractorStyle().SetCurrentStyleToTrackballCamera()
render_window.Render()
render_window_interactor.Start()
import vtk
points_list = [(0.0, 0.0, 0.0), (1.0, 0.0, 0.0), (1.0, 1.0, 0.0), (0.0, 1.0, 0.0),
(0.0, 0.0, 1.0), (1.0, 0.0, 1.0), (1.0, 1.0, 1.0), (0.0, 1.0, 1.0)]
pts = [(0, 1, 2, 3), (4, 5, 6, 7), (0, 1, 5, 4),
(1, 2, 6, 5), (2, 3, 7, 6), (3, 0, 4, 7)]
cube = vtk.vtkPolyData()
points = vtk.vtkPoints()
for i in range(8):
points.InsertNextPoint(points_list[i])
polys = vtk.vtkCellArray()
for i in range(6):
polys.InsertNextCell(4, pts[i])
cube.SetPoints(points)
cube.SetPolys(polys)
polyline_points = [[0.5, 0, 1.2], [0.5, 0.5, 1.2],[0, 0.5, 1.2]]
polylinedata = vtk.vtkPolyData()
points = vtk.vtkPoints()
for i in range(3):
points.InsertNextPoint(polyline_points[i])
polylinedata.SetPoints(points)
polyline = vtk.vtkPolyLine()
polyline.GetPointIds().SetNumberOfIds(3)
for i in range(3):
polyline.GetPointIds().SetId(i, i)
cell = vtk.vtkCellArray()
cell.InsertNextCell(polyline)
polylinedata.SetLines(cell)
polyLine = vtk.vtkPolyLine().SafeDownCast(polylinedata.GetCell(0))
polyplane = vtk.vtkPolyPlane()
polyplane.SetPolyLine(polyLine)
clipper = vtk.vtkClipDataSet()
clipper.SetInputData(cube)
clipper.SetClipFunction(polyplane)
clipper.Update()
mapper = vtk.vtkDataSetMapper()
# mapper.SetInputData(polylinedata)
mapper1 = vtk.vtkDataSetMapper()
mapper1.SetInputData(clipper.GetOutput())
actor = vtk.vtkActor()
# actor.SetMapper(mapper)
actor1 = vtk.vtkActor()
actor1.SetMapper(mapper1)
print(actor1.GetCenter())
render = vtk.vtkRenderer()
# render.AddActor(actor)
render.AddActor(actor1)
render_window = vtk.vtkRenderWindow()
render_window.AddRenderer(render)
render_window_interactor = vtk.vtkRenderWindowInteractor()
render_window_interactor.SetRenderWindow(render_window)
render_window_interactor.GetInteractorStyle().SetCurrentStyleToTrackballCamera()
render_window.Render()
render_window_interactor.Start()