test_aabb.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*************************************************************************/
  2. /* test_aabb.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. #ifndef TEST_AABB_H
  31. #define TEST_AABB_H
  32. #include "core/math/aabb.h"
  33. #include "core/string/print_string.h"
  34. #include "tests/test_macros.h"
  35. #include "thirdparty/doctest/doctest.h"
  36. namespace TestAABB {
  37. TEST_CASE("[AABB] Constructor methods") {
  38. const AABB aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  39. const AABB aabb_copy = AABB(aabb);
  40. CHECK_MESSAGE(
  41. aabb == aabb_copy,
  42. "AABBs created with the same dimensions but by different methods should be equal.");
  43. }
  44. TEST_CASE("[AABB] String conversion") {
  45. CHECK_MESSAGE(
  46. String(AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6))) == "-1.5, 2, -2.5 - 4, 5, 6",
  47. "The string representation shouild match the expected value.");
  48. }
  49. TEST_CASE("[AABB] Basic getters") {
  50. const AABB aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  51. CHECK_MESSAGE(
  52. aabb.get_position().is_equal_approx(Vector3(-1.5, 2, -2.5)),
  53. "get_position() should return the expected value.");
  54. CHECK_MESSAGE(
  55. aabb.get_size().is_equal_approx(Vector3(4, 5, 6)),
  56. "get_size() should return the expected value.");
  57. CHECK_MESSAGE(
  58. aabb.get_end().is_equal_approx(Vector3(2.5, 7, 3.5)),
  59. "get_end() should return the expected value.");
  60. }
  61. TEST_CASE("[AABB] Basic setters") {
  62. AABB aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  63. aabb.set_end(Vector3(100, 0, 100));
  64. CHECK_MESSAGE(
  65. aabb.is_equal_approx(AABB(Vector3(-1.5, 2, -2.5), Vector3(101.5, -2, 102.5))),
  66. "set_end() should result in the expected AABB.");
  67. aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  68. aabb.set_position(Vector3(-1000, -2000, -3000));
  69. CHECK_MESSAGE(
  70. aabb.is_equal_approx(AABB(Vector3(-1000, -2000, -3000), Vector3(4, 5, 6))),
  71. "set_position() should result in the expected AABB.");
  72. aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  73. aabb.set_size(Vector3(0, 0, -50));
  74. CHECK_MESSAGE(
  75. aabb.is_equal_approx(AABB(Vector3(-1.5, 2, -2.5), Vector3(0, 0, -50))),
  76. "set_size() should result in the expected AABB.");
  77. }
  78. TEST_CASE("[AABB] Area getters") {
  79. AABB aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  80. CHECK_MESSAGE(
  81. Math::is_equal_approx(aabb.get_area(), 120),
  82. "get_area() should return the expected value with positive size.");
  83. CHECK_MESSAGE(
  84. !aabb.has_no_area(),
  85. "Non-empty volumetric AABB should have an area.");
  86. aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(-4, 5, 6));
  87. CHECK_MESSAGE(
  88. Math::is_equal_approx(aabb.get_area(), -120),
  89. "get_area() should return the expected value with negative size (1 component).");
  90. aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(-4, -5, 6));
  91. CHECK_MESSAGE(
  92. Math::is_equal_approx(aabb.get_area(), 120),
  93. "get_area() should return the expected value with negative size (2 components).");
  94. aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(-4, -5, -6));
  95. CHECK_MESSAGE(
  96. Math::is_equal_approx(aabb.get_area(), -120),
  97. "get_area() should return the expected value with negative size (3 components).");
  98. aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 0, 6));
  99. CHECK_MESSAGE(
  100. aabb.has_no_area(),
  101. "Non-empty flat AABB should not have an area.");
  102. CHECK_MESSAGE(
  103. AABB().has_no_area(),
  104. "Empty AABB should not have an area.");
  105. }
  106. TEST_CASE("[AABB] Surface getters") {
  107. AABB aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  108. CHECK_MESSAGE(
  109. !aabb.has_no_surface(),
  110. "Non-empty volumetric AABB should have an surface.");
  111. aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 0, 6));
  112. CHECK_MESSAGE(
  113. !aabb.has_no_surface(),
  114. "Non-empty flat AABB should have a surface.");
  115. CHECK_MESSAGE(
  116. AABB().has_no_surface(),
  117. "Empty AABB should not have an surface.");
  118. }
  119. TEST_CASE("[AABB] Intersection") {
  120. const AABB aabb_big = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  121. AABB aabb_small = AABB(Vector3(-1.5, 2, -2.5), Vector3(1, 1, 1));
  122. CHECK_MESSAGE(
  123. aabb_big.intersects(aabb_small),
  124. "intersects() with fully contained AABB (touching the edge) should return the expected result.");
  125. aabb_small = AABB(Vector3(0.5, 1.5, -2), Vector3(1, 1, 1));
  126. CHECK_MESSAGE(
  127. aabb_big.intersects(aabb_small),
  128. "intersects() with partially contained AABB (overflowing on Y axis) should return the expected result.");
  129. aabb_small = AABB(Vector3(10, -10, -10), Vector3(1, 1, 1));
  130. CHECK_MESSAGE(
  131. !aabb_big.intersects(aabb_small),
  132. "intersects() with non-contained AABB should return the expected result.");
  133. aabb_small = AABB(Vector3(-1.5, 2, -2.5), Vector3(1, 1, 1));
  134. CHECK_MESSAGE(
  135. aabb_big.intersection(aabb_small).is_equal_approx(aabb_small),
  136. "intersection() with fully contained AABB (touching the edge) should return the expected result.");
  137. aabb_small = AABB(Vector3(0.5, 1.5, -2), Vector3(1, 1, 1));
  138. CHECK_MESSAGE(
  139. aabb_big.intersection(aabb_small).is_equal_approx(AABB(Vector3(0.5, 2, -2), Vector3(1, 0.5, 1))),
  140. "intersection() with partially contained AABB (overflowing on Y axis) should return the expected result.");
  141. aabb_small = AABB(Vector3(10, -10, -10), Vector3(1, 1, 1));
  142. CHECK_MESSAGE(
  143. aabb_big.intersection(aabb_small).is_equal_approx(AABB()),
  144. "intersection() with non-contained AABB should return the expected result.");
  145. CHECK_MESSAGE(
  146. aabb_big.intersects_plane(Plane(Vector3(0, 1, 0), 4)),
  147. "intersects_plane() should return the expected result.");
  148. CHECK_MESSAGE(
  149. aabb_big.intersects_plane(Plane(Vector3(0, -1, 0), -4)),
  150. "intersects_plane() should return the expected result.");
  151. CHECK_MESSAGE(
  152. !aabb_big.intersects_plane(Plane(Vector3(0, 1, 0), 200)),
  153. "intersects_plane() should return the expected result.");
  154. CHECK_MESSAGE(
  155. aabb_big.intersects_segment(Vector3(1, 3, 0), Vector3(0, 3, 0)),
  156. "intersects_segment() should return the expected result.");
  157. CHECK_MESSAGE(
  158. aabb_big.intersects_segment(Vector3(0, 3, 0), Vector3(0, -300, 0)),
  159. "intersects_segment() should return the expected result.");
  160. CHECK_MESSAGE(
  161. aabb_big.intersects_segment(Vector3(-50, 3, -50), Vector3(50, 3, 50)),
  162. "intersects_segment() should return the expected result.");
  163. CHECK_MESSAGE(
  164. !aabb_big.intersects_segment(Vector3(-50, 25, -50), Vector3(50, 25, 50)),
  165. "intersects_segment() should return the expected result.");
  166. CHECK_MESSAGE(
  167. aabb_big.intersects_segment(Vector3(0, 3, 0), Vector3(0, 3, 0)),
  168. "intersects_segment() should return the expected result with segment of length 0.");
  169. CHECK_MESSAGE(
  170. !aabb_big.intersects_segment(Vector3(0, 300, 0), Vector3(0, 300, 0)),
  171. "intersects_segment() should return the expected result with segment of length 0.");
  172. }
  173. TEST_CASE("[AABB] Merging") {
  174. const AABB aabb_big = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  175. AABB aabb_small = AABB(Vector3(-1.5, 2, -2.5), Vector3(1, 1, 1));
  176. CHECK_MESSAGE(
  177. aabb_big.merge(aabb_small).is_equal_approx(aabb_big),
  178. "merge() with fully contained AABB (touching the edge) should return the expected result.");
  179. aabb_small = AABB(Vector3(0.5, 1.5, -2), Vector3(1, 1, 1));
  180. CHECK_MESSAGE(
  181. aabb_big.merge(aabb_small).is_equal_approx(AABB(Vector3(-1.5, 1.5, -2.5), Vector3(4, 5.5, 6))),
  182. "merge() with partially contained AABB (overflowing on Y axis) should return the expected result.");
  183. aabb_small = AABB(Vector3(10, -10, -10), Vector3(1, 1, 1));
  184. CHECK_MESSAGE(
  185. aabb_big.merge(aabb_small).is_equal_approx(AABB(Vector3(-1.5, -10, -10), Vector3(12.5, 17, 13.5))),
  186. "merge() with non-contained AABB should return the expected result.");
  187. }
  188. TEST_CASE("[AABB] Encloses") {
  189. const AABB aabb_big = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  190. AABB aabb_small = AABB(Vector3(-1.5, 2, -2.5), Vector3(1, 1, 1));
  191. CHECK_MESSAGE(
  192. aabb_big.encloses(aabb_small),
  193. "encloses() with fully contained AABB (touching the edge) should return the expected result.");
  194. aabb_small = AABB(Vector3(0.5, 1.5, -2), Vector3(1, 1, 1));
  195. CHECK_MESSAGE(
  196. !aabb_big.encloses(aabb_small),
  197. "encloses() with partially contained AABB (overflowing on Y axis) should return the expected result.");
  198. aabb_small = AABB(Vector3(10, -10, -10), Vector3(1, 1, 1));
  199. CHECK_MESSAGE(
  200. !aabb_big.encloses(aabb_small),
  201. "encloses() with non-contained AABB should return the expected result.");
  202. }
  203. TEST_CASE("[AABB] Get endpoints") {
  204. const AABB aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  205. CHECK_MESSAGE(
  206. aabb.get_endpoint(0).is_equal_approx(Vector3(-1.5, 2, -2.5)),
  207. "The endpoint at index 0 should match the expected value.");
  208. CHECK_MESSAGE(
  209. aabb.get_endpoint(1).is_equal_approx(Vector3(-1.5, 2, 3.5)),
  210. "The endpoint at index 1 should match the expected value.");
  211. CHECK_MESSAGE(
  212. aabb.get_endpoint(2).is_equal_approx(Vector3(-1.5, 7, -2.5)),
  213. "The endpoint at index 2 should match the expected value.");
  214. CHECK_MESSAGE(
  215. aabb.get_endpoint(3).is_equal_approx(Vector3(-1.5, 7, 3.5)),
  216. "The endpoint at index 3 should match the expected value.");
  217. CHECK_MESSAGE(
  218. aabb.get_endpoint(4).is_equal_approx(Vector3(2.5, 2, -2.5)),
  219. "The endpoint at index 4 should match the expected value.");
  220. CHECK_MESSAGE(
  221. aabb.get_endpoint(5).is_equal_approx(Vector3(2.5, 2, 3.5)),
  222. "The endpoint at index 5 should match the expected value.");
  223. CHECK_MESSAGE(
  224. aabb.get_endpoint(6).is_equal_approx(Vector3(2.5, 7, -2.5)),
  225. "The endpoint at index 6 should match the expected value.");
  226. CHECK_MESSAGE(
  227. aabb.get_endpoint(7).is_equal_approx(Vector3(2.5, 7, 3.5)),
  228. "The endpoint at index 7 should match the expected value.");
  229. ERR_PRINT_OFF;
  230. CHECK_MESSAGE(
  231. aabb.get_endpoint(8).is_equal_approx(Vector3()),
  232. "The endpoint at invalid index 8 should match the expected value.");
  233. CHECK_MESSAGE(
  234. aabb.get_endpoint(-1).is_equal_approx(Vector3()),
  235. "The endpoint at invalid index -1 should match the expected value.");
  236. ERR_PRINT_ON;
  237. }
  238. TEST_CASE("[AABB] Get longest/shortest axis") {
  239. const AABB aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  240. CHECK_MESSAGE(
  241. aabb.get_longest_axis().is_equal_approx(Vector3(0, 0, 1)),
  242. "get_longest_axis() should return the expected value.");
  243. CHECK_MESSAGE(
  244. aabb.get_longest_axis_index() == Vector3::AXIS_Z,
  245. "get_longest_axis() should return the expected value.");
  246. CHECK_MESSAGE(
  247. Math::is_equal_approx(aabb.get_longest_axis_size(), 6),
  248. "get_longest_axis() should return the expected value.");
  249. CHECK_MESSAGE(
  250. aabb.get_shortest_axis().is_equal_approx(Vector3(1, 0, 0)),
  251. "get_shortest_axis() should return the expected value.");
  252. CHECK_MESSAGE(
  253. aabb.get_shortest_axis_index() == Vector3::AXIS_X,
  254. "get_shortest_axis() should return the expected value.");
  255. CHECK_MESSAGE(
  256. Math::is_equal_approx(aabb.get_shortest_axis_size(), 4),
  257. "get_shortest_axis() should return the expected value.");
  258. }
  259. #ifndef _MSC_VER
  260. #warning Support tests need to be re-done
  261. #endif
  262. /* Support function was actually broken. As it was fixed, the tests now fail. Tests need to be re-done.
  263. TEST_CASE("[AABB] Get support") {
  264. const AABB aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  265. CHECK_MESSAGE(
  266. aabb.get_support(Vector3(1, 0, 0)).is_equal_approx(Vector3(-1.5, 7, 3.5)),
  267. "get_support() should return the expected value.");
  268. CHECK_MESSAGE(
  269. aabb.get_support(Vector3(0.5, 1, 0)).is_equal_approx(Vector3(-1.5, 2, 3.5)),
  270. "get_support() should return the expected value.");
  271. CHECK_MESSAGE(
  272. aabb.get_support(Vector3(0.5, 1, -400)).is_equal_approx(Vector3(-1.5, 2, 3.5)),
  273. "get_support() should return the expected value.");
  274. CHECK_MESSAGE(
  275. aabb.get_support(Vector3(0, -1, 0)).is_equal_approx(Vector3(2.5, 7, 3.5)),
  276. "get_support() should return the expected value.");
  277. CHECK_MESSAGE(
  278. aabb.get_support(Vector3(0, -0.1, 0)).is_equal_approx(Vector3(2.5, 7, 3.5)),
  279. "get_support() should return the expected value.");
  280. CHECK_MESSAGE(
  281. aabb.get_support(Vector3()).is_equal_approx(Vector3(2.5, 7, 3.5)),
  282. "get_support() should return the expected value with a null vector.");
  283. }
  284. */
  285. TEST_CASE("[AABB] Grow") {
  286. const AABB aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  287. CHECK_MESSAGE(
  288. aabb.grow(0.25).is_equal_approx(AABB(Vector3(-1.75, 1.75, -2.75), Vector3(4.5, 5.5, 6.5))),
  289. "grow() with positive value should return the expected AABB.");
  290. CHECK_MESSAGE(
  291. aabb.grow(-0.25).is_equal_approx(AABB(Vector3(-1.25, 2.25, -2.25), Vector3(3.5, 4.5, 5.5))),
  292. "grow() with negative value should return the expected AABB.");
  293. CHECK_MESSAGE(
  294. aabb.grow(-10).is_equal_approx(AABB(Vector3(8.5, 12, 7.5), Vector3(-16, -15, -14))),
  295. "grow() with large negative value should return the expected AABB.");
  296. }
  297. TEST_CASE("[AABB] Has point") {
  298. const AABB aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  299. CHECK_MESSAGE(
  300. aabb.has_point(Vector3(-1, 3, 0)),
  301. "has_point() with contained point should return the expected value.");
  302. CHECK_MESSAGE(
  303. aabb.has_point(Vector3(2, 3, 0)),
  304. "has_point() with contained point should return the expected value.");
  305. CHECK_MESSAGE(
  306. aabb.has_point(Vector3(-1.5, 3, 0)),
  307. "has_point() with contained point on negative edge should return the expected value.");
  308. CHECK_MESSAGE(
  309. aabb.has_point(Vector3(2.5, 3, 0)),
  310. "has_point() with contained point on positive edge should return the expected value.");
  311. CHECK_MESSAGE(
  312. !aabb.has_point(Vector3(-20, 0, 0)),
  313. "has_point() with non-contained point should return the expected value.");
  314. }
  315. TEST_CASE("[AABB] Expanding") {
  316. const AABB aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  317. CHECK_MESSAGE(
  318. aabb.expand(Vector3(-1, 3, 0)).is_equal_approx(aabb),
  319. "expand() with contained point should return the expected AABB.");
  320. CHECK_MESSAGE(
  321. aabb.expand(Vector3(2, 3, 0)).is_equal_approx(aabb),
  322. "expand() with contained point should return the expected AABB.");
  323. CHECK_MESSAGE(
  324. aabb.expand(Vector3(-1.5, 3, 0)).is_equal_approx(aabb),
  325. "expand() with contained point on negative edge should return the expected AABB.");
  326. CHECK_MESSAGE(
  327. aabb.expand(Vector3(2.5, 3, 0)).is_equal_approx(aabb),
  328. "expand() with contained point on positive edge should return the expected AABB.");
  329. CHECK_MESSAGE(
  330. aabb.expand(Vector3(-20, 0, 0)).is_equal_approx(AABB(Vector3(-20, 0, -2.5), Vector3(22.5, 7, 6))),
  331. "expand() with non-contained point should return the expected AABB.");
  332. }
  333. } // namespace TestAABB
  334. #endif // TEST_AABB_H