Browse Source

add option to clamp to line or to just get closest point on infinite line.

Ben Houston 12 years ago
parent
commit
eeb7919e61
2 changed files with 16 additions and 6 deletions
  1. 8 3
      src/math/Line3.js
  2. 8 3
      test/unit/math/Line3.js

+ 8 - 3
src/math/Line3.js

@@ -68,9 +68,9 @@ THREE.extend( THREE.Line3.prototype, {
 		var startP = new THREE.Vector3();
 		var startEnd = new THREE.Vector3();
 
-		return function ( point, optionalTarget ) {
+		return function ( point, clampToLine, optionalTarget ) {
 
-			var result = optionalTarget || new THREE.Vector3();
+			var result = optionalTarget || new THREE.Vector3();			
 
 			startP.subVectors( point, this.start );
 			startEnd.subVectors( this.end, this.start );
@@ -79,7 +79,12 @@ THREE.extend( THREE.Line3.prototype, {
 			var startEnd_startP = startEnd.dot( startP );
 
 			var t = startEnd_startP / startEnd2;
-	        t = THREE.Math.clamp( t, 0, 1 );
+
+			if( clampToLine ) {
+				
+	        	t = THREE.Math.clamp( t, 0, 1 );
+
+	        }
 
 	        return result.copy( startEnd ).multiplyScalar( t ).add( this.start );
 

+ 8 - 3
test/unit/math/Line3.js

@@ -49,14 +49,19 @@ test( "closestPointToPoint", function() {
 	var a = new THREE.Line3( one3.clone(), new THREE.Vector3( 1, 1, 2 ) );
 
 	// nearby the ray
-	var b1 = a.closestPointToPoint( zero3.clone() );
+	var b1 = a.closestPointToPoint( zero3.clone(), true );
 	ok( b1.distanceTo( new THREE.Vector3( 1, 1, 1 ) ) < 0.0001, "Passed!" );
 
 	// nearby the ray
-	var b = a.closestPointToPoint( new THREE.Vector3( 1, 1, 5 ) );
+	var b2 = a.closestPointToPoint( zero3.clone(), false );
+	console.log( b2 );
+	ok( b2.distanceTo( new THREE.Vector3( 1, 1, 0 ) ) < 0.0001, "Passed!" );
+
+	// nearby the ray
+	var b = a.closestPointToPoint( new THREE.Vector3( 1, 1, 5 ), true );
 	ok( b.distanceTo( new THREE.Vector3( 1, 1, 2 ) ) < 0.0001, "Passed!" );
 
 	// exactly on the ray
-	var c = a.closestPointToPoint( one3.clone() );
+	var c = a.closestPointToPoint( one3.clone(), true );
 	ok( c.distanceTo( one3.clone() ) < 0.0001, "Passed!" );
 });