|
@@ -0,0 +1,289 @@
|
|
|
+/**
|
|
|
+ * @author mrdoob / http://mrdoob.com/
|
|
|
+ * @author WestLangley / http://github.com/WestLangley
|
|
|
+ * @author bhouston / http://exocortex.com
|
|
|
+ */
|
|
|
+
|
|
|
+THREE.Euler = function ( x, y, z, order ) {
|
|
|
+
|
|
|
+ this.x = x || 0;
|
|
|
+ this.y = y || 0;
|
|
|
+ this.z = z || 0;
|
|
|
+ this.order = order || THREE.Euler.DefaultOrder;
|
|
|
+
|
|
|
+};
|
|
|
+
|
|
|
+THREE.Euler.RotationOrders = [ 'XYZ', 'YZX', 'ZXY', 'XZY', 'YXZ', 'ZYX' ];
|
|
|
+
|
|
|
+THREE.Euler.DefaultOrder = 'XYZ';
|
|
|
+
|
|
|
+THREE.Euler.prototype = {
|
|
|
+
|
|
|
+ constructor: THREE.Euler,
|
|
|
+
|
|
|
+ set: function ( x, y, z, order ) {
|
|
|
+
|
|
|
+ this.x = x;
|
|
|
+ this.y = y;
|
|
|
+ this.z = z;
|
|
|
+ this.order = order || this.order;
|
|
|
+
|
|
|
+ return this;
|
|
|
+
|
|
|
+ },
|
|
|
+
|
|
|
+ copy: function ( e ) {
|
|
|
+
|
|
|
+ this.x = e.x;
|
|
|
+ this.y = e.y;
|
|
|
+ this.z = e.z;
|
|
|
+ this.order = e.order;
|
|
|
+
|
|
|
+ return this;
|
|
|
+
|
|
|
+ },
|
|
|
+
|
|
|
+ setFromRotationMatrix: function ( m, order ) {
|
|
|
+
|
|
|
+ // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
|
|
|
+
|
|
|
+ // clamp, to handle numerical problems
|
|
|
+
|
|
|
+ function clamp( x ) {
|
|
|
+
|
|
|
+ return Math.min( Math.max( x, -1 ), 1 );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ var te = m.elements;
|
|
|
+ var m11 = te[0], m12 = te[4], m13 = te[8];
|
|
|
+ var m21 = te[1], m22 = te[5], m23 = te[9];
|
|
|
+ var m31 = te[2], m32 = te[6], m33 = te[10];
|
|
|
+
|
|
|
+ order = order || this.order;
|
|
|
+
|
|
|
+ if ( order === 'XYZ' ) {
|
|
|
+
|
|
|
+ this.y = Math.asin( clamp( m13 ) );
|
|
|
+
|
|
|
+ if ( Math.abs( m13 ) < 0.99999 ) {
|
|
|
+
|
|
|
+ this.x = Math.atan2( - m23, m33 );
|
|
|
+ this.z = Math.atan2( - m12, m11 );
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ this.x = Math.atan2( m32, m22 );
|
|
|
+ this.z = 0;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ } else if ( order === 'YXZ' ) {
|
|
|
+
|
|
|
+ this.x = Math.asin( - clamp( m23 ) );
|
|
|
+
|
|
|
+ if ( Math.abs( m23 ) < 0.99999 ) {
|
|
|
+
|
|
|
+ this.y = Math.atan2( m13, m33 );
|
|
|
+ this.z = Math.atan2( m21, m22 );
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ this.y = Math.atan2( - m31, m11 );
|
|
|
+ this.z = 0;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ } else if ( order === 'ZXY' ) {
|
|
|
+
|
|
|
+ this.x = Math.asin( clamp( m32 ) );
|
|
|
+
|
|
|
+ if ( Math.abs( m32 ) < 0.99999 ) {
|
|
|
+
|
|
|
+ this.y = Math.atan2( - m31, m33 );
|
|
|
+ this.z = Math.atan2( - m12, m22 );
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ this.y = 0;
|
|
|
+ this.z = Math.atan2( m21, m11 );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ } else if ( order === 'ZYX' ) {
|
|
|
+
|
|
|
+ this.y = Math.asin( - clamp( m31 ) );
|
|
|
+
|
|
|
+ if ( Math.abs( m31 ) < 0.99999 ) {
|
|
|
+
|
|
|
+ this.x = Math.atan2( m32, m33 );
|
|
|
+ this.z = Math.atan2( m21, m11 );
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ this.x = 0;
|
|
|
+ this.z = Math.atan2( - m12, m22 );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ } else if ( order === 'YZX' ) {
|
|
|
+
|
|
|
+ this.z = Math.asin( clamp( m21 ) );
|
|
|
+
|
|
|
+ if ( Math.abs( m21 ) < 0.99999 ) {
|
|
|
+
|
|
|
+ this.x = Math.atan2( - m23, m22 );
|
|
|
+ this.y = Math.atan2( - m31, m11 );
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ this.x = 0;
|
|
|
+ this.y = Math.atan2( m13, m33 );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ } else if ( order === 'XZY' ) {
|
|
|
+
|
|
|
+ this.z = Math.asin( - clamp( m12 ) );
|
|
|
+
|
|
|
+ if ( Math.abs( m12 ) < 0.99999 ) {
|
|
|
+
|
|
|
+ this.x = Math.atan2( m32, m22 );
|
|
|
+ this.y = Math.atan2( m13, m11 );
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ this.x = Math.atan2( - m23, m33 );
|
|
|
+ this.y = 0;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ else {
|
|
|
+
|
|
|
+ console.warn( 'WARNING: Euler.setFromRotationMatrix() given unsupported order: ' + order )
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ this.order = order;
|
|
|
+
|
|
|
+ return this;
|
|
|
+
|
|
|
+ },
|
|
|
+
|
|
|
+ setFromQuaternion: function ( q, order ) {
|
|
|
+
|
|
|
+ // q is assumed to be normalized
|
|
|
+
|
|
|
+ // clamp, to handle numerical problems
|
|
|
+
|
|
|
+ function clamp( x ) {
|
|
|
+
|
|
|
+ return Math.min( Math.max( x, -1 ), 1 );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // http://www.mathworks.com/matlabcentral/fileexchange/20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/content/SpinCalc.m
|
|
|
+
|
|
|
+ var sqx = q.x * q.x;
|
|
|
+ var sqy = q.y * q.y;
|
|
|
+ var sqz = q.z * q.z;
|
|
|
+ var sqw = q.w * q.w;
|
|
|
+
|
|
|
+ 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 ) ) );
|
|
|
+ this.z = Math.atan2( 2 * ( q.z * q.w - q.x * q.y ), ( sqw + sqx - sqy - sqz ) );
|
|
|
+
|
|
|
+ } else if ( order === 'YXZ' ) {
|
|
|
+
|
|
|
+ this.x = Math.asin( clamp( 2 * ( q.x * q.w - q.y * q.z ) ) );
|
|
|
+ this.y = Math.atan2( 2 * ( q.x * q.z + q.y * q.w ), ( sqw - sqx - sqy + sqz ) );
|
|
|
+ this.z = Math.atan2( 2 * ( q.x * q.y + q.z * q.w ), ( sqw - sqx + sqy - sqz ) );
|
|
|
+
|
|
|
+ } else if ( order === 'ZXY' ) {
|
|
|
+
|
|
|
+ this.x = Math.asin( clamp( 2 * ( q.x * q.w + q.y * q.z ) ) );
|
|
|
+ this.y = Math.atan2( 2 * ( q.y * q.w - q.z * q.x ), ( sqw - sqx - sqy + sqz ) );
|
|
|
+ this.z = Math.atan2( 2 * ( q.z * q.w - q.x * q.y ), ( sqw - sqx + sqy - sqz ) );
|
|
|
+
|
|
|
+ } else if ( order === 'ZYX' ) {
|
|
|
+
|
|
|
+ this.x = Math.atan2( 2 * ( q.x * q.w + q.z * q.y ), ( sqw - sqx - sqy + sqz ) );
|
|
|
+ this.y = Math.asin( clamp( 2 * ( q.y * q.w - q.x * q.z ) ) );
|
|
|
+ this.z = Math.atan2( 2 * ( q.x * q.y + q.z * q.w ), ( sqw + sqx - sqy - sqz ) );
|
|
|
+
|
|
|
+ } else if ( order === 'YZX' ) {
|
|
|
+
|
|
|
+ this.x = Math.atan2( 2 * ( q.x * q.w - q.z * q.y ), ( sqw - sqx + sqy - sqz ) );
|
|
|
+ this.y = Math.atan2( 2 * ( q.y * q.w - q.x * q.z ), ( sqw + sqx - sqy - sqz ) );
|
|
|
+ this.z = Math.asin( clamp( 2 * ( q.x * q.y + q.z * q.w ) ) );
|
|
|
+
|
|
|
+ } else if ( order === 'XZY' ) {
|
|
|
+
|
|
|
+ this.x = Math.atan2( 2 * ( q.x * q.w + q.y * q.z ), ( sqw - sqx + sqy - sqz ) );
|
|
|
+ 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;
|
|
|
+
|
|
|
+ return this;
|
|
|
+
|
|
|
+ },
|
|
|
+
|
|
|
+ reorder: function() {
|
|
|
+
|
|
|
+ // WARNING: this discards revolution information -bhouston
|
|
|
+
|
|
|
+ var q = new THREE.Quaternion();
|
|
|
+
|
|
|
+ return function( newOrder ) {
|
|
|
+
|
|
|
+ q.setFromEuler( this );
|
|
|
+ this.setFromQuaternion( q, newOrder );
|
|
|
+
|
|
|
+ };
|
|
|
+
|
|
|
+
|
|
|
+ }(),
|
|
|
+
|
|
|
+ fromArray: function ( array ) {
|
|
|
+
|
|
|
+ this.x = array[ 0 ];
|
|
|
+ this.y = array[ 1 ];
|
|
|
+ this.z = array[ 2 ];
|
|
|
+ this.order = array[ 3 ];
|
|
|
+
|
|
|
+ return this;
|
|
|
+
|
|
|
+ },
|
|
|
+
|
|
|
+ toArray: function () {
|
|
|
+
|
|
|
+ return [ this.x, this.y, this.z, this.order ];
|
|
|
+
|
|
|
+ },
|
|
|
+
|
|
|
+ equals: function ( rotation ) {
|
|
|
+
|
|
|
+ return ( ( rotation.x === this.x ) && ( rotation.y === this.y ) && ( rotation.z === this.z ) && ( rotation.order === this.order ) );
|
|
|
+
|
|
|
+ },
|
|
|
+
|
|
|
+ clone: function () {
|
|
|
+
|
|
|
+ return new THREE.Euler( this.x, this.y, this.z, this.order );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+};
|