Search code examples
pythonvtk

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.


Solution

  • Change

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

    into

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