test_aabb.h 16 KB

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