|
@@ -4,6 +4,7 @@
|
|
* @author philogb / http://blog.thejit.org/
|
|
* @author philogb / http://blog.thejit.org/
|
|
* @author mikael emtinger / http://gomo.se/
|
|
* @author mikael emtinger / http://gomo.se/
|
|
* @author egraether / http://egraether.com/
|
|
* @author egraether / http://egraether.com/
|
|
|
|
+ * @author WestLangley / http://github.com/WestLangley
|
|
*/
|
|
*/
|
|
|
|
|
|
THREE.Vector3 = function ( x, y, z ) {
|
|
THREE.Vector3 = function ( x, y, z ) {
|
|
@@ -270,89 +271,207 @@ THREE.Vector3.prototype = {
|
|
|
|
|
|
},
|
|
},
|
|
|
|
|
|
- getRotationFromMatrix: function ( m, scale ) {
|
|
|
|
-
|
|
|
|
- var sx = scale ? scale.x : 1;
|
|
|
|
- var sy = scale ? scale.y : 1;
|
|
|
|
- var sz = scale ? scale.z : 1;
|
|
|
|
-
|
|
|
|
- var m11 = m.elements[0] / sx, m12 = m.elements[4] / sy, m13 = m.elements[8] / sz;
|
|
|
|
- var m21 = m.elements[1] / sx, m22 = m.elements[5] / sy, m23 = m.elements[9] / sz;
|
|
|
|
- var m33 = m.elements[10] / sz;
|
|
|
|
-
|
|
|
|
- this.y = Math.asin( m13 );
|
|
|
|
-
|
|
|
|
- var cosY = Math.cos( this.y );
|
|
|
|
-
|
|
|
|
- if ( Math.abs( cosY ) > 0.00001 ) {
|
|
|
|
-
|
|
|
|
- this.x = Math.atan2( - m23 / cosY, m33 / cosY );
|
|
|
|
- this.z = Math.atan2( - m12 / cosY, m11 / cosY );
|
|
|
|
-
|
|
|
|
- } else {
|
|
|
|
-
|
|
|
|
- this.x = 0;
|
|
|
|
- this.z = Math.atan2( m21, m22 );
|
|
|
|
-
|
|
|
|
|
|
+ setEulerFromRotationMatrix: 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 _order = order || 'XYZ',
|
|
|
|
+
|
|
|
|
+ te = m.elements,
|
|
|
|
+
|
|
|
|
+ m11 = te[0], m12 = te[4], m13 = te[8],
|
|
|
|
+ m21 = te[1], m22 = te[5], m23 = te[9],
|
|
|
|
+ m31 = te[2], m32 = te[6], m33 = te[10];
|
|
|
|
+
|
|
|
|
+ switch ( _order ) {
|
|
|
|
+
|
|
|
|
+ case '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;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case '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( m13, m11 );
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case '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 );
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case '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( m31, m33 );
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case '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( - m13, m33 );
|
|
|
|
+ this.y = 0;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ default: // '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( m21, m22 );
|
|
|
|
+ this.z = 0;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ break;
|
|
|
|
+
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
return this;
|
|
return this;
|
|
|
|
|
|
},
|
|
},
|
|
|
|
|
|
- /*
|
|
|
|
-
|
|
|
|
- // from http://www.mathworks.com/matlabcentral/fileexchange/20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/content/SpinCalc.m
|
|
|
|
- // order XYZ
|
|
|
|
-
|
|
|
|
- getEulerXYZFromQuaternion: function ( q ) {
|
|
|
|
-
|
|
|
|
- this.x = Math.atan2( 2 * ( q.x * q.w - q.y * q.z ), ( q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z ) );
|
|
|
|
- this.y = Math.asin( 2 * ( q.x * q.z + q.y * q.w ) );
|
|
|
|
- this.z = Math.atan2( 2 * ( q.z * q.w - q.x * q.y ), ( q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z ) );
|
|
|
|
-
|
|
|
|
- },
|
|
|
|
-
|
|
|
|
- // from http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/index.htm
|
|
|
|
- // order YZX (assuming heading == y, attitude == z, bank == x)
|
|
|
|
-
|
|
|
|
- getEulerYZXFromQuaternion: function ( q ) {
|
|
|
|
-
|
|
|
|
- var sqw = q.w * q.w;
|
|
|
|
- var sqx = q.x * q.x;
|
|
|
|
- var sqy = q.y * q.y;
|
|
|
|
- var sqz = q.z * q.z;
|
|
|
|
- var unit = sqx + sqy + sqz + sqw; // if normalised is one, otherwise is correction factor
|
|
|
|
- var test = q.x * q.y + q.z * q.w;
|
|
|
|
-
|
|
|
|
- if ( test > 0.499 * unit ) { // singularity at north pole
|
|
|
|
-
|
|
|
|
- this.y = 2 * Math.atan2( q.x, q.w );
|
|
|
|
- this.z = Math.PI / 2;
|
|
|
|
- this.x = 0;
|
|
|
|
-
|
|
|
|
- return;
|
|
|
|
-
|
|
|
|
|
|
+ setEulerFromQuaternion: function ( q, order ) {
|
|
|
|
+
|
|
|
|
+ // http://www.mathworks.com/matlabcentral/fileexchange/
|
|
|
|
+ // 20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/
|
|
|
|
+ // content/SpinCalc.m
|
|
|
|
+
|
|
|
|
+ // q is assumed to be normalized
|
|
|
|
+
|
|
|
|
+ // clamp, to handle numerical problems
|
|
|
|
+ function clamp( x ) { return Math.min( Math.max( x, -1 ), 1 ) };
|
|
|
|
+
|
|
|
|
+ var sqx = q.x * q.x,
|
|
|
|
+ sqy = q.y * q.y,
|
|
|
|
+ sqz = q.z * q.z,
|
|
|
|
+ sqw = q.w * q.w;
|
|
|
|
+
|
|
|
|
+ switch ( order ) {
|
|
|
|
+
|
|
|
|
+ case '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 ) );
|
|
|
|
+
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case '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 ) );
|
|
|
|
+
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case '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 ) );
|
|
|
|
+
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case '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 ) ) );
|
|
|
|
+
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case '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 ) ) );
|
|
|
|
+
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ default: // '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 ) );
|
|
|
|
+
|
|
}
|
|
}
|
|
-
|
|
|
|
- if ( test < -0.499 * unit ) { // singularity at south pole
|
|
|
|
-
|
|
|
|
- this.y = -2 * Math.atan2( q.x, q.w );
|
|
|
|
- this.z = -Math.PI / 2;
|
|
|
|
- this.x = 0;
|
|
|
|
-
|
|
|
|
- return;
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- this.y = Math.atan2( 2 * q.y * q.w - 2 * q.x * q.z, sqx - sqy - sqz + sqw );
|
|
|
|
- this.z = Math.asin( 2 * test / unit );
|
|
|
|
- this.x = Math.atan2( 2 * q.x * q.w - 2 * q.y * q.z, -sqx + sqy - sqz + sqw );
|
|
|
|
|
|
+
|
|
|
|
+ return this;
|
|
|
|
|
|
},
|
|
},
|
|
|
|
|
|
- */
|
|
|
|
-
|
|
|
|
getScaleFromMatrix: function ( m ) {
|
|
getScaleFromMatrix: function ( m ) {
|
|
|
|
|
|
var sx = this.set( m.elements[0], m.elements[1], m.elements[2] ).length();
|
|
var sx = this.set( m.elements[0], m.elements[1], m.elements[2] ).length();
|