DMat44Tests.cpp 4.4 KB

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