gkjohnson / three-bvh-csg

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

Possible raycast offset solution #178

Closed gkjohnson closed 1 year ago

gkjohnson commented 1 year ago

Related to #177

Offsets the ray to avoid floating point error on coplanar faces - but since these are being treated as whole triangles there should be no need to test for coplanarity?

TODO

gkjohnson commented 1 year ago

Triangle planes were not being calculated correctly and coplanar triangles were not being marked as coplanar.

Still some triangles being missed by coplanar count, tho:

image
gkjohnson commented 1 year ago

Commenting out these lines fixes the issue:

// skip the triangle if we don't intersect with it
if ( ! _splittingTriangle.intersectsTriangle( tri, _edge, true ) ) {

    performCoplanarIncrement( tri );
    continue;

}
gkjohnson commented 1 year ago

Using these functions in place of intersectsTriangle seems to fix the issue, but also increases the amount of triangulation. Why?

const getSide = v => Math.sign( plane.distanceToPoint( v ) );
const isOneSide = t => {

    let s = getSide( t.a ) || getSide( t.b ) || getSide( t.c );
    if ( s !== getSide( t.b ) ) return false;
    if ( s !== getSide( t.c ) ) return false;

    return true;

};

edit - we need to check if the "intersectsTriangle" function properly handles fully encapsulated triangles? Because the "coplanar" function does nothing if the triangles don't intersect at all. Could also try manually processing coplanarity on all triangles afterward.

gkjohnson commented 1 year ago

The intersects triangle function does handle fully encapsulated triangles.

It looks like using the plane is not a great way to determine coplanarity.

TODO