Browse Source

add ray.transformSelf( matrix4 ) with unit test for verification.

Ben Houston 12 years ago
parent
commit
3bffda430e
2 changed files with 37 additions and 0 deletions
  1. 9 0
      src/math/Ray.js
  2. 28 0
      test/core/Ray.js

+ 9 - 0
src/math/Ray.js

@@ -115,6 +115,15 @@ THREE.Ray.prototype = {
 
 	},
 
+	transformSelf: function ( matrix4 ) {
+
+		this.direction = matrix4.multiplyVector3( this.direction.addSelf( this.origin ) );
+		this.origin = matrix4.multiplyVector3( this.origin );
+		this.direction.subSelf( this.origin );
+
+		return this;
+	},
+
 	equals: function ( ray ) {
 
 		return ray.origin.equals( this.origin ) && ray.direction.equals( this.direction );

+ 28 - 0
test/core/Ray.js

@@ -147,3 +147,31 @@ test( "intersectPlane", function() {
 	var f = new THREE.Plane().setFromNormalAndCoplanarPoint( new THREE.Vector3( 1, 0, 0 ), zero3 );
 	ok( a.intersectPlane( f ) === undefined, "Passed!" );
 });
+
+
+test( "transformSelf", function() {
+	var a = new THREE.Ray( one3, new THREE.Vector3( 0, 0, 1 ) );
+	var m = new THREE.Matrix4().identity();
+
+	ok( a.clone().transformSelf( m ).equals( a ), "Passed!" );
+
+	a = new THREE.Ray( zero3, new THREE.Vector3( 0, 0, 1 ) );
+	m.rotateByAxis( new THREE.Vector3( 0, 0, 1 ), Math.PI );
+	ok( a.clone().transformSelf( m ).equals( a ), "Passed!" );
+
+	m.identity().rotateX( Math.PI );
+	var b = a.clone();
+	b.direction.negate();
+	var a2 = a.clone().transformSelf( m );
+	ok( a2.origin.distanceTo( b.origin ) < 0.0001, "Passed!" );
+	ok( a2.direction.distanceTo( b.direction ) < 0.0001, "Passed!" );
+
+	a.origin = new THREE.Vector3( 0, 0, 1 );
+	b.origin = new THREE.Vector3( 0, 0, -1 );
+	var a2 = a.clone().transformSelf( m );
+	ok( a2.origin.distanceTo( b.origin ) < 0.0001, "Passed!" );
+	ok( a2.direction.distanceTo( b.direction ) < 0.0001, "Passed!" );
+});
+
+
+