vtkContourTriangulator fails for closed, 2d rectangle.

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"

The vtkContourTriangulator has a bug where it sometimes fails with collinear points (like points 3, and point 6). It will succeed if you remove such points, but that is probably not the general solution you’re looking for. VTK’s Delaunay triangulation also has problems with such points.

I’m aware of the bug and know approximately where it is in the code (a particularly tricky part of the code), but haven’t found time yet to dig in and fix it. Examples like yours are very useful to the debugging process.

1 Like