QuatTests.cpp 12 KB

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