Euler.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. var clamp = THREE.Math.clamp;
  69. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  70. var te = m.elements;
  71. var m11 = te[0], m12 = te[4], m13 = te[8];
  72. var m21 = te[1], m22 = te[5], m23 = te[9];
  73. var m31 = te[2], m32 = te[6], m33 = te[10];
  74. order = order || this._order;
  75. if ( order === 'XYZ' ) {
  76. this._y = Math.asin( clamp( m13, -1, 1 ) );
  77. if ( Math.abs( m13 ) < 0.99999 ) {
  78. this._x = Math.atan2( - m23, m33 );
  79. this._z = Math.atan2( - m12, m11 );
  80. } else {
  81. this._x = Math.atan2( m32, m22 );
  82. this._z = 0;
  83. }
  84. } else if ( order === 'YXZ' ) {
  85. this._x = Math.asin( - clamp( m23, -1, 1 ) );
  86. if ( Math.abs( m23 ) < 0.99999 ) {
  87. this._y = Math.atan2( m13, m33 );
  88. this._z = Math.atan2( m21, m22 );
  89. } else {
  90. this._y = Math.atan2( - m31, m11 );
  91. this._z = 0;
  92. }
  93. } else if ( order === 'ZXY' ) {
  94. this._x = Math.asin( clamp( m32, -1, 1 ) );
  95. if ( Math.abs( m32 ) < 0.99999 ) {
  96. this._y = Math.atan2( - m31, m33 );
  97. this._z = Math.atan2( - m12, m22 );
  98. } else {
  99. this._y = 0;
  100. this._z = Math.atan2( m21, m11 );
  101. }
  102. } else if ( order === 'ZYX' ) {
  103. this._y = Math.asin( - clamp( m31, -1, 1 ) );
  104. if ( Math.abs( m31 ) < 0.99999 ) {
  105. this._x = Math.atan2( m32, m33 );
  106. this._z = Math.atan2( m21, m11 );
  107. } else {
  108. this._x = 0;
  109. this._z = Math.atan2( - m12, m22 );
  110. }
  111. } else if ( order === 'YZX' ) {
  112. this._z = Math.asin( clamp( m21, -1, 1 ) );
  113. if ( Math.abs( m21 ) < 0.99999 ) {
  114. this._x = Math.atan2( - m23, m22 );
  115. this._y = Math.atan2( - m31, m11 );
  116. } else {
  117. this._x = 0;
  118. this._y = Math.atan2( m13, m33 );
  119. }
  120. } else if ( order === 'XZY' ) {
  121. this._z = Math.asin( - clamp( m12, -1, 1 ) );
  122. if ( Math.abs( m12 ) < 0.99999 ) {
  123. this._x = Math.atan2( m32, m22 );
  124. this._y = Math.atan2( m13, m11 );
  125. } else {
  126. this._x = Math.atan2( - m23, m33 );
  127. this._y = 0;
  128. }
  129. } else {
  130. console.warn( 'WARNING: Euler.setFromRotationMatrix() given unsupported order: ' + order )
  131. }
  132. this._order = order;
  133. this._updateQuaternion();
  134. return this;
  135. },
  136. setFromQuaternion: function ( q, order, update ) {
  137. var clamp = THREE.Math.clamp;
  138. // q is assumed to be normalized
  139. // http://www.mathworks.com/matlabcentral/fileexchange/20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/content/SpinCalc.m
  140. var sqx = q.x * q.x;
  141. var sqy = q.y * q.y;
  142. var sqz = q.z * q.z;
  143. var sqw = q.w * q.w;
  144. order = order || this._order;
  145. if ( order === 'XYZ' ) {
  146. this._x = Math.atan2( 2 * ( q.x * q.w - q.y * q.z ), ( sqw - sqx - sqy + sqz ) );
  147. this._y = Math.asin( clamp( 2 * ( q.x * q.z + q.y * q.w ), -1, 1 ) );
  148. this._z = Math.atan2( 2 * ( q.z * q.w - q.x * q.y ), ( sqw + sqx - sqy - sqz ) );
  149. } else if ( order === 'YXZ' ) {
  150. this._x = Math.asin( clamp( 2 * ( q.x * q.w - q.y * q.z ), -1, 1 ) );
  151. this._y = Math.atan2( 2 * ( q.x * q.z + q.y * q.w ), ( sqw - sqx - sqy + sqz ) );
  152. this._z = Math.atan2( 2 * ( q.x * q.y + q.z * q.w ), ( sqw - sqx + sqy - sqz ) );
  153. } else if ( order === 'ZXY' ) {
  154. this._x = Math.asin( clamp( 2 * ( q.x * q.w + q.y * q.z ), -1, 1 ) );
  155. this._y = Math.atan2( 2 * ( q.y * q.w - q.z * q.x ), ( sqw - sqx - sqy + sqz ) );
  156. this._z = Math.atan2( 2 * ( q.z * q.w - q.x * q.y ), ( sqw - sqx + sqy - sqz ) );
  157. } else if ( order === 'ZYX' ) {
  158. this._x = Math.atan2( 2 * ( q.x * q.w + q.z * q.y ), ( sqw - sqx - sqy + sqz ) );
  159. this._y = Math.asin( clamp( 2 * ( q.y * q.w - q.x * q.z ), -1, 1 ) );
  160. this._z = Math.atan2( 2 * ( q.x * q.y + q.z * q.w ), ( sqw + sqx - sqy - sqz ) );
  161. } else if ( order === 'YZX' ) {
  162. this._x = Math.atan2( 2 * ( q.x * q.w - q.z * q.y ), ( sqw - sqx + sqy - sqz ) );
  163. this._y = Math.atan2( 2 * ( q.y * q.w - q.x * q.z ), ( sqw + sqx - sqy - sqz ) );
  164. this._z = Math.asin( clamp( 2 * ( q.x * q.y + q.z * q.w ), -1, 1 ) );
  165. } else if ( order === 'XZY' ) {
  166. this._x = Math.atan2( 2 * ( q.x * q.w + q.y * q.z ), ( sqw - sqx + sqy - sqz ) );
  167. this._y = Math.atan2( 2 * ( q.x * q.z + q.y * q.w ), ( sqw + sqx - sqy - sqz ) );
  168. this._z = Math.asin( clamp( 2 * ( q.z * q.w - q.x * q.y ), -1, 1 ) );
  169. } else {
  170. console.warn( 'WARNING: Euler.setFromQuaternion() given unsupported order: ' + order )
  171. }
  172. this._order = order;
  173. if ( update !== false ) this._updateQuaternion();
  174. return this;
  175. },
  176. reorder: function () {
  177. // WARNING: this discards revolution information -bhouston
  178. var q = new THREE.Quaternion();
  179. return function ( newOrder ) {
  180. q.setFromEuler( this );
  181. this.setFromQuaternion( q, newOrder );
  182. };
  183. }(),
  184. fromArray: function ( array ) {
  185. this._x = array[ 0 ];
  186. this._y = array[ 1 ];
  187. this._z = array[ 2 ];
  188. if ( array[ 3 ] !== undefined ) this._order = array[ 3 ];
  189. this._updateQuaternion();
  190. return this;
  191. },
  192. toArray: function () {
  193. return [ this._x, this._y, this._z, this._order ];
  194. },
  195. equals: function ( euler ) {
  196. return ( euler._x === this._x ) && ( euler._y === this._y ) && ( euler._z === this._z ) && ( euler._order === this._order );
  197. },
  198. clone: function () {
  199. return new THREE.Euler( this._x, this._y, this._z, this._order );
  200. }
  201. };
粤ICP备19079148号