VTK:使用 vtkDelaunay2D 创建多边形和孔不起作用
VTK: creating polygon and a hole with vtkDelaunay2D not working
我想在 VTK 中渲染一个有洞的多边形。我发现 vtkDelaunay2D class 应该可以解决问题。我按照这里的教程 vtkDelaunay2D Example 进行了操作,效果非常好。
之后我尝试用不同的孔创建不同的多边形。我不明白为什么它不起作用,想知道我必须改变什么。
它应该是这样的。
现实中是这样的。
这是我使用的代码:
import vtk
import random
points = vtk.vtkPoints()
ls = [
[2, 2], [-2, 2], [-2, -2], [2, -2],
[5, 5], [-5, 5], [-8, 0], [-5, -5], [5, -5], [8, 0],
[10, 10], [-10, 10], [-10, -10], [10, -10]
]
for x, y in ls:
points.InsertNextPoint(x, y, 0)
aPolyData = vtk.vtkPolyData()
aPolyData.SetPoints(points)
aCellArray = vtk.vtkCellArray()
cPolygon = vtk.vtkPolygon()
start = 0
for idd in range(start, start + 4):
cPolygon.GetPointIds().InsertNextId(idd)
aCellArray.InsertNextCell(cPolygon)
boundary = vtk.vtkPolyData()
boundary.SetPoints(aPolyData.GetPoints())
boundary.SetPolys(aCellArray)
delaunay = vtk.vtkDelaunay2D()
delaunay.SetInputData(aPolyData)
delaunay.SetSourceData(boundary)
################################## Actors etc:
meshMapper = vtk.vtkPolyDataMapper()
meshMapper.SetInputConnection(delaunay.GetOutputPort())
colors = vtk.vtkNamedColors()
meshActor = vtk.vtkActor()
meshActor.SetMapper(meshMapper)
meshActor.GetProperty().EdgeVisibilityOn()
meshActor.GetProperty().SetEdgeColor(colors.GetColor3d("Peacock"))
meshActor.GetProperty().SetInterpolationToFlat()
boundaryMapper = vtk.vtkPolyDataMapper()
boundaryMapper.SetInputData(boundary)
boundaryActor = vtk.vtkActor()
boundaryActor.SetMapper(boundaryMapper)
boundaryActor.GetProperty().SetColor(colors.GetColor3d("Raspberry"))
boundaryActor.GetProperty().SetLineWidth(3)
boundaryActor.GetProperty().EdgeVisibilityOn()
boundaryActor.GetProperty().SetEdgeColor(1, 1, 0)
boundaryActor.GetProperty().SetRepresentationToWireframe()
renderer = vtk.vtkRenderer()
renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(renderer)
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)
renderer.AddActor(meshActor)
renderer.AddActor(boundaryActor)
renderer.SetBackground(colors.GetColor3d("Mint"))
renderWindow.SetSize(640, 480)
renderWindow.Render()
renderWindowInteractor.Start()
据我所知应该可以。就像在示例中一样,我创建了一组点,在前 4 个点(应该是洞)周围定义了一个边界,然后渲染它。
改变
for idd in range(start, start + 4):
进入
for idd in reversed(range(start, start + 4)):
我想在 VTK 中渲染一个有洞的多边形。我发现 vtkDelaunay2D class 应该可以解决问题。我按照这里的教程 vtkDelaunay2D Example 进行了操作,效果非常好。
之后我尝试用不同的孔创建不同的多边形。我不明白为什么它不起作用,想知道我必须改变什么。
这是我使用的代码:
import vtk
import random
points = vtk.vtkPoints()
ls = [
[2, 2], [-2, 2], [-2, -2], [2, -2],
[5, 5], [-5, 5], [-8, 0], [-5, -5], [5, -5], [8, 0],
[10, 10], [-10, 10], [-10, -10], [10, -10]
]
for x, y in ls:
points.InsertNextPoint(x, y, 0)
aPolyData = vtk.vtkPolyData()
aPolyData.SetPoints(points)
aCellArray = vtk.vtkCellArray()
cPolygon = vtk.vtkPolygon()
start = 0
for idd in range(start, start + 4):
cPolygon.GetPointIds().InsertNextId(idd)
aCellArray.InsertNextCell(cPolygon)
boundary = vtk.vtkPolyData()
boundary.SetPoints(aPolyData.GetPoints())
boundary.SetPolys(aCellArray)
delaunay = vtk.vtkDelaunay2D()
delaunay.SetInputData(aPolyData)
delaunay.SetSourceData(boundary)
################################## Actors etc:
meshMapper = vtk.vtkPolyDataMapper()
meshMapper.SetInputConnection(delaunay.GetOutputPort())
colors = vtk.vtkNamedColors()
meshActor = vtk.vtkActor()
meshActor.SetMapper(meshMapper)
meshActor.GetProperty().EdgeVisibilityOn()
meshActor.GetProperty().SetEdgeColor(colors.GetColor3d("Peacock"))
meshActor.GetProperty().SetInterpolationToFlat()
boundaryMapper = vtk.vtkPolyDataMapper()
boundaryMapper.SetInputData(boundary)
boundaryActor = vtk.vtkActor()
boundaryActor.SetMapper(boundaryMapper)
boundaryActor.GetProperty().SetColor(colors.GetColor3d("Raspberry"))
boundaryActor.GetProperty().SetLineWidth(3)
boundaryActor.GetProperty().EdgeVisibilityOn()
boundaryActor.GetProperty().SetEdgeColor(1, 1, 0)
boundaryActor.GetProperty().SetRepresentationToWireframe()
renderer = vtk.vtkRenderer()
renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(renderer)
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)
renderer.AddActor(meshActor)
renderer.AddActor(boundaryActor)
renderer.SetBackground(colors.GetColor3d("Mint"))
renderWindow.SetSize(640, 480)
renderWindow.Render()
renderWindowInteractor.Start()
据我所知应该可以。就像在示例中一样,我创建了一组点,在前 4 个点(应该是洞)周围定义了一个边界,然后渲染它。
改变
for idd in range(start, start + 4):
进入
for idd in reversed(range(start, start + 4)):