Euler.js 6.6 KB

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