Convex shape decomposition

I recently started developing with the svn-version of box2d with xcode3 under osx 10.5 and I've been running into some problems. I've been trying to get ewjordans convex shape decomposition to work, but no success. This is what I have done:

- I created a folder in "Source" named "Contrib" and copied b2Polygon.cpp/h and b2Triangle.cpp/h into that folder
- I copied "ConvexDecomposition.h" into the "Tests" folder
- I edited "TestEntries" and added the needed entries for convex decomposition

When I try to build the whole project I get the following errors:
Code:
error: ../Collision/b2Shape.h: No such file or directory
error: expected constructor, destructor, or type conversion before '*' token
error: 'b2_maxPolyVertices' was not declared in this scope
error: 'b2PolyDef' has not been declared
error: expected `)' before '*' token
In member function 'bool b2Polygon::IsUsable(bool)':
error: 'b2_maxPolyVertices' was not declared in this scope
error: 'b2_maxPolyVertices' was not declared in this scope
At global scope:
error: variable or field 'AddTo' declared void
error: 'int b2Polygon::AddTo' is not a static member of 'class b2Polygon'
error: 'b2PolyDef' was not declared in this scope
error: 'pd' was not declared in this scope
error: expected ',' or ';' before '{' token
error: expected constructor, destructor, or type conversion before '*' token
In function 'b2Polygon TraceEdge(b2Polygon*)':
error: 'class b2PolyNode' has no member named 'connected'
error: 'class b2PolyNode' has no member named 'connected'
error: 'class b2PolyNode' has no member named 'connected'
error: 'class b2PolyNode' has no member named 'connected'
error: 'class b2PolyNode' has no member named 'connected'
error: 'class b2PolyNode' has no member named 'connected'
error: 'class b2PolyNode' has no member named 'connected'
error: 'class b2PolyNode' has no member named 'connected'
error: 'class b2PolyNode' has no member named 'connected'
error: 'class b2PolyNode' has no member named 'connected'
error: 'class b2PolyNode' has no member named 'connected'
error: 'class b2PolyNode' has no member named 'connected'
error: 'class b2PolyNode' has no member named 'connected'
In member function 'void b2PolyNode::AddConnection(b2PolyNode&)':
error: 'connected' was not declared in this scope
In member function 'void b2PolyNode::RemoveConnection(b2PolyNode&)':
error: 'connected' was not declared in this scope
error: 'connected' was not declared in this scope
In member function 'void b2PolyNode::RemoveConnectionByIndex(int32)':
error: 'connected' was not declared in this scope
In member function 'bool b2PolyNode::IsConnectedTo(b2PolyNode&)':
error: 'connected' was not declared in this scope
In member function 'b2PolyNode* b2PolyNode::GetRightestConnection(b2PolyNode&)':
error: 'connected' was not declared in this scope
error: 'connected' was not declared in this scope


I can fix some of those errors by setting the right path for b2Shape.h (which is located in "Collision/Shapes" in box2d 2.0 but was located "Collision/" in box2d 1.4.3), renaming b2PolyDef to b2PolygonDef and removing the parentheses in the declaration of the connected-array in class b2PolyNode. Still 7 errors are remaining in b2Polygon.cpp:

Code:
error: 'struct b2PolygonDef' has no member named 'localPosition'
error: 'struct b2PolygonDef' has no member named 'localPosition'
error: 'struct b2PolygonDef' has no member named 'localRotation'
error: 'struct b2PolygonDef' has no member named 'localRotation'
error: 'struct b2BodyDef' has no member named 'AddShape'
error: 'struct b2BodyDef' has no member named 'AddShape'
error: 'struct b2BodyDef' has no member named 'AddShape'


I can uncomment the lines where localPosition/localRotation is used but I can't fix the last 3 errors because AddShape does not exist.
Hopefully I didn't forget something important that you guys need to know, this is my first posting. Thank you in advance...
Ah, right - most of those errors are due to the fact that I wrote this utility to a post 1.4.3 but pre 2.0 version (right before b2PolyDef turned into b2PolygonDef, I think). Those leftover ones (missing AddShape) will require me to redo some things, because the way shapes are added to bodies has now changed. That thing with the "connected" variable is an instance of XCode being more strict than Visual C++, sorry about that (I've got a new Mac, so I'll be checking things on XCode from now on, too).

I'm actually busy right now getting the Java port of 2.0 done, but as soon as I'm done with that I'll figure out what changes need to be made to integrate this utility with the release version of 2.0. Sorry for the trouble!
Hi,

Just wondering, why is this functionality being implemented in 2.5.0 and not sooner?

Version 2.5.0
- Convex decomposition.
- Kinematic bodies.
I put that on the TODO list before Eric kindly volunteered for the task. I'm looking forward to removing that item from the list. 🙂

I'm sure Eric has his hands full with the Java port of 2.0 and the convex decomposition.
Great, it's difficult to actually find the download link in the forum, maybe critical (for me) development like this should be branched in subversion?
I'm probably about 90% done with the Java port now, which has the helpful side effect that I know now what I need to change to update this code for 2.0. I'll put this in the repository as soon as I can make these changes and do a couple tests.
Hey Eric, I've got some more data that is causing us some problems after being ran through your algorithm. The two pictures show what is going on and the txt file has all the vertex data for this concave shape. Its like the vertex #5 gets moved a little or something, I admittedly haven't traced through at all so I don't really understand what is going on. Also we simply call the DecomposePolygon function, not DecomposeAndAddTo or whatever its called.
I'll have to look more closely at this example, but off the top of my head it looks like the decomposition is creating a polygon with two sides that are too close to parallel for Box2d to accept them, so it's collapsing them into one side, thus leaving a gap in the polygon.

This is a tricky problem to prevent on my end in the general case, as there are a lot of competing restrictions on the geometry that can go into Box2d, so it's impossible to always scrub things so that they work right. However, in your case the input geometry is not pathological at all, so the decomposition should not be removing any of it. I have a good idea what I should check for (if the offending polygon has a decent volume, it can be split across one of its diagonals to "fix" the bad angle), so I'll take a look at this soon.

In the meantime, you might get away with using the results anyways - a small crack like that shouldn't cause any serious physics problems, as I doubt anything could get stuck in there, though if you're drawing based on the shapes that might be an issue.

I'll try to get a fix in for this with the update for 2.0.
Okay yea, I took a little time today and figured out what was going on. You're right we end up with a polygon that has nearly parallel edges and so the offending vert is merged in. I like the idea of spliting the offending polygon as in most of the cases I've seen that would help.

anyways sorry I paniced a little before digging into it and seeing what was happening.
Okay this time I think I found a problem. I basically was blindly letting your algorithm handle all of our collision data, I never looked at it just got the verts in the poly and called DecomposePolygon on it. (I know its wastefull but .... meh). So today I passed it a completely convex shape and it returned 4 polys back to me. This is really only odd because we used to be using your old version and it worked fine on this very same shape. Anyways heres the vertex dump:

    x:-0.356346f, y:-0.000957f
    x:-0.312642f, y:0.180575f
    x:-0.174703f, y:0.309679f
    x:0.004308f, y:0.359696f
    x:0.172506f, y:0.316000f
    x:0.302397f, y:0.185786f
    x:0.355486f, y:0.000597f
    x:0.313692f, y:-0.160935f
    x:0.183145f, y:-0.303458f
    x:-0.014116f, y:-0.352042f
    x:-0.190331f, y:-0.300758f
    x:-0.312628f, y:-0.167970f

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