Convex shape decomposition

Quote:
The minimum shape radius must be bigger than b2_toiSlop or you will get an assertion when the core shape is computed.

Ok, that's why I'm not seeing it happen now - I'm still using one of the earlier 2.0 prototypes, I guess before you added that check. Just so I make sure I'm checking the same way, do you remember off the top of your head which file that check appears in?

Quote:
For the case you mentioned, you should just eliminate the middle vertex. Can't you just detect the shallow angle and eliminate the vertex?

Right, and that's what I've been doing, but as allanon noted, the first problem there is that a very shallow triangle is eliminated altogether - this can probably just be seen as a limitation of the engine, so these should just be removed (there's no way to force simulation of a shape that can't simulate properly). The real problem is that in a decomposition, if one of the decomposed polygons has a very shallow angle, removing the vertex (cutting off a triangle) ends up leaving a small, thin hole, usually touching an external vertex. Which things can get stuck in...

Ah, which brings up another issue - it doesn't look like CCD quite solves the issue where things get stuck in the "seams" of compound objects (probably because while the true geometry actually encloses the interior, the core shapes leave gaps). I could attempt to mitigate this by double covering the seams (if possible), but I'm not sure if that's going to cause problems of its own with the physics - any suggestions?

For the moment, I'm going to shoot for merely getting the output of this thing to provide valid input for Box2d in as many cases as possible, at the cost of potentially leaving out bits of the original geometry. I'll upload a new version in a couple days (I should also have the edge tracing working better by then, too) and let's see how it works in practice. If there are serious problems, we can figure out what to do from there.

Oh, also, is anyone that's planning on using this still using 1.4.3, or have you all moved on to the 2.0 prototype? I'm probably going to make the switch to that pretty soon, which means a few things will have to change - let me know if you would like me to keep the 1.4.3 code in there with a preprocessor conditional or something like that, otherwise I'll excise it pretty soon.
Yeah, I can see it's a difficulty to avoid these narrow triangles. I think you're best bet to solve the problem once and for all is using a better triangulation algorithm, there are some out there which aim for that sort of goal. A quick browse of the internet points to Triangle which seems to solve the problem pretty nicely, and has source and large page of references.
I'm not terribly concerned about this limitation.

For static terrain I will be supporting line segement soups and these will not require a core shape. So the segment ends can directly touch.

For moving objects people should try your tool and see the result. If they don't like the result then they can hand author the decomposition. I think your tool should do it's best to create something that works in most cases and produces something that doesn't crash. There is not a perfect solution.

It is possible on my end to prevent collisions with specified polygon edges via flags. This should go on the TODO list.
Yup, I would love to use Triangle as-is, but it's under a non-commercial license so that's probably not an option, though if I'm correct it's okay to use for generation of your geometry as long as you don't redistribute it in your code. Currently I decompose without adding internal vertices, but you're right, things will get a bit better if I allow additional vertices to be created. I'll start digging into the literature on constrained conforming Delaunay triangulations to see how obnoxious this code will be to put together. The trick is going to be figuring out how to make this stuff fail the most gracefully (for instance, when the geometry is just too fine to both satisfy the minimal angle and minimal radius requirements - if you look at the output of Triangle, some of the triangles are extremely tiny, which won't work for Box2d).

Quote:
For moving objects people should try your tool and see the result. If they don't like the result then they can hand author the decomposition. I think your tool should do it's best to create something that works in most cases and produces something that doesn't crash. There is not a perfect solution.

True enough. I won't drive myself too crazy over a few edge cases as long as it's generally working.
Quote:
It is possible on my end to prevent collisions with specified polygon edges via flags. This should go on the TODO list.

Excellent, that will at least ensure that things can at least slide apart if they get stuck (currently friction can make things stick together for good, pretty much). I'll mark certain edges as internal when that's available in the engine.
Quote:
Excellent, that will at least ensure that things can at least slide apart if they get stuck (currently friction can make things stick together for good, pretty much).


I'll use the next best edge to resolve overlap, leading to a quick resolution in most cases (I hope).
Okay, here's another update to this code.

Changes:
- Triangulation has been tweaked so that during ear clipping the minimum angle is maximized, leading in many cases to better triangulations. Not a perfect solution, but notably better than before.
- "Pinch points" are now handled
- TraceEdge(b2Polygon*) has been rewritten and appears to actually work now to decompose non simple oriented paths - see ConvexDecomposition.h for an example use. This is about as close as you can get to general purpose user input sanitization.
- Added polygon validity checking before attempting to add to b2ShapeDef, which should catch most unusable pieces before they are sent into Box2d and cause assertions.
- If B2_POLYGON_REPORT_ERRORS (at the top of b2Polygon.cpp) is true, which it is by default, any errors will be dumped to the console. I'm particularly interested in failures of triangulation; invalid polygons are polygons that generally speaking have details too fine for Box2d to use, so there's not much I can do about those.

