Convex Triangle Merging

It's been a while now that it is no longer necessary to consider the creation of core shapes when triangulating our concave shapes. The thin polygon "skin" works very well.

But it is still a good idea to minimize the number of fixtures and shapes used in a body to help speed up collisions.

I am pretty certain that as of now the best way to perform convex decomposition is by first using the poly2tri library to produce a constrained delaunay triangulation (CDT) using the polygon segments as the constrained edges.

This is great for rendering but we want to minimize the number of polygons in our b2Body.

In a previous post I made in the thread titled "Convex shape decomposition" here I put up some pictures at the end of the post which show the result of ewjordan's PolygonizeTriangles algorithm on the output of a CDT (This particular code used the GTS library's CDT... poly2tri is superior).

This is the general idea behind PolygonizeTriangles: Start at a triangle and merge it with adjacent triangles if the resultant polygon is convex, and has less than b2_maxPolygonVertices vertices. As you can see from my highly zig-zaggy and snakey examples this works fairly well, and there aren't clear examples of shapes which could be much better merged.

However if you consider a triangulation that has points on the inside of the shape, it's quite easy to have cases where we have convex shapes that the algorithm will be unable to recognize:

Image

This pentagon triangulation should be merged into a single pentagon for Box2D but the algorithm is unable to recognize that adding more triangles will return the polygon to being convex, and will simply leave this shape as 3 polygons.

Does anybody have ideas for improving the algorithm?

I am thinking along the lines of marching along edges. Performing a DFS on the edge graph, being careful to turn only in directions that maintain convexity, and limiting depth to b2_maxPolygonVertices.

Edit:

I gotta say, the greedy algorithm for merging works excellent, though. The one I just wrote runs a good deal faster than the triangulation does, at least for shapes without *too* many vertices. Even with the monkey test (1204 points) the merge step completes in under 3 ms where the triangulation finishes in 1.7ms... I'm fairly certain the algorithm is O(n^2) as well. I may be able to make it more efficient. But it's hardly worth even considering because of how fast it already is.

The original issue that I brought up in this post is still somewhat relevant though. The addition of inner points to make the triangulation even more "good" is quite possible especially on geometries that have gentle curves. As it is now there are always a few thin slivers that show up near the edge of a shape, and that's not optimal for box2d. We need to keep the edge areas as solid as possible.

For most use cases, it's entirely possible to get away with concave geometries by using segment fixtures. What i'm solving essentially boils down to concave to concave collisions.
I wonder if you could do something like "for each internal point, check if the polygon caused by the removal of the point would be convex". It might give a better result if the narrowest polygons were checked first, especially those on the exterior.

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