2
0

QuatTests.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include "UnitTestFramework.h"
  4. #include <Jolt/Math/Mat44.h>
  5. #include <Jolt/Math/Quat.h>
  6. #include <random>
  7. TEST_SUITE("QuatTests")
  8. {
  9. TEST_CASE("TestQuatEqual")
  10. {
  11. CHECK(Quat(1, 2, 3, 4) == Quat(1, 2, 3, 4));
  12. CHECK(Quat(1, 2, 3, 4) != Quat(0, 2, 3, 4));
  13. CHECK(Quat(1, 2, 3, 4) != Quat(1, 0, 3, 4));
  14. CHECK(Quat(1, 2, 3, 4) != Quat(1, 2, 0, 4));
  15. CHECK(Quat(1, 2, 3, 4) != Quat(1, 2, 3, 0));
  16. }
  17. TEST_CASE("TestQuatZero")
  18. {
  19. Quat zero = Quat::sZero();
  20. CHECK(zero == Quat(0, 0, 0, 0));
  21. }
  22. TEST_CASE("TestQuatIdentity")
  23. {
  24. Quat identity = Quat::sIdentity();
  25. CHECK_APPROX_EQUAL(identity.GetX(), 0.0f);
  26. CHECK_APPROX_EQUAL(identity.GetY(), 0.0f);
  27. CHECK_APPROX_EQUAL(identity.GetZ(), 0.0f);
  28. CHECK_APPROX_EQUAL(identity.GetW(), 1.0f);
  29. }
  30. TEST_CASE("TestQuatIsNaN")
  31. {
  32. CHECK(Quat(numeric_limits<float>::quiet_NaN(), 0, 0, 0).IsNaN());
  33. CHECK(Quat(0, numeric_limits<float>::quiet_NaN(), 0, 0).IsNaN());
  34. CHECK(Quat(0, 0, numeric_limits<float>::quiet_NaN(), 0).IsNaN());
  35. CHECK(Quat(0, 0, 0, numeric_limits<float>::quiet_NaN()).IsNaN());
  36. }
  37. TEST_CASE("TestQuatOperators")
  38. {
  39. CHECK(-Quat(1, 2, 3, 4) == Quat(-1, -2, -3, -4));
  40. CHECK(Quat(1, 2, 3, 4) + Quat(5, 6, 7, 8) == Quat(6, 8, 10, 12));
  41. CHECK(Quat(5, 6, 7, 8) - Quat(4, 3, 2, 1) == Quat(1, 3, 5, 7));
  42. CHECK(Quat(1, 2, 3, 4) * 5.0f == Quat(5, 10, 15, 20));
  43. CHECK(5.0f * Quat(1, 2, 3, 4) == Quat(5, 10, 15, 20));
  44. CHECK(Quat(2, 4, 6, 8) / 2.0f == Quat(1, 2, 3, 4));
  45. }
  46. TEST_CASE("TestQuatPerpendicular")
  47. {
  48. Quat q1(1, 2, 3, 4);
  49. CHECK(q1.GetPerpendicular().Dot(q1) == 0.0f);
  50. Quat q2(-5, 4, -3, 2);
  51. CHECK(q2.GetPerpendicular().Dot(q2) == 0.0f);
  52. }
  53. TEST_CASE("TestQuatNormalized")
  54. {
  55. CHECK(Quat(1, 0, 0, 0).IsNormalized());
  56. CHECK(Quat(-0.7071067f, 0.7071067f, 0, 0).IsNormalized());
  57. CHECK(Quat(0.5773502f, -0.5773502f, 0.5773502f, 0).IsNormalized());
  58. CHECK(Quat(0.5f, -0.5f, 0.5f, -0.5f).IsNormalized());
  59. CHECK(!Quat(2, 0, 0, 0).IsNormalized());
  60. CHECK(!Quat(0, 2, 0, 0).IsNormalized());
  61. CHECK(!Quat(0, 0, 2, 0).IsNormalized());
  62. CHECK(!Quat(0, 0, 0, 2).IsNormalized());
  63. }
  64. TEST_CASE("TestQuatConvertMatrix")
  65. {
  66. UnitTestRandom random;
  67. uniform_real_distribution<float> zero_to_two_pi(0.0f, 2.0f * JPH_PI);
  68. for (int i = 0; i < 1000; ++i)
  69. {
  70. Vec3 axis = Vec3::sRandom(random);
  71. float angle = zero_to_two_pi(random);
  72. Mat44 m1 = Mat44::sRotation(axis, angle);
  73. Quat q1 = m1.GetQuaternion();
  74. Quat q2 = Quat::sRotation(axis, angle);
  75. CHECK_APPROX_EQUAL(q1, q2);
  76. Mat44 m2 = Mat44::sRotation(q2);
  77. CHECK_APPROX_EQUAL(m1, m2);
  78. }
  79. }
  80. TEST_CASE("TestQuatMultiplyVec3")
  81. {
  82. UnitTestRandom random;
  83. uniform_real_distribution<float> zero_to_two_pi(0.0f, 2.0f * JPH_PI);
  84. for (int i = 0; i < 1000; ++i)
  85. {
  86. Vec3 axis = Vec3::sRandom(random);
  87. float angle = zero_to_two_pi(random);
  88. Mat44 m1 = Mat44::sRotation(axis, angle);
  89. Quat q1 = Quat::sRotation(axis, angle);
  90. Vec3 rv = 10.0f * Vec3::sRandom(random);
  91. Vec3 r1 = m1 * rv;
  92. Vec3 r2 = q1 * rv;
  93. CHECK_APPROX_EQUAL(r1, r2, 1.0e-5f);
  94. }
  95. }
  96. TEST_CASE("TestQuatRotateAxisXYZ")
  97. {
  98. UnitTestRandom random;
  99. uniform_real_distribution<float> zero_to_two_pi(0.0f, 2.0f * JPH_PI);
  100. for (int i = 0; i < 1000; ++i)
  101. {
  102. Vec3 axis = Vec3::sRandom(random);
  103. float angle = zero_to_two_pi(random);
  104. Quat q1 = Quat::sRotation(axis, angle);
  105. Vec3 r1 = q1 * Vec3::sAxisX();
  106. Vec3 r2 = q1.RotateAxisX();
  107. CHECK_APPROX_EQUAL(r1, r2, 1.0e-5f);
  108. r1 = q1 * Vec3::sAxisY();
  109. r2 = q1.RotateAxisY();
  110. CHECK_APPROX_EQUAL(r1, r2, 1.0e-5f);
  111. r1 = q1 * Vec3::sAxisZ();
  112. r2 = q1.RotateAxisZ();
  113. CHECK_APPROX_EQUAL(r1, r2, 1.0e-5f);
  114. }
  115. }
  116. TEST_CASE("TestQuatMultiplyQuat")
  117. {
  118. {
  119. // We use a right handed system, so test that: i * j = k
  120. Quat r1 = Quat(1, 0, 0, 0) * Quat(0, 1, 0, 0);
  121. Quat r2 = Quat(0, 0, 1, 0);
  122. CHECK(r1.IsClose(r2));
  123. }
  124. {
  125. // Test: j * i = -k
  126. Quat r1 = Quat(0, 1, 0, 0) * Quat(1, 0, 0, 0);
  127. Quat r2 = Quat(0, 0, -1, 0);
  128. CHECK(r1.IsClose(r2));
  129. }
  130. {
  131. // Test predefined multiplication
  132. Quat r1 = Quat(2, 3, 4, 1) * Quat(6, 7, 8, 5);
  133. Quat r2 = Quat(12, 30, 24, -60);
  134. CHECK(r1.IsClose(r2));
  135. }
  136. // Compare random matrix multiplications with quaternion multiplications
  137. UnitTestRandom random;
  138. uniform_real_distribution<float> zero_to_two_pi(0.0f, 2.0f * JPH_PI);
  139. for (int i = 0; i < 1000; ++i)
  140. {
  141. Vec3 axis1 = Vec3::sRandom(random);
  142. float angle1 = zero_to_two_pi(random);
  143. Quat q1 = Quat::sRotation(axis1, angle1);
  144. Mat44 m1 = Mat44::sRotation(axis1, angle1);
  145. Vec3 axis2 = Vec3::sRandom(random);
  146. float angle2 = zero_to_two_pi(random);
  147. Quat q2 = Quat::sRotation(axis2, angle2);
  148. Mat44 m2 = Mat44::sRotation(axis2, angle2);
  149. Quat r1 = q1 * q2;
  150. Quat r2 = (m1 * m2).GetQuaternion();
  151. CHECK_APPROX_EQUAL(r1, r2);
  152. }
  153. }
  154. TEST_CASE("TestQuatRotationAxisAngle")
  155. {
  156. Mat44 r1 = Mat44::sRotation(Vec3(1, 0, 0), 0.1f * JPH_PI);
  157. Mat44 r2 = Mat44::sRotation(Quat::sRotation(Vec3(1, 0, 0), 0.1f * JPH_PI));
  158. CHECK_APPROX_EQUAL(r1, r2);
  159. r1 = Mat44::sRotation(Vec3(0, 1, 0), 0.2f * JPH_PI);
  160. r2 = Mat44::sRotation(Quat::sRotation(Vec3(0, 1, 0), 0.2f * JPH_PI));
  161. CHECK_APPROX_EQUAL(r1, r2);
  162. r1 = Mat44::sRotation(Vec3(0, 0, 1), 0.3f * JPH_PI);
  163. r2 = Mat44::sRotation(Quat::sRotation(Vec3(0, 0, 1), 0.3f * JPH_PI));
  164. CHECK_APPROX_EQUAL(r1, r2);
  165. }
  166. TEST_CASE("TestQuatGetAxisAngle")
  167. {
  168. // Test identity rotation
  169. {
  170. Vec3 axis;
  171. float angle;
  172. Quat::sIdentity().GetAxisAngle(axis, angle);
  173. CHECK_APPROX_EQUAL(Vec3::sZero(), axis);
  174. CHECK_APPROX_EQUAL(0.0f, angle);
  175. }
  176. {
  177. Vec3 axis;
  178. float angle;
  179. (-Quat::sIdentity()).GetAxisAngle(axis, angle);
  180. CHECK_APPROX_EQUAL(Vec3::sZero(), axis);
  181. CHECK_APPROX_EQUAL(0.0f, angle);
  182. }
  183. // Test positive rotation
  184. Quat q1 = Quat::sRotation(Vec3(0, 1, 0), 0.2f * JPH_PI);
  185. {
  186. Vec3 axis;
  187. float angle;
  188. q1.GetAxisAngle(axis, angle);
  189. CHECK_APPROX_EQUAL(Vec3(0, 1, 0), axis);
  190. CHECK_APPROX_EQUAL(0.2f * JPH_PI, angle, 1.0e-5f);
  191. }
  192. {
  193. Vec3 axis;
  194. float angle;
  195. (-q1).GetAxisAngle(axis, angle);
  196. CHECK_APPROX_EQUAL(Vec3(0, 1, 0), axis);
  197. CHECK_APPROX_EQUAL(0.2f * JPH_PI, angle, 1.0e-5f);
  198. }
  199. // Test negative rotation
  200. Quat q2 = Quat::sRotation(Vec3(0, 1, 0), -0.2f * JPH_PI);
  201. {
  202. Vec3 axis;
  203. float angle;
  204. q2.GetAxisAngle(axis, angle);
  205. CHECK_APPROX_EQUAL(Vec3(0, -1, 0), axis);
  206. CHECK_APPROX_EQUAL(0.2f * JPH_PI, angle, 1.0e-5f);
  207. }
  208. {
  209. Vec3 axis;
  210. float angle;
  211. (-q2).GetAxisAngle(axis, angle);
  212. CHECK_APPROX_EQUAL(Vec3(0, -1, 0), axis);
  213. CHECK_APPROX_EQUAL(0.2f * JPH_PI, angle, 1.0e-5f);
  214. }
  215. // Test keeping range between [0, PI]
  216. Quat q3 = Quat::sRotation(Vec3(0, 1, 0), 1.1f * JPH_PI);
  217. {
  218. Vec3 axis;
  219. float angle;
  220. q3.GetAxisAngle(axis, angle);
  221. CHECK_APPROX_EQUAL(Vec3(0, -1, 0), axis);
  222. CHECK_APPROX_EQUAL(0.9f * JPH_PI, angle, 1.0e-5f);
  223. }
  224. {
  225. Vec3 axis;
  226. float angle;
  227. (-q3).GetAxisAngle(axis, angle);
  228. CHECK_APPROX_EQUAL(Vec3(0, -1, 0), axis);
  229. CHECK_APPROX_EQUAL(0.9f * JPH_PI, angle, 1.0e-5f);
  230. }
  231. }
  232. TEST_CASE("TestQuatInverse")
  233. {
  234. UnitTestRandom random;
  235. uniform_real_distribution<float> zero_to_two_pi(0.0f, 2.0f * JPH_PI);
  236. for (int i = 0; i < 1000; ++i)
  237. {
  238. Vec3 axis = Vec3::sRandom(random);
  239. float angle = zero_to_two_pi(random);
  240. Quat q1 = Quat::sRotation(axis, angle);
  241. Quat q2 = q1.Inversed();
  242. CHECK_APPROX_EQUAL(Quat::sIdentity(), q1 * q2);
  243. }
  244. }
  245. TEST_CASE("TestQuatConjugate")
  246. {
  247. CHECK(Quat(1, 2, 3, 4).Conjugated() == Quat(-1, -2, -3, 4));
  248. CHECK(Quat(-1, -2, -3, -4).Conjugated() == Quat(1, 2, 3, -4));
  249. }
  250. TEST_CASE("TestQuatEnsureWPositive")
  251. {
  252. CHECK(Quat(1, -2, 3, -4).EnsureWPositive() == Quat(-1, 2, -3, 4));
  253. CHECK(Quat(-4, 5, -6, 7).EnsureWPositive() == Quat(-4, 5, -6, 7));
  254. CHECK(Quat(1, 2, 3, 0).EnsureWPositive() == Quat(1, 2, 3, 0));
  255. }
  256. TEST_CASE("TestQuatStoreFloat3")
  257. {
  258. Float3 q1;
  259. Quat(0.7071067f, 0, 0, -0.7071067f).StoreFloat3(&q1);
  260. CHECK(q1 == Float3(-0.7071067f, 0, 0));
  261. Float3 q2;
  262. Quat(0, 0.7071067f, 0, 0.7071067f).StoreFloat3(&q2);
  263. CHECK(q2 == Float3(0, 0.7071067f, 0));
  264. Float3 q3;
  265. Quat(0, 0, 1, 0).StoreFloat3(&q3);
  266. CHECK(q3 == Float3(0, 0, 1));
  267. }
  268. TEST_CASE("TestQuatGetTwistAxis")
  269. {
  270. Quat q1 = Quat::sRotation(Vec3::sAxisX(), DegreesToRadians(-10.0f));
  271. Quat q2 = Quat::sRotation(Vec3::sAxisY(), DegreesToRadians(20.0f));
  272. Quat q = q1 * q2;
  273. Quat twist1 = q.GetTwist(Vec3::sAxisX());
  274. CHECK_APPROX_EQUAL(twist1, q1);
  275. Quat swing1 = twist1.Inversed() * q;
  276. CHECK_APPROX_EQUAL(swing1, q2);
  277. Quat twist2 = swing1.GetTwist(Vec3::sAxisY());
  278. CHECK_APPROX_EQUAL(twist2, q2);
  279. Quat swing2 = twist2.Inversed() * swing1;
  280. CHECK_APPROX_EQUAL(swing2, Quat::sIdentity());
  281. }
  282. TEST_CASE("TestQuatGetRotationAngle")
  283. {
  284. Quat q1 = Quat::sRotation(Vec3::sAxisX(), DegreesToRadians(-10.0f));
  285. Quat q2 = Quat::sRotation(Vec3::sAxisY(), DegreesToRadians(20.0f));
  286. Quat q3 = Quat::sRotation(Vec3::sAxisZ(), DegreesToRadians(-95.0f));
  287. float a = q1.GetRotationAngle(Vec3::sAxisX());
  288. CHECK_APPROX_EQUAL(a, DegreesToRadians(-10.0f), 1.0e-5f);
  289. a = q2.GetRotationAngle(Vec3::sAxisY());
  290. CHECK_APPROX_EQUAL(a, DegreesToRadians(20.0f), 1.0e-5f);
  291. a = q3.GetRotationAngle(Vec3::sAxisZ());
  292. CHECK_APPROX_EQUAL(a, DegreesToRadians(-95.0f), 1.0e-5f);
  293. a = (q1 * q2).GetRotationAngle(Vec3::sAxisX());
  294. CHECK_APPROX_EQUAL(a, DegreesToRadians(-10.0f), 1.0e-5f);
  295. a = (q3 * q1).GetRotationAngle(Vec3::sAxisX());
  296. CHECK_APPROX_EQUAL(a, DegreesToRadians(-10.0f), 1.0e-5f);
  297. }
  298. TEST_CASE("TestQuatGetEulerAngles")
  299. {
  300. Vec3 input(DegreesToRadians(-10.0f), DegreesToRadians(20.0f), DegreesToRadians(-95.0f));
  301. Quat qx = Quat::sRotation(Vec3::sAxisX(), input.GetX());
  302. Quat qy = Quat::sRotation(Vec3::sAxisY(), input.GetY());
  303. Quat qz = Quat::sRotation(Vec3::sAxisZ(), input.GetZ());
  304. Quat q = qz * qy * qx;
  305. Quat q2 = Quat::sEulerAngles(input);
  306. CHECK_APPROX_EQUAL(q, q2);
  307. Vec3 angles = q2.GetEulerAngles();
  308. CHECK_APPROX_EQUAL(angles, input);
  309. }
  310. TEST_CASE("TestQuatRotationFromTo")
  311. {
  312. {
  313. Vec3 v1(10, 0, 0);
  314. Vec3 v2(20, 0, 0);
  315. Quat q = Quat::sFromTo(v1, v2);
  316. CHECK_APPROX_EQUAL(q, Quat::sIdentity());
  317. }
  318. {
  319. Vec3 v1(10, 0, 0);
  320. Vec3 v2(0, 20, 0);
  321. Quat q = Quat::sFromTo(v1, v2);
  322. CHECK_APPROX_EQUAL(v2.Normalized(), (q * v1).Normalized());
  323. }
  324. {
  325. Vec3 v1(10, 0, 0);
  326. Vec3 v2(-20, 0, 0);
  327. Quat q = Quat::sFromTo(v1, v2);
  328. CHECK_APPROX_EQUAL(v2.Normalized(), (q * v1).Normalized());
  329. }
  330. }
  331. TEST_CASE("TestQuatRotationFromToRandom")
  332. {
  333. UnitTestRandom random;
  334. uniform_real_distribution<float> one_to_ten(1.0f, 10.0f);
  335. for (int i = 0; i < 1000; ++i)
  336. {
  337. Vec3 v1 = one_to_ten(random) * Vec3::sRandom(random);
  338. Vec3 v2 = one_to_ten(random) * Vec3::sRandom(random);
  339. Quat q = Quat::sFromTo(v1, v2);
  340. Vec3 v1t = (q * v1).Normalized();
  341. Vec3 v2t = v2.Normalized();
  342. CHECK_APPROX_EQUAL(v2t, v1t, 1.0e-5f);
  343. }
  344. }
  345. }