test_aabb.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /**************************************************************************/
  2. /* test_aabb.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_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. aabb.get_volume() == doctest::Approx(120),
  83. "get_volume() should return the expected value with positive size.");
  84. CHECK_MESSAGE(
  85. aabb.has_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. aabb.get_volume() == doctest::Approx(-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. aabb.get_volume() == doctest::Approx(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. aabb.get_volume() == doctest::Approx(-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_volume(),
  102. "Non-empty flat AABB should not have a volume.");
  103. CHECK_MESSAGE(
  104. !AABB().has_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_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_surface(),
  115. "Non-empty flat AABB should have a surface.");
  116. aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 0, 0));
  117. CHECK_MESSAGE(
  118. aabb.has_surface(),
  119. "Non-empty linear AABB should have a surface.");
  120. CHECK_MESSAGE(
  121. !AABB().has_surface(),
  122. "Empty AABB should not have an surface.");
  123. }
  124. TEST_CASE("[AABB] Intersection") {
  125. const AABB aabb_big = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  126. AABB aabb_small = AABB(Vector3(-1.5, 2, -2.5), Vector3(1, 1, 1));
  127. CHECK_MESSAGE(
  128. aabb_big.intersects(aabb_small),
  129. "intersects() with fully contained AABB (touching the edge) should return the expected result.");
  130. aabb_small = AABB(Vector3(0.5, 1.5, -2), Vector3(1, 1, 1));
  131. CHECK_MESSAGE(
  132. aabb_big.intersects(aabb_small),
  133. "intersects() with partially contained AABB (overflowing on Y axis) should return the expected result.");
  134. aabb_small = AABB(Vector3(10, -10, -10), Vector3(1, 1, 1));
  135. CHECK_MESSAGE(
  136. !aabb_big.intersects(aabb_small),
  137. "intersects() with non-contained AABB should return the expected result.");
  138. aabb_small = AABB(Vector3(-1.5, 2, -2.5), Vector3(1, 1, 1));
  139. CHECK_MESSAGE(
  140. aabb_big.intersection(aabb_small).is_equal_approx(aabb_small),
  141. "intersection() with fully contained AABB (touching the edge) should return the expected result.");
  142. aabb_small = AABB(Vector3(0.5, 1.5, -2), Vector3(1, 1, 1));
  143. CHECK_MESSAGE(
  144. aabb_big.intersection(aabb_small).is_equal_approx(AABB(Vector3(0.5, 2, -2), Vector3(1, 0.5, 1))),
  145. "intersection() with partially contained AABB (overflowing on Y axis) should return the expected result.");
  146. aabb_small = AABB(Vector3(10, -10, -10), Vector3(1, 1, 1));
  147. CHECK_MESSAGE(
  148. aabb_big.intersection(aabb_small).is_equal_approx(AABB()),
  149. "intersection() with non-contained AABB should return the expected result.");
  150. CHECK_MESSAGE(
  151. aabb_big.intersects_plane(Plane(Vector3(0, 1, 0), 4)),
  152. "intersects_plane() should return the expected result.");
  153. CHECK_MESSAGE(
  154. aabb_big.intersects_plane(Plane(Vector3(0, -1, 0), -4)),
  155. "intersects_plane() should return the expected result.");
  156. CHECK_MESSAGE(
  157. !aabb_big.intersects_plane(Plane(Vector3(0, 1, 0), 200)),
  158. "intersects_plane() should return the expected result.");
  159. CHECK_MESSAGE(
  160. aabb_big.intersects_segment(Vector3(1, 3, 0), Vector3(0, 3, 0)),
  161. "intersects_segment() should return the expected result.");
  162. CHECK_MESSAGE(
  163. aabb_big.intersects_segment(Vector3(0, 3, 0), Vector3(0, -300, 0)),
  164. "intersects_segment() should return the expected result.");
  165. CHECK_MESSAGE(
  166. aabb_big.intersects_segment(Vector3(-50, 3, -50), Vector3(50, 3, 50)),
  167. "intersects_segment() should return the expected result.");
  168. CHECK_MESSAGE(
  169. !aabb_big.intersects_segment(Vector3(-50, 25, -50), Vector3(50, 25, 50)),
  170. "intersects_segment() should return the expected result.");
  171. CHECK_MESSAGE(
  172. aabb_big.intersects_segment(Vector3(0, 3, 0), Vector3(0, 3, 0)),
  173. "intersects_segment() should return the expected result with segment of length 0.");
  174. CHECK_MESSAGE(
  175. !aabb_big.intersects_segment(Vector3(0, 300, 0), Vector3(0, 300, 0)),
  176. "intersects_segment() should return the expected result with segment of length 0.");
  177. }
  178. TEST_CASE("[AABB] Merging") {
  179. const AABB aabb_big = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  180. AABB aabb_small = AABB(Vector3(-1.5, 2, -2.5), Vector3(1, 1, 1));
  181. CHECK_MESSAGE(
  182. aabb_big.merge(aabb_small).is_equal_approx(aabb_big),
  183. "merge() with fully contained AABB (touching the edge) should return the expected result.");
  184. aabb_small = AABB(Vector3(0.5, 1.5, -2), Vector3(1, 1, 1));
  185. CHECK_MESSAGE(
  186. aabb_big.merge(aabb_small).is_equal_approx(AABB(Vector3(-1.5, 1.5, -2.5), Vector3(4, 5.5, 6))),
  187. "merge() with partially contained AABB (overflowing on Y axis) should return the expected result.");
  188. aabb_small = AABB(Vector3(10, -10, -10), Vector3(1, 1, 1));
  189. CHECK_MESSAGE(
  190. aabb_big.merge(aabb_small).is_equal_approx(AABB(Vector3(-1.5, -10, -10), Vector3(12.5, 17, 13.5))),
  191. "merge() with non-contained AABB should return the expected result.");
  192. }
  193. TEST_CASE("[AABB] Encloses") {
  194. const AABB aabb_big = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  195. AABB aabb_small = AABB(Vector3(-1.5, 2, -2.5), Vector3(1, 1, 1));
  196. CHECK_MESSAGE(
  197. aabb_big.encloses(aabb_small),
  198. "encloses() with fully contained AABB (touching the edge) should return the expected result.");
  199. aabb_small = AABB(Vector3(0.5, 1.5, -2), Vector3(1, 1, 1));
  200. CHECK_MESSAGE(
  201. !aabb_big.encloses(aabb_small),
  202. "encloses() with partially contained AABB (overflowing on Y axis) should return the expected result.");
  203. aabb_small = AABB(Vector3(10, -10, -10), Vector3(1, 1, 1));
  204. CHECK_MESSAGE(
  205. !aabb_big.encloses(aabb_small),
  206. "encloses() with non-contained AABB should return the expected result.");
  207. }
  208. TEST_CASE("[AABB] Get endpoints") {
  209. const AABB aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  210. CHECK_MESSAGE(
  211. aabb.get_endpoint(0).is_equal_approx(Vector3(-1.5, 2, -2.5)),
  212. "The endpoint at index 0 should match the expected value.");
  213. CHECK_MESSAGE(
  214. aabb.get_endpoint(1).is_equal_approx(Vector3(-1.5, 2, 3.5)),
  215. "The endpoint at index 1 should match the expected value.");
  216. CHECK_MESSAGE(
  217. aabb.get_endpoint(2).is_equal_approx(Vector3(-1.5, 7, -2.5)),
  218. "The endpoint at index 2 should match the expected value.");
  219. CHECK_MESSAGE(
  220. aabb.get_endpoint(3).is_equal_approx(Vector3(-1.5, 7, 3.5)),
  221. "The endpoint at index 3 should match the expected value.");
  222. CHECK_MESSAGE(
  223. aabb.get_endpoint(4).is_equal_approx(Vector3(2.5, 2, -2.5)),
  224. "The endpoint at index 4 should match the expected value.");
  225. CHECK_MESSAGE(
  226. aabb.get_endpoint(5).is_equal_approx(Vector3(2.5, 2, 3.5)),
  227. "The endpoint at index 5 should match the expected value.");
  228. CHECK_MESSAGE(
  229. aabb.get_endpoint(6).is_equal_approx(Vector3(2.5, 7, -2.5)),
  230. "The endpoint at index 6 should match the expected value.");
  231. CHECK_MESSAGE(
  232. aabb.get_endpoint(7).is_equal_approx(Vector3(2.5, 7, 3.5)),
  233. "The endpoint at index 7 should match the expected value.");
  234. ERR_PRINT_OFF;
  235. CHECK_MESSAGE(
  236. aabb.get_endpoint(8).is_equal_approx(Vector3()),
  237. "The endpoint at invalid index 8 should match the expected value.");
  238. CHECK_MESSAGE(
  239. aabb.get_endpoint(-1).is_equal_approx(Vector3()),
  240. "The endpoint at invalid index -1 should match the expected value.");
  241. ERR_PRINT_ON;
  242. }
  243. TEST_CASE("[AABB] Get longest/shortest axis") {
  244. const AABB aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  245. CHECK_MESSAGE(
  246. aabb.get_longest_axis() == Vector3(0, 0, 1),
  247. "get_longest_axis() should return the expected value.");
  248. CHECK_MESSAGE(
  249. aabb.get_longest_axis_index() == Vector3::AXIS_Z,
  250. "get_longest_axis_index() should return the expected value.");
  251. CHECK_MESSAGE(
  252. aabb.get_longest_axis_size() == 6,
  253. "get_longest_axis_size() should return the expected value.");
  254. CHECK_MESSAGE(
  255. aabb.get_shortest_axis() == Vector3(1, 0, 0),
  256. "get_shortest_axis() should return the expected value.");
  257. CHECK_MESSAGE(
  258. aabb.get_shortest_axis_index() == Vector3::AXIS_X,
  259. "get_shortest_axis_index() should return the expected value.");
  260. CHECK_MESSAGE(
  261. aabb.get_shortest_axis_size() == 4,
  262. "get_shortest_axis_size() should return the expected value.");
  263. }
  264. TEST_CASE("[AABB] Get support") {
  265. const AABB aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  266. CHECK_MESSAGE(
  267. aabb.get_support(Vector3(1, 0, 0)).is_equal_approx(Vector3(2.5, 2, -2.5)),
  268. "get_support() should return the expected value.");
  269. CHECK_MESSAGE(
  270. aabb.get_support(Vector3(0.5, 1, 0)).is_equal_approx(Vector3(2.5, 7, -2.5)),
  271. "get_support() should return the expected value.");
  272. CHECK_MESSAGE(
  273. aabb.get_support(Vector3(0.5, 1, -400)).is_equal_approx(Vector3(2.5, 7, -2.5)),
  274. "get_support() should return the expected value.");
  275. CHECK_MESSAGE(
  276. aabb.get_support(Vector3(0, -1, 0)).is_equal_approx(Vector3(-1.5, 2, -2.5)),
  277. "get_support() should return the expected value.");
  278. CHECK_MESSAGE(
  279. aabb.get_support(Vector3(0, -0.1, 0)).is_equal_approx(Vector3(-1.5, 2, -2.5)),
  280. "get_support() should return the expected value.");
  281. CHECK_MESSAGE(
  282. aabb.get_support(Vector3()).is_equal_approx(Vector3(-1.5, 2, -2.5)),
  283. "get_support() should return the expected value with a null vector.");
  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(-20, 0, 0)),
  307. "has_point() with non-contained point should return the expected value.");
  308. CHECK_MESSAGE(
  309. aabb.has_point(Vector3(-1.5, 3, 0)),
  310. "has_point() with positive size should include point on near face (X axis).");
  311. CHECK_MESSAGE(
  312. aabb.has_point(Vector3(2.5, 3, 0)),
  313. "has_point() with positive size should include point on far face (X axis).");
  314. CHECK_MESSAGE(
  315. aabb.has_point(Vector3(0, 2, 0)),
  316. "has_point() with positive size should include point on near face (Y axis).");
  317. CHECK_MESSAGE(
  318. aabb.has_point(Vector3(0, 7, 0)),
  319. "has_point() with positive size should include point on far face (Y axis).");
  320. CHECK_MESSAGE(
  321. aabb.has_point(Vector3(0, 3, -2.5)),
  322. "has_point() with positive size should include point on near face (Z axis).");
  323. CHECK_MESSAGE(
  324. aabb.has_point(Vector3(0, 3, 3.5)),
  325. "has_point() with positive size should include point on far face (Z axis).");
  326. }
  327. TEST_CASE("[AABB] Expanding") {
  328. const AABB aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  329. CHECK_MESSAGE(
  330. aabb.expand(Vector3(-1, 3, 0)).is_equal_approx(aabb),
  331. "expand() with contained point should return the expected AABB.");
  332. CHECK_MESSAGE(
  333. aabb.expand(Vector3(2, 3, 0)).is_equal_approx(aabb),
  334. "expand() with contained point should return the expected AABB.");
  335. CHECK_MESSAGE(
  336. aabb.expand(Vector3(-1.5, 3, 0)).is_equal_approx(aabb),
  337. "expand() with contained point on negative edge should return the expected AABB.");
  338. CHECK_MESSAGE(
  339. aabb.expand(Vector3(2.5, 3, 0)).is_equal_approx(aabb),
  340. "expand() with contained point on positive edge should return the expected AABB.");
  341. CHECK_MESSAGE(
  342. aabb.expand(Vector3(-20, 0, 0)).is_equal_approx(AABB(Vector3(-20, 0, -2.5), Vector3(22.5, 7, 6))),
  343. "expand() with non-contained point should return the expected AABB.");
  344. }
  345. TEST_CASE("[AABB] Finite number checks") {
  346. const Vector3 x(0, 1, 2);
  347. const Vector3 infinite(NAN, NAN, NAN);
  348. CHECK_MESSAGE(
  349. AABB(x, x).is_finite(),
  350. "AABB with all components finite should be finite");
  351. CHECK_FALSE_MESSAGE(
  352. AABB(infinite, x).is_finite(),
  353. "AABB with one component infinite should not be finite.");
  354. CHECK_FALSE_MESSAGE(
  355. AABB(x, infinite).is_finite(),
  356. "AABB with one component infinite should not be finite.");
  357. CHECK_FALSE_MESSAGE(
  358. AABB(infinite, infinite).is_finite(),
  359. "AABB with two components infinite should not be finite.");
  360. }
  361. } // namespace TestAABB
  362. #endif // TEST_AABB_H