Skip to content

Commit

Permalink
Merge pull request #545 from gkjohnson/fix-intersect-error
Browse files Browse the repository at this point in the history
Fix triangle x triangle intersect error
  • Loading branch information
gkjohnson authored Jul 21, 2023
2 parents 0da0369 + 0f77e3e commit ffa3d79
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
49 changes: 35 additions & 14 deletions src/math/ExtendedTriangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { Triangle, Vector3, Line3, Sphere, Plane } from 'three';
import { SeparatingAxisBounds } from './SeparatingAxisBounds.js';
import { closestPointsSegmentToSegment, sphereIntersectTriangle } from './MathUtilities.js';

const DIST_EPSILON = 1e-15;
const ZERO_EPSILON = 1e-15;
function isNearZero( value ) {

return Math.abs( value ) < DIST_EPSILON;
return Math.abs( value ) < ZERO_EPSILON;

}

Expand Down Expand Up @@ -144,22 +144,21 @@ ExtendedTriangle.prototype.intersectsTriangle = ( function () {
const edge = new Line3();
const edge1 = new Line3();
const edge2 = new Line3();
const tempPoint = new Vector3();

function triIntersectPlane( tri, plane, targetEdge ) {

// find the edge that intersects the other triangle plane
const points = tri.points;
let found = false;
let count = 0;
let startPointIntersection = - 1;
for ( let i = 0; i < 3; i ++ ) {

const start = points[ i ];
const end = points[ ( i + 1 ) % 3 ];
edge.start.copy( start );
edge.end.copy( end );
const { start, end } = edge;
start.copy( points[ i ] );
end.copy( points[ ( i + 1 ) % 3 ] );
edge.delta( dir );

const targetPoint = found ? targetEdge.start : targetEdge.end;
const startIntersects = isNearZero( plane.distanceToPoint( start ) );
if ( isNearZero( plane.normal.dot( dir ) ) && startIntersects ) {

Expand All @@ -171,23 +170,45 @@ ExtendedTriangle.prototype.intersectsTriangle = ( function () {
}

// check if the start point is near the plane because "intersectLine" is not robust to that case
const doesIntersect = plane.intersectLine( edge, targetPoint );
const doesIntersect = plane.intersectLine( edge, tempPoint );
if ( ! doesIntersect && startIntersects ) {

targetPoint.copy( start );
tempPoint.copy( start );

}

if ( ( doesIntersect || startIntersects ) && ! isNearZero( targetPoint.distanceTo( end ) ) ) {
// ignore the end point
if ( ( doesIntersect || startIntersects ) && ! isNearZero( tempPoint.distanceTo( end ) ) ) {

count ++;
if ( found ) {
if ( count <= 1 ) {

// assign to the start or end point and save which index was snapped to
// the start point if necessary
const point = count === 1 ? targetEdge.start : targetEdge.end;
point.copy( tempPoint );
if ( startIntersects ) {

startPointIntersection = count;

}

} else if ( count >= 2 ) {

// if we're here that means that there must have been one point that had
// snapped to the start point so replace it here
const point = startPointIntersection === 1 ? targetEdge.start : targetEdge.end;
point.copy( tempPoint );
count = 2;
break;

}

found = true;
count ++;
if ( count === 2 && startPointIntersection === - 1 ) {

break;

}

}

Expand Down
6 changes: 3 additions & 3 deletions test/IntersectionUtils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ describe( 'Triangle Intersection line', () => {

} );

it.skip( 'triangles should return a correct intersection (issue #543)', () => {
it( 'triangles should return a correct intersection (issue #543)', () => {

t1.a.set( - 15.430519104003906, 29.432968139648445, - 25 );
t1.b.set( - 15.430519104003906, 29.43296813964843, 25 );
Expand All @@ -386,8 +386,8 @@ describe( 'Triangle Intersection line', () => {
t2.c.set( - 16.118995666503906, 26.962722778320312, - 2.8487582206726074 );
t2.needsUpdate = true;

expected.start.set( - 2.4950, - 7.2915, - 4.1065 );
expected.end.set( - 5.7815, - 7.2915, - 4.0989 );
expected.start.set( - 15.4305, 29.433, - 1.9059 );
expected.end.set( - 13.053, 29.4323, - 2.0522 );

expect( t1.intersectsTriangle( t2, target ) ).toBe( true );
expectLinesToBeClose( target, expected );
Expand Down

0 comments on commit ffa3d79

Please sign in to comment.