Browse Source

improve handling of unspecified orders in Euler.setFrom*() functions. Fill out Euler.clamp().

Ben Houston 12 years ago
parent
commit
9b86340bdc
2 changed files with 41 additions and 6 deletions
  1. 22 5
      src/math/Euler.js
  2. 19 1
      src/math/Math.js

+ 22 - 5
src/math/Euler.js

@@ -26,7 +26,7 @@ THREE.Euler.prototype = {
 		this.x = x;
 		this.y = y;
 		this.z = z;
-		this.order = order;
+		this.order = order || this.order;
 
 		return this;
 
@@ -60,7 +60,9 @@ THREE.Euler.prototype = {
 		var m21 = te[1], m22 = te[5], m23 = te[9];
 		var m31 = te[2], m32 = te[6], m33 = te[10];
 
-		if ( order === undefined || order === 'XYZ' ) {
+		order = order || this.order;
+
+		if ( order === 'XYZ' ) {
 
 			this.y = Math.asin( clamp( m13 ) );
 
@@ -156,6 +158,11 @@ THREE.Euler.prototype = {
 
 			}
 
+		}
+		else {
+
+			console.warn( 'WARNING: Euler.setFromRotationMatrix() given unsupported order: ' + order )
+		
 		}
 
 		this.order = order;
@@ -183,7 +190,9 @@ THREE.Euler.prototype = {
 		var sqz = q.z * q.z;
 		var sqw = q.w * q.w;
 
-		if ( order === undefined || order === 'XYZ' ) {
+		order = order || this.order;
+
+		if ( order === 'XYZ' ) {
 
 			this.x = Math.atan2( 2 * ( q.x * q.w - q.y * q.z ), ( sqw - sqx - sqy + sqz ) );
 			this.y = Math.asin(  clamp( 2 * ( q.x * q.z + q.y * q.w ) ) );
@@ -219,6 +228,11 @@ THREE.Euler.prototype = {
 			this.y = Math.atan2( 2 * ( q.x * q.z + q.y * q.w ), ( sqw + sqx - sqy - sqz ) );
 			this.z = Math.asin(  clamp( 2 * ( q.z * q.w - q.x * q.y ) ) );
 
+		}
+		else {
+
+			console.warn( 'WARNING: Euler.setFromQuaternion() given unsupported order: ' + order )
+		
 		}
 
 		this.order = order;
@@ -229,8 +243,11 @@ THREE.Euler.prototype = {
 
 	clamp: function() {
 
-		// todo
-		console.error( "ERROR: Euler.clamp() is not yet implemented.");
+		// clamps to the range [ -Math.PI, Math.PI )
+
+		this.x = THREE.Math.fpModulus( this.x + Math.PI, THREE.Math.PI2, true ) - Math.PI;
+		this.y = THREE.Math.fpModulus( this.y + Math.PI, THREE.Math.PI2, true ) - Math.PI;
+		this.z = THREE.Math.fpModulus( this.z + Math.PI, THREE.Math.PI2, true ) - Math.PI;
 		
 	},
 

+ 19 - 1
src/math/Math.js

@@ -4,6 +4,8 @@
 
 THREE.Math = {
 
+	PI2: Math.PI * 2,
+
 	// Clamp value to range <a, b>
 
 	clamp: function ( x, a, b ) {
@@ -113,6 +115,22 @@ THREE.Math = {
 
 		};
 
-	}()
+	}(),
+
+	fpModulus: function( value, divisor, upperBound ) {
+
+		if( value < 0 ) {
+
+			value += Math.ceil( (- value) / divisor ) * divisor;
+
+		}
+		else if( value >= divisor ) {
+
+			value -= Math.floor( value / divisor ) * divisor;
+
+		}
+
+		return value;
+	}
 
 };