QuatTests.cpp 8.3 KB

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