gkjohnson / three-bvh-csg

A flexible, memory compact, fast and dynamic CSG implementation on top of three-mesh-bvh
MIT License
623 stars 49 forks source link

Fix "isTriDegenerate" function #192

Closed gkjohnson closed 10 months ago

gkjohnson commented 10 months ago

The calculation of "angle2" can currently be negative

const EPSILON = 1e-14;
const _AB = new Vector3();
const _AC = new Vector3();
const _CB = new Vector3();

const _BA = new Vector3();
const _CA = new Vector3();
const _BC = new Vector3();

export function isTriDegenerate( tri, eps = EPSILON ) {

    // compute angles to determine whether they're degenerate
    _AB.subVectors( tri.b, tri.a );
    _AC.subVectors( tri.c, tri.a );
    _CB.subVectors( tri.c, tri.b );

    _BA.subVectors( tri.a, tri.b );
    _CA.subVectors( tri.a, tri.c );
    _BC.subVectors( tri.b, tri.c );

    const angle1 = _AB.angleTo( _AC );              // AB v AC
    const angle2 = _BA.angleTo( _BC );              // AB v BC
    const angle3 = _CA.angleTo( _CB );      // angle1 - angle2

    return Math.abs( angle1 ) < eps ||
        Math.abs( angle2 ) < eps ||
        Math.abs( angle3 ) < eps ||
        tri.a.distanceToSquared( tri.b ) < eps ||
        tri.a.distanceToSquared( tri.c ) < eps ||
        tri.b.distanceToSquared( tri.c ) < eps;

}