How to clip a polygon without triangulate?

I want to clip a vtkPolygon in vtkUnstructuredGrid, the result should only have one vtkcell(vtkPolygon).
I used vtkClipdataset, vtkTablebaseclipdataset, but the two algorithm may generate more than one vtkcell(vtkTriangle) in the result. Is there any method to meet the previous requirement?
Here is the code, thanks.

points = vtkPoints()
points.InsertNextPoint(1, 0, 0)
points.InsertNextPoint(1, 1, 0)
points.InsertNextPoint(-1, 1, 1)
points.InsertNextPoint(-1, 0, 1)

polygon = vtkPolygon()
polygon.GetPointIds().SetNumberOfIds(4)
polygon.GetPointIds().SetId(0, 0)
polygon.GetPointIds().SetId(1, 1)
polygon.GetPointIds().SetId(2, 2)
polygon.GetPointIds().SetId(3, 3)

u_grid = vtkUnstructuredGrid()
u_grid.InsertNextCell(polygon.GetCellType(), polygon.GetPointIds())
u_grid.SetPoints(points)

origin = (0, 0, 0)
normal = (1, 0, 0)

clip_plane_1 = vtkPlane()
clip_plane_1.SetOrigin(origin)
clip_plane_1.SetNormal(normal)

clipper = vtkClipDataSet()
clipper.SetClipFunction(clip_plane_1)
clipper.SetInputData(u_grid)
clipper.Update()
result = clipper.GetOutput()
print(result.GetNumberOfCells()) # number = 3

writer = vtkXMLUnstructuredGridWriter()
writer.SetFileName(file_path)
writer.SetInputData(vtk_model)
writer.Write()

Result in Paraview:

Something like this might work, since the clipper doesn’t triangulate polylines:

polygon -> polyline -> clipper -> polyline -> polygon

If the clipper breaks the polyline into multiple cells, then vtkStripper (with JoinContiguousSegmentsOn()) can be used to reassemble them back into a polyline.

In general, though, there is no filter in VTK for clipping polygons without triangulation.

The basic problem is that polygons can be concave. Concave polygons when clipped can produce multiple polygons. Converting to polylines and back, if polygons are concave, will also produce multiple output polygons. A filter could be written that checks for concave/convex and processes the polygons appropriately (at the expense of performance).

2 Likes

Thanks for your suggestion.
I have solved the problem by the follow filter:

vtkClipDataSet -> vtkFeatureEdges -> vtkStripper 
-> vtkDecimatePolylineFilter -> vtkContourLoopExtraction -> vtkAppendFilter

Yes, indeed. It will be more complex to clip a concave polygon.
Fortunately, there are some triangles need to be clip in my program. :smiley: