How to triangulate polygon with holes?

I have a set of 3d points defining a polygon A, and a set of points defining polygon B. All these points are planar, and A is actually a loop within B.
My question is: how can i generate a triangulated polygon with hole? ie. the green region on the right.

image

I searched relevant documentation and found that vtkContourTriangulator::TriangulateContours(https://vtk.org/doc/nightly/html/classvtkContourTriangulator.html#a69c679d25ea3ccad36ad9fcd45864a3a) can do this. However, I don’t know how to use this function. Is there any example code for this?

Thanks.

1 Like

An example is here:

https://examples.vtk.org/site/Cxx/Modelling/ContourTriangulator/

The typical usage of this class is

vtkNew<vtkContourTriangulator> triangulator;
triangulator->SetInputData(contourData);
triangulator->Update();
vtkPolyData *polygons = triangulator->GetOutput();

If you have concentric contours, you can append them before triangulating them. For good results, the contours should be in opposite directions. If B is counterclockwise, then A should be clockwise.

vtkNew<vtkAppendPolyData> appendData;
appendData->AddInputData(contourB);
appendData->AddInputData(contourA);
appendData->Update();

vtkNew<vtkContourTriangulator> triangulator;
triangulator->SetInputData(appendData->GetOutput());
triangulator->Update();
vtkPolyData *polygons = triangulator->GetOutput();
2 Likes

thanks! that’s exatcly what I’m looking for.

I tried the code above and encountered an empty result. After a bit of toggle I found that the root cause is I forgot to set the lines of polygon’s polydata. For anyone who needs to do the same, remember to set the lines before using vtkContourTriangulator.