DMat44Tests.cpp 4.5 KB

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