Skip to content

Commit

Permalink
Fix error threshold issue with coplanar tris
Browse files Browse the repository at this point in the history
  • Loading branch information
gkjohnson committed Jan 2, 2024
1 parent b972b6a commit d9fcf58
Showing 1 changed file with 33 additions and 8 deletions.
41 changes: 33 additions & 8 deletions src/core/operations/operationsUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export const INVERT_TRI = 0;
export const ADD_TRI = 1;
export const SKIP_TRI = 2;

const FLOATING_COPLANAR_EPSILON = 1e-14;

let _debugContext = null;
export function setDebugContext( debugData ) {

Expand Down Expand Up @@ -132,17 +134,40 @@ export function collectIntersectingTriangles( a, b ) {

intersectsTriangles( triangleA, triangleB, ia, ib ) {

if ( ! isTriDegenerate( triangleA ) && ! isTriDegenerate( triangleB ) && triangleA.intersectsTriangle( triangleB, _edge, true ) ) {
if ( ! isTriDegenerate( triangleA ) && ! isTriDegenerate( triangleB ) ) {

// due to floating point error it's possible that we can have two overlapping, coplanar triangles
// that are a _tiny_ fraction of a value away from each other. If we find that case then check the
// distance between triangles and if it's small enough consider them intersecting.
let intersected = triangleA.intersectsTriangle( triangleB, _edge, true );
if ( ! intersected ) {

const pa = triangleA.plane;
const pb = triangleB.plane;
const na = pa.normal;
const nb = pb.normal;

if ( na.dot( nb ) === 1 && Math.abs( pa.constant - pb.constant ) < FLOATING_COPLANAR_EPSILON ) {

intersected = true;

}

}

if ( intersected ) {

let va = a.geometry.boundsTree.resolveTriangleIndex( ia );
let vb = b.geometry.boundsTree.resolveTriangleIndex( ib );
aIntersections.add( va, vb );
bIntersections.add( vb, va );

let va = a.geometry.boundsTree.resolveTriangleIndex( ia );
let vb = b.geometry.boundsTree.resolveTriangleIndex( ib );
aIntersections.add( va, vb );
bIntersections.add( vb, va );
if ( _debugContext ) {

if ( _debugContext ) {
_debugContext.addEdge( _edge );
_debugContext.addIntersectingTriangles( ia, triangleA, ib, triangleB );

_debugContext.addEdge( _edge );
_debugContext.addIntersectingTriangles( ia, triangleA, ib, triangleB );
}

}

Expand Down

0 comments on commit d9fcf58

Please sign in to comment.