Anyone who has worked with constructive solid geometry (CSG), built their own voxel engine or used ready-made models has run into the triangle optimization problem. The number and placement of triangles in a model may be less than optimal. Of course, if a designer made the model for you personally, you can ask them to fix it. But what if the model is procedural and the designer cannot fix every procedural part?
We have to do it automatically.
For example, let's take one part from the procedural stage generator written in JavaScript:

As we can see, the model has a very large number of triangles that do not affect its geometric parameters. Many ready-made solutions and libraries for model optimization already exist, but none does the job the way we need it. All the more so because we need to do it fast and clean, without distorting the texture mesh or the final model.
Before starting to build the model optimization module, I tried dozens of ready-made libraries and various off-the-shelf solutions, but none came even close to the result I needed. After making sure there was no ready solution, I started building my own.
The solution looked simple, but that was only how it seemed to me. As a result, a whole month went into the implementation.
So how does it work? Let's go through the steps: we combine all triangles into groups depending on the texture and the texture mesh. This gives us groups containing only triangles with a single texture and a single texture mesh whose overall picture will not change when vertices are removed.
After that we convert our triangles into polygons and remove redundant vertices. Once we do that, we already have a good-quality model.
But that is only the beginning.
Converting polygons back into triangles is not the easiest task; the reverse path is harder. You can do the conversion with ready-made libraries, but one big disappointment awaits us here. Ready-made libraries such as earcut, libtess and a few others manage to place triangles so badly that the defects are hard to fix even in an editor.
After a dozen sketches on a sheet of paper, I decided to use Delaunay triangulation (created back in 1934 by the Soviet mathematician Boris Delaunay), but this is far from simple.
Delaunay triangulation cannot be applied in its pure form. Even though the number and placement of triangles would always be optimal:

In theory everything is fine, but in reality a problem appears immediately: the triangles end up in the wrong places:

Red marks the expected subdivision for our structure. But a surprise awaited us here: the triangles crossed the final structure. I had to improve the method, and the task came down to rebuilding the triangles that intersect our edge. After the transformations the result was as follows:

Now the triangles are in the right order, filling the polygon optimally. I could have stopped there.
But that is far from everything: since JavaScript in browsers is not the fastest, and CPU physics calculations in JavaScript are too sensitive to the number of triangles, I applied an additional transformation:

The image shows the original part and the same part after the transformations. From the polygon we got 6 triangles, excellently placed, and after the final transformation there are 4, which was the goal. Of course, this changes the model and micro cracks become noticeable in it, but this transformation also cuts the model down by another 5–10%.
And here is the final result: after all the transformations our model looks like this:

The original model contained
4148 triangles,
after conversion to polygons and removal of redundant vertices: 1174 triangles, with the final optimization applied we got
994 triangles.
Result: we reduced the model from
4148 triangles to 994.
This is a great result: the model now uses less memory, it renders faster on the graphics card, and a physics engine written in JavaScript no longer pays such a high price for processing that many triangles.