Mat44Tests.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. TEST_SUITE("Mat44Tests")
  7. {
  8. TEST_CASE("TestMat44Identity")
  9. {
  10. Mat44 identity = Mat44::sIdentity();
  11. for (int row = 0; row < 4; ++row)
  12. for (int col = 0; col < 4; ++col)
  13. if (row != col)
  14. CHECK(identity(row, col) == 0.0f);
  15. else
  16. CHECK(identity(row, col) == 1.0f);
  17. }
  18. TEST_CASE("TestMat44Construct")
  19. {
  20. Mat44 mat(Vec4(1, 2, 3, 4), Vec4(5, 6, 7, 8), Vec4(9, 10, 11, 12), Vec4(13, 14, 15, 16));
  21. CHECK(mat(0, 0) == 1.0f);
  22. CHECK(mat(1, 0) == 2.0f);
  23. CHECK(mat(2, 0) == 3.0f);
  24. CHECK(mat(3, 0) == 4.0f);
  25. CHECK(mat(0, 1) == 5.0f);
  26. CHECK(mat(1, 1) == 6.0f);
  27. CHECK(mat(2, 1) == 7.0f);
  28. CHECK(mat(3, 1) == 8.0f);
  29. CHECK(mat(0, 2) == 9.0f);
  30. CHECK(mat(1, 2) == 10.0f);
  31. CHECK(mat(2, 2) == 11.0f);
  32. CHECK(mat(3, 2) == 12.0f);
  33. CHECK(mat(0, 3) == 13.0f);
  34. CHECK(mat(1, 3) == 14.0f);
  35. CHECK(mat(2, 3) == 15.0f);
  36. CHECK(mat(3, 3) == 16.0f);
  37. Mat44 mat2(mat);
  38. CHECK(mat2(0, 0) == 1.0f);
  39. CHECK(mat2(1, 0) == 2.0f);
  40. CHECK(mat2(2, 0) == 3.0f);
  41. CHECK(mat2(3, 0) == 4.0f);
  42. CHECK(mat2(0, 1) == 5.0f);
  43. CHECK(mat2(1, 1) == 6.0f);
  44. CHECK(mat2(2, 1) == 7.0f);
  45. CHECK(mat2(3, 1) == 8.0f);
  46. CHECK(mat2(0, 2) == 9.0f);
  47. CHECK(mat2(1, 2) == 10.0f);
  48. CHECK(mat2(2, 2) == 11.0f);
  49. CHECK(mat2(3, 2) == 12.0f);
  50. CHECK(mat2(0, 3) == 13.0f);
  51. CHECK(mat2(1, 3) == 14.0f);
  52. CHECK(mat2(2, 3) == 15.0f);
  53. CHECK(mat2(3, 3) == 16.0f);
  54. }
  55. TEST_CASE("TestMat44Translation")
  56. {
  57. CHECK(Mat44::sTranslation(Vec3(2, 3, 4)) == Mat44(Vec4(1, 0, 0, 0), Vec4(0, 1, 0, 0), Vec4(0, 0, 1, 0), Vec4(2, 3, 4, 1)));
  58. }
  59. TEST_CASE("TestMat44Scale")
  60. {
  61. CHECK(Mat44::sScale(2) == Mat44(Vec4(2, 0, 0, 0), Vec4(0, 2, 0, 0), Vec4(0, 0, 2, 0), Vec4(0, 0, 0, 1)));
  62. CHECK(Mat44::sScale(Vec3(2, 3, 4)) == Mat44(Vec4(2, 0, 0, 0), Vec4(0, 3, 0, 0), Vec4(0, 0, 4, 0), Vec4(0, 0, 0, 1)));
  63. }
  64. TEST_CASE("TestMat44RotationSafe")
  65. {
  66. Mat44 mat(Vec4(1, 2, 3, 4), Vec4(5, 6, 7, 8), Vec4(9, 10, 11, 12), Vec4(13, 14, 15, 16));
  67. CHECK(mat.GetRotationSafe() == Mat44(Vec4(1, 2, 3, 0), Vec4(5, 6, 7, 0), Vec4(9, 10, 11, 0), Vec4(0, 0, 0, 1)));
  68. }
  69. TEST_CASE("TestMat44LoadStore")
  70. {
  71. Mat44 mat(Vec4(1, 2, 3, 4), Vec4(5, 6, 7, 8), Vec4(9, 10, 11, 12), Vec4(13, 14, 15, 16));
  72. Float4 storage[4];
  73. mat.StoreFloat4x4(storage);
  74. CHECK(storage[0].x == 1.0f);
  75. CHECK(storage[0].y == 2.0f);
  76. CHECK(storage[0].z == 3.0f);
  77. CHECK(storage[0].w == 4.0f);
  78. CHECK(storage[1].x == 5.0f);
  79. CHECK(storage[1].y == 6.0f);
  80. CHECK(storage[1].z == 7.0f);
  81. CHECK(storage[1].w == 8.0f);
  82. CHECK(storage[2].x == 9.0f);
  83. CHECK(storage[2].y == 10.0f);
  84. CHECK(storage[2].z == 11.0f);
  85. CHECK(storage[2].w == 12.0f);
  86. CHECK(storage[3].x == 13.0f);
  87. CHECK(storage[3].y == 14.0f);
  88. CHECK(storage[3].z == 15.0f);
  89. CHECK(storage[3].w == 16.0f);
  90. Mat44 mat2 = Mat44::sLoadFloat4x4(storage);
  91. CHECK(mat2 == mat);
  92. }
  93. TEST_CASE("TestMat44MultiplyMat44")
  94. {
  95. Mat44 mat(Vec4(1, 2, 3, 4), Vec4(5, 6, 7, 8), Vec4(9, 10, 11, 12), Vec4(13, 14, 15, 16));
  96. Mat44 mat2(Vec4(17, 18, 19, 20), Vec4(21, 22, 23, 24), Vec4(25, 26, 27, 28), Vec4(29, 30, 31, 32));
  97. Mat44 result = mat * mat2;
  98. CHECK(result == Mat44(Vec4(538, 612, 686, 760), Vec4(650, 740, 830, 920), Vec4(762, 868, 974, 1080), Vec4(874, 996, 1118, 1240)));
  99. }
  100. TEST_CASE("TestMat44MultiplyVec3")
  101. {
  102. Mat44 mat(Vec4(1, 2, 3, 4), Vec4(5, 6, 7, 8), Vec4(9, 10, 11, 12), Vec4(13, 14, 15, 16));
  103. Vec3 vec(17, 18, 19);
  104. Vec3 result = mat * vec;
  105. CHECK(result == Vec3(291, 346, 401));
  106. result = mat.Multiply3x3(vec);
  107. CHECK(result == Vec3(278, 332, 386));
  108. result = mat.Multiply3x3Transposed(vec);
  109. CHECK(result == Vec3(110, 326, 542));
  110. }
  111. TEST_CASE("TestMat44MultiplyVec4")
  112. {
  113. Mat44 mat(Vec4(1, 2, 3, 4), Vec4(5, 6, 7, 8), Vec4(9, 10, 11, 12), Vec4(13, 14, 15, 16));
  114. Vec4 vec(17, 18, 19, 20);
  115. Vec4 result = mat * vec;
  116. CHECK(result == Vec4(538, 612, 686, 760));
  117. }
  118. TEST_CASE("TestMat44Scale")
  119. {
  120. Mat44 mat(Vec4(1, 2, 3, 4), Vec4(5, 6, 7, 8), Vec4(9, 10, 11, 12), Vec4(13, 14, 15, 16));
  121. Mat44 result = mat * 2.0f;
  122. CHECK(result == Mat44(Vec4(2, 4, 6, 8), Vec4(10, 12, 14, 16), Vec4(18, 20, 22, 24), Vec4(26, 28, 30, 32)));
  123. CHECK(result != mat);
  124. result *= 0.5f;
  125. CHECK(result == mat);
  126. }
  127. TEST_CASE("TestMat44Transposed")
  128. {
  129. Mat44 mat(Vec4(1, 2, 3, 4), Vec4(5, 6, 7, 8), Vec4(9, 10, 11, 12), Vec4(13, 14, 15, 16));
  130. Mat44 result = mat.Transposed();
  131. CHECK(result == Mat44(Vec4(1, 5, 9, 13), Vec4(2, 6, 10, 14), Vec4(3, 7, 11, 15), Vec4(4, 8, 12, 16)));
  132. }
  133. TEST_CASE("TestMat44Transposed3x3")
  134. {
  135. Mat44 mat(Vec4(1, 2, 3, 4), Vec4(5, 6, 7, 8), Vec4(9, 10, 11, 12), Vec4(13, 14, 15, 16));
  136. Mat44 result = mat.Transposed3x3();
  137. CHECK(result == Mat44(Vec4(1, 5, 9, 0), Vec4(2, 6, 10, 0), Vec4(3, 7, 11, 0), Vec4(0, 0, 0, 1)));
  138. }
  139. TEST_CASE("TestMat44Multiply3x3")
  140. {
  141. Mat44 mat1(Vec4(1, 2, 3, 0), Vec4(4, 5, 6, 0), Vec4(7, 8, 9, 0), Vec4(10, 11, 12, 1));
  142. Mat44 mat2(Vec4(13, 14, 15, 0), Vec4(16, 17, 18, 0), Vec4(19, 20, 21, 0), Vec4(22, 23, 24, 1));
  143. Mat44 result = mat1.Multiply3x3(mat2);
  144. CHECK(result.GetColumn4(3) == Vec4(0, 0, 0, 1));
  145. Mat44 result2 = mat1.GetRotationSafe() * mat2.GetRotationSafe();
  146. CHECK(result == result2);
  147. }
  148. TEST_CASE("TestMat44Multiply3x3LeftTransposed")
  149. {
  150. Mat44 mat1(Vec4(1, 2, 3, 4), Vec4(5, 6, 7, 8), Vec4(9, 10, 11, 12), Vec4(13, 14, 15, 16));
  151. Mat44 mat2(Vec4(17, 18, 19, 20), Vec4(21, 22, 23, 24), Vec4(25, 26, 27, 28), Vec4(29, 30, 31, 32));
  152. Mat44 result = mat1.Multiply3x3LeftTransposed(mat2);
  153. CHECK(result.GetColumn4(3) == Vec4(0, 0, 0, 1));
  154. Mat44 result2 = mat1.GetRotationSafe().Transposed() * mat2.GetRotationSafe();
  155. CHECK(result == result2);
  156. }
  157. TEST_CASE("TestMat44Multiply3x3RightTransposed")
  158. {
  159. Mat44 mat1(Vec4(1, 2, 3, 0), Vec4(4, 5, 6, 0), Vec4(7, 8, 9, 0), Vec4(10, 11, 12, 1));
  160. Mat44 mat2(Vec4(13, 14, 15, 0), Vec4(16, 17, 18, 0), Vec4(19, 20, 21, 0), Vec4(22, 23, 24, 1));
  161. Mat44 result = mat1.Multiply3x3RightTransposed(mat2);
  162. CHECK(result.GetColumn4(3) == Vec4(0, 0, 0, 1));
  163. Mat44 result2 = mat1.GetRotationSafe() * mat2.GetRotationSafe().Transposed();
  164. CHECK(result == result2);
  165. }
  166. TEST_CASE("TestMat44Inversed")
  167. {
  168. Mat44 mat(Vec4(0, 2, 0, 8), Vec4(4, 0, 16, 0), Vec4(0, 16, 0, 4), Vec4(8, 0, 2, 0));
  169. Mat44 inverse = mat.Inversed();
  170. Mat44 identity = mat * inverse;
  171. CHECK(identity == Mat44::sIdentity());
  172. }
  173. TEST_CASE("TestMat44Inversed3x3")
  174. {
  175. Mat44 mat(Vec4(1, 2, 0, 0), Vec4(4, 0, 8, 0), Vec4(0, 16, 0, 0), Vec4(1, 2, 3, 1));
  176. Mat44 inverse = mat.Inversed3x3();
  177. CHECK(inverse.GetColumn4(3) == Vec4(0, 0, 0, 1));
  178. Mat44 identity = mat.Multiply3x3(inverse);
  179. CHECK(identity == Mat44::sIdentity());
  180. }
  181. TEST_CASE("TestMat44SetInversed3x3")
  182. {
  183. Mat44 mat(Vec4(1, 2, 0, 0), Vec4(4, 0, 8, 0), Vec4(0, 16, 0, 0), Vec4(1, 2, 3, 1));
  184. // First test succeeding inverse
  185. Mat44 inverse;
  186. CHECK(inverse.SetInversed3x3(mat));
  187. CHECK(inverse.GetColumn4(3) == Vec4(0, 0, 0, 1));
  188. Mat44 identity = mat.Multiply3x3(inverse);
  189. CHECK(identity == Mat44::sIdentity());
  190. // Now make singular
  191. mat(0, 0) = 0.0f;
  192. CHECK(!inverse.SetInversed3x3(mat));
  193. }
  194. TEST_CASE("TestMat44GetDeterminant3x3")
  195. {
  196. Mat44 mat(Vec4(1, 2, 0, 0), Vec4(4, 0, 8, 0), Vec4(0, 16, 0, 0), Vec4(1, 2, 3, 1));
  197. CHECK(mat.GetDeterminant3x3() == -128);
  198. }
  199. TEST_CASE("TestMat44Adjointed3x3")
  200. {
  201. Mat44 mat(Vec4(1, 2, 3, 0), Vec4(5, 6, 7, 0), Vec4(9, 10, 11, 0), Vec4(13, 14, 15, 16));
  202. Mat44 result = mat.Adjointed3x3();
  203. CHECK(result == Mat44(Vec4(-4, 8, -4, 0), Vec4(8, -16, 8, 0), Vec4(-4, 8, -4, 0), Vec4(0, 0, 0, 1)));
  204. }
  205. TEST_CASE("TestMat44RotationXYZ")
  206. {
  207. Mat44 rot = Mat44::sRotationX(0.5f * JPH_PI);
  208. Vec3 v = rot * Vec3(1, 0, 0);
  209. CHECK(v == Vec3(1, 0, 0));
  210. v = rot * Vec3(0, 1, 0);
  211. CHECK_APPROX_EQUAL(v, Vec3(0, 0, 1));
  212. v = rot * Vec3(0, 0, 1);
  213. CHECK_APPROX_EQUAL(v, Vec3(0, -1, 0));
  214. rot = Mat44::sRotationY(0.5f * JPH_PI);
  215. v = rot * Vec3(1, 0, 0);
  216. CHECK_APPROX_EQUAL(v, Vec3(0, 0, -1));
  217. v = rot * Vec3(0, 1, 0);
  218. CHECK(v == Vec3(0, 1, 0));
  219. v = rot * Vec3(0, 0, 1);
  220. CHECK_APPROX_EQUAL(v, Vec3(1, 0, 0));
  221. rot = Mat44::sRotationZ(0.5f * JPH_PI);
  222. v = rot * Vec3(1, 0, 0);
  223. CHECK_APPROX_EQUAL(v, Vec3(0, 1, 0));
  224. v = rot * Vec3(0, 1, 0);
  225. CHECK_APPROX_EQUAL(v, Vec3(-1, 0, 0));
  226. v = rot * Vec3(0, 0, 1);
  227. CHECK(v == Vec3(0, 0, 1));
  228. }
  229. TEST_CASE("TestMat44RotationAxisAngle")
  230. {
  231. Mat44 r1 = Mat44::sRotationX(0.1f * JPH_PI);
  232. Mat44 r2 = Mat44::sRotation(Vec3(1, 0, 0), 0.1f * JPH_PI);
  233. CHECK_APPROX_EQUAL(r1, r2);
  234. r1 = Mat44::sRotationY(0.2f * JPH_PI);
  235. r2 = Mat44::sRotation(Vec3(0, 1, 0), 0.2f * JPH_PI);
  236. CHECK_APPROX_EQUAL(r1, r2);
  237. r1 = Mat44::sRotationZ(0.3f * JPH_PI);
  238. r2 = Mat44::sRotation(Vec3(0, 0, 1), 0.3f * JPH_PI);
  239. CHECK_APPROX_EQUAL(r1, r2);
  240. }
  241. TEST_CASE("TestMat44CrossProduct")
  242. {
  243. Vec3 v1(1, 2, 3);
  244. Vec3 v2(4, 5, 6);
  245. Vec3 v3 = v1.Cross(v2);
  246. Vec3 v4 = Mat44::sCrossProduct(v1) * v2;
  247. CHECK(v3 == v4);
  248. }
  249. TEST_CASE("TestMat44OuterProduct")
  250. {
  251. Vec3 v1(1, 2, 3);
  252. Vec3 v2(4, 5, 6);
  253. CHECK(Mat44::sOuterProduct(v1, v2) == Mat44(Vec4(1 * 4, 2 * 4, 3 * 4, 0), Vec4(1 * 5, 2 * 5, 3 * 5, 0), Vec4(1 * 6, 2 * 6, 3 * 6, 0), Vec4(0, 0, 0, 1)));
  254. }
  255. TEST_CASE("TestMat44QuatLeftMultiply")
  256. {
  257. Quat p(2, 3, 4, 1);
  258. Quat q(6, 7, 8, 5);
  259. Quat r1 = p * q;
  260. Quat r2 = Quat(Mat44::sQuatLeftMultiply(p) * q.GetXYZW());
  261. CHECK(r1 == r2);
  262. }
  263. TEST_CASE("TestMat44QuatRightMultiply")
  264. {
  265. Quat p(2, 3, 4, 1);
  266. Quat q(6, 7, 8, 5);
  267. Quat r1 = q * p;
  268. Quat r2 = Quat(Mat44::sQuatRightMultiply(p) * q.GetXYZW());
  269. CHECK(r1 == r2);
  270. }
  271. TEST_CASE("TestMat44InverseRotateTranslate")
  272. {
  273. Quat rot = Quat::sRotation(Vec3(0, 1, 0), 0.2f * JPH_PI);
  274. Vec3 pos(2, 3, 4);
  275. Mat44 m1 = Mat44::sRotationTranslation(rot, pos).Inversed();
  276. Mat44 m2 = Mat44::sInverseRotationTranslation(rot, pos);
  277. CHECK_APPROX_EQUAL(m1, m2);
  278. }
  279. TEST_CASE("TestMat44InversedRotationTranslation")
  280. {
  281. Quat rot = Quat::sRotation(Vec3(0, 1, 0), 0.2f * JPH_PI);
  282. Vec3 pos(2, 3, 4);
  283. Mat44 m1 = Mat44::sRotationTranslation(rot, pos).InversedRotationTranslation();
  284. Mat44 m2 = Mat44::sInverseRotationTranslation(rot, pos);
  285. CHECK_APPROX_EQUAL(m1, m2);
  286. }
  287. TEST_CASE("TestMat44PrePostScaled")
  288. {
  289. Mat44 m(Vec4(2, 3, 4, 0), Vec4(5, 6, 7, 0), Vec4(8, 9, 10, 0), Vec4(11, 12, 13, 1));
  290. Vec3 v(14, 15, 16);
  291. CHECK(m.PreScaled(v) == m * Mat44::sScale(v));
  292. CHECK(m.PostScaled(v) == Mat44::sScale(v) * m);
  293. }
  294. TEST_CASE("TestMat44PrePostTranslated")
  295. {
  296. Mat44 m(Vec4(2, 3, 4, 0), Vec4(5, 6, 7, 0), Vec4(8, 9, 10, 0), Vec4(11, 12, 13, 1));
  297. Vec3 v(14, 15, 16);
  298. CHECK(m.PreTranslated(v) == m * Mat44::sTranslation(v));
  299. CHECK(m.PostTranslated(v) == Mat44::sTranslation(v) * m);
  300. }
  301. TEST_CASE("TestMat44Decompose")
  302. {
  303. // Create a rotation/translation matrix
  304. Quat rot = Quat::sRotation(Vec3(1, 1, 1).Normalized(), 0.2f * JPH_PI);
  305. Vec3 pos(2, 3, 4);
  306. Mat44 rotation_translation = Mat44::sRotationTranslation(rot, pos);
  307. // Scale the matrix
  308. Vec3 scale(2, 1, 3);
  309. Mat44 m1 = rotation_translation * Mat44::sScale(scale);
  310. // Decompose scale
  311. Vec3 scale_out;
  312. Mat44 m2 = m1.Decompose(scale_out);
  313. // Check individual components
  314. CHECK_APPROX_EQUAL(rotation_translation, m2);
  315. CHECK_APPROX_EQUAL(scale, scale_out);
  316. }
  317. TEST_CASE("TestMat44DecomposeSkewed")
  318. {
  319. // Create a rotation/translation matrix
  320. Quat rot = Quat::sRotation(Vec3(1, 1, 1).Normalized(), 0.2f * JPH_PI);
  321. Vec3 pos(2, 3, 4);
  322. Mat44 rotation_translation = Mat44::sRotationTranslation(rot, pos);
  323. // Skew the matrix by applying a non-uniform scale
  324. Mat44 skewed_rotation_translation = Mat44::sScale(Vec3(1.0f, 0.99f, 0.98f)) * rotation_translation;
  325. float val = skewed_rotation_translation.GetAxisX().Cross(skewed_rotation_translation.GetAxisY()).Dot(skewed_rotation_translation.GetAxisZ());
  326. CHECK(abs(val - 1.0f) > 0.01f); // Check not matrix is no longer perpendicular
  327. // Scale the matrix
  328. Vec3 scale(2, 1, 3);
  329. Mat44 m1 = skewed_rotation_translation * Mat44::sScale(scale);
  330. // Decompose scale
  331. Vec3 scale_out;
  332. Mat44 m2 = m1.Decompose(scale_out);
  333. // Check individual components
  334. CHECK_APPROX_EQUAL(m2.GetAxisX(), skewed_rotation_translation.GetAxisX().Normalized()); // Check X axis didn't change
  335. CHECK_APPROX_EQUAL(m2.GetAxisY(), skewed_rotation_translation.GetAxisY().Normalized(), 0.003f); // Y axis may move a bit
  336. CHECK_APPROX_EQUAL(m2.GetAxisZ(), skewed_rotation_translation.GetAxisZ().Normalized(), 0.02f); // Z axis may move a bit
  337. CHECK_APPROX_EQUAL(m2.GetAxisX().Cross(m2.GetAxisY()).Dot(m2.GetAxisZ()), 1.0f); // Check perpendicular
  338. CHECK_APPROX_EQUAL(scale, scale_out, 0.05f); // Scale may change a bit
  339. }
  340. }