fast-triangle-triangle-intersection icon indicating copy to clipboard operation
fast-triangle-triangle-intersection copied to clipboard

Floating-Point Precision Causes Unexpected Intersection Errors

Open GuoJiechang opened this issue 9 months ago • 1 comments

I encountered an issue when performing intersection tests with triangles where small floating-point differences lead to unexpected errors.

Triangle values to reproduce the error:

// Case that causes intersection error
__t1.a.set(0, 50.000000000000014, 50);
__t1.b.set(0, 49.999999999999986, -50);
__t1.c.set(100, 50.000000000000014, 50);
__t2.a.set(17, 50, -24.543842315673828);
__t2.b.set(18, 50, -23.144481658935547);
__t2.c.set(18, 49, -23.595157623291016);

Image

However, if I round the floating-point values to exact integers, the issue disappears:

// Case without error
__t1.a.set(0, 50, 50);
__t1.b.set(0, 50, -50);
__t1.c.set(100, 50, 50);
__t2.a.set(17, 50, -24.543842315673828);
__t2.b.set(18, 50, -23.144481658935547);
__t2.c.set(18, 49, -23.595157623291016);

Image

Expected Behavior: Small floating-point differences should not cause the intersection result to be wrong if the triangles are effectively the same in real-world positioning.

Potential solution: in src/utils.ts, function orient3D(), line 40. Round the determinate to an integer. const det = Math.round(_matrix4.determinant());

But I'd like to know if it is the best solution and if there are any potential issues.

Thank you!

GuoJiechang avatar Feb 26 '25 22:02 GuoJiechang