浏览代码

add ability to get closestPointToPointParameter on a line.

Ben Houston 12 年之前
父节点
当前提交
dca460bf4e
共有 2 个文件被更改,包括 19 次插入8 次删除
  1. 14 6
      src/math/Line3.js
  2. 5 2
      test/unit/math/Line3.js

+ 14 - 6
src/math/Line3.js

@@ -63,15 +63,13 @@ THREE.extend( THREE.Line3.prototype, {
 
 	},
 
-	closestPointToPoint: function() {
+	closestPointToPointParameter: function() {
 
 		var startP = new THREE.Vector3();
 		var startEnd = new THREE.Vector3();
 
-		return function ( point, clampToLine, optionalTarget ) {
-
-			var result = optionalTarget || new THREE.Vector3();			
-
+		return function ( point, clampToLine ) {
+		
 			startP.subVectors( point, this.start );
 			startEnd.subVectors( this.end, this.start );
 
@@ -86,12 +84,22 @@ THREE.extend( THREE.Line3.prototype, {
 
 	        }
 
-	        return result.copy( startEnd ).multiplyScalar( t ).add( this.start );
+	        return t;
 
 		};
 
 	}(),
 
+	closestPointToPoint: function ( point, clampToLine, optionalTarget ) {
+
+		var t = this.closestPointToPointParameter( point, clampToLine );
+
+		var result = optionalTarget || new THREE.Vector3();			
+
+        return this.delta( result ).multiplyScalar( t ).add( this.start );
+
+	},
+
 	transform: function ( matrix ) {
 
 		this.start.applyMatrix4( matrix );

+ 5 - 2
test/unit/math/Line3.js

@@ -44,24 +44,27 @@ test( "at", function() {
 	ok( a.at( 2 ).distanceTo( new THREE.Vector3( 1, 1, 3 ) ) < 0.0001, "Passed!" );
 });
 
-
-test( "closestPointToPoint", function() {
+test( "closestPointToPoint/closestPointToPointParameter", function() {
 	var a = new THREE.Line3( one3.clone(), new THREE.Vector3( 1, 1, 2 ) );
 
 	// nearby the ray
+	ok( a.closestPointToPointParameter( zero3.clone(), true ) == 0, "Passed!" );
 	var b1 = a.closestPointToPoint( zero3.clone(), true );
 	ok( b1.distanceTo( new THREE.Vector3( 1, 1, 1 ) ) < 0.0001, "Passed!" );
 
 	// nearby the ray
+	ok( a.closestPointToPointParameter( zero3.clone(), false ) == -1, "Passed!" );
 	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
+	ok( a.closestPointToPointParameter( new THREE.Vector3( 1, 1, 5 ), true ) == 1, "Passed!" );
 	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
+	ok( a.closestPointToPointParameter( one3.clone(), true ) == 0, "Passed!" );
 	var c = a.closestPointToPoint( one3.clone(), true );
 	ok( c.distanceTo( one3.clone() ) < 0.0001, "Passed!" );
 });