test_aabb.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. #pragma once
  31. #include "core/math/aabb.h"
  32. #include "tests/test_macros.h"
  33. namespace TestAABB {
  34. TEST_CASE("[AABB] Constructor methods") {
  35. constexpr AABB aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  36. constexpr AABB aabb_copy = AABB(aabb);
  37. CHECK_MESSAGE(
  38. aabb == aabb_copy,
  39. "AABBs created with the same dimensions but by different methods should be equal.");
  40. }
  41. TEST_CASE("[AABB] String conversion") {
  42. CHECK_MESSAGE(
  43. String(AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6))) == "[P: (-1.5, 2.0, -2.5), S: (4.0, 5.0, 6.0)]",
  44. "The string representation should match the expected value.");
  45. }
  46. TEST_CASE("[AABB] Basic getters") {
  47. constexpr AABB aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  48. CHECK_MESSAGE(
  49. aabb.get_position().is_equal_approx(Vector3(-1.5, 2, -2.5)),
  50. "get_position() should return the expected value.");
  51. CHECK_MESSAGE(
  52. aabb.get_size().is_equal_approx(Vector3(4, 5, 6)),
  53. "get_size() should return the expected value.");
  54. CHECK_MESSAGE(
  55. aabb.get_end().is_equal_approx(Vector3(2.5, 7, 3.5)),
  56. "get_end() should return the expected value.");
  57. CHECK_MESSAGE(
  58. aabb.get_center().is_equal_approx(Vector3(0.5, 4.5, 0.5)),
  59. "get_center() 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] Volume getters") {
  79. AABB aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  80. CHECK_MESSAGE(
  81. aabb.get_volume() == doctest::Approx(120),
  82. "get_volume() should return the expected value with positive size.");
  83. CHECK_MESSAGE(
  84. aabb.has_volume(),
  85. "Non-empty volumetric AABB should have a volume.");
  86. aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(-4, 5, 6));
  87. CHECK_MESSAGE(
  88. aabb.get_volume() == doctest::Approx(-120),
  89. "get_volume() 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. aabb.get_volume() == doctest::Approx(120),
  93. "get_volume() 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. aabb.get_volume() == doctest::Approx(-120),
  97. "get_volume() 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_volume(),
  101. "Non-empty flat AABB should not have a volume.");
  102. CHECK_MESSAGE(
  103. !AABB().has_volume(),
  104. "Empty AABB should not have a volume.");
  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_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_surface(),
  114. "Non-empty flat AABB should have a surface.");
  115. aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 0, 0));
  116. CHECK_MESSAGE(
  117. aabb.has_surface(),
  118. "Non-empty linear AABB should have a surface.");
  119. CHECK_MESSAGE(
  120. !AABB().has_surface(),
  121. "Empty AABB should not have an surface.");
  122. }
  123. TEST_CASE("[AABB] Intersection") {
  124. constexpr AABB aabb_big = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  125. AABB aabb_small = AABB(Vector3(-1.5, 2, -2.5), Vector3(1, 1, 1));
  126. CHECK_MESSAGE(
  127. aabb_big.intersects(aabb_small),
  128. "intersects() with fully contained AABB (touching the edge) should return the expected result.");
  129. aabb_small = AABB(Vector3(0.5, 1.5, -2), Vector3(1, 1, 1));
  130. CHECK_MESSAGE(
  131. aabb_big.intersects(aabb_small),
  132. "intersects() with partially contained AABB (overflowing on Y axis) should return the expected result.");
  133. aabb_small = AABB(Vector3(10, -10, -10), Vector3(1, 1, 1));
  134. CHECK_MESSAGE(
  135. !aabb_big.intersects(aabb_small),
  136. "intersects() with non-contained AABB should return the expected result.");
  137. aabb_small = AABB(Vector3(-1.5, 2, -2.5), Vector3(1, 1, 1));
  138. CHECK_MESSAGE(
  139. aabb_big.intersection(aabb_small).is_equal_approx(aabb_small),
  140. "intersection() with fully contained AABB (touching the edge) should return the expected result.");
  141. aabb_small = AABB(Vector3(0.5, 1.5, -2), Vector3(1, 1, 1));
  142. CHECK_MESSAGE(
  143. aabb_big.intersection(aabb_small).is_equal_approx(AABB(Vector3(0.5, 2, -2), Vector3(1, 0.5, 1))),
  144. "intersection() with partially contained AABB (overflowing on Y axis) should return the expected result.");
  145. aabb_small = AABB(Vector3(10, -10, -10), Vector3(1, 1, 1));
  146. CHECK_MESSAGE(
  147. aabb_big.intersection(aabb_small).is_equal_approx(AABB()),
  148. "intersection() with non-contained AABB 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), -4)),
  154. "intersects_plane() should return the expected result.");
  155. CHECK_MESSAGE(
  156. !aabb_big.intersects_plane(Plane(Vector3(0, 1, 0), 200)),
  157. "intersects_plane() should return the expected result.");
  158. CHECK_MESSAGE(
  159. aabb_big.intersects_segment(Vector3(1, 3, 0), Vector3(0, 3, 0)),
  160. "intersects_segment() should return the expected result.");
  161. CHECK_MESSAGE(
  162. aabb_big.intersects_segment(Vector3(0, 3, 0), Vector3(0, -300, 0)),
  163. "intersects_segment() should return the expected result.");
  164. CHECK_MESSAGE(
  165. aabb_big.intersects_segment(Vector3(-50, 3, -50), Vector3(50, 3, 50)),
  166. "intersects_segment() should return the expected result.");
  167. CHECK_MESSAGE(
  168. !aabb_big.intersects_segment(Vector3(-50, 25, -50), Vector3(50, 25, 50)),
  169. "intersects_segment() should return the expected result.");
  170. CHECK_MESSAGE(
  171. aabb_big.intersects_segment(Vector3(0, 3, 0), Vector3(0, 3, 0)),
  172. "intersects_segment() should return the expected result with segment of length 0.");
  173. CHECK_MESSAGE(
  174. !aabb_big.intersects_segment(Vector3(0, 300, 0), Vector3(0, 300, 0)),
  175. "intersects_segment() should return the expected result with segment of length 0.");
  176. CHECK_MESSAGE( // Simple ray intersection test.
  177. aabb_big.intersects_ray(Vector3(-100, 3, 0), Vector3(1, 0, 0)),
  178. "intersects_ray() should return true when ray points directly to AABB from outside.");
  179. CHECK_MESSAGE( // Ray parallel to an edge.
  180. !aabb_big.intersects_ray(Vector3(10, 10, 0), Vector3(0, 1, 0)),
  181. "intersects_ray() should return false for ray parallel and outside of AABB.");
  182. CHECK_MESSAGE( // Ray origin inside aabb.
  183. aabb_big.intersects_ray(Vector3(1, 1, 1), Vector3(0, 1, 0)),
  184. "intersects_ray() should return true for rays originating inside the AABB.");
  185. CHECK_MESSAGE( // Ray pointing away from aabb.
  186. !aabb_big.intersects_ray(Vector3(-10, 0, 0), Vector3(-1, 0, 0)),
  187. "intersects_ray() should return false when ray points away from AABB.");
  188. CHECK_MESSAGE( // Ray along a diagonal of aabb.
  189. aabb_big.intersects_ray(Vector3(0, 0, 0), Vector3(1, 1, 1)),
  190. "intersects_ray() should return true for rays along the AABB diagonal.");
  191. CHECK_MESSAGE( // Ray originating at aabb edge.
  192. aabb_big.intersects_ray(aabb_big.position, Vector3(-1, 0, 0)),
  193. "intersects_ray() should return true for rays starting on AABB's edge.");
  194. CHECK_MESSAGE( // Ray with zero direction inside.
  195. aabb_big.intersects_ray(Vector3(-1, 3, -2), Vector3(0, 0, 0)),
  196. "intersects_ray() should return true because its inside.");
  197. CHECK_MESSAGE( // Ray with zero direction outside.
  198. !aabb_big.intersects_ray(Vector3(-1000, 3, -2), Vector3(0, 0, 0)),
  199. "intersects_ray() should return false for being outside.");
  200. // Finding ray intersections.
  201. constexpr AABB aabb_simple = AABB(Vector3(), Vector3(1, 1, 1));
  202. bool inside = false;
  203. Vector3 intersection_point;
  204. Vector3 intersection_normal;
  205. // Borders.
  206. aabb_simple.find_intersects_ray(Vector3(0.5, 0, 0.5), Vector3(0, 1, 0), inside, &intersection_point, &intersection_normal);
  207. CHECK_MESSAGE(inside == false, "find_intersects_ray() should return outside on borders.");
  208. CHECK_MESSAGE(intersection_point.is_equal_approx(Vector3(0.5, 0, 0.5)), "find_intersects_ray() border intersection point incorrect.");
  209. CHECK_MESSAGE(intersection_normal.is_equal_approx(Vector3(0, -1, 0)), "find_intersects_ray() border intersection normal incorrect.");
  210. aabb_simple.find_intersects_ray(Vector3(0.5, 1, 0.5), Vector3(0, -1, 0), inside, &intersection_point, &intersection_normal);
  211. CHECK_MESSAGE(inside == false, "find_intersects_ray() should return outside on borders.");
  212. CHECK_MESSAGE(intersection_point.is_equal_approx(Vector3(0.5, 1, 0.5)), "find_intersects_ray() border intersection point incorrect.");
  213. CHECK_MESSAGE(intersection_normal.is_equal_approx(Vector3(0, 1, 0)), "find_intersects_ray() border intersection normal incorrect.");
  214. // Inside.
  215. aabb_simple.find_intersects_ray(Vector3(0.5, 0.1, 0.5), Vector3(0, 1, 0), inside, &intersection_point, &intersection_normal);
  216. CHECK_MESSAGE(inside == true, "find_intersects_ray() should return inside when inside.");
  217. CHECK_MESSAGE(intersection_point.is_equal_approx(Vector3(0.5, 0, 0.5)), "find_intersects_ray() inside backtracking intersection point incorrect.");
  218. CHECK_MESSAGE(intersection_normal.is_equal_approx(Vector3(0, -1, 0)), "find_intersects_ray() inside intersection normal incorrect.");
  219. // Zero sized AABB.
  220. constexpr AABB aabb_zero = AABB(Vector3(), Vector3(1, 0, 1));
  221. aabb_zero.find_intersects_ray(Vector3(0.5, 0, 0.5), Vector3(0, 1, 0), inside, &intersection_point, &intersection_normal);
  222. CHECK_MESSAGE(inside == false, "find_intersects_ray() should return outside on borders of zero sized AABB.");
  223. CHECK_MESSAGE(intersection_point.is_equal_approx(Vector3(0.5, 0, 0.5)), "find_intersects_ray() border intersection point incorrect for zero sized AABB.");
  224. CHECK_MESSAGE(intersection_normal.is_equal_approx(Vector3(0, -1, 0)), "find_intersects_ray() border intersection normal incorrect for zero sized AABB.");
  225. aabb_zero.find_intersects_ray(Vector3(0.5, 0, 0.5), Vector3(0, -1, 0), inside, &intersection_point, &intersection_normal);
  226. CHECK_MESSAGE(inside == false, "find_intersects_ray() should return outside on borders of zero sized AABB.");
  227. CHECK_MESSAGE(intersection_point.is_equal_approx(Vector3(0.5, 0, 0.5)), "find_intersects_ray() border intersection point incorrect for zero sized AABB.");
  228. CHECK_MESSAGE(intersection_normal.is_equal_approx(Vector3(0, 1, 0)), "find_intersects_ray() border intersection normal incorrect for zero sized AABB.");
  229. aabb_zero.find_intersects_ray(Vector3(0.5, -1, 0.5), Vector3(0, 1, 0), inside, &intersection_point, &intersection_normal);
  230. CHECK_MESSAGE(inside == false, "find_intersects_ray() should return outside on borders of zero sized AABB.");
  231. CHECK_MESSAGE(intersection_point.is_equal_approx(Vector3(0.5, 0, 0.5)), "find_intersects_ray() border intersection point incorrect for zero sized AABB.");
  232. CHECK_MESSAGE(intersection_normal.is_equal_approx(Vector3(0, -1, 0)), "find_intersects_ray() border intersection normal incorrect for zero sized AABB.");
  233. }
  234. TEST_CASE("[AABB] Merging") {
  235. constexpr AABB aabb_big = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  236. AABB aabb_small = AABB(Vector3(-1.5, 2, -2.5), Vector3(1, 1, 1));
  237. CHECK_MESSAGE(
  238. aabb_big.merge(aabb_small).is_equal_approx(aabb_big),
  239. "merge() with fully contained AABB (touching the edge) should return the expected result.");
  240. aabb_small = AABB(Vector3(0.5, 1.5, -2), Vector3(1, 1, 1));
  241. CHECK_MESSAGE(
  242. aabb_big.merge(aabb_small).is_equal_approx(AABB(Vector3(-1.5, 1.5, -2.5), Vector3(4, 5.5, 6))),
  243. "merge() with partially contained AABB (overflowing on Y axis) should return the expected result.");
  244. aabb_small = AABB(Vector3(10, -10, -10), Vector3(1, 1, 1));
  245. CHECK_MESSAGE(
  246. aabb_big.merge(aabb_small).is_equal_approx(AABB(Vector3(-1.5, -10, -10), Vector3(12.5, 17, 13.5))),
  247. "merge() with non-contained AABB should return the expected result.");
  248. }
  249. TEST_CASE("[AABB] Encloses") {
  250. constexpr AABB aabb_big = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  251. CHECK_MESSAGE(
  252. aabb_big.encloses(aabb_big),
  253. "encloses() with itself should return the expected result.");
  254. AABB aabb_small = AABB(Vector3(-1.5, 2, -2.5), Vector3(1, 1, 1));
  255. CHECK_MESSAGE(
  256. aabb_big.encloses(aabb_small),
  257. "encloses() with fully contained AABB (touching the edge) should return the expected result.");
  258. aabb_small = AABB(Vector3(1.5, 6, 2.5), Vector3(1, 1, 1));
  259. CHECK_MESSAGE(
  260. aabb_big.encloses(aabb_small),
  261. "encloses() with fully contained AABB (touching the edge) should return the expected result.");
  262. aabb_small = AABB(Vector3(0.5, 1.5, -2), Vector3(1, 1, 1));
  263. CHECK_MESSAGE(
  264. !aabb_big.encloses(aabb_small),
  265. "encloses() with partially contained AABB (overflowing on Y axis) should return the expected result.");
  266. aabb_small = AABB(Vector3(10, -10, -10), Vector3(1, 1, 1));
  267. CHECK_MESSAGE(
  268. !aabb_big.encloses(aabb_small),
  269. "encloses() with non-contained AABB should return the expected result.");
  270. }
  271. TEST_CASE("[AABB] Get endpoints") {
  272. constexpr AABB aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  273. CHECK_MESSAGE(
  274. aabb.get_endpoint(0).is_equal_approx(Vector3(-1.5, 2, -2.5)),
  275. "The endpoint at index 0 should match the expected value.");
  276. CHECK_MESSAGE(
  277. aabb.get_endpoint(1).is_equal_approx(Vector3(-1.5, 2, 3.5)),
  278. "The endpoint at index 1 should match the expected value.");
  279. CHECK_MESSAGE(
  280. aabb.get_endpoint(2).is_equal_approx(Vector3(-1.5, 7, -2.5)),
  281. "The endpoint at index 2 should match the expected value.");
  282. CHECK_MESSAGE(
  283. aabb.get_endpoint(3).is_equal_approx(Vector3(-1.5, 7, 3.5)),
  284. "The endpoint at index 3 should match the expected value.");
  285. CHECK_MESSAGE(
  286. aabb.get_endpoint(4).is_equal_approx(Vector3(2.5, 2, -2.5)),
  287. "The endpoint at index 4 should match the expected value.");
  288. CHECK_MESSAGE(
  289. aabb.get_endpoint(5).is_equal_approx(Vector3(2.5, 2, 3.5)),
  290. "The endpoint at index 5 should match the expected value.");
  291. CHECK_MESSAGE(
  292. aabb.get_endpoint(6).is_equal_approx(Vector3(2.5, 7, -2.5)),
  293. "The endpoint at index 6 should match the expected value.");
  294. CHECK_MESSAGE(
  295. aabb.get_endpoint(7).is_equal_approx(Vector3(2.5, 7, 3.5)),
  296. "The endpoint at index 7 should match the expected value.");
  297. ERR_PRINT_OFF;
  298. CHECK_MESSAGE(
  299. aabb.get_endpoint(8).is_equal_approx(Vector3()),
  300. "The endpoint at invalid index 8 should match the expected value.");
  301. CHECK_MESSAGE(
  302. aabb.get_endpoint(-1).is_equal_approx(Vector3()),
  303. "The endpoint at invalid index -1 should match the expected value.");
  304. ERR_PRINT_ON;
  305. }
  306. TEST_CASE("[AABB] Get longest/shortest axis") {
  307. constexpr AABB aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  308. CHECK_MESSAGE(
  309. aabb.get_longest_axis() == Vector3(0, 0, 1),
  310. "get_longest_axis() should return the expected value.");
  311. CHECK_MESSAGE(
  312. aabb.get_longest_axis_index() == Vector3::AXIS_Z,
  313. "get_longest_axis_index() should return the expected value.");
  314. CHECK_MESSAGE(
  315. aabb.get_longest_axis_size() == 6,
  316. "get_longest_axis_size() should return the expected value.");
  317. CHECK_MESSAGE(
  318. aabb.get_shortest_axis() == Vector3(1, 0, 0),
  319. "get_shortest_axis() should return the expected value.");
  320. CHECK_MESSAGE(
  321. aabb.get_shortest_axis_index() == Vector3::AXIS_X,
  322. "get_shortest_axis_index() should return the expected value.");
  323. CHECK_MESSAGE(
  324. aabb.get_shortest_axis_size() == 4,
  325. "get_shortest_axis_size() should return the expected value.");
  326. }
  327. TEST_CASE("[AABB] Get support") {
  328. constexpr AABB aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  329. CHECK_MESSAGE(
  330. aabb.get_support(Vector3(1, 0, 0)) == Vector3(2.5, 2, -2.5),
  331. "get_support() should return the expected value.");
  332. CHECK_MESSAGE(
  333. aabb.get_support(Vector3(0.5, 1, 1)) == Vector3(2.5, 7, 3.5),
  334. "get_support() should return the expected value.");
  335. CHECK_MESSAGE(
  336. aabb.get_support(Vector3(0.5, 1, -400)) == Vector3(2.5, 7, -2.5),
  337. "get_support() should return the expected value.");
  338. CHECK_MESSAGE(
  339. aabb.get_support(Vector3(0, -1, 0)) == Vector3(-1.5, 2, -2.5),
  340. "get_support() should return the expected value.");
  341. CHECK_MESSAGE(
  342. aabb.get_support(Vector3(0, -0.1, 0)) == Vector3(-1.5, 2, -2.5),
  343. "get_support() should return the expected value.");
  344. CHECK_MESSAGE(
  345. aabb.get_support(Vector3()) == Vector3(-1.5, 2, -2.5),
  346. "get_support() should return the AABB position when given a zero vector.");
  347. }
  348. TEST_CASE("[AABB] Grow") {
  349. constexpr AABB aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  350. CHECK_MESSAGE(
  351. aabb.grow(0.25).is_equal_approx(AABB(Vector3(-1.75, 1.75, -2.75), Vector3(4.5, 5.5, 6.5))),
  352. "grow() with positive value should return the expected AABB.");
  353. CHECK_MESSAGE(
  354. aabb.grow(-0.25).is_equal_approx(AABB(Vector3(-1.25, 2.25, -2.25), Vector3(3.5, 4.5, 5.5))),
  355. "grow() with negative value should return the expected AABB.");
  356. CHECK_MESSAGE(
  357. aabb.grow(-10).is_equal_approx(AABB(Vector3(8.5, 12, 7.5), Vector3(-16, -15, -14))),
  358. "grow() with large negative value should return the expected AABB.");
  359. }
  360. TEST_CASE("[AABB] Has point") {
  361. constexpr AABB aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  362. CHECK_MESSAGE(
  363. aabb.has_point(Vector3(-1, 3, 0)),
  364. "has_point() with contained point should return the expected value.");
  365. CHECK_MESSAGE(
  366. aabb.has_point(Vector3(2, 3, 0)),
  367. "has_point() with contained point should return the expected value.");
  368. CHECK_MESSAGE(
  369. !aabb.has_point(Vector3(-20, 0, 0)),
  370. "has_point() with non-contained point should return the expected value.");
  371. CHECK_MESSAGE(
  372. aabb.has_point(Vector3(-1.5, 3, 0)),
  373. "has_point() with positive size should include point on near face (X axis).");
  374. CHECK_MESSAGE(
  375. aabb.has_point(Vector3(2.5, 3, 0)),
  376. "has_point() with positive size should include point on far face (X axis).");
  377. CHECK_MESSAGE(
  378. aabb.has_point(Vector3(0, 2, 0)),
  379. "has_point() with positive size should include point on near face (Y axis).");
  380. CHECK_MESSAGE(
  381. aabb.has_point(Vector3(0, 7, 0)),
  382. "has_point() with positive size should include point on far face (Y axis).");
  383. CHECK_MESSAGE(
  384. aabb.has_point(Vector3(0, 3, -2.5)),
  385. "has_point() with positive size should include point on near face (Z axis).");
  386. CHECK_MESSAGE(
  387. aabb.has_point(Vector3(0, 3, 3.5)),
  388. "has_point() with positive size should include point on far face (Z axis).");
  389. }
  390. TEST_CASE("[AABB] Expanding") {
  391. constexpr AABB aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  392. CHECK_MESSAGE(
  393. aabb.expand(Vector3(-1, 3, 0)).is_equal_approx(aabb),
  394. "expand() with contained point should return the expected AABB.");
  395. CHECK_MESSAGE(
  396. aabb.expand(Vector3(2, 3, 0)).is_equal_approx(aabb),
  397. "expand() with contained point should return the expected AABB.");
  398. CHECK_MESSAGE(
  399. aabb.expand(Vector3(-1.5, 3, 0)).is_equal_approx(aabb),
  400. "expand() with contained point on negative edge should return the expected AABB.");
  401. CHECK_MESSAGE(
  402. aabb.expand(Vector3(2.5, 3, 0)).is_equal_approx(aabb),
  403. "expand() with contained point on positive edge should return the expected AABB.");
  404. CHECK_MESSAGE(
  405. aabb.expand(Vector3(-20, 0, 0)).is_equal_approx(AABB(Vector3(-20, 0, -2.5), Vector3(22.5, 7, 6))),
  406. "expand() with non-contained point should return the expected AABB.");
  407. }
  408. TEST_CASE("[AABB] Finite number checks") {
  409. constexpr Vector3 x(0, 1, 2);
  410. constexpr Vector3 infinite(Math::NaN, Math::NaN, Math::NaN);
  411. CHECK_MESSAGE(
  412. AABB(x, x).is_finite(),
  413. "AABB with all components finite should be finite");
  414. CHECK_FALSE_MESSAGE(
  415. AABB(infinite, x).is_finite(),
  416. "AABB with one component infinite should not be finite.");
  417. CHECK_FALSE_MESSAGE(
  418. AABB(x, infinite).is_finite(),
  419. "AABB with one component infinite should not be finite.");
  420. CHECK_FALSE_MESSAGE(
  421. AABB(infinite, infinite).is_finite(),
  422. "AABB with two components infinite should not be finite.");
  423. }
  424. } // namespace TestAABB