Convex shape decomposition

I put together a quick Processing (== Java, for all intents and purposes) sketch for a polygon convex decomposition algorithm: http://www.ewjordan.com/earClip/. I had a question, though - for the purposes of Box2D, would it be better to have the decomposition be quick and non-optimal (potentially produce more polygons than the minimum necessary to cover a region), or slow and closer to optimal? Unfortunately the code for a truly optimal convex decomposition gets pretty nasty, so right now I just apply an extremely naive algorithm, just walking over the triangle list and trying to combine neighbors sequentially, but if you think it's worth it to minimize the polygon count as much as possible, there are some other tricks that could be employed so that you will at least come very close to an optimal decomposition (at a speed hit, of course - the current algorithm is O(N^2) in vertices, and to do better without seriously restructuring the algorithm would probably require an additional pass through the vertices for each bit, bringing us to O(N^3)). Speed doesn't matter much at initialization, but if people are dynamically creating shapes all over the place, it might be an issue.

In any case, take a look at the applet, let me know if you think this kind of decomposition would do the job okay. If so, I'll clean up the code a bit, add the maximum number of vertices and max shapes per body restrictions (this would be good, because it would allow people to pass convex polygons with too many vertices to the decomposition algo and get back a list of polygons that do the same job) and put together a C++ version.
This looks good. Fewer polyogns is better, but no imperative.

Have you thought about how this would be hooked up to Box2D? Somehow I would like to encourage people to use this as a preprocessing step. Preferably they would cook the results into their data files.
Yep, I thought a little about how to integrate this. Ideally something like this would not be totally built in, as people may prefer to substitute their own decomposition algorithms and not be forced to use this implementation (if, for instance, they needed either minimal decompositions or faster ones). I think a good solution to this would be to include an abstract decomposition interface and a default implementation, but add a hook that allows a user to tell the engine to use a subclass instead (which they would extend from the abstract one as they wished).

Since it would usually be good to force some sort of decomposition before trying to use the shapes, you could include a flag on each polygon that indicates whether it might need to be decomposed. If the flag is true when the shape is bound to the body (which it would be by default), then it is passed through the filter before attaching it; alternatively, if the user is already sure that the shape is "safe," they can unset that flag and bypass the extra steps. This way you're not forcing anyone to do anything (if they hate the whole idea, they can always declare a decomposition implementation that does absolutely nothing), but you're making sure they have to explicitly decide to ignore the check.

Let me know if this sounds reasonable and/or if you have other ideas. In a few days I'll have a bit of free time, and I'll try to get it working in the Java port and then start moving it to C++.

Oh, re: cooking results into data files - it would be easy enough to return the results of a polygonization to the user, but it would require them to explicitly call for the decomposition.
Well, we will also have a convex hull tool that will take vertices and spit out other vertices. In a similar manner, your tool will take vertices and spit out multiple arrays of vertices.

Would it be simpler to have a utility call to process the vertices? I'm worried that if convex decomposition is in the body creation loop, then it will be abused and create some unnecessary frame rate dips.
I think the question is: what is the desired behavior of the engine when an inexperienced user comes along, one that maybe doesn't realize there is anything special about a non-convex polygon? There are two main options, either 1) automatically check any polygon that is not flagged as "safe" before it is used, thus potentially causing a speed hit to users that are dynamically creating lots of shapes, or 2) do nothing by default, provide a utility to decompose the polygons as needed, and don't try anything clever.

It's ultimately up to you - my thinking is that it would be generally more user friendly to handle it automatically as much as possible, to the extent that can be done without impacting performance (and with an option to turn this off if it turned out to be causing performance problems). But there's definitely an argument for the other side, too; some users will be inconvenienced or surprised by the behavior either way, and I'm not sure how it all balances since I don't know how people will be using the code in practice.

Perhaps if you did want to automatically incorporate decomposition a convexity test could be added in the shape definition constructor, and if it failed, it would call the decomposition algorithm and store the results in the shape def. This would require some restructuring of that class so that it could hold arrays of polygons instead of just a single polygon, of course, but would avoid the issue of recomputing the same decomposition many times for the same shape.

Ultimately, though, it probably would be much simpler just to leave it as a utility and expect people to know enough to call it when they need to.
How about a non-convex shape type?
Mike
I haven't gotten any bug reports from people confused about polygon convexity. Also, I have a convexity check that is fast (but not sufficient).

Let's keep this as a utility for now. As long as we document it, I'm sure people will use it. We'll also make a test case for it. This is how other physics engines tend to work.

Thanks a lot for doing this project! I'm sure many users will find it valuable.
Sounds good, I'll let you know when I get it all working right.
Almost done with this, after a break from it (== me completely forgetting I was working on it...oops!). I've got this set up to work as a utility, just taking in an outline and passing back a list of polygons. I made the code respect the max vertices per polygon limitation, but it does not pay any attention to the max polygons per body one - that is easy for the user to worry about himself, and it's really not something that this should need to know about. It now handles any orientation as well, though I'm still not checking simplicity.

While I was at it I added a quick function to return the convex hull of a set of points, since you (Erin) mentioned that before, which seems to be working ok, though it's not optimal (O(N^2) worst case). I was wondering, before I start porting the code I have from Java, are there any other geometry routines that would be worth coding while I'm still thinking about this? I don't want to bloat this too much, but if there are any requests from anyone, just let me know.
Convex hull too? Great!

If you are willing, some test cases and documentation would be great.

BTW, I've got Newton's Cradle running with CCD.

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