Concave Polygon Tessellation / Triangulation

Hey all,

Again a thread about those concave polygons... 🙂

I would like to ask everyone working with this to share experience, ideas and code, so we can finally find the best methods for decompositing concave polygons.

As I see there are 2 approaches:
* Rectangles between vertices
* Tessellation into simple, non-overlapping polygons

I have played a bit with the rectangles method (code: concavePoly(...)). It works nice, but has a bit problems with "sharp edges" and gaps, and generates an awful lot of shapes...

The other approach is tessellation, the breaking up of self-intersecting polygons into simple polygons (test pseudo code):
Image

This seems very interesting, as with this method it gets down to 4 shapes, whereas the rectangle method needs at least 7 (if the gaps are filled with circles, 14).

Anyone done this before? How are you working with concave polygons?

[edit]title (convex -> triangulation)[/edit]

----
Elements -- Box2D API for Python
http://elements.linuxuser.at
I find a lot of infos about the OpenGL tessellation. This page explains a bit more how opengl is doing it.
Image

OpenGL is first detecting all intersections and creating the different regions as GL_TRIANGLE, GL_TRIANGLE_FAN, GL_TRIANGLE_STRIP and GL_LINE_LOOP


In general, there are a lot of infos about polygon triangulation. This page describes an implementation based on Seidel's algorithm which proceeds like this:

Image

[EDIT]Triangulation, Seidel's algorithm[/EDIT]
[EDIT2]

Another implementation is the ear-clipping algorithm (which also ewjordan started implementing a while ago).

