I am using vtkContourTriangulator to triangulate 2d shapes. But it fails for a simple rectangular shape:
In related post: vtkContourTriangulator (Triangulation failed) - Development - VTK it is mentioned that triangulation may not work on planes but I could not find it in the documentation.
Is there something simple that I can do to fix this, in general?
Reproduce:
from vtkmodules.vtkCommonCore import vtkIdList, vtkPoints
from vtkmodules.vtkCommonDataModel import vtkCellArray
from vtkmodules.vtkFiltersGeneral import vtkContourTriangulator
vertices = [(-60, 0.0, 0.0),
(-60, -2.5, 0.0),
(-60, -2.5, 4.0),
(-60, 0.0, 4.0),
(-60, 2.5, 4.0),
(-60, 2.5, 0.0),
(-60, 0.0, 0.0)]
out_points = list(vertices)
# make a vtk polydata object from these points
vtkPts = vtkPoints()
for p in out_points:
vtkPts.InsertNextPoint(*p)
# add the polygon to the polydata
cellArray = vtkCellArray()
# use vtkContourTriangulator to triangulate the polygon
idList = vtkIdList()
for i in range(len(vertices)):
idList.InsertNextId(i)
result = vtkContourTriangulator.TriangulatePolygon(idList, vtkPts, cellArray)
if result != 1:
import matplotlib.pyplot as plt
# plot in 3d
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for i,p in enumerate(out_points):
ax.scatter(*p)
# add label with number
ax.text(*p, s=str(i))
plt.title(f"Triangulation of the following {len(vertices)} points failed")
for v in vertices:
print(v)
plt.show()
assert result == 1, "Triangulation failed"