Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Robust, fast complex polygon (with holes) triangulation c/c++ library with permissive license [closed]

I am a developer of the open-source game, Bitfighter. As per the following SO post, we have used the excellent 'Triangle' library for mesh-zone generation for use with our in-game AI (robots):

Polygon Triangulation with Holes

However, we ran into a small snag when wanting to package our game for Debian - the use of the 'Triangle' library will make our game be considered as 'non-free'.

We have been extremely pleased with the performance of the 'Triangle' library, and don't really want to give it up; however, we don't like dealing with license issues either. Therefore we have embarked upon a quest to find a suitable, permissively-licensed replacement that can match 'Triangle' in its robustness and speed.

We're looking for a C or C++ library for dividing large, complex, areas into triangles, that can handle any type of irregular polygons placed together in any manner, as well as holes. Robustness is our primary need, with speed almost as important.

I have found poly2tri, but it suffers from a bug in which it cannot handle polygons with coincident edges.

We have found several libraries, but all seem to suffer from one thing or another: either too slow, or don't handle holes, or suffer from some bug. Currently we are testing out polypartition and we have high hopes.

What are the best alternatives to the great 'Triangle' library, but have a permissive license?

like image 896
raptor Avatar asked Apr 16 '13 17:04

raptor


People also ask

How do you triangulate a polygon with holes?

Let a polygon P with h holes have n vertices total, counting vertices on the holes as well as on the outer boundary. Then a triangulation of P has t = n + 2h - 2 triangles, and a quadrilateralization has q = n/2 + h — 1 quadrilaterals.

How do you triangulate a polygon?

1) For each vertex in the polygon, compute the angle between the two linked edges. 2) Sort vertices by decreasing angle relative to the interior of the polygon. 3) If there is less than 3 vertices in the set, we're done. 4) Take the last vertex in the set and output the triangle formed by it and its two neighbours.

What is ear clipping triangulation?

2.1 Basic Algorithm The ear clipping triangulation algorithm consists of searching an ear and then cut- ting it off from current polygon. The original version of Meisters's ear clipping al- gorithm runs in O(n3) time, with O(n) time spent on checking whether a newly formed triangle is valid.


1 Answers

I found a solution. It was poly2tri after all, with the use of the excellent Clipper library, and some minor algorithmic additions to the inputs.

Our process is as follows:

  1. Run all our holes through Clipper using a union with NonZero winding (this means that inner holes are wound the opposite direction as outer ones). Clipper also guarantees nice clean input points with no repeats within epsilon.
  2. Filter our holes into ones that are wound counter-clockwise and clockwise. Clockwise holes meant the hole was circuitous and that there was another concentric area inside that needed to be triangulated
  3. Using poly2tri, triangulate the outer bounds and each clockwise polygon found, using the rest of the holes as inputs to poly2tri if they were found within one of the bounds.

Result: poly2tri seems to triangulate just about as fast as Triangle and has so far been very robust with everything we've thrown at it.

For those interested, here are our code changes.

Update

I have attempted to pull out our clipper-to-poly2tri code, with our robustness additions, into a separate library which I started here: clip2tri

like image 166
raptor Avatar answered Oct 19 '22 04:10

raptor