Euler.js 6.7 KB

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