Convex shape decomposition

Building on some Delaunay Triangulation code by Paul Bourke, and using Eric Jordan (ewjordan)'s b2Polygon/b2Triangle classes, I've made a working method to decompose any 2-d polygon into convex components. The way that I've gone about doing it, it will work for complex, self-intersecting polygons, as well as polygons with an arbitrary number of holes, and even holes within holes (suspended solid areas inside holes -- think concentric circles).

Here is the method:

  • Make delaunay triangulation out of the vertices of the polygon
  • Find edges of the polygon for which there does not exist a triangle from the triangulation that shares an edge with it (in other words, a polygon edge that crosses some triangles)
  • Split the triangles into 2 or 3 more triangles to allow for these edges
  • Delete triangles which are not contained within the polygon (ray-cast test)
  • Merge the triangles together into convex polygons
  • Delete any bad polygons (too small, etc) and add them to b2Body

Currently it works perfectly except I do not have a good convex polygon triangle-merging method, and for certain bad shapes it'll still leave a bunch of small shapes not merged together.

One problem with this approach is that for each triangle that is split up along the middle, i.e., into a quadrilateral and a triangle, I automatically split the quadrilateral into two triangles. This was because my triangle merger only took triangles. The problem with this, however, is that it generates a large number of extra splits, and isn't good for performance.

I've found that there are certain types of concave polygons, which do not adapt well to this method because of the nature of the Delaunay triangulation.

A simple example:
Image

This shape is fairly trivial and will not cause any problems on my method because all it has to do is split two triangles, because the delaunay triangulation is this:

Image


However, if you have a shape that has perhaps 20 zig-zags, the triangulation might turn out having a triangle-edge crossing several of the polygon-edges, and this causes that one triangle to be split several times. The triangle is split into 3 sub-triangles with the first polygon-edge, and each subsequent polygon-edge can theoretically split ALL THREE of them, etc. This is exponential behavior, and although it would be fairly difficult to come up with a situation that calls for such a shape, it is very undesired. Generally, if you take your mouse and move it randomly while clicking, after about 30-40 vertices, the results will not be pretty.

Luckily there is a solution! I am certain that this aforementioned issue of there being too many triangle splits being done will not be a show-stopper for almost any implementation of box2d, but in the case that the triangulation turns out drastically different from the underlying polygon shape (which, once again, is quite rare and only occurs with the ugliest of polygons), we can split it up using this method described here: http://www.flipcode.com/archives/Merging_Polygons_And_Sub-Pixel_Gaps.shtml

This is an illustration of the algorithm:
Image

For each concave vertex (notch, if you will) on the polygon, slice up one of the offending sides.

Avoiding unnecessary slicing, we'd only want to separate the polygon once at each notch:
Image
Image
Image

This method should be very effective for splitting up the kinds of shapes that don't match the triangulation well (e.g. Long segments with many points close together). Shallowly concave shapes (which are ideal for the triangulation method), tends to cause it to make lots of unsightly cuts, so this should complement the triangulation-based method.


I have not discussed constrained delaunay triangulations yet, which involve changing the delaunay triangulation algorithm a bit to allow for segments to be included. If anyone can find some working code for that, I think I can simplify this much further. I'm pretty sure that a minimal-constrained-delaunay triangulator is perfect for this task.
Finished! Allows holes and everything. 😀 Download here:
viewtopic.php?f=6&t=975&p=5800#p5800

This current method works more than well enough for most any game implementation, and a constrained delaunay method will only improve on the performance quite a bit as well as reduce some possible extra splitting.
Hi everyone, looking through the forum I could not find a pointer to the definitive implementation of non-convex polygon decomposition; only progressive development (and limited to C++); does a "definitive" (or up-to-date) solution exist?
ewjordan, did you include the decomposition algorithm somewhere in jbox2d?

Thank you very much.
I'm still waiting for someone to figure out an efficient constrained delaunay triangulation. My method is a little ugly.
Have you seen http://www.ewjordan.com/earClip/ ?
Seems to be quite nice code
It's been a while (and a lot of school work) but I haven't given up on this yet.

