From cbd01e834c5194098d466d55b728f8db9633bba7 Mon Sep 17 00:00:00 2001 From: Garrett Johnson Date: Sat, 16 Sep 2023 00:27:38 +0200 Subject: [PATCH 01/18] Update triangle tim --- src/utils/trimBeneathTriPlane.js | 77 ++++++++++++++++---------------- 1 file changed, 38 insertions(+), 39 deletions(-) diff --git a/src/utils/trimBeneathTriPlane.js b/src/utils/trimBeneathTriPlane.js index 25ef775..26cd132 100644 --- a/src/utils/trimBeneathTriPlane.js +++ b/src/utils/trimBeneathTriPlane.js @@ -1,76 +1,75 @@ -import { Vector3 } from 'three'; -import { getPlaneYAtPoint } from './planeUtils.js'; +import { Plane, Vector3 } from 'three'; import { isYProjectedTriangleDegenerate } from './triangleLineUtils.js'; const EPSILON = 1e-16; -const _lineDirection = /* @__PURE__ */ new Vector3(); +const UP_VECTOR = /* @__PURE__ */ new Vector3( 0, 1, 0 ); +const _plane = /* @__PURE__ */ new Plane(); const _planeHit = /* @__PURE__ */ new Vector3(); -const _planePoint = /* @__PURE__ */ new Vector3(); +const _lineDirection = /* @__PURE__ */ new Vector3(); export function trimToBeneathTriPlane( tri, line, lineTarget ) { - const { plane } = tri; - lineTarget.copy( line ); + // if the triangle is insignificant then skip it + if ( isYProjectedTriangleDegenerate( tri ) ) { + + return false; + + } + // update triangle if needed if ( tri.needsUpdate ) { tri.update(); } - // if the triangle is insignificant then skip it - if ( isYProjectedTriangleDegenerate( tri ) ) { + // if the plane is not facing up then flip the direction + _plane.copy( tri.plane ); + if ( _plane.normal.dot( UP_VECTOR ) < 0 ) { - return false; + _plane.normal.multiplyScalar( - 1 ); + _plane.constant *= - 1; } // if the line and plane are coplanar then return that we can't trim - line.delta( _lineDirection ); - - const areCoplanar = Math.abs( plane.normal.dot( _lineDirection ) ) < EPSILON; - if ( areCoplanar ) { + line.delta( _lineDirection ).normalize(); + if ( Math.abs( _plane.normal.dot( _lineDirection ) ) < EPSILON ) { return false; } - // if the line does intersect the plane then trim - const doesLineIntersect = plane.intersectLine( line, _planeHit ); - if ( doesLineIntersect ) { - - const { start, end } = lineTarget; - - // test the line side with the largest segment extending beyond the plane - let testPoint, otherPoint; - if ( start.distanceToSquared( _planeHit ) > end.distanceToSquared( _planeHit ) ) { - - testPoint = start; - otherPoint = end; + // find the point that's below the plane. If both points are below the plane + // then we assume we're dealing with floating point error + const isStartBelow = _plane.distanceToPoint( line.start ) < 0; + const isEndBelow = _plane.distanceToPoint( line.end ) < 0; + if ( isStartBelow && isEndBelow ) { - } else { + // if the whole line is below then just copy that + lineTarget.copy( line ); + return true; - testPoint = end; - otherPoint = start; + } else if ( ! isStartBelow && ! isEndBelow ) { - } + // if it's wholly above then skip it + return false; - // get the center point of the line segment and the plane hit - getPlaneYAtPoint( tri.plane, testPoint, _planePoint ); + } else if ( _plane.intersectLine( line, _planeHit ) ) { - // adjust the appropriate line point align with the plane hit point - const isAbove = testPoint.y > _planePoint.y; - if ( isAbove ) { + if ( isStartBelow ) { - testPoint.copy( _planeHit ); + lineTarget.start.copy( line.start ); + lineTarget.end.copy( _planeHit ); + return true; - } else { + } else if ( isEndBelow ) { - otherPoint.copy( _planeHit ); + lineTarget.end.copy( line.end ); + lineTarget.start.copy( _planeHit ); + return true; } - return true; - } return false; From eb337a535442016fc4d8c8be05721cd702ba2d71 Mon Sep 17 00:00:00 2001 From: Garrett Johnson Date: Sat, 16 Sep 2023 00:42:37 +0200 Subject: [PATCH 02/18] example title update --- example/edgeProjection.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/edgeProjection.html b/example/edgeProjection.html index 57e93e6..64ed99e 100644 --- a/example/edgeProjection.html +++ b/example/edgeProjection.html @@ -1,7 +1,7 @@ - three-mesh-bvh - Projected Edge Generation + three-edge-projection - Projected Edge Generation