Skip to content

Commit dc547c7

Browse files
authored
Plane: Add clampToLine to intersectLine(). (#33398)
1 parent 6814f96 commit dc547c7

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

src/math/Plane.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,10 @@ class Plane {
210210
*
211211
* @param {Line3} line - The line to compute the intersection for.
212212
* @param {Vector3} target - The target vector that is used to store the method's result.
213-
* @return {?Vector3} The intersection point.
213+
* @param {boolean} [clampToLine=true] - Whether to clamp the intersection to the line segment.
214+
* @return {?Vector3} The intersection point. Returns `null` if no intersection is detected.
214215
*/
215-
intersectLine( line, target ) {
216+
intersectLine( line, target, clampToLine = true ) {
216217

217218
const direction = line.delta( _vector1 );
218219

@@ -234,7 +235,7 @@ class Plane {
234235

235236
const t = - ( line.start.dot( this.normal ) + this.constant ) / denominator;
236237

237-
if ( t < 0 || t > 1 ) {
238+
if ( ( clampToLine === true ) && ( t < 0 || t > 1 ) ) {
238239

239240
return null;
240241

test/unit/src/math/Plane.tests.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,17 @@ export default QUnit.module( 'Maths', () => {
229229
a.intersectLine( l1, point );
230230
assert.ok( point.equals( new Vector3( 3, 0, 0 ) ), 'Passed!' );
231231

232+
// plane lies outside the segment's endpoints
233+
a = new Plane( new Vector3( 1, 0, 0 ), - 20 );
234+
const l2 = new Line3( new Vector3( - 10, 0, 0 ), new Vector3( 10, 0, 0 ) );
235+
236+
assert.strictEqual( a.intersectLine( l2, point ), null, 'Default clamps to segment and returns null' );
237+
assert.strictEqual( a.intersectLine( l2, point, true ), null, 'Explicit clampToLine=true returns null' );
238+
239+
const result = a.intersectLine( l2, point, false );
240+
assert.ok( result === point, 'clampToLine=false returns the target vector' );
241+
assert.ok( point.equals( new Vector3( 20, 0, 0 ) ), 'clampToLine=false returns infinite-line intersection' );
242+
232243
} );
233244

234245
QUnit.test( 'intersectsBox', ( assert ) => {

0 commit comments

Comments
 (0)