Convex shape decomposition

Ok, I took a quick look at what's going on, and I confirmed that this is a bug. I took a shortcut when deciding whether to correct orientation before decomposition (confusingly enough for me, it looks like my algorithm internally converts everything to have a clockwise orientation, an artifact of the fact that when debugging I was drawing with a library where the "up" y direction points down the screen...oops! - shouldn't matter for you, though), assuming that if orientation was wrong the triangulation would fail. It turns out that this isn't the case, and I'll need to actually verify orientation before doing the triangulation. This will actually speed up the algorithm a bit in some cases, so it's a good thing overall.

I'll fix this up soon and upload an updated copy. Thanks again for pointing it out!
No prob, thanks for fixing it 😀
I'm about 99% sure that you have some memory leaks in b2Ploygon

in this function:
Code:
void b2Polygon::MergeParallelEdges(float32 tolerance) {
   if (nVertices <= 3) return;
   bool* mergeMe = new bool[nVertices];
   int32 newNVertices = nVertices;
   for (int32 i = 0; i < nVertices; ++i) {
      int32 lower = (i == 0) ? (nVertices - 1) : (i - 1);
      int32 middle = i;
      int32 upper = (i == nVertices - 1) ? (0) : (i + 1);
      float32 dx0 = x[middle] - x[lower];
      float32 dy0 = y[middle] - y[lower];
      float32 dx1 = x[upper] - x[middle];
      float32 dy1 = y[upper] - y[middle];
      float32 norm0 = sqrtf(dx0*dx0+dy0*dy0);
      float32 norm1 = sqrtf(dx1*dx1+dy1*dy1);
      b2Assert(norm0 > 0.0f && norm1 > 0.0f);
      dx0 /= norm0; dy0 /= norm0;
      dx1 /= norm1; dy1 /= norm1;
      float32 cross = dx0 * dy1 - dx1 * dy0;
      if (fabs(cross) < tolerance) {
         mergeMe[i] = true;
         --newNVertices;
      } else {
         mergeMe[i] = false;
      }
   }
   if(newNVertices == nVertices) return;
   float32* newx = new float32[newNVertices];
   float32* newy = new float32[newNVertices];
   int32 currIndex = 0;
   for (int32 i=0; i < nVertices; ++i) {
      if (mergeMe[i]) continue;
      newx[currIndex] = x[i];
      newy[currIndex] = y[i];
      ++currIndex;
   }
   delete[] x;
   delete[] y;
   delete[] mergeMe;
   x = newx;
   y = newy;
   nVertices = newNVertices;
}


I'm not sure if its something I'm doing incorrectly but the statement "if(newNVertices == nVertices) return;" can cause the bool array (mergeMe) thats newed to skip deletion.

and in this function: (sorry its long so I hope I get this explained well)
Code:
int32 PolygonizeTriangles(b2Triangle* triangulated, int32 triangulatedLength, b2Polygon* polys, int32 polysLength) {
   int32 polyIndex = 0;

   if (triangulatedLength == 0) {
      return 0;
   }
   else {
      int* covered = new int[triangulatedLength];
      for (int32 i = 0; i < triangulatedLength; ++i) {
         covered[i] = 0;
      }

      bool notDone = true;
      while (notDone) {
         int32 currTri = -1;
         for (int32 i = 0; i < triangulatedLength; ++i) {
            if (covered[i])
               continue;
            currTri = i;
            break;
         }
         if (currTri == -1) {
            notDone = false;
         }
         else {
            b2Polygon poly(triangulated[currTri]);
            covered[currTri] = 1;
            for (int32 i = 0; i < triangulatedLength; i++) {
               if (covered[i]) {
                  continue;
               }
               b2Polygon* newP = poly.Add(triangulated[i]);
               if (!newP) {
                  continue;
               }
               if (newP->nVertices > b2Polygon::maxVerticesPerPolygon) {
                  delete newP;
                  continue;
               }
               if (newP->IsConvex()) {
                  poly.Set(*newP);
                  delete newP;
                  covered[i] = 1;
               }
            }
            if (polyIndex < polysLength) polys[polyIndex].Set(poly);
            polyIndex++;
         }
      }
      delete[] covered;
   }
   int32 upperLim = (polyIndex > polysLength)?polysLength:polyIndex;
   for (int32 i=0; i < upperLim; ++i) {
      polys[i].MergeParallelEdges(FLT_EPSILON);
   }
   return polyIndex;
}


In the part where you have b2Polygon *newP = poly.Add(...) you only delete newP if newP->nVertices > b2Polygon::maxVerticesPerPolygon or if newP is convex. So say neither of those cases are true the next time through the loop you are setting newP = to a new polygon but not deleteing the old one. Again, I may be doing something wrong but I don't think so.

Let me know if you need more info or anything else.
Yup, those are definitely leaks, thanks for the heads up - I had checked my memory usage while running this stuff and didn't see anything, but my test cases must not have been passing through those paths (also possible that the amount of memory leaked just wasn't enough to show up when I tried it). I'll fix this up ASAP.

BTW, I got the CW/CCW stuff resolved, so I'll upload a new version with all these fixes very soon - I'm also putting in a general filter for non-simple polygons, too, (it will simplify these by tracing the outline, then decompose that into convex pieces) which will let you sanitize any sort of user input. Once that's in place I can start "fuzzing" the thing to break it and work out any remaining bugs (it's tricky to generate a random simple polygon, so support for self intersection is crucial to do randomized testing).
Sorry to anyone that's depending on these fixes, it's going to take me a few more days to get them out thanks to an unexpected hardware failure (my Powerbook finally died on me, and that was my only machine with C++ tools on it - I'm going to get a temporary box set up in the next day or two, so hopefully I can finish this up and get it online soon).

If anyone needs this stuff ASAP, please e-mail me (address: my username here at gmail) and I can at least send you the source as it is once I pull it from the hard drive - as things are, the memory leaks and CW/CCW stuff should be fixed, but the non-simple handling is not done yet (that's what I was working on as my computer died, as luck would have it...).
Hey ewjordan,

That sucks, about the Powerbook. How's the coding going? I'm about ready to start re-implementing a proper map editor for my game, so I could use the code soon.
I'm not at my home computer now, but I should be able to get the fixes uploaded at some point over the next 24 hrs when I get there - sorry about the delay, I should have known better than to rely on a five year old laptop for anything! Looks like the non-simple handling will take a little more debugging, there are a bunch of edge cases that still cause trouble right now, but everything else appears to be working and the memory leaks seem to be fixed, so I'll upload ASAP.
Awesome, thank you!
I'm also interested in this -- excellent work ewjordan!
No problem, glad it will be useful - I'm finishing it up as we speak. Turns out my Mac was too forgiving on a couple of memory management issues that are causing occasional heap corruption now that I'm using Windows, so that's the reason for the delay, but I just traced the cause, so it's almost there, I promise (again)!

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