mRotation.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. MatrixF imat, xmat, ymat, zmat;
  215. xmat.set(EulerF(x, 0, 0));
  216. ymat.set(EulerF(0.0f, y, 0.0f));
  217. zmat.set(EulerF(0, 0, z));
  218. imat.mul(zmat, xmat);
  219. returnMat.mul(imat, ymat);
  220. }
  221. else
  222. {
  223. AngAxisF aa;
  224. aa.set(Point3F(x, y, z), w);
  225. MatrixF tempMat;
  226. aa.setMatrix(&tempMat);
  227. EulerF eul = tempMat.toEuler();
  228. MatrixF imat, xmat, ymat, zmat;
  229. xmat.set(EulerF(eul.x, 0, 0));
  230. ymat.set(EulerF(0.0f, eul.y, 0.0f));
  231. zmat.set(EulerF(0, 0, eul.z));
  232. imat.mul(zmat, xmat);
  233. returnMat.mul(imat, ymat);
  234. }
  235. return returnMat;
  236. }
  237. QuatF RotationF::asQuatF() const
  238. {
  239. QuatF returnQuat;
  240. if (mRotationType == Euler)
  241. {
  242. returnQuat.set(EulerF(x, y, z));
  243. }
  244. else
  245. {
  246. AngAxisF aa;
  247. aa.set(Point3F(x, y, z), w);
  248. returnQuat.set(aa);
  249. }
  250. return returnQuat;
  251. }
  252. void RotationF::normalize()
  253. {
  254. if (mRotationType == Euler)
  255. {
  256. EulerF eul = EulerF(x, y, z);
  257. eul.normalize();
  258. set(eul);
  259. }
  260. else
  261. {
  262. QuatF quat;
  263. quat.set(Point3F(x, y, z), w);
  264. quat.normalize();
  265. set(quat);
  266. }
  267. }
  268. //Testing
  269. #ifdef TORQUE_TESTS_ENABLED
  270. TEST(Maths, RotationF_Calculations)
  271. {
  272. //TODO: implement unit test
  273. };
  274. #endif
  275. DefineEngineFunction(AddRotation, RotationF, (RotationF a, RotationF b, const char* returnType), ("Euler"),
  276. "Adds two rotations together.\n"
  277. "@param a Rotation one."
  278. "@param b Rotation two."
  279. "@returns v sum of both rotations."
  280. "@ingroup Math")
  281. {
  282. RotationF ret;
  283. RotationF sum = a + b;
  284. if (String(returnType) == String("Euler"))
  285. ret.set(sum.asEulerF());
  286. else
  287. ret.set(sum.asAxisAngle());
  288. return ret;
  289. }
  290. DefineEngineFunction(SubtractRotation, RotationF, (RotationF a, RotationF b), ,
  291. "Subtracts two rotations.\n"
  292. "@param a Rotation one."
  293. "@param b Rotation two."
  294. "@returns v difference of both rotations."
  295. "@ingroup Math")
  296. {
  297. return a - b;
  298. }
  299. DefineEngineFunction(InterpolateRotation, RotationF, (RotationF a, RotationF b, F32 factor), ,
  300. "Interpolates between two rotations.\n"
  301. "@param a Rotation one."
  302. "@param b Rotation two."
  303. "@param factor The amount to interpolate between the two."
  304. "@returns v, interpolated result."
  305. "@ingroup Math")
  306. {
  307. RotationF result;
  308. result.interpolate(a, b, factor);
  309. return result;
  310. }
  311. DefineEngineFunction(RotationLookAt, RotationF, (Point3F origin, Point3F target, Point3F up),
  312. (Point3F(0, 0, 0), Point3F(0, 0, 0), Point3F(0, 0, 1)),
  313. "Provides a rotation orientation to look at a target from a given position.\n"
  314. "@param origin Position of the object doing the looking."
  315. "@param target Position to be looked at."
  316. "@param up The up angle to orient the rotation."
  317. "@returns v orientation result."
  318. "@ingroup Math")
  319. {
  320. RotationF result;
  321. result.lookAt(origin, target, up);
  322. return result;
  323. }
  324. DefineEngineFunction(setRotationRightVector, RotationF, (RotationF rot, VectorF rightVec), ,
  325. "Sets the right vector of the rotation.\n"
  326. "@param Starting rotation."
  327. "@param New up vector."
  328. "@returns New rotation with the set right vector."
  329. "@ingroup Math")
  330. {
  331. rot.asMatrixF().setColumn(0, rightVec);
  332. return rot;
  333. }
  334. DefineEngineFunction(setRotationUpVector, RotationF, (RotationF rot, VectorF upVec), ,
  335. "Sets the up vector of the rotation.\n"
  336. "@param Starting rotation."
  337. "@param New up vector."
  338. "@returns New rotation with the set up vector."
  339. "@ingroup Math")
  340. {
  341. rot.asMatrixF().setColumn(2, upVec);
  342. return rot;
  343. }
  344. DefineEngineFunction(getRotationForwardVector, VectorF, (RotationF rot), ,
  345. "Get the forward vector of a rotation.\n"
  346. "@ingroup Math")
  347. {
  348. return rot.asMatrixF().getForwardVector();
  349. }
  350. DefineEngineFunction(getRotationRightVector, VectorF, (RotationF rot), ,
  351. "Gets the right vector of a rotation.\n"
  352. "@param Our rotation."
  353. "@ingroup Math")
  354. {
  355. return rot.asMatrixF().getRightVector();
  356. }
  357. DefineEngineFunction(getRotationUpVector, VectorF, (RotationF rot), ,
  358. "Gets the up vector of a rotation.\n"
  359. "@param Our rotation."
  360. "@ingroup Math")
  361. {
  362. return rot.asMatrixF().getUpVector();
  363. }
  364. DefineEngineFunction(getRotationDirection, Point3F, (RotationF rot),,
  365. "Gets the direction from the rotation's angles.\n"
  366. "@param Our rotation."
  367. "@ingroup Math")
  368. {
  369. return rot.getDirection();
  370. }