test_aabb.h 16 KB

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