# How to clip a polygon without triangulate?

**URL:** https://discourse.vtk.org/t/how-to-clip-a-polygon-without-triangulate/14607
**Category:** Support
**Tags:** python, code
**Created:** [September 24, 2024, 4:32am UTC](https://discourse.vtk.org/t/how-to-clip-a-polygon-without-triangulate/14607 "2024-09-24T04:32:00Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![altair0985](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@altair0985](https://discourse.vtk.org/u/altair0985)
#### Post date: [September 24, 2024, 4:32am UTC](https://discourse.vtk.org/t/how-to-clip-a-polygon-without-triangulate/14607/1 "2024-09-24T04:32:00Z")

</div>

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.

```auto
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:

 ![image](https://discourse.vtk.org/uploads/default/original/2X/1/1c739477e4b271a627cd1a11ac1f978a6deae817.png)

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [September 25, 2024, 12:16pm UTC](https://discourse.vtk.org/t/how-to-clip-a-polygon-without-triangulate/14607/2 "2024-09-25T12:16:59Z")

</div>

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

```plaintext
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.

---

<div class="post-metadata">

### Author: ![will.schroeder](https://discourse.vtk.org/user_avatar/discourse.vtk.org/will.schroeder/32/233_2.png) [@will.schroeder](https://discourse.vtk.org/u/will.schroeder)
#### Post date: [September 25, 2024, 12:22pm UTC](https://discourse.vtk.org/t/how-to-clip-a-polygon-without-triangulate/14607/3 "2024-09-25T12:22:49Z")

</div>

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).

---

<div class="post-metadata">

### Author: ![altair0985](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@altair0985](https://discourse.vtk.org/u/altair0985)
#### Post date: [September 26, 2024, 9:01am UTC](https://discourse.vtk.org/t/how-to-clip-a-polygon-without-triangulate/14607/4 "2024-09-26T09:01:28Z")

</div>

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

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

```

---

<div class="post-metadata">

### Author: ![altair0985](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@altair0985](https://discourse.vtk.org/u/altair0985)
#### Post date: [September 26, 2024, 9:08am UTC](https://discourse.vtk.org/t/how-to-clip-a-polygon-without-triangulate/14607/5 "2024-09-26T09:08:01Z")

</div>

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