Known issues:
- Decomposition still fails when presented with almost collinear "polygons," I'm working on this - this is the usual cause if the utility fails to create anything at all.
- Sometimes pieces of your geometry just won't be added. This could either be because the details are too small to work with the engine, or it could be because of crappy triangulation. Think about it, and let me know if it's the latter.
- TraceEdge can be slow and memory intensive (at least O(N^2) in both) - I can't promise to improve the speed a whole lot right now, but if I get a chance I might switch from using static arrays to growable ones which should improve memory use quite a bit.
- Though this is still not fully updated for 2.0, I've been working with an early version of 2.0, so b2_toiSlop is used - if you use 1.4.3, you can deal with this by altering a variable near the top of b2Polygon.cpp. A full 2.0 switchover will be coming very soon.

Let me know of any problems, questions, suggestions - this is getting close to the point where it should reasonably handle most input, esp. if you use TraceEdge, so give a holler if it's failing on you, especially if it either hangs or generates something that makes Box2d assert.
Yay! Fun algorithms stuff!

I'm not sure exactly which algorithms you're using for convex hull generation and concave decomposition, but you should be able to get considerably better than O(n^2) for the convex hull problem at least, and there are some heuristics for concave triangulation that give pretty good results in pretty good time.

Andrews' algorithm, in particular, works very well in 2D, and can be done in-place on an already-allocated array of vertices. I believe it is O(n lg n) with a relatively small constant. I think a 2D-ized Quickhull implementation could probably do even better.

I've implemented all of this in the past in 3D for a real-time project; when I get home I'll see if I can find the code and whip up a 2D-ized version. I've also got a slower but much better version of the decomposition thing around somewhere (that I put in an editing tool, instead of a real-time application). I know I've got the hull stuff, but my decomposition code breaks everything down into triangles, not n-gons, if that matters.

Out of curiosity, though, which algorithms are you using? Most of the decomposition algorithms I recall involve adding interior vertices.
Re-reading my post, I'd like to apologize in advance for being a huge nerd.
Hey, being a huge nerd is why we're here, reading about/developing physics applications.
Okay, a rundown of the algorithms.

Convex Hull: I used the gift-wrap algorithm for this, where you start at an "outer" point (I think I pick the minimum y value) and just walk along the edge of the points counterclockwise. This actually gives O(N*M) time, where N is the number of points and M is the resulting number of vertices. This is worst-case O(N^2) when you're taking the convex hull of a convex polygon, but it performs much better on a generic cloud. I'm not too sure what the typical use case is for a convex hull algorithm, so maybe a guaranteed O(n log n) implementation would be better, depending.

Triangulation: I'm doing a totally naive and unoptimized ear clipping triangulation, with no added vertices. When choosing an ear to clip, I always pick the triangle with the greatest minimum angle - this is a hacked together attempt to create nice triangulations. This algorithm is definitely slower than it needs to be, and it doesn't produce as nice triangulations as it could - it should be O(N^2), but I think due to some array handling garbage it might even be worse in my implementation. I've been meaning to work on an algorithm that does add vertices, either edge or interior, but I just haven't had the time lately.

Polygonization: I forget the name of this algorithm; it's fairly simple, you start with a seed triangle, then you add as many triangles as possible to it without making a non-convex polygon, then you start with another seed, and so on. I forget exactly how I implemented this, but it's probably O(N^2) in the number of triangles.

Edge tracing: First I create a node graph by finding all vertices and intersections of lines and storing the connectivities. The intersection testing is O(N^2) or worse (for convenience of programming, I may have taken a couple shortcuts that bump this up a bit when there are lots of intersections), and the node storage could be O(N^2) worst case (as I haven't made the arrays dynamic, they are currently always sized for worst case). Then you just walk along the edge of the node graph and generate the outline of the shape, which is much faster.

I'm not likely to spend much time myself optimizing these things unless someone really pushes me to because they need them running at interactive speeds. Currently, for polygons of reasonable size most of these algorithms work fast enough not to severely lag a program, though I wouldn't recommend using them every frame. However, I'll very happily incorporate rewrites, patches, suggestions, etc. - honestly, all this stuff is quite outside my area of expertise, but since nobody else was doing it I figured I'd give it a go.

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