test_transform.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*************************************************************************/
  2. /* test_transform.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "test_transform.h"
  31. #include "core/math/random_number_generator.h"
  32. #include "core/math/transform.h"
  33. #include "core/math/vector3.h"
  34. #include "core/os/os.h"
  35. #include "core/ustring.h"
  36. // #define GODOT_TEST_TRANSFORM_NON_UNIFORM_SCALE_TESTS_ENABLED
  37. namespace TestTransform {
  38. bool test_plane() {
  39. bool pass = true;
  40. // test non-uniform scaling, forward and inverse
  41. Transform tr;
  42. tr.scale(Vector3(1, 2, 3));
  43. Plane p(Vector3(1, 1, 1), Vector3(1, 1, 1).normalized());
  44. Plane p2 = tr.xform(p);
  45. Plane p3 = tr.xform_inv(p2);
  46. if (!p3.normal.is_equal_approx(p.normal)) {
  47. OS::get_singleton()->print("Fail due to Transform::xform(Plane)\n");
  48. pass = false;
  49. }
  50. return pass;
  51. }
  52. bool test_aabb_regular() {
  53. bool pass = true;
  54. Transform tr;
  55. tr.basis = Basis(Vector3(Math_PI, 0, 0));
  56. tr.origin = Vector3(1, 2, 3);
  57. AABB bb(Vector3(1, 1, 1), Vector3(2, 3, 4));
  58. // Test forward xform.
  59. AABB bb2 = tr.xform(bb);
  60. AABB bb3 = tr.xform_inv(bb2);
  61. if (!bb3.position.is_equal_approx(bb.position)) {
  62. OS::get_singleton()->print("Fail due to Transform::xform_inv(AABB) position\n");
  63. pass = false;
  64. }
  65. if (!bb3.size.is_equal_approx(bb.size)) {
  66. OS::get_singleton()->print("Fail due to Transform::xform_inv(AABB) size\n");
  67. pass = false;
  68. }
  69. if (!pass) {
  70. String string = String("bb2 : ") + String(Variant(bb2));
  71. OS::get_singleton()->print("\t%ls\n", string.c_str());
  72. string = String("bb3 : ") + String(Variant(bb3));
  73. OS::get_singleton()->print("\t%ls\n", string.c_str());
  74. }
  75. return pass;
  76. }
  77. bool test_aabb_non_uniform_scale() {
  78. bool pass = true;
  79. Transform tr;
  80. tr.scale(Vector3(1, 2, 3));
  81. AABB bb(Vector3(1, 1, 1), Vector3(2, 3, 4));
  82. // Test forward xform.
  83. AABB bb2 = tr.xform(bb);
  84. if (!bb2.position.is_equal_approx(Vector3(1, 2, 3))) {
  85. OS::get_singleton()->print("Fail with non-uniform scale due to Transform::xform(AABB) position\n");
  86. pass = false;
  87. }
  88. if (!bb2.size.is_equal_approx(Vector3(2, 6, 12))) {
  89. OS::get_singleton()->print("Fail with non-uniform scale due to Transform::xform(AABB) size\n");
  90. pass = false;
  91. }
  92. // Now test inverse.
  93. // This will fail if using the transpose and not the affine_inverse.
  94. bb2.position = Vector3(1, 2, 3);
  95. bb2.size = Vector3(2, 6, 12);
  96. AABB bb3 = tr.xform_inv(bb2);
  97. if (!bb3.position.is_equal_approx(bb.position)) {
  98. OS::get_singleton()->print("Fail with non-uniform scale due to Transform::xform_inv(AABB) position\n");
  99. pass = false;
  100. }
  101. if (!bb3.size.is_equal_approx(bb.size)) {
  102. OS::get_singleton()->print("Fail with non-uniform scale due to Transform::xform_inv(AABB) size\n");
  103. pass = false;
  104. }
  105. if (!pass) {
  106. String string = String("bb2 : ") + String(Variant(bb2));
  107. OS::get_singleton()->print("\t%ls\n", string.c_str());
  108. string = String("bb3 : ") + String(Variant(bb3));
  109. OS::get_singleton()->print("\t%ls\n", string.c_str());
  110. }
  111. return pass;
  112. }
  113. bool test_aabb() {
  114. bool pass = true;
  115. if (!test_aabb_regular()) {
  116. pass = false;
  117. }
  118. #ifdef GODOT_TEST_TRANSFORM_NON_UNIFORM_SCALE_TESTS_ENABLED
  119. if (!test_aabb_non_uniform_scale()) {
  120. pass = false;
  121. }
  122. #endif
  123. return pass;
  124. }
  125. bool test_vector3_regular() {
  126. bool pass = true;
  127. Transform tr;
  128. RandomNumberGenerator rng;
  129. const real_t range = 1800.0;
  130. const real_t range_rot = Math_PI;
  131. bool passed_multi = true;
  132. for (int n = 0; n < 1000; n++) {
  133. Vector3 pt_test = Vector3(rng.randf_range(-range, range), rng.randf_range(-range, range), rng.randf_range(-range, range));
  134. tr.origin = Vector3(rng.randf_range(-range, range), rng.randf_range(-range, range), rng.randf_range(-range, range));
  135. tr.basis = Basis(Vector3(rng.randf_range(-range_rot, range_rot), rng.randf_range(-range_rot, range_rot), rng.randf_range(-range_rot, range_rot)));
  136. Vector3 pt = tr.xform(pt_test);
  137. pt = tr.xform_inv(pt);
  138. if (!pt.is_equal_approx(pt_test, 0.1)) {
  139. passed_multi = false;
  140. }
  141. }
  142. if (!passed_multi) {
  143. OS::get_singleton()->print("Failed multitest due to Transform::xform and xform_inv(Vector3)\n");
  144. pass = false;
  145. }
  146. return pass;
  147. }
  148. bool test_vector3_non_uniform_scale() {
  149. bool pass = true;
  150. // Regular scale.
  151. Transform tr;
  152. tr.scale(Vector3(3, 3, 3));
  153. Vector3 pt(1, 1, 1);
  154. Vector3 res = tr.xform(pt);
  155. if (!res.is_equal_approx(Vector3(3, 3, 3))) {
  156. OS::get_singleton()->print("Fail with scale due to Transform::xform(Vector3)\n");
  157. pass = false;
  158. }
  159. res = tr.xform_inv(res);
  160. if (!res.is_equal_approx(pt)) {
  161. OS::get_singleton()->print("Fail with scale due to Transform::xform_inv(Vector3)\n");
  162. pass = false;
  163. }
  164. // Non uniform scale.
  165. tr.scale(Vector3(1, 2, 3));
  166. res = tr.xform(pt);
  167. if (!res.is_equal_approx(Vector3(1, 2, 3))) {
  168. OS::get_singleton()->print("Fail with non-uniform scale due to Transform::xform(Vector3)\n");
  169. pass = false;
  170. }
  171. pt = Vector3(1, 2, 3);
  172. res = tr.xform_inv(pt);
  173. if (!res.is_equal_approx(Vector3(1, 1, 1))) {
  174. OS::get_singleton()->print("Fail with non-uniform scale due to Transform::xform_inv(Vector3)\n");
  175. pass = false;
  176. }
  177. return pass;
  178. }
  179. bool test_vector3() {
  180. bool pass = true;
  181. if (!test_vector3_regular()) {
  182. pass = false;
  183. }
  184. #ifdef GODOT_TEST_TRANSFORM_NON_UNIFORM_SCALE_TESTS_ENABLED
  185. if (!test_vector3_non_uniform_scale()) {
  186. pass = false;
  187. }
  188. #endif
  189. return pass;
  190. }
  191. MainLoop *test() {
  192. OS::get_singleton()->print("Start Transform checks.\n");
  193. bool success = true;
  194. if (!test_vector3()) {
  195. success = false;
  196. }
  197. if (!test_plane()) {
  198. success = false;
  199. }
  200. if (!test_aabb()) {
  201. success = false;
  202. }
  203. if (success) {
  204. OS::get_singleton()->print("Transform checks passed.\n");
  205. } else {
  206. OS::get_singleton()->print("Transform checks FAILED.\n");
  207. }
  208. return nullptr;
  209. }
  210. } // namespace TestTransform