QuatTests.cpp 13 KB

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