test_transform_3d.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**************************************************************************/
  2. /* test_transform_3d.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef TEST_TRANSFORM_3D_H
  31. #define TEST_TRANSFORM_3D_H
  32. #include "core/math/transform_3d.h"
  33. #include "tests/test_macros.h"
  34. namespace TestTransform3D {
  35. Transform3D create_dummy_transform() {
  36. return Transform3D(Basis(Vector3(1, 2, 3), Vector3(4, 5, 6), Vector3(7, 8, 9)), Vector3(10, 11, 12));
  37. }
  38. Transform3D identity() {
  39. return Transform3D();
  40. }
  41. TEST_CASE("[Transform3D] translation") {
  42. Vector3 offset = Vector3(1, 2, 3);
  43. // Both versions should give the same result applied to identity.
  44. CHECK(identity().translated(offset) == identity().translated_local(offset));
  45. // Check both versions against left and right multiplications.
  46. Transform3D orig = create_dummy_transform();
  47. Transform3D T = identity().translated(offset);
  48. CHECK(orig.translated(offset) == T * orig);
  49. CHECK(orig.translated_local(offset) == orig * T);
  50. }
  51. TEST_CASE("[Transform3D] scaling") {
  52. Vector3 scaling = Vector3(1, 2, 3);
  53. // Both versions should give the same result applied to identity.
  54. CHECK(identity().scaled(scaling) == identity().scaled_local(scaling));
  55. // Check both versions against left and right multiplications.
  56. Transform3D orig = create_dummy_transform();
  57. Transform3D S = identity().scaled(scaling);
  58. CHECK(orig.scaled(scaling) == S * orig);
  59. CHECK(orig.scaled_local(scaling) == orig * S);
  60. }
  61. TEST_CASE("[Transform3D] rotation") {
  62. Vector3 axis = Vector3(1, 2, 3).normalized();
  63. real_t phi = 1.0;
  64. // Both versions should give the same result applied to identity.
  65. CHECK(identity().rotated(axis, phi) == identity().rotated_local(axis, phi));
  66. // Check both versions against left and right multiplications.
  67. Transform3D orig = create_dummy_transform();
  68. Transform3D R = identity().rotated(axis, phi);
  69. CHECK(orig.rotated(axis, phi) == R * orig);
  70. CHECK(orig.rotated_local(axis, phi) == orig * R);
  71. }
  72. TEST_CASE("[Transform3D] Finite number checks") {
  73. const Vector3 y(0, 1, 2);
  74. const Vector3 infinite_vec(NAN, NAN, NAN);
  75. const Basis x(y, y, y);
  76. const Basis infinite_basis(infinite_vec, infinite_vec, infinite_vec);
  77. CHECK_MESSAGE(
  78. Transform3D(x, y).is_finite(),
  79. "Transform3D with all components finite should be finite");
  80. CHECK_FALSE_MESSAGE(
  81. Transform3D(x, infinite_vec).is_finite(),
  82. "Transform3D with one component infinite should not be finite.");
  83. CHECK_FALSE_MESSAGE(
  84. Transform3D(infinite_basis, y).is_finite(),
  85. "Transform3D with one component infinite should not be finite.");
  86. CHECK_FALSE_MESSAGE(
  87. Transform3D(infinite_basis, infinite_vec).is_finite(),
  88. "Transform3D with two components infinite should not be finite.");
  89. }
  90. TEST_CASE("[Transform3D] Rotate around global origin") {
  91. // Start with the default orientation, but not centered on the origin.
  92. // Rotating should rotate both our basis and the origin.
  93. Transform3D transform = Transform3D();
  94. transform.origin = Vector3(0, 0, 1);
  95. Transform3D expected = Transform3D();
  96. expected.origin = Vector3(0, 0, -1);
  97. expected.basis[0] = Vector3(-1, 0, 0);
  98. expected.basis[2] = Vector3(0, 0, -1);
  99. const Transform3D rotated_transform = transform.rotated(Vector3(0, 1, 0), Math_PI);
  100. CHECK_MESSAGE(rotated_transform.is_equal_approx(expected), "The rotated transform should have a new orientation and basis.");
  101. }
  102. TEST_CASE("[Transform3D] Rotate in-place (local rotation)") {
  103. // Start with the default orientation.
  104. // Local rotation should not change the origin, only the basis.
  105. Transform3D transform = Transform3D();
  106. transform.origin = Vector3(1, 2, 3);
  107. Transform3D expected = Transform3D();
  108. expected.origin = Vector3(1, 2, 3);
  109. expected.basis[0] = Vector3(-1, 0, 0);
  110. expected.basis[2] = Vector3(0, 0, -1);
  111. const Transform3D rotated_transform = Transform3D(transform.rotated_local(Vector3(0, 1, 0), Math_PI));
  112. CHECK_MESSAGE(rotated_transform.is_equal_approx(expected), "The rotated transform should have a new orientation but still be based on the same origin.");
  113. }
  114. } // namespace TestTransform3D
  115. #endif // TEST_TRANSFORM_3D_H