瀏覽代碼

address @mrdoob's and @WestLangley concerning new math additions, add unit tests.

Ben Houston 10 年之前
父節點
當前提交
82c3f50f62
共有 7 個文件被更改,包括 80 次插入81 次删除
  1. 5 3
      src/math/Euler.js
  2. 0 2
      src/math/Matrix3.js
  3. 19 41
      src/math/Matrix4.js
  4. 0 34
      src/math/Quaternion.js
  5. 11 1
      test/unit/math/Euler.js
  6. 1 0
      test/unit/math/Matrix3.js
  7. 44 0
      test/unit/math/Matrix4.js

+ 5 - 3
src/math/Euler.js

@@ -103,8 +103,6 @@ THREE.Euler.prototype = {
 
 	setFromRotationMatrix: function ( m, order, update ) {
 
-		if ( ! ( m instanceof THREE.Matrix4 ) ) throw new Error( 'expecting a Matrix4' );
-
 		var clamp = THREE.Math.clamp;
 
 		// assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
@@ -242,7 +240,7 @@ THREE.Euler.prototype = {
 
 	}(),
 
-	setFromVector: function ( v, order ) {
+	setFromVector3: function ( v, order ) {
 
 		return this.set( v.x, v.y, v.z, order || this._order );
 
@@ -291,10 +289,14 @@ THREE.Euler.prototype = {
 	toVector3: function ( optionalResult ) {
 
 		if( optionalResult ) {
+
 			return optionalResult.set( this._x, this._y, this._z );
+
 		}
 		else {
+
 			return new THREE.Vector3( this._x, this._y, this._z );
+
 		}
 
 	},

+ 0 - 2
src/math/Matrix3.js

@@ -137,8 +137,6 @@ THREE.Matrix3.prototype = {
 
 	getInverse: function ( matrix, throwOnInvertible ) {
 
-		if ( ! ( matrix instanceof THREE.Matrix4 ) ) throw new Error( 'expecting a Matrix4' );
-
 		// input: THREE.Matrix4
 		// ( based on http://code.google.com/p/webgl-mjs/ )
 

+ 19 - 41
src/math/Matrix4.js

@@ -94,9 +94,9 @@ THREE.Matrix4.prototype = {
  
  		var te = this.elements;
  
-		xAxis.set( te[0], te[1], te[2] );
-		yAxis.set( te[4], te[5], te[6] );
-		zAxis.set( te[8], te[9], te[10] );
+		xAxis.set( te[ 0 ], te[ 1 ], te[ 2 ] );
+		yAxis.set( te[ 4 ], te[ 5 ], te[ 6 ] );
+		zAxis.set( te[ 8 ], te[ 9 ], te[ 10 ] );
  
  		return this;
  		
@@ -104,12 +104,12 @@ THREE.Matrix4.prototype = {
  
 	makeBasis: function ( xAxis, yAxis, zAxis ) {
 
-		this.identity();
-
-		var te = this.elements;
-	    te.elements[0] = xAxis.x; te.elements[1] = xAxis.y; te.elements[2] = xAxis.z;
-	    te.elements[4] = yAxis.x; te.elements[5] = yAxis.y; te.elements[6] = yAxis.z;
-	    te.elements[8] = zAxis.x; te.elements[9] = zAxis.y; te.elements[10] = zAxis.z;
+		this.set(
+			xAxis.x, yAxis.x, zAxis.x, 0,
+			xAxis.y, yAxis.y, zAxis.y, 0,
+			xAxis.z, yAxis.z, zAxis.z, 0,
+			0,       0,       0,       1
+		);
 
 	    return this;
 
@@ -146,29 +146,20 @@ THREE.Matrix4.prototype = {
 
 	}(),
 
-	makeShear: function ( vector3Shear, reverseStyle ) {
+	makeShear: function ( s, reverseStyle ) {
 
-		var xy = vector3Shear.x;
-    	var xz = vector3Shear.y;
-    	var yz = vector3Shear.z;
+		// Maya style
+		this.set(
+			1,  s.x, s.y, 0,
+			0,  1,   s.z, 0,
+			0,  0,   1,   0,
+			0,  0,   0,   1
+		);
 
 		if ( reverseStyle ) {
 
-		  this.set(
-		    1,  0,  0,  0,
-		    xy, 1,  0,  0,
-		    xz, yz, 1,  0,
-		    0,   0,  0,  1
-		  );
-
-		} else {
-		  // Maya style
-		  this.set(
-		    1,  xy, xz, 0,
-		    0,  1,  yz, 0,
-		    0,  0,  1,  0,
-		    0,  0,  0,  1
-		  );
+		 	this.transpose();
+
 		}
 
 	    return this;
@@ -441,19 +432,6 @@ THREE.Matrix4.prototype = {
 
 	},
 
-
-	multiplyList: function ( listOfMatrices ) {
-
-		for (var i = 0, il = listOfMatrices.length; i < il ; i++) {
-
-		  this.multiplyMatrices( this, listOfMatrices[ i ] );
-
-		}
-
-		return this;
-
-	},
-
 	multiplyToArray: function ( a, b, r ) {
 
 		var te = this.elements;

+ 0 - 34
src/math/Quaternion.js

@@ -190,7 +190,6 @@ THREE.Quaternion.prototype = {
 		// http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
 
 		// assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
-		if ( ! ( m instanceof THREE.Matrix4 ) ) throw new Error( 'expecting a Matrix4' );
 
 		var te = m.elements,
 
@@ -320,39 +319,6 @@ THREE.Quaternion.prototype = {
 
 	},
 
-	add: function ( q ) {
-
-		this._x += q._x;
-		this._y += q._y;
-		this._z += q._z;
-		this._w += q._w;
-
-		return this;
-
-	},
-
-	sub: function ( q ) {
-
-		this._x -= q._x;
-		this._y -= q._y;
-		this._z -= q._z;
-		this._w -= q._w;
-
-		return this;
-
-	},
-
-	multiplyScalar: function ( s ) {
-
-		this._x *= s;
-		this._y *= s;
-		this._z *= s;
-		this._w *= s;
-
-		return this;
-
-	},
-
 	lengthSq: function () {
 
 		return this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w;

+ 11 - 1
test/unit/math/Euler.js

@@ -55,13 +55,23 @@ test( "clone/copy/equals", function() {
 
 });
 
-test( "set", function() {
+test( "set/setFromVector3/toVector3", function() {
 	var a = new THREE.Euler();
 
 	a.set( 0, 1, 0, "ZYX" );
 	ok( a.equals( eulerAzyx ), "Passed!" );
 	ok( ! a.equals( eulerAxyz ), "Passed!" );
 	ok( ! a.equals( eulerZero ), "Passed!" );
+
+	var vec = new THREE.Vector3( 0, 1, 0 );
+
+	var b = new THREE.Euler().setFromVector3( vec, "ZYX" );
+	console.log( a, b );
+	ok( a.equals( b ), "Passed!" );
+
+	var c = b.toVector3();
+	console.log( c, vec );
+	ok( c.equals( vec ), "Passed!" );	
 });
 
 test( "Quaternion.setFromEuler/Euler.fromQuaternion", function() {

+ 1 - 0
test/unit/math/Matrix3.js

@@ -124,6 +124,7 @@ test( "multiplyScalar", function() {
 	ok( b.elements[8] == 8*2 );
 });
 
+
 test( "determinant", function() {
 	var a = new THREE.Matrix3();
 	ok( a.determinant() == 1, "Passed!" );

+ 44 - 0
test/unit/math/Matrix4.js

@@ -211,6 +211,50 @@ test( "getInverse", function() {
 	}
 });
 
+test( "makeShear", function() {
+	var a = new THREE.Matrix4();
+	var b = new THREE.Matrix4();
+	var shear = new THREE.Vector3( 1, 2, 3 );
+	a.makeShear( shear )
+	// ensure it isn't identity
+	ok( ! matrixEquals4( a, b ), "Passed!" ); 
+	b.makeShear( shear, true );
+	var c = a.clone().transpose();
+	// ensure that one is transpose of the other
+	ok( matrixEquals4( b, c ), "Passed!" );
+	// TODO: Think of better tests for shear
+});
+
+test( "makeBasis/extractBasis", function() {
+	var identityBasis = [ new THREE.Vector3( 1, 0, 0 ), new THREE.Vector3( 0, 1, 0 ), new THREE.Vector3( 0, 0, 1 ) ];
+	var a = new THREE.Matrix4().makeBasis( identityBasis[0], identityBasis[1], identityBasis[2] );
+	var identity = new THREE.Matrix4();
+	ok( matrixEquals4( a, identity ), "Passed!" );
+
+	var testBases = [ [ new THREE.Vector3( 0, 1, 0 ), new THREE.Vector3( -1, 0, 0 ), new THREE.Vector3( 0, 0, 1 ) ] ]
+	for( var i = 0; i < testBases.length; i ++ ) {
+		var testBasis = testBases[i];
+		var b = new THREE.Matrix4().makeBasis( testBasis[0], testBasis[1], testBasis[2] );
+		var outBasis = [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ];
+		b.extractBasis( outBasis[0], outBasis[1], outBasis[2] );
+		// check what goes in, is what comes out.
+		for( var j = 0; j < outBasis.length; j ++ ) {
+			console.log( outBasis[j], testBasis[j] );
+			ok( outBasis[j].equals( testBasis[j] ), "Passed!" );		
+		}
+
+		// get the basis out the hard war
+		for( var j = 0; j < identityBasis.length; j ++ ) {
+			outBasis[j].copy( identityBasis[j] );
+			outBasis[j].applyMatrix4( b );
+		}
+		// did the multiply method of basis extraction work?
+		for( var j = 0; j < outBasis.length; j ++ ) {
+			ok( outBasis[j].equals( testBasis[j] ), "Passed!" );		
+		}
+	}	
+});
+
 test( "transpose", function() {
 	var a = new THREE.Matrix4();
 	var b = a.clone().transpose();