mRotation.cpp 8.3 KB

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