See also:
* Overview of some polygon triangulation algorithms
* Delaunay_triangulation
Image
[/EDIT2]
Figured I'd weigh in here, as I've done a bit of work on this (somewhere in the convex decomposition thread there's the C++ code, which actually mostly works, though the version there is not 2.0 ready - I have to upload that still).

There are standard algorithms for dealing with all this stuff, and they work okay. I've gotten triangulation + recombination into polygons working pretty well, and I will check that in to SVN once I take another pass at it (soon - I'm on vacation right now, but I'm getting back on Friday, so soon thereafter). But it's not ideal.

First problem: convex decomposition

This is easily achieved when starting from a simple polygon by 1) triangulating (ear clipping, Seidel's algo, some sort of Delaunay triangulation, etc.), and 2) recombining triangles into polygons (a bunch of different ways to do this). While it's easy to emit a mathematically legitimate set of convex polygons, it's a lot tougher to constrain these polygons to conform to Box2d's requirements, which include:

- Edges can't be too close to parallel
- Area must be greater than a minimum value
- Each edge must be more than b2_toiSlop away from the centroid

In particular, the third one causes a lot of problems, as a normal triangulation often creates thin slivers. Navigating around this stuff, in fact, turns out to be much harder than just getting the decomposition to work right. Currently what I've been doing is 1) biasing the triangulator towards "nicer" triangles, at a slight speed hit, and 2) just throwing away polygons that don't satisfy all these requirements at the end of the process. This works, but it leaves geometry gaps in some cases, which is far from ideal.

I've asked around, and I can't find any algorithms that are designed with these types of restrictions in mind. Constrained conforming Delauney triangulations seem pretty close to what we want, but still end up producing some small vertices (plus, I have no idea how to add vertices - there seem to be no decent explanations online of the "standard" way to do this!). So it's probably worth thinking about what the best thing to do is when you come across a "bad" polygon. Lately I'm thinking that maybe the offending polygons should just be split along near-parallel angles and expanded away from the centroid until they satisfy all these restrictions, as at least this would not leave gaps.

What do you think? Is it better to have gaps, or cover areas that are supposed to be empty (and possibly double cover other areas)? Or should we just fail? (I don't like that option, because in many cases this utility will be used to sanitize user input)

The other subtlety that must be dealt with is how to handle sequential or non-sequential identical vertices (or at least near enough to identical so that precision issues cause trouble), which usually crash triangulators - in this case I split the polygon or merge the vertices, depending. This works well, but it makes the problem a lot messier bookkeeping-wise because you all of a sudden have no certainty when you enter triangulation as to how many triangles you'll end up with. If I get a chance to re-write everything, I'm going to do it with this in mind, as it's messy to wedge this in when you started with the n-2 triangles assumption.

Second problem: dealing with non-simple (i.e. self-intersecting) polygons

This is actually a bit more straightforward. The simplest algorithm (the one that I use currently) is to create a list of all the segment intersections and generate a graph with connections between these nodes, and then walk along the edge of the graph tracing out the outline, which will then be a simple polygon. So far I haven't seen any problems with this algorithm (once I worked out the kinks, esp. dealing with very close vertices and merging them and whatnot), other than the fact that it's not incredibly fast as written.


I can significantly improve the performance characteristics of all of these algorithms in my implementation. But it seems foolish to do so if the overall approach itself going to need reworking to adapt better to the type of output that we prefer for use in the physics engine.

So here's what I'd really love to find, that I think would really help the quality of the results: a good triangulation algorithm that automatically adds internal vertices to a non-convex polygon in a way that makes as many triangles as possible satisfy Box2d's requirements. Right now I only work with existing vertices, which I think is a bad idea (I was about to switch from ear-clipping to Seidel's algorithm for speed purposes, but Seidel's algorithm still produces slivers, so the quality of results is not any better, and at least I can bias ear clipping towards "fat" triangles - this is harder with Seidel's algorithm). But I just can't find any info on doing this stuff - all the constrained Delaunay triangulation algorithm explanations seem to assume that the set of points is already set. If anyone knows anything about this stuff, please let me know!

Edit: For non-simple polygons there is a pretty good set of algorithms at http://geometryalgorithms.com/Archive/a ... 0Algorithm that could be used to quickly split them into simple pieces. I was actually planning to switch my naive segment intersection algo to Bentley-Ottman, which has nice usual-case running time, so it might be convenient to start from that implementation. The implementation is offered under the following license:

// Copyright 2001, softSurfer (http://www.softsurfer.com)
// This code may be freely used and modified for any purpose
// providing that this copyright notice is included with it.
// SoftSurfer makes no warranty for this code, and cannot be held
// liable for any real or imagined damage resulting from its use.
// Users of this code must verify correctness for their application.

I'm a little nervous about this, because it seems that it might obligate users of the library to include the copyright notice even if they don't distribute source - anyone have any thoughts on that? I'm no legal expert, the "included with it" part might only mean that you have to include the notice with the code, not the compiled version...
Chris' diagram suggests a fourth problem too:
Any normal decomposition results in non-overlapping shapes. So it would fill the black shape with 2 triangles and 2 quadrilaterals at best. But to a human eye, it seems more natural to use 4 triangles, with some overlap. This is more efficient for Box2D, and marginally more robust (as things can "fall through" cracks in the geometry of nonbullet shapes).
Similarly when attacking an "X" shape, it is best made of two rectangles, not 3.
But I'm betting that such considerations are almost impossible to implement, except in trivial or pre-programmed cases.

Anyway, as I mentioned in the original thread, Triangle seems all powerful. It's not free enough to just copy it over to Box2D, but he has very conviently listed all the academic papers from which he drew his inspiration, so you can probably find out how he manages to add extra vertices by reading those. Granted this is probably pretty tedious.
Ah, you're right, I see http://www.cs.cmu.edu/~quake/tripaper/triangle3.html has some concrete info. Looks quite complicated, but if the results are good, it might be worth it.

FWIW, I'm still not positive that Delauney triangulations are a cure here, because most Delauney meshes I've seen have some pretty tiny triangles. I'm definitely willing to give them a shot, though.

Also, good point about sometimes preferring overlapping results to non-overlapping ones (though Erin did make the point that sometimes this leads to redundant contact points being generated) - we should think a bit about methods to do this, I think this could help ease some of the problems I talked about above.
My game has a some Constructive Set Geometry. That basically means I need a really good tessellator. I looked at some really great libraries, but the best aren't free for commercial use. However, after all is said and done I am using the OpenGL tessellator. It works great. And it's available everywhere. I haven't done any performance tests yet, but I imagine that for most uses the tessellation will not be done in a time critical loop

-nick
Yeah, the OpenGL tessellation works fine. But opengl isn't available everywhere.
For example on the XO Laptop where Box2D is running quite nice...
Also it is mathematically very interesting 🙂

(and i agree that in first instance performance is a secondard priority, as the tessellation will usually be only done here and then)

----
Elements -- Box2D API for Python
http://elements.linuxuser.at
You can easily lift the tesselator out of the opengl reference implementation. For example see CTess (http://mollyrocket.com/tools.html). The GLU tesselator provided by the ref implementation is one of the best and most robust triangulators out there. The catch is that the source has nasty license. The license allows similar usage as MIT license, but it has some "anti competition" clauses there which are probably really annoying in real life. If you want to use the glu tesselator in a realtime case I recommend adding simple pool allocator for that extra speed.
here is a pdf version of the license text: http://linuxuser.at/download/SGIFreeSWLicB.1.1.pdf
[edit]see also:
* Silicon Graphics: OpenGL Licenses
* OpenGL Standard (Reference) Implementation (FAQ)
[/edit]

[edit2]
FAQ wrote:
One way in which the SGI Free Software License B is like BSD (and other similar licenses) is that it allows the user to use and modify the software in practically any way it likes - including even making commercial products from it.
[/edit2]
Here's what the FSF has to say:
Quote:
SGI Free Software License B, version 1.1

The SGI Free Software License B, although its name says "free", is not a free software License. It has three major problems. 1. It restricts its patent license to unmodified versions of the software. 2. It terminates if your use of the software infringes copyrights or patents which are not SGI's. This is problematic because it gives SGI grounds to sue you even when you have done nothing to them. 3. The license requires you to inform SGI of legal problems with the software. This violates your privacy rights, and can conflict with professional confidentiality requirements, such as attorney-client privilege.


Not sure what to make of that, whether it's a "problem" or not.

Question to anyone that's used the tessellator before: do you know if you can constrain it to produce triangles that are fatter than some threshold? Can't see why you'd need to for graphics, but you never know, they might have put that feature in...

Original thread: https://box2d.org/forum/viewtopic.php?t=719