2
0

QuatTests.cpp 11 KB

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