ImathEuler.h 26 KB

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