DMat44Tests.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2022 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include "UnitTestFramework.h"
  5. #include <Jolt/Math/DMat44.h>
  6. TEST_SUITE("DMat44Tests")
  7. {
  8. TEST_CASE("TestDMat44Zero")
  9. {
  10. DMat44 zero = DMat44::sZero();
  11. CHECK(zero.GetAxisX() == Vec3::sZero());
  12. CHECK(zero.GetAxisY() == Vec3::sZero());
  13. CHECK(zero.GetAxisZ() == Vec3::sZero());
  14. CHECK(zero.GetTranslation() == DVec3::sZero());
  15. }
  16. TEST_CASE("TestDMat44Identity")
  17. {
  18. DMat44 identity = DMat44::sIdentity();
  19. CHECK(identity == DMat44(Vec4(1, 0, 0, 0), Vec4(0, 1, 0, 0), Vec4(0, 0, 1, 0), DVec3(0, 0, 0)));
  20. }
  21. TEST_CASE("TestDMat44Construct")
  22. {
  23. DMat44 mat(Vec4(1, 2, 3, 4), Vec4(5, 6, 7, 8), Vec4(9, 10, 11, 12), DVec3(13, 14, 15));
  24. CHECK(mat.GetColumn4(0) == Vec4(1, 2, 3, 4));
  25. CHECK(mat.GetColumn4(1) == Vec4(5, 6, 7, 8));
  26. CHECK(mat.GetColumn4(2) == Vec4(9, 10, 11, 12));
  27. CHECK(mat.GetTranslation() == DVec3(13, 14, 15));
  28. DMat44 mat2(mat);
  29. CHECK(mat2.GetColumn4(0) == Vec4(1, 2, 3, 4));
  30. CHECK(mat2.GetColumn4(1) == Vec4(5, 6, 7, 8));
  31. CHECK(mat2.GetColumn4(2) == Vec4(9, 10, 11, 12));
  32. CHECK(mat2.GetTranslation() == DVec3(13, 14, 15));
  33. }
  34. TEST_CASE("TestDMat44Scale")
  35. {
  36. CHECK(DMat44::sScale(Vec3(2, 3, 4)) == DMat44(Vec4(2, 0, 0, 0), Vec4(0, 3, 0, 0), Vec4(0, 0, 4, 0), DVec3(0, 0, 0)));
  37. }
  38. TEST_CASE("TestDMat44Rotation")
  39. {
  40. DMat44 mat(Vec4(1, 2, 3, 4), Vec4(5, 6, 7, 8), Vec4(9, 10, 11, 12), DVec3(13, 14, 15));
  41. CHECK(mat.GetRotation() == Mat44(Vec4(1, 2, 3, 4), Vec4(5, 6, 7, 8), Vec4(9, 10, 11, 12), Vec4(0, 0, 0, 1)));
  42. }
  43. TEST_CASE("TestMat44SetRotation")
  44. {
  45. DMat44 mat(Vec4(1, 2, 3, 4), Vec4(5, 6, 7, 8), Vec4(9, 10, 11, 12), DVec3(13, 14, 15));
  46. Mat44 mat2(Vec4(17, 18, 19, 20), Vec4(21, 22, 23, 24), Vec4(25, 26, 27, 28), Vec4(29, 30, 31, 32));
  47. mat.SetRotation(mat2);
  48. CHECK(mat == DMat44(Vec4(17, 18, 19, 20), Vec4(21, 22, 23, 24), Vec4(25, 26, 27, 28), DVec3(13, 14, 15)));
  49. }
  50. TEST_CASE("TestDMat44MultiplyMat44")
  51. {
  52. DMat44 mat(Vec4(1, 2, 3, 0), Vec4(5, 6, 7, 0), Vec4(9, 10, 11, 0), DVec3(13, 14, 15));
  53. Mat44 mat2(Vec4(17, 18, 19, 0), Vec4(21, 22, 23, 0), Vec4(25, 26, 27, 0), Vec4(29, 30, 31, 1));
  54. DMat44 result = mat * mat2;
  55. CHECK(result == DMat44(Vec4(278, 332, 386, 0), Vec4(338, 404, 470, 0), Vec4(398, 476, 554, 0), DVec3(471, 562, 653)));
  56. }
  57. TEST_CASE("TestDMat44MultiplyDMat44")
  58. {
  59. DMat44 mat(Vec4(1, 2, 3, 0), Vec4(5, 6, 7, 0), Vec4(9, 10, 11, 0), DVec3(13, 14, 15));
  60. DMat44 mat2(Vec4(17, 18, 19, 0), Vec4(21, 22, 23, 0), Vec4(25, 26, 27, 0), DVec3(29, 30, 31));
  61. DMat44 result = mat * mat2;
  62. CHECK(result == DMat44(Vec4(278, 332, 386, 0), Vec4(338, 404, 470, 0), Vec4(398, 476, 554, 0), DVec3(471, 562, 653)));
  63. }
  64. TEST_CASE("TestDMat44MultiplyVec3")
  65. {
  66. DMat44 mat(Vec4(1, 2, 3, 4), Vec4(5, 6, 7, 8), Vec4(9, 10, 11, 12), DVec3(13, 14, 15));
  67. Vec3 vec(17, 18, 19);
  68. DVec3 result = mat * DVec3(vec);
  69. CHECK(result == DVec3(291, 346, 401));
  70. DVec3 result2 = mat * vec;
  71. CHECK(result2 == DVec3(291, 346, 401));
  72. Vec3 result3 = mat.Multiply3x3(vec);
  73. CHECK(result3 == Vec3(278, 332, 386));
  74. Vec3 result4 = mat.Multiply3x3Transposed(vec);
  75. CHECK(result4 == Vec3(110, 326, 542));
  76. }
  77. TEST_CASE("TestDMat44Inversed")
  78. {
  79. DMat44 mat(Vec4(1, 16, 2, 0), Vec4(2, 8, 4, 0), Vec4(8, 4, 1, 0), DVec3(4, 2, 8));
  80. DMat44 inverse = mat.Inversed();
  81. DMat44 identity = mat * inverse;
  82. CHECK_APPROX_EQUAL(identity, DMat44::sIdentity());
  83. }
  84. TEST_CASE("TestDMat44InverseRotateTranslate")
  85. {
  86. Quat rot = Quat::sRotation(Vec3(0, 1, 0), 0.2f * JPH_PI);
  87. DVec3 pos(2, 3, 4);
  88. DMat44 m1 = DMat44::sRotationTranslation(rot, pos).Inversed();
  89. DMat44 m2 = DMat44::sInverseRotationTranslation(rot, pos);
  90. CHECK_APPROX_EQUAL(m1, m2);
  91. }
  92. TEST_CASE("TestDMat44InversedRotationTranslation")
  93. {
  94. Quat rot = Quat::sRotation(Vec3(0, 1, 0), 0.2f * JPH_PI);
  95. DVec3 pos(2, 3, 4);
  96. DMat44 m1 = DMat44::sRotationTranslation(rot, pos).InversedRotationTranslation();
  97. DMat44 m2 = DMat44::sInverseRotationTranslation(rot, pos);
  98. CHECK_APPROX_EQUAL(m1, m2);
  99. }
  100. TEST_CASE("TestDMat44PrePostScaled")
  101. {
  102. DMat44 m(Vec4(2, 3, 4, 0), Vec4(5, 6, 7, 0), Vec4(8, 9, 10, 0), DVec3(11, 12, 13));
  103. Vec3 v(14, 15, 16);
  104. CHECK(m.PreScaled(v) == m * DMat44::sScale(v));
  105. CHECK(m.PostScaled(v) == DMat44::sScale(v) * m);
  106. }
  107. TEST_CASE("TestDMat44PrePostTranslated")
  108. {
  109. DMat44 m(Vec4(2, 3, 4, 0), Vec4(5, 6, 7, 0), Vec4(8, 9, 10, 0), DVec3(11, 12, 13));
  110. Vec3 v(14, 15, 16);
  111. CHECK_APPROX_EQUAL(m.PreTranslated(v), m * DMat44::sTranslation(DVec3(v)));
  112. CHECK_APPROX_EQUAL(m.PostTranslated(v), DMat44::sTranslation(DVec3(v)) * m);
  113. }
  114. TEST_CASE("TestDMat44Decompose")
  115. {
  116. // Create a rotation/translation matrix
  117. Quat rot = Quat::sRotation(Vec3(1, 1, 1).Normalized(), 0.2f * JPH_PI);
  118. DVec3 pos(2, 3, 4);
  119. DMat44 rotation_translation = DMat44::sRotationTranslation(rot, pos);
  120. // Scale the matrix
  121. Vec3 scale(2, 1, 3);
  122. DMat44 m1 = rotation_translation * DMat44::sScale(scale);
  123. // Decompose scale
  124. Vec3 scale_out;
  125. DMat44 m2 = m1.Decompose(scale_out);
  126. // Check individual components
  127. CHECK_APPROX_EQUAL(rotation_translation, m2);
  128. CHECK_APPROX_EQUAL(scale, scale_out);
  129. }
  130. }