When I ported my code to compile on GNU/linux, my code that does the triangulations changed slightly in behavior and basically just crashes and burns. I'd try to debug it but it's honestly looking like a lost cause. It's too much for me to figure out in any decent amount of time.

But there's good news!
Here's something I found:

http://gts.sourceforge.net/reference/gt ... tions.html

The GTS library is licenced under LGPL so it looks like we're good to go. Will post with results when I get it working.


and koslakk, I'm trying to minimize the amount of sharp "sliver" type triangles, and any of the simpler triangulation algorithms just aren't going to cut it. Constrained Delaunay is pretty much optimal for what I want.
Also this may be even more useful but I don't know if the source is available.

http://www.cs.gmu.edu/~jmlien/research/app-cd/
I could not get GTS to work, but I came across this: http://www.cs.nyu.edu/exact/core/cdt/

This does everything I want and it just *worked* when ran the makefile. Stay tuned.
dc443 wrote:
I could not get GTS to work, but I came across this: http://www.cs.nyu.edu/exact/core/cdt/

This does everything I want and it just *worked* when ran the makefile. Stay tuned.


This kind of just blows up when I started testing with randomized input, i.e, a series of random points plus a series of segments connecting them which do not intersect each other. It would either give up and quit, or infinite loop, and I think i saw it segfault at some point. I even wrote this entire algorithm which takes a set of edges from a triangulation and calculates the triangles. Hopefully that will be of use later since it took a bit of work.

I'll go back to looking at GTS.

Edit: I decided to stop spamming the thread since I've already made 4 posts in a row...

So far, GTS is a LOT more stable and I am having quite a bit of success with randomized input tests being run in a loop.

However after a couple hundred or thousand triangulations on point clouds and randomized (non intersecting) constraint segments, it seems to always run into an internal assertion which I do not know if I will be able to provide a workaround for, i.e. detect a problem with the data input before it gets fed to the triangulator which will in turn bring the entire program to a screeching halt.

Actual input should be a lot more friendly than random points with random segments, though, since in the case of convex decomposition, all the points are going to be connected with constraining edges, which are the edges of the polygon.

Here's a screenshot of my test cases (it's tearing near the bottom because VSync isn't on. oh well):
Image

I might also add that I have absolutely no idea how this might be incorporated into the box2d library itself. I am currently doing lots of hackish things and my program links with the GTS library, and getting it to work nicely was quite a bit of work.

Stay tuned for more results...


edit:

Okay so Ive completed the basic convex decomposition using an actual Constrained Delaunay Triangulation algorithm paired with ewjordan's triangle polygonizer.
These were drawn by just clicking to draw the shapes.
Image
Image

The second one is a particularly pathological case and it would have definitely caused my earlier algorithm to crash because the regular delaunay triangulation of the point cloud would look absolutely nothing like the actual shape whereas the constrained DT is very natural. Notice how the spiky parts are all made of triangles which make up the entire spike. Similarly with the first one you can see it handles smooth shapes very nicely and is practically optimal. I read somewhere that finding an optimal convex decomposition is an NP-hard problem. I'll have to do some tests but this should run quickly for any kind of shape that you would ever want to draw. I'll sit down and have a click-fest later. Its like 5AM.
Hi Ewjordan.

I did a small port of your code to match the current setup of PolygonShape.

I'v got everything running and i tested by using your "gearshape" and that worked..
Now that i try to add my own complex shape and decomp it using your code its not working though.

Its failing within the "TriangulatePolygon" method on these lines:
Code:
         int32 nA = TriangulatePolygon(pA.x,pA.y,pA.nVertices,mergeA);
         int32 nB = TriangulatePolygon(pB.x,pB.y,pB.nVertices,mergeB);
         if (nA==-1 || nB==-1){
            delete[] mergeA;
            delete[] mergeB;
            return -1;
         }


Any ideas why this is ?

Cheers
Emil

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