mRotation.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "math/mRotation.h"
  23. #ifdef TORQUE_TESTS_ENABLED
  24. #include "testing/unitTesting.h"
  25. #endif
  26. //====================================================================
  27. //Eulers setup
  28. //====================================================================
  29. RotationF::RotationF(EulerF _euler, UnitFormat format)
  30. {
  31. set(_euler.x, _euler.y, _euler.z, format);
  32. }
  33. RotationF::RotationF(F32 _x, F32 _y, F32 _z, UnitFormat format)
  34. {
  35. set(_x, _y, _z, format);
  36. }
  37. void RotationF::set(EulerF _euler, UnitFormat format)
  38. {
  39. x = format == Degrees ? mDegToRad(_euler.x) : _euler.x;
  40. y = format == Degrees ? mDegToRad(_euler.y) : _euler.y;
  41. z = format == Degrees ? mDegToRad(_euler.z) : _euler.z;
  42. mRotationType = Euler;
  43. }
  44. void RotationF::set(F32 _x, F32 _y, F32 _z, UnitFormat format)
  45. {
  46. EulerF tempEul;
  47. if (format == Degrees)
  48. {
  49. tempEul.set(mDegToRad(_x), mDegToRad(_y), mDegToRad(_z));
  50. }
  51. else
  52. {
  53. tempEul.set(_x, _y, _z);
  54. }
  55. set(tempEul);
  56. }
  57. //====================================================================
  58. //AxisAngle setup
  59. //====================================================================
  60. RotationF::RotationF(AngAxisF _aa, UnitFormat format)
  61. {
  62. set(_aa, format);
  63. }
  64. void RotationF::set(AngAxisF _aa, UnitFormat format)
  65. {
  66. x = _aa.axis.x;
  67. y = _aa.axis.y;
  68. z = _aa.axis.z;
  69. w = format == Degrees ? mDegToRad(_aa.angle) : _aa.angle;
  70. mRotationType = AxisAngle;
  71. }
  72. //====================================================================
  73. //QuatF setup
  74. //====================================================================
  75. RotationF::RotationF(QuatF _quat)
  76. {
  77. set(_quat);
  78. }
  79. void RotationF::set(QuatF _quat)
  80. {
  81. AngAxisF tmpAA;
  82. tmpAA.set(_quat);
  83. set(tmpAA);
  84. }
  85. //====================================================================
  86. //MatrixF setup
  87. //====================================================================
  88. RotationF::RotationF(MatrixF _mat)
  89. {
  90. set(_mat);
  91. }
  92. void RotationF::set(MatrixF _mat)
  93. {
  94. set(_mat.toEuler());
  95. }
  96. //
  97. inline F32 RotationF::len() const
  98. {
  99. return asEulerF().len();
  100. }
  101. inline void RotationF::interpolate(const RotationF& _from, const RotationF& _to, F32 _factor)
  102. {
  103. QuatF tmpQuat;
  104. tmpQuat.interpolate(_from.asQuatF(), _to.asQuatF(), _factor);
  105. set(tmpQuat);
  106. }
  107. void RotationF::lookAt(const Point3F& _origin, const Point3F& _target, const Point3F& _up)
  108. {
  109. MatrixF mat;
  110. VectorF newForward = _target - _origin;
  111. newForward.normalize();
  112. VectorF up(0.0f, 0.0f, 1.0f);
  113. VectorF axisX;
  114. VectorF axisY = newForward;
  115. VectorF axisZ;
  116. if (_up != VectorF::Zero)
  117. up = _up;
  118. // Validate and normalize input:
  119. F32 lenSq;
  120. lenSq = axisY.lenSquared();
  121. if (lenSq < 0.000001f)
  122. {
  123. //degenerate forward vector
  124. axisY.set(0.0f, 1.0f, 0.0f);
  125. }
  126. else
  127. {
  128. axisY /= mSqrt(lenSq);
  129. }
  130. lenSq = up.lenSquared();
  131. if (lenSq < 0.000001f)
  132. {
  133. //degenerate up vector - too small
  134. up.set(0.0f, 0.0f, 1.0f);
  135. }
  136. else
  137. {
  138. up /= mSqrt(lenSq);
  139. }
  140. if (fabsf(mDot(up, axisY)) > 0.9999f)
  141. {
  142. //degenerate up vector - same as forward
  143. F32 tmp = up.x;
  144. up.x = -up.y;
  145. up.y = up.z;
  146. up.z = tmp;
  147. }
  148. // construct the remaining axes:
  149. mCross(axisY, up, &axisX);
  150. mCross(axisX, axisY, &axisZ);
  151. mat.setColumn(0, axisX);
  152. mat.setColumn(1, axisY);
  153. mat.setColumn(2, axisZ);
  154. set(mat);
  155. }
  156. //========================================================
  157. EulerF RotationF::asEulerF(UnitFormat _format) const
  158. {
  159. if (mRotationType == Euler)
  160. {
  161. if (_format == Degrees)
  162. {
  163. return EulerF(mRadToDeg(x), mRadToDeg(y), mRadToDeg(z));
  164. }
  165. else
  166. {
  167. return EulerF(x, y, z);
  168. }
  169. }
  170. else
  171. {
  172. EulerF returnEuler = asMatrixF().toEuler();
  173. if (_format == Degrees)
  174. {
  175. returnEuler.x = mRadToDeg(returnEuler.x);
  176. returnEuler.y = mRadToDeg(returnEuler.y);
  177. returnEuler.z = mRadToDeg(returnEuler.z);
  178. }
  179. return returnEuler;
  180. }
  181. }
  182. AngAxisF RotationF::asAxisAngle(UnitFormat format) const
  183. {
  184. AngAxisF returnAA;
  185. if (mRotationType == Euler)
  186. {
  187. returnAA.set(EulerF(x, y, z));
  188. }
  189. else
  190. {
  191. returnAA.set(Point3F(x, y, z), w);
  192. }
  193. if (format == Radians)
  194. {
  195. returnAA.angle = mDegToRad(returnAA.angle);
  196. }
  197. return returnAA;
  198. }
  199. MatrixF RotationF::asMatrixF() const
  200. {
  201. MatrixF returnMat;
  202. if (mRotationType == Euler)
  203. {
  204. returnMat.set(EulerF(x, y, z));
  205. }
  206. else
  207. {
  208. AngAxisF aa;
  209. aa.set(Point3F(x, y, z), w);
  210. aa.setMatrix(&returnMat);
  211. }
  212. return returnMat;
  213. }
  214. QuatF RotationF::asQuatF() const
  215. {
  216. QuatF returnQuat;
  217. if (mRotationType == Euler)
  218. {
  219. returnQuat.set(EulerF(x, y, z));
  220. }
  221. else
  222. {
  223. AngAxisF aa;
  224. aa.set(Point3F(x, y, z), w);
  225. returnQuat.set(aa);
  226. }
  227. return returnQuat;
  228. }
  229. void RotationF::normalize()
  230. {
  231. if (mRotationType == Euler)
  232. {
  233. EulerF eul = EulerF(x, y, z);
  234. eul.normalize();
  235. set(eul);
  236. }
  237. else
  238. {
  239. QuatF quat;
  240. quat.set(Point3F(x, y, z), w);
  241. quat.normalize();
  242. set(quat);
  243. }
  244. }
  245. //Testing
  246. #ifdef TORQUE_TESTS_ENABLED
  247. TEST(Maths, RotationF_Calculations)
  248. {
  249. //TODO: implement unit test
  250. };
  251. #endif