Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

python - VTK: creating polygon and a hole with vtkDelaunay2D not working

I would like to render a Polygon with a hole in VTK. I found that the vtkDelaunay2D class should do the trick. I followed the tutorial here vtkDelaunay2D Example and it worked perfectly.

Afterwards I tried to create a different polygon with a different hole. And I don't get why it's not working and would like to know what I would have to change.

Should look like this This is how it should look like. Reality This is how it looks like in reality.

This is the code I used:

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

As far as I can tell it should work. Like in the example I create a set of points, define a boundary around the first 4 points (the should be hole) and then just render it.

question from:https://stackoverflow.com/questions/65871868/vtk-creating-polygon-and-a-hole-with-vtkdelaunay2d-not-working

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Change

for idd in range(start, start + 4):

into

for idd in reversed(range(start, start + 4)):


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...