mRotation.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. #include "console/console.h"
  24. #include "console/engineAPI.h"
  25. #ifdef TORQUE_TESTS_ENABLED
  26. #include "testing/unitTesting.h"
  27. #endif
  28. //====================================================================
  29. //Eulers setup
  30. //====================================================================
  31. RotationF::RotationF(EulerF _euler, UnitFormat format)
  32. {
  33. set(_euler.x, _euler.y, _euler.z, format);
  34. }
  35. RotationF::RotationF(F32 _x, F32 _y, F32 _z, UnitFormat format)
  36. {
  37. set(_x, _y, _z, format);
  38. }
  39. void RotationF::set(EulerF _euler, UnitFormat format)
  40. {
  41. x = format == Degrees ? mDegToRad(_euler.x) : _euler.x;
  42. y = format == Degrees ? mDegToRad(_euler.y) : _euler.y;
  43. z = format == Degrees ? mDegToRad(_euler.z) : _euler.z;
  44. mRotationType = Euler;
  45. }
  46. void RotationF::set(F32 _x, F32 _y, F32 _z, UnitFormat format)
  47. {
  48. EulerF tempEul;
  49. if (format == Degrees)
  50. {
  51. tempEul.set(mDegToRad(_x), mDegToRad(_y), mDegToRad(_z));
  52. }
  53. else
  54. {
  55. tempEul.set(_x, _y, _z);
  56. }
  57. set(tempEul);
  58. }
  59. //====================================================================
  60. //AxisAngle setup
  61. //====================================================================
  62. RotationF::RotationF(AngAxisF _aa, UnitFormat format)
  63. {
  64. set(_aa, format);
  65. }
  66. void RotationF::set(AngAxisF _aa, UnitFormat format)
  67. {
  68. x = _aa.axis.x;
  69. y = _aa.axis.y;
  70. z = _aa.axis.z;
  71. w = format == Degrees ? mDegToRad(_aa.angle) : _aa.angle;
  72. mRotationType = AxisAngle;
  73. }
  74. //====================================================================
  75. //QuatF setup
  76. //====================================================================
  77. RotationF::RotationF(QuatF _quat)
  78. {
  79. set(_quat);
  80. }
  81. void RotationF::set(QuatF _quat)
  82. {
  83. AngAxisF tmpAA;
  84. tmpAA.set(_quat);
  85. set(tmpAA);
  86. }
  87. //====================================================================
  88. //MatrixF setup
  89. //====================================================================
  90. RotationF::RotationF(MatrixF _mat)
  91. {
  92. set(_mat);
  93. }
  94. void RotationF::set(MatrixF _mat)
  95. {
  96. set(_mat.toEuler());
  97. }
  98. //
  99. inline F32 RotationF::len() const
  100. {
  101. return asEulerF().len();
  102. }
  103. inline void RotationF::interpolate(const RotationF& _from, const RotationF& _to, F32 _factor)
  104. {
  105. QuatF tmpQuat;
  106. tmpQuat.interpolate(_from.asQuatF(), _to.asQuatF(), _factor);
  107. set(tmpQuat);
  108. }
  109. void RotationF::lookAt(const Point3F& _origin, const Point3F& _target, const Point3F& _up)
  110. {
  111. MatrixF mat;
  112. VectorF newForward = _target - _origin;
  113. newForward.normalize();
  114. VectorF up(0.0f, 0.0f, 1.0f);
  115. VectorF axisX;
  116. VectorF axisY = newForward;
  117. VectorF axisZ;
  118. if (_up != VectorF::Zero)
  119. up = _up;
  120. // Validate and normalize input:
  121. F32 lenSq;
  122. lenSq = axisY.lenSquared();
  123. if (lenSq < 0.000001f)
  124. {
  125. //degenerate forward vector
  126. axisY.set(0.0f, 1.0f, 0.0f);
  127. }
  128. else
  129. {
  130. axisY /= mSqrt(lenSq);
  131. }
  132. lenSq = up.lenSquared();
  133. if (lenSq < 0.000001f)
  134. {
  135. //degenerate up vector - too small
  136. up.set(0.0f, 0.0f, 1.0f);
  137. }
  138. else
  139. {
  140. up /= mSqrt(lenSq);
  141. }
  142. if (fabsf(mDot(up, axisY)) > 0.9999f)
  143. {
  144. //degenerate up vector - same as forward
  145. F32 tmp = up.x;
  146. up.x = -up.y;
  147. up.y = up.z;
  148. up.z = tmp;
  149. }
  150. // construct the remaining axes:
  151. mCross(axisY, up, &axisX);
  152. mCross(axisX, axisY, &axisZ);
  153. mat.setColumn(0, axisX);
  154. mat.setColumn(1, axisY);
  155. mat.setColumn(2, axisZ);
  156. set(mat);
  157. }
  158. //========================================================
  159. EulerF RotationF::asEulerF(UnitFormat _format) const
  160. {
  161. if (mRotationType == Euler)
  162. {
  163. if (_format == Degrees)
  164. {
  165. return EulerF(mRadToDeg(x), mRadToDeg(y), mRadToDeg(z));
  166. }
  167. else
  168. {
  169. return EulerF(x, y, z);
  170. }
  171. }
  172. else
  173. {
  174. EulerF returnEuler = asMatrixF().toEuler();
  175. if (_format == Degrees)
  176. {
  177. returnEuler.x = mRadToDeg(returnEuler.x);
  178. returnEuler.y = mRadToDeg(returnEuler.y);
  179. returnEuler.z = mRadToDeg(returnEuler.z);
  180. }
  181. return returnEuler;
  182. }
  183. }
  184. AngAxisF RotationF::asAxisAngle(UnitFormat format) const
  185. {
  186. AngAxisF returnAA;
  187. if (mRotationType == Euler)
  188. {
  189. returnAA.set(EulerF(x, y, z));
  190. }
  191. else
  192. {
  193. returnAA.set(Point3F(x, y, z), w);
  194. }
  195. if (format == Radians)
  196. {
  197. returnAA.angle = mDegToRad(returnAA.angle);
  198. }
  199. return returnAA;
  200. }
  201. MatrixF RotationF::asMatrixF() const
  202. {
  203. MatrixF returnMat;
  204. if (mRotationType == Euler)
  205. {
  206. returnMat.set(EulerF(x, y, z));
  207. }
  208. else
  209. {
  210. AngAxisF aa;
  211. aa.set(Point3F(x, y, z), w);
  212. aa.setMatrix(&returnMat);
  213. }
  214. return returnMat;
  215. }
  216. QuatF RotationF::asQuatF() const
  217. {
  218. QuatF returnQuat;
  219. if (mRotationType == Euler)
  220. {
  221. returnQuat.set(EulerF(x, y, z));
  222. }
  223. else
  224. {
  225. AngAxisF aa;
  226. aa.set(Point3F(x, y, z), w);
  227. returnQuat.set(aa);
  228. }
  229. return returnQuat;
  230. }
  231. void RotationF::normalize()
  232. {
  233. if (mRotationType == Euler)
  234. {
  235. EulerF eul = EulerF(x, y, z);
  236. eul.normalize();
  237. set(eul);
  238. }
  239. else
  240. {
  241. QuatF quat;
  242. quat.set(Point3F(x, y, z), w);
  243. quat.normalize();
  244. set(quat);
  245. }
  246. }
  247. //Testing
  248. #ifdef TORQUE_TESTS_ENABLED
  249. TEST(Maths, RotationF_Calculations)
  250. {
  251. //TODO: implement unit test
  252. };
  253. #endif
  254. DefineConsoleStaticMethod(Rotation, Add, RotationF, (RotationF a, RotationF b), ,
  255. "Adds two rotations together.\n"
  256. "@param a Rotation one."
  257. "@param b Rotation two."
  258. "@returns v sum of both rotations."
  259. "@ingroup Math")
  260. {
  261. return a + b;
  262. }
  263. DefineConsoleStaticMethod(Rotation, Subtract, RotationF, (RotationF a, RotationF b), ,
  264. "Subtracts two rotations.\n"
  265. "@param a Rotation one."
  266. "@param b Rotation two."
  267. "@returns v difference of both rotations."
  268. "@ingroup Math")
  269. {
  270. return a - b;
  271. }
  272. DefineConsoleStaticMethod(Rotation, Interpolate, RotationF, (RotationF a, RotationF b, F32 factor), ,
  273. "Interpolates between two rotations.\n"
  274. "@param a Rotation one."
  275. "@param b Rotation two."
  276. "@param factor The amount to interpolate between the two."
  277. "@returns v, interpolated result."
  278. "@ingroup Math")
  279. {
  280. RotationF result;
  281. result.interpolate(a, b, factor);
  282. return result;
  283. }
  284. DefineConsoleStaticMethod(Rotation, LookAt, RotationF, (Point3F origin, Point3F target, Point3F up),
  285. (Point3F(0, 0, 0), Point3F(0, 0, 0), Point3F(0, 0, 1)),
  286. "Provides a rotation orientation to look at a target from a given position.\n"
  287. "@param origin Position of the object doing the looking."
  288. "@param target Position to be looked at."
  289. "@param up The up angle to orient the rotation."
  290. "@returns v orientation result."
  291. "@ingroup Math")
  292. {
  293. RotationF result;
  294. result.lookAt(origin, target, up);
  295. return result;
  296. }