Bläddra i källkod

Merge pull request #5788 from bhouston/math-improvements

Various Math improvements from Clara.io
Mr.doob 10 år sedan
förälder
incheckning
2036197926

+ 14 - 0
docs/api/math/Euler.html

@@ -91,6 +91,20 @@
     WARNING: this discards revolution information.
     </div>
 
+    <h3>[method:Euler setFromVector3]([page:Vector3 vector], [page:String order]) [page:Euler this]</h3>
+    <div>
+    vector -- [page:Vector3].
+    order -- [page:string] Order of axes, defaults to 'XYZ' (must be upper case)
+    </div>
+    <div>
+    Optionally Vector3 to the XYZ parameters of Euler, and order to the Euler's order property.
+    </div>
+
+    <h3>[method:Vector3 toVector3]()</h3>
+    <div>
+    Returns the Euler's XYZ properties as a Vector3.
+    </div>
+
     <h3>[method:Euler fromArray]([page:Array array]) [page:Euler this]</h3>
     <div>
     array -- [page:Array] of length 3 or 4. array[3] is an optional order argument.

+ 10 - 0
docs/api/math/Matrix4.html

@@ -71,6 +71,16 @@
 		Copies the translation component of the supplied matrix *m* into this matrix translation component.
 		</div>
 
+		<h3>[method:Matrix4 makeBasis]( [page:Vector3 xAxis], [page:Vector3 zAxis], [page:Vector3 zAxis] ) [page:Matrix4 this]</h3>
+		<div>
+		Creates the basis matrix consisting of the three provided axis vectors.  Returns the current matrix.
+		</div>
+
+		<h3>[method:Matrix4 extractBasis]( [page:Vector3 xAxis], [page:Vector3 zAxis], [page:Vector3 zAxis] ) [page:Matrix4 this]</h3>
+		<div>
+		Extracts basis of into the three axis vectors provided.  Returns the current matrix.
+		</div>
+
 		<h3>[method:Matrix4 extractRotation]( [page:Matrix4 m] ) [page:Matrix4 this]</h3>
 		<div>
 		Extracts the rotation of the supplied matrix *m* into this matrix rotation component.

+ 21 - 0
src/math/Euler.js

@@ -240,6 +240,12 @@ THREE.Euler.prototype = {
 
 	}(),
 
+	setFromVector3: function ( v, order ) {
+
+		return this.set( v.x, v.y, v.z, order || this._order );
+
+	},
+
 	reorder: function () {
 
 		// WARNING: this discards revolution information -bhouston
@@ -280,6 +286,21 @@ 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 );
+
+		}
+
+	},
+
 	onChange: function ( callback ) {
 
 		this.onChangeCallback = callback;

+ 25 - 0
src/math/Matrix4.js

@@ -90,6 +90,31 @@ THREE.Matrix4.prototype = {
 
 	},
 
+	extractBasis: function ( xAxis, yAxis, zAxis ) {
+ 
+ 		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 ] );
+ 
+ 		return this;
+ 		
+ 	},
+ 
+	makeBasis: function ( xAxis, yAxis, zAxis ) {
+
+		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;
+
+	},
+
 	extractRotation: function () {
 
 		var v1 = new THREE.Vector3();

+ 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!" );

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

@@ -211,6 +211,36 @@ test( "getInverse", function() {
 	}
 });
 
+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();