Open
Description
I find that adding this criterion to hasTriangleFlip significantly reduces the number of flipped or self-intersecting triangles in flat or smooth regions. The idea is taken from another mesh simplification repository https://github.com/sp4cerat/Fast-Quadric-Mesh-Simplification. See src.cmd/Simplify.h function bool flipped(...).
In a triangle ABC, where C is going to be replaced by point D, this check looks for cases where AD and BD are in almost the same direction.
The following is code I added to my local clone of meshoptimizer:
// check for abcd nearly coplanar case
// da * db too close to 1?
Vector3 bd = {d.x - b.x, d.y - b.y, d.z - b.z};
float normSquared = ed.x * ed.x + ed.y * ed.y + ed.z * ed.z;
normSquared *= bd.x * bd.x + bd.y * bd.y + bd.z * bd.z;
float daDotDb = ed.x * bd.x + ed.y * bd.y + ed.z * bd.z;
float len = sqrtf(normSquared);
if (len > 1e-7f) {
daDotDb /= len;
if (daDotDb > 0.999f) {
return true;
}
}
Here is a simple comparison for a mesh output by marching cubes.