In /Contributions/Utilities/ConvexDecomposition/b2Polygon.cpp
There is a convenience function DecomposeConvexAndAddTo...
...which decomposes a perhaps concave single polygon into multiple convex-only polygons and affixes them to a b2Body. It works great but I'd like to serialize the final decomposed convex polygons' coordinates instead of adding them to a b2Body.
I would just use the DecomposeConvex function to do so (in the same manner DecomposeConvexAndAddTo does above) BUT the problem is, above in DecomposeConvexAndAddTo there is a critical looking chunk of code, prefaced by the following comment...
I'm not asking anyone to do this for me - I'm content to keep trying (tried all day!), but I thought it couldn't hurt to ask if anyone had already done this and wouldn't mind sharing 🙂
There is a convenience function DecomposeConvexAndAddTo...
Code:
void DecomposeConvexAndAddTo(b2Polygon* p, b2Body* bd, b2FixtureDef* prototype) {
if (p->nVertices < 3) return;
b2Polygon* decomposed = new b2Polygon[p->nVertices - 2]; //maximum number of polys.
int32 nPolys = DecomposeConvex(p, decomposed, p->nVertices - 2);
// printf("npolys: %d",nPolys);
b2FixtureDef* pdarray = new b2FixtureDef[2*p->nVertices];//extra space in case of splits
int32 extra = 0;
for (int32 i = 0; i < nPolys; ++i) {
b2FixtureDef* toAdd = &pdarray[i+extra];
*toAdd = *prototype;
//Hmm, shouldn't have to do all this...
/*
toAdd->type = prototype->type;
toAdd->friction = prototype->friction;
toAdd->restitution = prototype->restitution;
toAdd->density = prototype->density;
toAdd->userData = prototype->userData;
toAdd->categoryBits = prototype->categoryBits;
toAdd->maskBits = prototype->maskBits;
toAdd->groupIndex = prototype->groupIndex;//*/
//decomposed[i].print();
b2Polygon curr = decomposed[i];
//TODO ewjordan: move this triangle handling to a better place so that
//it happens even if this convenience function is not called.
if (curr.nVertices == 3){
//Check here for near-parallel edges, since we can't
//handle this in merge routine
for (int j=0; j<3; ++j){
int32 lower = (j == 0) ? (curr.nVertices - 1) : (j - 1);
int32 middle = j;
int32 upper = (j == curr.nVertices - 1) ? (0) : (j + 1);
float32 dx0 = curr.x[middle] - curr.x[lower]; float32 dy0 = curr.y[middle] - curr.y[lower];
float32 dx1 = curr.x[upper] - curr.x[middle]; float32 dy1 = curr.y[upper] - curr.y[middle];
float32 norm0 = sqrtf(dx0*dx0+dy0*dy0); float32 norm1 = sqrtf(dx1*dx1+dy1*dy1);
if ( !(norm0 > 0.0f && norm1 > 0.0f) ) {
//Identical points, don't do anything!
goto Skip;
}
dx0 /= norm0; dy0 /= norm0;
dx1 /= norm1; dy1 /= norm1;
float32 cross = dx0 * dy1 - dx1 * dy0;
float32 dot = dx0*dx1 + dy0*dy1;
if (fabs(cross) < b2_angularSlop && dot > 0) {
//Angle too close, split the triangle across from this point.
//This is guaranteed to result in two triangles that satify
//the tolerance (one of the angles is 90 degrees)
float32 dx2 = curr.x[lower] - curr.x[upper]; float32 dy2 = curr.y[lower] - curr.y[upper];
float32 norm2 = sqrtf(dx2*dx2+dy2*dy2);
if (norm2 == 0.0f) {
goto Skip;
}
dx2 /= norm2; dy2 /= norm2;
float32 thisArea = curr.GetArea();
float32 thisHeight = 2.0f * thisArea / norm2;
float32 buffer2 = dx2;
dx2 = dy2; dy2 = -buffer2;
//Make two new polygons
//printf("dx2: %f, dy2: %f, thisHeight: %f, middle: %d\n",dx2,dy2,thisHeight,middle);
float32 newX1[3] = { curr.x[middle]+dx2*thisHeight, curr.x[lower], curr.x[middle] };
float32 newY1[3] = { curr.y[middle]+dy2*thisHeight, curr.y[lower], curr.y[middle] };
float32 newX2[3] = { newX1[0], curr.x[middle], curr.x[upper] };
float32 newY2[3] = { newY1[0], curr.y[middle], curr.y[upper] };
b2Polygon p1(newX1,newY1,3);
b2Polygon p2(newX2,newY2,3);
if (p1.IsUsable()){
p1.AddTo(*toAdd);
bd->CreateFixture(toAdd);
++extra;
} else if (B2_POLYGON_REPORT_ERRORS){
printf("Didn't add unusable polygon. Dumping vertices:\n");
p1.print();
}
if (p2.IsUsable()){
p2.AddTo(pdarray[i+extra]);
bd->CreateFixture(toAdd);
} else if (B2_POLYGON_REPORT_ERRORS){
printf("Didn't add unusable polygon. Dumping vertices:\n");
p2.print();
}
goto Skip;
}
}
}
if (decomposed[i].IsUsable()){
decomposed[i].AddTo(*toAdd);
bd->CreateFixture((const b2FixtureDef*)toAdd);
} else if (B2_POLYGON_REPORT_ERRORS){
printf("Didn't add unusable polygon. Dumping vertices:\n");
decomposed[i].print();
}
Skip:
;
}
delete[] pdarray;
delete[] decomposed;
return;// pdarray; //needs to be deleted after body is created
}
if (p->nVertices < 3) return;
b2Polygon* decomposed = new b2Polygon[p->nVertices - 2]; //maximum number of polys.
int32 nPolys = DecomposeConvex(p, decomposed, p->nVertices - 2);
// printf("npolys: %d",nPolys);
b2FixtureDef* pdarray = new b2FixtureDef[2*p->nVertices];//extra space in case of splits
int32 extra = 0;
for (int32 i = 0; i < nPolys; ++i) {
b2FixtureDef* toAdd = &pdarray[i+extra];
*toAdd = *prototype;
//Hmm, shouldn't have to do all this...
/*
toAdd->type = prototype->type;
toAdd->friction = prototype->friction;
toAdd->restitution = prototype->restitution;
toAdd->density = prototype->density;
toAdd->userData = prototype->userData;
toAdd->categoryBits = prototype->categoryBits;
toAdd->maskBits = prototype->maskBits;
toAdd->groupIndex = prototype->groupIndex;//*/
//decomposed[i].print();
b2Polygon curr = decomposed[i];
//TODO ewjordan: move this triangle handling to a better place so that
//it happens even if this convenience function is not called.
if (curr.nVertices == 3){
//Check here for near-parallel edges, since we can't
//handle this in merge routine
for (int j=0; j<3; ++j){
int32 lower = (j == 0) ? (curr.nVertices - 1) : (j - 1);
int32 middle = j;
int32 upper = (j == curr.nVertices - 1) ? (0) : (j + 1);
float32 dx0 = curr.x[middle] - curr.x[lower]; float32 dy0 = curr.y[middle] - curr.y[lower];
float32 dx1 = curr.x[upper] - curr.x[middle]; float32 dy1 = curr.y[upper] - curr.y[middle];
float32 norm0 = sqrtf(dx0*dx0+dy0*dy0); float32 norm1 = sqrtf(dx1*dx1+dy1*dy1);
if ( !(norm0 > 0.0f && norm1 > 0.0f) ) {
//Identical points, don't do anything!
goto Skip;
}
dx0 /= norm0; dy0 /= norm0;
dx1 /= norm1; dy1 /= norm1;
float32 cross = dx0 * dy1 - dx1 * dy0;
float32 dot = dx0*dx1 + dy0*dy1;
if (fabs(cross) < b2_angularSlop && dot > 0) {
//Angle too close, split the triangle across from this point.
//This is guaranteed to result in two triangles that satify
//the tolerance (one of the angles is 90 degrees)
float32 dx2 = curr.x[lower] - curr.x[upper]; float32 dy2 = curr.y[lower] - curr.y[upper];
float32 norm2 = sqrtf(dx2*dx2+dy2*dy2);
if (norm2 == 0.0f) {
goto Skip;
}
dx2 /= norm2; dy2 /= norm2;
float32 thisArea = curr.GetArea();
float32 thisHeight = 2.0f * thisArea / norm2;
float32 buffer2 = dx2;
dx2 = dy2; dy2 = -buffer2;
//Make two new polygons
//printf("dx2: %f, dy2: %f, thisHeight: %f, middle: %d\n",dx2,dy2,thisHeight,middle);
float32 newX1[3] = { curr.x[middle]+dx2*thisHeight, curr.x[lower], curr.x[middle] };
float32 newY1[3] = { curr.y[middle]+dy2*thisHeight, curr.y[lower], curr.y[middle] };
float32 newX2[3] = { newX1[0], curr.x[middle], curr.x[upper] };
float32 newY2[3] = { newY1[0], curr.y[middle], curr.y[upper] };
b2Polygon p1(newX1,newY1,3);
b2Polygon p2(newX2,newY2,3);
if (p1.IsUsable()){
p1.AddTo(*toAdd);
bd->CreateFixture(toAdd);
++extra;
} else if (B2_POLYGON_REPORT_ERRORS){
printf("Didn't add unusable polygon. Dumping vertices:\n");
p1.print();
}
if (p2.IsUsable()){
p2.AddTo(pdarray[i+extra]);
bd->CreateFixture(toAdd);
} else if (B2_POLYGON_REPORT_ERRORS){
printf("Didn't add unusable polygon. Dumping vertices:\n");
p2.print();
}
goto Skip;
}
}
}
if (decomposed[i].IsUsable()){
decomposed[i].AddTo(*toAdd);
bd->CreateFixture((const b2FixtureDef*)toAdd);
} else if (B2_POLYGON_REPORT_ERRORS){
printf("Didn't add unusable polygon. Dumping vertices:\n");
decomposed[i].print();
}
Skip:
;
}
delete[] pdarray;
delete[] decomposed;
return;// pdarray; //needs to be deleted after body is created
}
...which decomposes a perhaps concave single polygon into multiple convex-only polygons and affixes them to a b2Body. It works great but I'd like to serialize the final decomposed convex polygons' coordinates instead of adding them to a b2Body.
I would just use the DecomposeConvex function to do so (in the same manner DecomposeConvexAndAddTo does above) BUT the problem is, above in DecomposeConvexAndAddTo there is a critical looking chunk of code, prefaced by the following comment...
Code:
//TODO ewjordan: move this triangle handling to a better place so that
//it happens even if this convenience function is not called.
...it does some final processing on any decomposed polygons found to be triangles and I'm having a hard time pulling this out of DecomposeConvexAndAddTo as Ewjordan's comment suggests, so it can happen even if I'm not using the convenience function DecomposeConvexAndAddTo.//it happens even if this convenience function is not called.
I'm not asking anyone to do this for me - I'm content to keep trying (tried all day!), but I thought it couldn't hurt to ask if anyone had already done this and wouldn't mind sharing 🙂