2

I am looking for a way to triangulate and fill the interior of the contour of a non simple (possibly self intersecting) polygon. I found some very good algorithms online to triangulate simple curves or find the convex hull of a set of points, but my need is to find the contour of a list of points that I cannot rely on it being a Jordan curve (without punching holes for every self intersection like, say, mapbox/earcut, does)

I tried a python port of mapbox's earcut (https://github.com/joshuaskelly/earcut-python) which is great but punches holes for self intersections I couldn't try scipy.spacial since it does not support non simple curves at all.

I'm performing my research in Python and plan to port it to JavaScript eventually but for now I am language agnostic and any algorithm would do.

import earcut
from shapely.ops import cascaded_union
from shapely.geometry import Polygon
points = [[(0,0), (100,0), (100,50), (200,100), (0,100), (20,20), (80,20), (80,80), (20,80)],[]]
data = earcut.flatten(points)
flattrangles = earcut.earcut(data['vertices'])
alltriangles = [[points[0][j] for j in tr] for tr in triangles.T.tolist()]

cascaded_union([Polygon(tr) for tr in alltriangles])

Expected one large polygon with no holes Got a polygon with two triangular holes where the self intersections are

Eskapp
  • 2,737
  • 2
  • 21
  • 32
Oded Badt
  • 21
  • 1
  • 2
    It would be really helpful if you could post a sketch of what you want the outcome to be, versus what would be an unwanted outcome. I can only think of one unambiguous way to fill in a self intersecting polygon, and it involves holes at the self-intersections, which you mention is *not* what you want to do... – vlsd Apr 15 '19 at 16:59
  • 1
    @vlsd, I think he means this: https://en.wikipedia.org/wiki/Nonzero-rule – Matt Timmermans Apr 15 '19 at 17:19
  • 1
    @MattTimmermans nonzero is what I was thinking about (TIL about even/odd, thanks!), but even then there's the possibility of "holes" just like the excellent visual example on Wikipedia shows. – vlsd Apr 15 '19 at 20:09
  • 1
    If your goal is really to fill, there is no need to triangulate. You can go for straight polygon filling, with the parity or nonzero rule. – Yves Daoust Apr 16 '19 at 13:47
  • @YvesDaoust: my goal is indeed to fill the polygon eventually, but I do want to triangulate for vectorizaing and rendering via opengl. I can write an algorithm that rasterizes using a scan line, but would really prefer a library that is well tested that does it for me, as for holes, I'm convinced now, thanks to the comments above that I need to restate the question and seek nonzero rule triangulation, and really cannot use the "no holes" constraint which is clearly not well defined. I'll edit this post soon – Oded Badt Apr 17 '19 at 06:33
  • this thread might hold some answers https://stackoverflow.com/questions/406301/polygon-triangulation-with-holes/626302#626302 – vlsd Apr 22 '19 at 17:07
  • Great comments, thanks all you guys, each and every one helped me in reaching my goal – Oded Badt Apr 29 '19 at 13:50

0 Answers0