Convex shape decomposition

Shouldn't 'wise-ness' (CCW vs CW) of the verts have some effect on weather the shape is convex or concave?

For example: (using nice round points for ease)
v0 = (0, 0)
v1 = (-2, -1)
v2 = (-1, -2)

Shouldn't the above be considered concave
and

v0 = (-1, -2)
v1 = (-2, -1)
v2 = (0, 0)

be considered convex?
AFAIK winding order has no effect on clockness, if you want to prove it to yourself, try drawing the same shape CW and CCW -- the end result is identical.

also, triangles can't be concave so your example doesn't work 😉
raigan wrote:
AFAIK winding order has no effect on clockness, if you want to prove it to yourself, try drawing the same shape CW and CCW -- the end result is identical.

also, triangles can't be concave so your example doesn't work 😉


I suppose I understand winding order to be the same as clockness so I don't understand what you're trying to say.

Maybe I have done a poor job of explaining what I wanted....Let me try again:

Obviously a triangle is convex but what I mean is that a lot of algorithms that do convex decomposition require the verts in a CCW order, and I though his java applet mentioned that but I can't check at the mo because his site is down, so I expected that the first set of verts would fail because they were CW and the second set should pass because they are CCW AND convex.

How bout that...any better?
raigan is correct, vertex order and concavity are two completely different issues (winding number, though more complex in general, reduces to the CW/CCW distinction for triangles when you use "normal" geometries). The applet at ewjordan.com only worked for CCW orientation, I think (I might have patched it up before uploading, but I can't tell because it's down, as you noticed - I have no idea why it's down, btw) but the C++ code I've uploaded here should work for either, though the output will always come out CCW oriented because that's what Box2D needs.

A polygon is convex if and only if you can draw a line between every two pairs of vertices without the line passing outside of the polygon.

So with things as they are now, the algorithm should accept either CW or CCW polygons. In fact, the only real restriction is that the polygons do not self-intersect (points that equal each other will make the currently uploaded version break, but I've fixed that bug since I uploaded). I'm currently adding the ability to trace the outer edge of an arbitrary non-simple (allowing self-intersection) polygon, which along with convex hull and convex decomposition should give you just about all the tools you need to sanitize any sort of geometrical input.

If there's enough interest, I can think about adding things like polygons with holes and CSG operations (union, intersections, and differences), but if nobody really needs it, I won't bother - I'm definitely not going to put together a full geometry library here, but I'm happy to throw together a few more things if people would find them useful.
I thought of a feature I'd like. Can you make sure that when a polygon is broken down into convex shapes, each shape has less than b2_maxPolyVertices?

This will let users merrily place as many vertices as they like and let your algorithm do the dirty work 🙂

Edit: Oh..you already did this. Wonderful 😀

Holes would be nice, but I personally don't have any use for them just yet.

I love that it allows CW/CCW because that's been driving me a little insane. My CW-reverser isn't working so well.

Just FYI: I'm snooping through your code now... you said you're a little rusty with C++.

Code:
b2PolyDef* deleteMe = DecomposeConvexAndAddTo(&pgon, &bd, &polyprot);
delete[] deleteMe;


Should just be
Code:
delete DeleteMe;


Reference: http://www.cplusplus.com/doc/tutorial/dynamic.html

"The result of deleting an array object with delete is undefined, as is deleting an individual object with delete[]."
Thanks for the tip - actually, though, in this case the deleteMe pointer is to the first element of an array, so the delete[] call gets rid of the whole array. Eventually I'm going to get rid of that issue anyways, as it's rude to make the user delete anything, but at the moment Box2d requires the b2ShapeDefs to be in scope when the body is created, so the whole array needs to be on the heap.

In any case, glad you're finding it useful!
Oh.. didn't realize it was returning an array. My bad! Sorry.

Anyway, just got it working. Works wonderfully! Thank you so much. This is fantastic!
I think I found a glitch... making a triangular shape like this with 4 vertices:

Image

Causes it to create a shape like this (it looks like it doesn't get decomposed at all):

Image

You'll notice the green shape got decomposed properly.. so I think I implemented it properly.. nevertheless, here's my code:

Code:
void World::CreatePoly(vector<b2Vec2> poly, bool draw_static)
{
   b2PolyDef pd;
   if(draw_static) pd.density   = 0;
   else         pd.density   = 10;
   pd.friction      = .3f;
   pd.restitution   = .2f;

   float *x, *y;

   x = new float[(int)poly.size()];
   y = new float[(int)poly.size()];

   for(int i=0; i<(int)poly.size(); ++i)
   {
      x[i] = poly[i].x;
      y[i] = poly[i].y;
   }

   b2Polygon pgon(x,y,(int)poly.size());

   delete x;
   delete y;

   b2BodyDef bd;
   DecomposeConvexAndAddTo(&pgon, &bd, &pd);
   m_world->CreateBody(&bd);
}


Actually, starting from the bottom right and moving left it works fine, but going the other way around causes the glitch. Looks like the winding-order problem isn't quite fixed. When you place the vertices in the wrong order, it still detects collisions though. Normally when you screw up the order with Box2D it just doesn't collide with anything... actually, you'll notice the thick blue line along the bottom...I think your code is turning that bottom piece into a triangle. The bottom (blue) triangle seems more opaque... which means it's been shaded twice... I don't know if that's just OpenGL's doing (it doesn't like concave shapes either if they're drawn in the wrong order) or you actually have overlapping triangles there...

I hope that's thorough enough 🙂

In short: breaks with any 4-vertex ccw concave polygon
Ok, thanks for the report, I'll look into it - that's a case that definitely needs to be handled correctly, and since it's the smallest polygon that actually needs to be decomposed it probably means there's an off-by-one bug or something like that.

In case you see this before I get a chance to try to figure it out, are you closing the polygons by yourself (i.e. doubling the first point), or letting them close themselves? Not sure this should matter, but it might.
Letting them close themselves.

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