Euler.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author WestLangley / http://github.com/WestLangley
  4. * @author bhouston / http://exocortex.com
  5. */
  6. THREE.Euler = function ( x, y, z, order ) {
  7. this.x = x || 0;
  8. this.y = y || 0;
  9. this.z = z || 0;
  10. this.order = order || THREE.Euler.DefaultOrder;
  11. };
  12. THREE.Euler.RotationOrders = [ 'XYZ', 'YZX', 'ZXY', 'XZY', 'YXZ', 'ZYX' ];
  13. THREE.Euler.DefaultOrder = 'XYZ';
  14. THREE.Euler.prototype = {
  15. constructor: THREE.Euler,
  16. set: function ( x, y, z, order ) {
  17. this.x = x;
  18. this.y = y;
  19. this.z = z;
  20. this.order = order || this.order;
  21. return this;
  22. },
  23. copy: function ( e ) {
  24. this.x = e.x;
  25. this.y = e.y;
  26. this.z = e.z;
  27. this.order = e.order;
  28. return this;
  29. },
  30. setFromRotationMatrix: function ( m, order ) {
  31. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  32. // clamp, to handle numerical problems
  33. function clamp( x ) {
  34. return Math.min( Math.max( x, -1 ), 1 );
  35. }
  36. var te = m.elements;
  37. var m11 = te[0], m12 = te[4], m13 = te[8];
  38. var m21 = te[1], m22 = te[5], m23 = te[9];
  39. var m31 = te[2], m32 = te[6], m33 = te[10];
  40. order = order || this.order;
  41. if ( order === 'XYZ' ) {
  42. this.y = Math.asin( clamp( m13 ) );
  43. if ( Math.abs( m13 ) < 0.99999 ) {
  44. this.x = Math.atan2( - m23, m33 );
  45. this.z = Math.atan2( - m12, m11 );
  46. } else {
  47. this.x = Math.atan2( m32, m22 );
  48. this.z = 0;
  49. }
  50. } else if ( order === 'YXZ' ) {
  51. this.x = Math.asin( - clamp( m23 ) );
  52. if ( Math.abs( m23 ) < 0.99999 ) {
  53. this.y = Math.atan2( m13, m33 );
  54. this.z = Math.atan2( m21, m22 );
  55. } else {
  56. this.y = Math.atan2( - m31, m11 );
  57. this.z = 0;
  58. }
  59. } else if ( order === 'ZXY' ) {
  60. this.x = Math.asin( clamp( m32 ) );
  61. if ( Math.abs( m32 ) < 0.99999 ) {
  62. this.y = Math.atan2( - m31, m33 );
  63. this.z = Math.atan2( - m12, m22 );
  64. } else {
  65. this.y = 0;
  66. this.z = Math.atan2( m21, m11 );
  67. }
  68. } else if ( order === 'ZYX' ) {
  69. this.y = Math.asin( - clamp( m31 ) );
  70. if ( Math.abs( m31 ) < 0.99999 ) {
  71. this.x = Math.atan2( m32, m33 );
  72. this.z = Math.atan2( m21, m11 );
  73. } else {
  74. this.x = 0;
  75. this.z = Math.atan2( - m12, m22 );
  76. }
  77. } else if ( order === 'YZX' ) {
  78. this.z = Math.asin( clamp( m21 ) );
  79. if ( Math.abs( m21 ) < 0.99999 ) {
  80. this.x = Math.atan2( - m23, m22 );
  81. this.y = Math.atan2( - m31, m11 );
  82. } else {
  83. this.x = 0;
  84. this.y = Math.atan2( m13, m33 );
  85. }
  86. } else if ( order === 'XZY' ) {
  87. this.z = Math.asin( - clamp( m12 ) );
  88. if ( Math.abs( m12 ) < 0.99999 ) {
  89. this.x = Math.atan2( m32, m22 );
  90. this.y = Math.atan2( m13, m11 );
  91. } else {
  92. this.x = Math.atan2( - m23, m33 );
  93. this.y = 0;
  94. }
  95. }
  96. else {
  97. console.warn( 'WARNING: Euler.setFromRotationMatrix() given unsupported order: ' + order )
  98. }
  99. this.order = order;
  100. return this;
  101. },
  102. setFromQuaternion: function ( q, order ) {
  103. // q is assumed to be normalized
  104. // clamp, to handle numerical problems
  105. function clamp( x ) {
  106. return Math.min( Math.max( x, -1 ), 1 );
  107. }
  108. // http://www.mathworks.com/matlabcentral/fileexchange/20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/content/SpinCalc.m
  109. var sqx = q.x * q.x;
  110. var sqy = q.y * q.y;
  111. var sqz = q.z * q.z;
  112. var sqw = q.w * q.w;
  113. order = order || this.order;
  114. if ( order === 'XYZ' ) {
  115. this.x = Math.atan2( 2 * ( q.x * q.w - q.y * q.z ), ( sqw - sqx - sqy + sqz ) );
  116. this.y = Math.asin( clamp( 2 * ( q.x * q.z + q.y * q.w ) ) );
  117. this.z = Math.atan2( 2 * ( q.z * q.w - q.x * q.y ), ( sqw + sqx - sqy - sqz ) );
  118. } else if ( order === 'YXZ' ) {
  119. this.x = Math.asin( clamp( 2 * ( q.x * q.w - q.y * q.z ) ) );
  120. this.y = Math.atan2( 2 * ( q.x * q.z + q.y * q.w ), ( sqw - sqx - sqy + sqz ) );
  121. this.z = Math.atan2( 2 * ( q.x * q.y + q.z * q.w ), ( sqw - sqx + sqy - sqz ) );
  122. } else if ( order === 'ZXY' ) {
  123. this.x = Math.asin( clamp( 2 * ( q.x * q.w + q.y * q.z ) ) );
  124. this.y = Math.atan2( 2 * ( q.y * q.w - q.z * q.x ), ( sqw - sqx - sqy + sqz ) );
  125. this.z = Math.atan2( 2 * ( q.z * q.w - q.x * q.y ), ( sqw - sqx + sqy - sqz ) );
  126. } else if ( order === 'ZYX' ) {
  127. this.x = Math.atan2( 2 * ( q.x * q.w + q.z * q.y ), ( sqw - sqx - sqy + sqz ) );
  128. this.y = Math.asin( clamp( 2 * ( q.y * q.w - q.x * q.z ) ) );
  129. this.z = Math.atan2( 2 * ( q.x * q.y + q.z * q.w ), ( sqw + sqx - sqy - sqz ) );
  130. } else if ( order === 'YZX' ) {
  131. this.x = Math.atan2( 2 * ( q.x * q.w - q.z * q.y ), ( sqw - sqx + sqy - sqz ) );
  132. this.y = Math.atan2( 2 * ( q.y * q.w - q.x * q.z ), ( sqw + sqx - sqy - sqz ) );
  133. this.z = Math.asin( clamp( 2 * ( q.x * q.y + q.z * q.w ) ) );
  134. } else if ( order === 'XZY' ) {
  135. this.x = Math.atan2( 2 * ( q.x * q.w + q.y * q.z ), ( sqw - sqx + sqy - sqz ) );
  136. this.y = Math.atan2( 2 * ( q.x * q.z + q.y * q.w ), ( sqw + sqx - sqy - sqz ) );
  137. this.z = Math.asin( clamp( 2 * ( q.z * q.w - q.x * q.y ) ) );
  138. }
  139. else {
  140. console.warn( 'WARNING: Euler.setFromQuaternion() given unsupported order: ' + order )
  141. }
  142. this.order = order;
  143. return this;
  144. },
  145. reorder: function() {
  146. // WARNING: this discards revolution information -bhouston
  147. var q = new THREE.Quaternion();
  148. return function( newOrder ) {
  149. q.setFromEuler( this );
  150. this.setFromQuaternion( q, newOrder );
  151. };
  152. }(),
  153. fromArray: function ( array ) {
  154. this.x = array[ 0 ];
  155. this.y = array[ 1 ];
  156. this.z = array[ 2 ];
  157. this.order = array[ 3 ];
  158. return this;
  159. },
  160. toArray: function () {
  161. return [ this.x, this.y, this.z, this.order ];
  162. },
  163. equals: function ( rotation ) {
  164. return ( ( rotation.x === this.x ) && ( rotation.y === this.y ) && ( rotation.z === this.z ) && ( rotation.order === this.order ) );
  165. },
  166. clone: function () {
  167. return new THREE.Euler( this.x, this.y, this.z, this.order );
  168. }
  169. };