ImathEuler.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2002-2012, Industrial Light & Magic, a division of Lucas
  4. // Digital Ltd. LLC
  5. //
  6. // All rights reserved.
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. // * Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // * Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following disclaimer
  15. // in the documentation and/or other materials provided with the
  16. // distribution.
  17. // * Neither the name of Industrial Light & Magic nor the names of
  18. // its contributors may be used to endorse or promote products derived
  19. // from this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. //
  33. ///////////////////////////////////////////////////////////////////////////
  34. #ifndef INCLUDED_IMATHEULER_H
  35. #define INCLUDED_IMATHEULER_H
  36. //----------------------------------------------------------------------
  37. //
  38. // template class Euler<T>
  39. //
  40. // This class represents euler angle orientations. The class
  41. // inherits from Vec3 to it can be freely cast. The additional
  42. // information is the euler priorities rep. This class is
  43. // essentially a rip off of Ken Shoemake's GemsIV code. It has
  44. // been modified minimally to make it more understandable, but
  45. // hardly enough to make it easy to grok completely.
  46. //
  47. // There are 24 possible combonations of Euler angle
  48. // representations of which 12 are common in CG and you will
  49. // probably only use 6 of these which in this scheme are the
  50. // non-relative-non-repeating types.
  51. //
  52. // The representations can be partitioned according to two
  53. // criteria:
  54. //
  55. // 1) Are the angles measured relative to a set of fixed axis
  56. // or relative to each other (the latter being what happens
  57. // when rotation matrices are multiplied together and is
  58. // almost ubiquitous in the cg community)
  59. //
  60. // 2) Is one of the rotations repeated (ala XYX rotation)
  61. //
  62. // When you construct a given representation from scratch you
  63. // must order the angles according to their priorities. So, the
  64. // easiest is a softimage or aerospace (yaw/pitch/roll) ordering
  65. // of ZYX.
  66. //
  67. // float x_rot = 1;
  68. // float y_rot = 2;
  69. // float z_rot = 3;
  70. //
  71. // Eulerf angles(z_rot, y_rot, x_rot, Eulerf::ZYX);
  72. // -or-
  73. // Eulerf angles( V3f(z_rot,y_rot,z_rot), Eulerf::ZYX );
  74. //
  75. // If instead, the order was YXZ for instance you would have to
  76. // do this:
  77. //
  78. // float x_rot = 1;
  79. // float y_rot = 2;
  80. // float z_rot = 3;
  81. //
  82. // Eulerf angles(y_rot, x_rot, z_rot, Eulerf::YXZ);
  83. // -or-
  84. // Eulerf angles( V3f(y_rot,x_rot,z_rot), Eulerf::YXZ );
  85. //
  86. // Notice how the order you put the angles into the three slots
  87. // should correspond to the enum (YXZ) ordering. The input angle
  88. // vector is called the "ijk" vector -- not an "xyz" vector. The
  89. // ijk vector order is the same as the enum. If you treat the
  90. // Euler<> as a Vec<> (which it inherts from) you will find the
  91. // angles are ordered in the same way, i.e.:
  92. //
  93. // V3f v = angles;
  94. // // v.x == y_rot, v.y == x_rot, v.z == z_rot
  95. //
  96. // If you just want the x, y, and z angles stored in a vector in
  97. // that order, you can do this:
  98. //
  99. // V3f v = angles.toXYZVector()
  100. // // v.x == x_rot, v.y == y_rot, v.z == z_rot
  101. //
  102. // If you want to set the Euler with an XYZVector use the
  103. // optional layout argument:
  104. //
  105. // Eulerf angles(x_rot, y_rot, z_rot,
  106. // Eulerf::YXZ,
  107. // Eulerf::XYZLayout);
  108. //
  109. // This is the same as:
  110. //
  111. // Eulerf angles(y_rot, x_rot, z_rot, Eulerf::YXZ);
  112. //
  113. // Note that this won't do anything intelligent if you have a
  114. // repeated axis in the euler angles (e.g. XYX)
  115. //
  116. // If you need to use the "relative" versions of these, you will
  117. // need to use the "r" enums.
  118. //
  119. // The units of the rotation angles are assumed to be radians.
  120. //
  121. //----------------------------------------------------------------------
  122. #include "ImathMath.h"
  123. #include "ImathVec.h"
  124. #include "ImathQuat.h"
  125. #include "ImathMatrix.h"
  126. #include "ImathLimits.h"
  127. #include "ImathNamespace.h"
  128. #include <iostream>
  129. IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
  130. #if (defined _WIN32 || defined _WIN64) && defined _MSC_VER
  131. // Disable MS VC++ warnings about conversion from double to float
  132. #pragma warning(disable:4244)
  133. #endif
  134. template <class T>
  135. class Euler : public Vec3<T>
  136. {
  137. public:
  138. using Vec3<T>::x;
  139. using Vec3<T>::y;
  140. using Vec3<T>::z;
  141. enum Order
  142. {
  143. //
  144. // All 24 possible orderings
  145. //
  146. XYZ = 0x0101, // "usual" orderings
  147. XZY = 0x0001,
  148. YZX = 0x1101,
  149. YXZ = 0x1001,
  150. ZXY = 0x2101,
  151. ZYX = 0x2001,
  152. XZX = 0x0011, // first axis repeated
  153. XYX = 0x0111,
  154. YXY = 0x1011,
  155. YZY = 0x1111,
  156. ZYZ = 0x2011,
  157. ZXZ = 0x2111,
  158. XYZr = 0x2000, // relative orderings -- not common
  159. XZYr = 0x2100,
  160. YZXr = 0x1000,
  161. YXZr = 0x1100,
  162. ZXYr = 0x0000,
  163. ZYXr = 0x0100,
  164. XZXr = 0x2110, // relative first axis repeated
  165. XYXr = 0x2010,
  166. YXYr = 0x1110,
  167. YZYr = 0x1010,
  168. ZYZr = 0x0110,
  169. ZXZr = 0x0010,
  170. // ||||
  171. // VVVV
  172. // Legend: ABCD
  173. // A -> Initial Axis (0==x, 1==y, 2==z)
  174. // B -> Parity Even (1==true)
  175. // C -> Initial Repeated (1==true)
  176. // D -> Frame Static (1==true)
  177. //
  178. Legal = XYZ | XZY | YZX | YXZ | ZXY | ZYX |
  179. XZX | XYX | YXY | YZY | ZYZ | ZXZ |
  180. XYZr| XZYr| YZXr| YXZr| ZXYr| ZYXr|
  181. XZXr| XYXr| YXYr| YZYr| ZYZr| ZXZr,
  182. Min = 0x0000,
  183. Max = 0x2111,
  184. Default = XYZ
  185. };
  186. enum Axis { X = 0, Y = 1, Z = 2 };
  187. enum InputLayout { XYZLayout, IJKLayout };
  188. //--------------------------------------------------------------------
  189. // Constructors -- all default to ZYX non-relative ala softimage
  190. // (where there is no argument to specify it)
  191. //
  192. // The Euler-from-matrix constructors assume that the matrix does
  193. // not include shear or non-uniform scaling, but the constructors
  194. // do not examine the matrix to verify this assumption. If necessary,
  195. // you can adjust the matrix by calling the removeScalingAndShear()
  196. // function, defined in ImathMatrixAlgo.h.
  197. //--------------------------------------------------------------------
  198. Euler();
  199. Euler(const Euler&);
  200. Euler(Order p);
  201. Euler(const Vec3<T> &v, Order o = Default, InputLayout l = IJKLayout);
  202. Euler(T i, T j, T k, Order o = Default, InputLayout l = IJKLayout);
  203. Euler(const Euler<T> &euler, Order newp);
  204. Euler(const Matrix33<T> &, Order o = Default);
  205. Euler(const Matrix44<T> &, Order o = Default);
  206. //---------------------------------
  207. // Algebraic functions/ Operators
  208. //---------------------------------
  209. const Euler<T>& operator= (const Euler<T>&);
  210. const Euler<T>& operator= (const Vec3<T>&);
  211. //--------------------------------------------------------
  212. // Set the euler value
  213. // This does NOT convert the angles, but setXYZVector()
  214. // does reorder the input vector.
  215. //--------------------------------------------------------
  216. static bool legal(Order);
  217. void setXYZVector(const Vec3<T> &);
  218. Order order() const;
  219. void setOrder(Order);
  220. void set(Axis initial,
  221. bool relative,
  222. bool parityEven,
  223. bool firstRepeats);
  224. //------------------------------------------------------------
  225. // Conversions, toXYZVector() reorders the angles so that
  226. // the X rotation comes first, followed by the Y and Z
  227. // in cases like XYX ordering, the repeated angle will be
  228. // in the "z" component
  229. //
  230. // The Euler-from-matrix extract() functions assume that the
  231. // matrix does not include shear or non-uniform scaling, but
  232. // the extract() functions do not examine the matrix to verify
  233. // this assumption. If necessary, you can adjust the matrix
  234. // by calling the removeScalingAndShear() function, defined
  235. // in ImathMatrixAlgo.h.
  236. //------------------------------------------------------------
  237. void extract(const Matrix33<T>&);
  238. void extract(const Matrix44<T>&);
  239. void extract(const Quat<T>&);
  240. Matrix33<T> toMatrix33() const;
  241. Matrix44<T> toMatrix44() const;
  242. Quat<T> toQuat() const;
  243. Vec3<T> toXYZVector() const;
  244. //---------------------------------------------------
  245. // Use this function to unpack angles from ijk form
  246. //---------------------------------------------------
  247. void angleOrder(int &i, int &j, int &k) const;
  248. //---------------------------------------------------
  249. // Use this function to determine mapping from xyz to ijk
  250. // - reshuffles the xyz to match the order
  251. //---------------------------------------------------
  252. void angleMapping(int &i, int &j, int &k) const;
  253. //----------------------------------------------------------------------
  254. //
  255. // Utility methods for getting continuous rotations. None of these
  256. // methods change the orientation given by its inputs (or at least
  257. // that is the intent).
  258. //
  259. // angleMod() converts an angle to its equivalent in [-PI, PI]
  260. //
  261. // simpleXYZRotation() adjusts xyzRot so that its components differ
  262. // from targetXyzRot by no more than +-PI
  263. //
  264. // nearestRotation() adjusts xyzRot so that its components differ
  265. // from targetXyzRot by as little as possible.
  266. // Note that xyz here really means ijk, because
  267. // the order must be provided.
  268. //
  269. // makeNear() adjusts "this" Euler so that its components differ
  270. // from target by as little as possible. This method
  271. // might not make sense for Eulers with different order
  272. // and it probably doesn't work for repeated axis and
  273. // relative orderings (TODO).
  274. //
  275. //-----------------------------------------------------------------------
  276. static float angleMod (T angle);
  277. static void simpleXYZRotation (Vec3<T> &xyzRot,
  278. const Vec3<T> &targetXyzRot);
  279. static void nearestRotation (Vec3<T> &xyzRot,
  280. const Vec3<T> &targetXyzRot,
  281. Order order = XYZ);
  282. void makeNear (const Euler<T> &target);
  283. bool frameStatic() const { return _frameStatic; }
  284. bool initialRepeated() const { return _initialRepeated; }
  285. bool parityEven() const { return _parityEven; }
  286. Axis initialAxis() const { return _initialAxis; }
  287. protected:
  288. bool _frameStatic : 1; // relative or static rotations
  289. bool _initialRepeated : 1; // init axis repeated as last
  290. bool _parityEven : 1; // "parity of axis permutation"
  291. #if defined _WIN32 || defined _WIN64
  292. Axis _initialAxis ; // First axis of rotation
  293. #else
  294. Axis _initialAxis : 2; // First axis of rotation
  295. #endif
  296. };
  297. //--------------------
  298. // Convenient typedefs
  299. //--------------------
  300. typedef Euler<float> Eulerf;
  301. typedef Euler<double> Eulerd;
  302. //---------------
  303. // Implementation
  304. //---------------
  305. template<class T>
  306. inline void
  307. Euler<T>::angleOrder(int &i, int &j, int &k) const
  308. {
  309. i = _initialAxis;
  310. j = _parityEven ? (i+1)%3 : (i > 0 ? i-1 : 2);
  311. k = _parityEven ? (i > 0 ? i-1 : 2) : (i+1)%3;
  312. }
  313. template<class T>
  314. inline void
  315. Euler<T>::angleMapping(int &i, int &j, int &k) const
  316. {
  317. int m[3];
  318. m[_initialAxis] = 0;
  319. m[(_initialAxis+1) % 3] = _parityEven ? 1 : 2;
  320. m[(_initialAxis+2) % 3] = _parityEven ? 2 : 1;
  321. i = m[0];
  322. j = m[1];
  323. k = m[2];
  324. }
  325. template<class T>
  326. inline void
  327. Euler<T>::setXYZVector(const Vec3<T> &v)
  328. {
  329. int i,j,k;
  330. angleMapping(i,j,k);
  331. (*this)[i] = v.x;
  332. (*this)[j] = v.y;
  333. (*this)[k] = v.z;
  334. }
  335. template<class T>
  336. inline Vec3<T>
  337. Euler<T>::toXYZVector() const
  338. {
  339. int i,j,k;
  340. angleMapping(i,j,k);
  341. return Vec3<T>((*this)[i],(*this)[j],(*this)[k]);
  342. }
  343. template<class T>
  344. Euler<T>::Euler() :
  345. Vec3<T>(0,0,0),
  346. _frameStatic(true),
  347. _initialRepeated(false),
  348. _parityEven(true),
  349. _initialAxis(X)
  350. {}
  351. template<class T>
  352. Euler<T>::Euler(typename Euler<T>::Order p) :
  353. Vec3<T>(0,0,0),
  354. _frameStatic(true),
  355. _initialRepeated(false),
  356. _parityEven(true),
  357. _initialAxis(X)
  358. {
  359. setOrder(p);
  360. }
  361. template<class T>
  362. inline Euler<T>::Euler( const Vec3<T> &v,
  363. typename Euler<T>::Order p,
  364. typename Euler<T>::InputLayout l )
  365. {
  366. setOrder(p);
  367. if ( l == XYZLayout ) setXYZVector(v);
  368. else { x = v.x; y = v.y; z = v.z; }
  369. }
  370. template<class T>
  371. inline Euler<T>::Euler(const Euler<T> &euler)
  372. {
  373. operator=(euler);
  374. }
  375. template<class T>
  376. inline Euler<T>::Euler(const Euler<T> &euler,Order p)
  377. {
  378. setOrder(p);
  379. Matrix33<T> M = euler.toMatrix33();
  380. extract(M);
  381. }
  382. template<class T>
  383. inline Euler<T>::Euler( T xi, T yi, T zi,
  384. typename Euler<T>::Order p,
  385. typename Euler<T>::InputLayout l)
  386. {
  387. setOrder(p);
  388. if ( l == XYZLayout ) setXYZVector(Vec3<T>(xi,yi,zi));
  389. else { x = xi; y = yi; z = zi; }
  390. }
  391. template<class T>
  392. inline Euler<T>::Euler( const Matrix33<T> &M, typename Euler::Order p )
  393. {
  394. setOrder(p);
  395. extract(M);
  396. }
  397. template<class T>
  398. inline Euler<T>::Euler( const Matrix44<T> &M, typename Euler::Order p )
  399. {
  400. setOrder(p);
  401. extract(M);
  402. }
  403. template<class T>
  404. inline void Euler<T>::extract(const Quat<T> &q)
  405. {
  406. extract(q.toMatrix33());
  407. }
  408. template<class T>
  409. void Euler<T>::extract(const Matrix33<T> &M)
  410. {
  411. int i,j,k;
  412. angleOrder(i,j,k);
  413. if (_initialRepeated)
  414. {
  415. //
  416. // Extract the first angle, x.
  417. //
  418. x = Math<T>::atan2 (M[j][i], M[k][i]);
  419. //
  420. // Remove the x rotation from M, so that the remaining
  421. // rotation, N, is only around two axes, and gimbal lock
  422. // cannot occur.
  423. //
  424. Vec3<T> r (0, 0, 0);
  425. r[i] = (_parityEven? -x: x);
  426. Matrix44<T> N;
  427. N.rotate (r);
  428. N = N * Matrix44<T> (M[0][0], M[0][1], M[0][2], 0,
  429. M[1][0], M[1][1], M[1][2], 0,
  430. M[2][0], M[2][1], M[2][2], 0,
  431. 0, 0, 0, 1);
  432. //
  433. // Extract the other two angles, y and z, from N.
  434. //
  435. T sy = Math<T>::sqrt (N[j][i]*N[j][i] + N[k][i]*N[k][i]);
  436. y = Math<T>::atan2 (sy, N[i][i]);
  437. z = Math<T>::atan2 (N[j][k], N[j][j]);
  438. }
  439. else
  440. {
  441. //
  442. // Extract the first angle, x.
  443. //
  444. x = Math<T>::atan2 (M[j][k], M[k][k]);
  445. //
  446. // Remove the x rotation from M, so that the remaining
  447. // rotation, N, is only around two axes, and gimbal lock
  448. // cannot occur.
  449. //
  450. Vec3<T> r (0, 0, 0);
  451. r[i] = (_parityEven? -x: x);
  452. Matrix44<T> N;
  453. N.rotate (r);
  454. N = N * Matrix44<T> (M[0][0], M[0][1], M[0][2], 0,
  455. M[1][0], M[1][1], M[1][2], 0,
  456. M[2][0], M[2][1], M[2][2], 0,
  457. 0, 0, 0, 1);
  458. //
  459. // Extract the other two angles, y and z, from N.
  460. //
  461. T cy = Math<T>::sqrt (N[i][i]*N[i][i] + N[i][j]*N[i][j]);
  462. y = Math<T>::atan2 (-N[i][k], cy);
  463. z = Math<T>::atan2 (-N[j][i], N[j][j]);
  464. }
  465. if (!_parityEven)
  466. *this *= -1;
  467. if (!_frameStatic)
  468. {
  469. T t = x;
  470. x = z;
  471. z = t;
  472. }
  473. }
  474. template<class T>
  475. void Euler<T>::extract(const Matrix44<T> &M)
  476. {
  477. int i,j,k;
  478. angleOrder(i,j,k);
  479. if (_initialRepeated)
  480. {
  481. //
  482. // Extract the first angle, x.
  483. //
  484. x = Math<T>::atan2 (M[j][i], M[k][i]);
  485. //
  486. // Remove the x rotation from M, so that the remaining
  487. // rotation, N, is only around two axes, and gimbal lock
  488. // cannot occur.
  489. //
  490. Vec3<T> r (0, 0, 0);
  491. r[i] = (_parityEven? -x: x);
  492. Matrix44<T> N;
  493. N.rotate (r);
  494. N = N * M;
  495. //
  496. // Extract the other two angles, y and z, from N.
  497. //
  498. T sy = Math<T>::sqrt (N[j][i]*N[j][i] + N[k][i]*N[k][i]);
  499. y = Math<T>::atan2 (sy, N[i][i]);
  500. z = Math<T>::atan2 (N[j][k], N[j][j]);
  501. }
  502. else
  503. {
  504. //
  505. // Extract the first angle, x.
  506. //
  507. x = Math<T>::atan2 (M[j][k], M[k][k]);
  508. //
  509. // Remove the x rotation from M, so that the remaining
  510. // rotation, N, is only around two axes, and gimbal lock
  511. // cannot occur.
  512. //
  513. Vec3<T> r (0, 0, 0);
  514. r[i] = (_parityEven? -x: x);
  515. Matrix44<T> N;
  516. N.rotate (r);
  517. N = N * M;
  518. //
  519. // Extract the other two angles, y and z, from N.
  520. //
  521. T cy = Math<T>::sqrt (N[i][i]*N[i][i] + N[i][j]*N[i][j]);
  522. y = Math<T>::atan2 (-N[i][k], cy);
  523. z = Math<T>::atan2 (-N[j][i], N[j][j]);
  524. }
  525. if (!_parityEven)
  526. *this *= -1;
  527. if (!_frameStatic)
  528. {
  529. T t = x;
  530. x = z;
  531. z = t;
  532. }
  533. }
  534. template<class T>
  535. Matrix33<T> Euler<T>::toMatrix33() const
  536. {
  537. int i,j,k;
  538. angleOrder(i,j,k);
  539. Vec3<T> angles;
  540. if ( _frameStatic ) angles = (*this);
  541. else angles = Vec3<T>(z,y,x);
  542. if ( !_parityEven ) angles *= -1.0;
  543. T ci = Math<T>::cos(angles.x);
  544. T cj = Math<T>::cos(angles.y);
  545. T ch = Math<T>::cos(angles.z);
  546. T si = Math<T>::sin(angles.x);
  547. T sj = Math<T>::sin(angles.y);
  548. T sh = Math<T>::sin(angles.z);
  549. T cc = ci*ch;
  550. T cs = ci*sh;
  551. T sc = si*ch;
  552. T ss = si*sh;
  553. Matrix33<T> M;
  554. if ( _initialRepeated )
  555. {
  556. M[i][i] = cj; M[j][i] = sj*si; M[k][i] = sj*ci;
  557. M[i][j] = sj*sh; M[j][j] = -cj*ss+cc; M[k][j] = -cj*cs-sc;
  558. M[i][k] = -sj*ch; M[j][k] = cj*sc+cs; M[k][k] = cj*cc-ss;
  559. }
  560. else
  561. {
  562. M[i][i] = cj*ch; M[j][i] = sj*sc-cs; M[k][i] = sj*cc+ss;
  563. M[i][j] = cj*sh; M[j][j] = sj*ss+cc; M[k][j] = sj*cs-sc;
  564. M[i][k] = -sj; M[j][k] = cj*si; M[k][k] = cj*ci;
  565. }
  566. return M;
  567. }
  568. template<class T>
  569. Matrix44<T> Euler<T>::toMatrix44() const
  570. {
  571. int i,j,k;
  572. angleOrder(i,j,k);
  573. Vec3<T> angles;
  574. if ( _frameStatic ) angles = (*this);
  575. else angles = Vec3<T>(z,y,x);
  576. if ( !_parityEven ) angles *= -1.0;
  577. T ci = Math<T>::cos(angles.x);
  578. T cj = Math<T>::cos(angles.y);
  579. T ch = Math<T>::cos(angles.z);
  580. T si = Math<T>::sin(angles.x);
  581. T sj = Math<T>::sin(angles.y);
  582. T sh = Math<T>::sin(angles.z);
  583. T cc = ci*ch;
  584. T cs = ci*sh;
  585. T sc = si*ch;
  586. T ss = si*sh;
  587. Matrix44<T> M;
  588. if ( _initialRepeated )
  589. {
  590. M[i][i] = cj; M[j][i] = sj*si; M[k][i] = sj*ci;
  591. M[i][j] = sj*sh; M[j][j] = -cj*ss+cc; M[k][j] = -cj*cs-sc;
  592. M[i][k] = -sj*ch; M[j][k] = cj*sc+cs; M[k][k] = cj*cc-ss;
  593. }
  594. else
  595. {
  596. M[i][i] = cj*ch; M[j][i] = sj*sc-cs; M[k][i] = sj*cc+ss;
  597. M[i][j] = cj*sh; M[j][j] = sj*ss+cc; M[k][j] = sj*cs-sc;
  598. M[i][k] = -sj; M[j][k] = cj*si; M[k][k] = cj*ci;
  599. }
  600. return M;
  601. }
  602. template<class T>
  603. Quat<T> Euler<T>::toQuat() const
  604. {
  605. Vec3<T> angles;
  606. int i,j,k;
  607. angleOrder(i,j,k);
  608. if ( _frameStatic ) angles = (*this);
  609. else angles = Vec3<T>(z,y,x);
  610. if ( !_parityEven ) angles.y = -angles.y;
  611. T ti = angles.x*0.5;
  612. T tj = angles.y*0.5;
  613. T th = angles.z*0.5;
  614. T ci = Math<T>::cos(ti);
  615. T cj = Math<T>::cos(tj);
  616. T ch = Math<T>::cos(th);
  617. T si = Math<T>::sin(ti);
  618. T sj = Math<T>::sin(tj);
  619. T sh = Math<T>::sin(th);
  620. T cc = ci*ch;
  621. T cs = ci*sh;
  622. T sc = si*ch;
  623. T ss = si*sh;
  624. T parity = _parityEven ? 1.0 : -1.0;
  625. Quat<T> q;
  626. Vec3<T> a;
  627. if ( _initialRepeated )
  628. {
  629. a[i] = cj*(cs + sc);
  630. a[j] = sj*(cc + ss) * parity,
  631. a[k] = sj*(cs - sc);
  632. q.r = cj*(cc - ss);
  633. }
  634. else
  635. {
  636. a[i] = cj*sc - sj*cs,
  637. a[j] = (cj*ss + sj*cc) * parity,
  638. a[k] = cj*cs - sj*sc;
  639. q.r = cj*cc + sj*ss;
  640. }
  641. q.v = a;
  642. return q;
  643. }
  644. template<class T>
  645. inline bool
  646. Euler<T>::legal(typename Euler<T>::Order order)
  647. {
  648. return (order & ~Legal) ? false : true;
  649. }
  650. template<class T>
  651. typename Euler<T>::Order
  652. Euler<T>::order() const
  653. {
  654. int foo = (_initialAxis == Z ? 0x2000 : (_initialAxis == Y ? 0x1000 : 0));
  655. if (_parityEven) foo |= 0x0100;
  656. if (_initialRepeated) foo |= 0x0010;
  657. if (_frameStatic) foo++;
  658. return (Order)foo;
  659. }
  660. template<class T>
  661. inline void Euler<T>::setOrder(typename Euler<T>::Order p)
  662. {
  663. set( p & 0x2000 ? Z : (p & 0x1000 ? Y : X), // initial axis
  664. !(p & 0x1), // static?
  665. !!(p & 0x100), // permutation even?
  666. !!(p & 0x10)); // initial repeats?
  667. }
  668. template<class T>
  669. void Euler<T>::set(typename Euler<T>::Axis axis,
  670. bool relative,
  671. bool parityEven,
  672. bool firstRepeats)
  673. {
  674. _initialAxis = axis;
  675. _frameStatic = !relative;
  676. _parityEven = parityEven;
  677. _initialRepeated = firstRepeats;
  678. }
  679. template<class T>
  680. const Euler<T>& Euler<T>::operator= (const Euler<T> &euler)
  681. {
  682. x = euler.x;
  683. y = euler.y;
  684. z = euler.z;
  685. _initialAxis = euler._initialAxis;
  686. _frameStatic = euler._frameStatic;
  687. _parityEven = euler._parityEven;
  688. _initialRepeated = euler._initialRepeated;
  689. return *this;
  690. }
  691. template<class T>
  692. const Euler<T>& Euler<T>::operator= (const Vec3<T> &v)
  693. {
  694. x = v.x;
  695. y = v.y;
  696. z = v.z;
  697. return *this;
  698. }
  699. template<class T>
  700. std::ostream& operator << (std::ostream &o, const Euler<T> &euler)
  701. {
  702. char a[3] = { 'X', 'Y', 'Z' };
  703. const char* r = euler.frameStatic() ? "" : "r";
  704. int i,j,k;
  705. euler.angleOrder(i,j,k);
  706. if ( euler.initialRepeated() ) k = i;
  707. return o << "("
  708. << euler.x << " "
  709. << euler.y << " "
  710. << euler.z << " "
  711. << a[i] << a[j] << a[k] << r << ")";
  712. }
  713. template <class T>
  714. float
  715. Euler<T>::angleMod (T angle)
  716. {
  717. const T pi = static_cast<T>(M_PI);
  718. angle = fmod(T (angle), T (2 * pi));
  719. if (angle < -pi) angle += 2 * pi;
  720. if (angle > +pi) angle -= 2 * pi;
  721. return angle;
  722. }
  723. template <class T>
  724. void
  725. Euler<T>::simpleXYZRotation (Vec3<T> &xyzRot, const Vec3<T> &targetXyzRot)
  726. {
  727. Vec3<T> d = xyzRot - targetXyzRot;
  728. xyzRot[0] = targetXyzRot[0] + angleMod(d[0]);
  729. xyzRot[1] = targetXyzRot[1] + angleMod(d[1]);
  730. xyzRot[2] = targetXyzRot[2] + angleMod(d[2]);
  731. }
  732. template <class T>
  733. void
  734. Euler<T>::nearestRotation (Vec3<T> &xyzRot, const Vec3<T> &targetXyzRot,
  735. Order order)
  736. {
  737. int i,j,k;
  738. Euler<T> e (0,0,0, order);
  739. e.angleOrder(i,j,k);
  740. simpleXYZRotation(xyzRot, targetXyzRot);
  741. Vec3<T> otherXyzRot;
  742. otherXyzRot[i] = M_PI+xyzRot[i];
  743. otherXyzRot[j] = M_PI-xyzRot[j];
  744. otherXyzRot[k] = M_PI+xyzRot[k];
  745. simpleXYZRotation(otherXyzRot, targetXyzRot);
  746. Vec3<T> d = xyzRot - targetXyzRot;
  747. Vec3<T> od = otherXyzRot - targetXyzRot;
  748. T dMag = d.dot(d);
  749. T odMag = od.dot(od);
  750. if (odMag < dMag)
  751. {
  752. xyzRot = otherXyzRot;
  753. }
  754. }
  755. template <class T>
  756. void
  757. Euler<T>::makeNear (const Euler<T> &target)
  758. {
  759. Vec3<T> xyzRot = toXYZVector();
  760. Vec3<T> targetXyz;
  761. if (order() != target.order())
  762. {
  763. Euler<T> targetSameOrder = Euler<T>(target, order());
  764. targetXyz = targetSameOrder.toXYZVector();
  765. }
  766. else
  767. {
  768. targetXyz = target.toXYZVector();
  769. }
  770. nearestRotation(xyzRot, targetXyz, order());
  771. setXYZVector(xyzRot);
  772. }
  773. #if (defined _WIN32 || defined _WIN64) && defined _MSC_VER
  774. #pragma warning(default:4244)
  775. #endif
  776. IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
  777. #endif // INCLUDED_IMATHEULER_H