test_aabb.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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.0, -2.5), S: (4.0, 5.0, 6.0)]",
  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. CHECK_MESSAGE( // Simple ray intersection test.
  178. aabb_big.intersects_ray(Vector3(-100, 3, 0), Vector3(1, 0, 0)),
  179. "intersects_ray() should return true when ray points directly to AABB from outside.");
  180. CHECK_MESSAGE( // Ray parallel to an edge.
  181. !aabb_big.intersects_ray(Vector3(10, 10, 0), Vector3(0, 1, 0)),
  182. "intersects_ray() should return false for ray parallel and outside of AABB.");
  183. CHECK_MESSAGE( // Ray origin inside aabb.
  184. aabb_big.intersects_ray(Vector3(1, 1, 1), Vector3(0, 1, 0)),
  185. "intersects_ray() should return true for rays originating inside the AABB.");
  186. CHECK_MESSAGE( // Ray pointing away from aabb.
  187. !aabb_big.intersects_ray(Vector3(-10, 0, 0), Vector3(-1, 0, 0)),
  188. "intersects_ray() should return false when ray points away from AABB.");
  189. CHECK_MESSAGE( // Ray along a diagonal of aabb.
  190. aabb_big.intersects_ray(Vector3(0, 0, 0), Vector3(1, 1, 1)),
  191. "intersects_ray() should return true for rays along the AABB diagonal.");
  192. CHECK_MESSAGE( // Ray originating at aabb edge.
  193. aabb_big.intersects_ray(aabb_big.position, Vector3(-1, 0, 0)),
  194. "intersects_ray() should return true for rays starting on AABB's edge.");
  195. CHECK_MESSAGE( // Ray with zero direction inside.
  196. aabb_big.intersects_ray(Vector3(-1, 3, -2), Vector3(0, 0, 0)),
  197. "intersects_ray() should return true because its inside.");
  198. CHECK_MESSAGE( // Ray with zero direction outside.
  199. !aabb_big.intersects_ray(Vector3(-1000, 3, -2), Vector3(0, 0, 0)),
  200. "intersects_ray() should return false for being outside.");
  201. // Finding ray intersections.
  202. const AABB aabb_simple = AABB(Vector3(), Vector3(1, 1, 1));
  203. bool inside = false;
  204. Vector3 intersection_point;
  205. Vector3 intersection_normal;
  206. // Borders.
  207. aabb_simple.find_intersects_ray(Vector3(0.5, 0, 0.5), Vector3(0, 1, 0), inside, &intersection_point, &intersection_normal);
  208. CHECK_MESSAGE(inside == false, "find_intersects_ray() should return outside on borders.");
  209. CHECK_MESSAGE(intersection_point.is_equal_approx(Vector3(0.5, 0, 0.5)), "find_intersects_ray() border intersection point incorrect.");
  210. CHECK_MESSAGE(intersection_normal.is_equal_approx(Vector3(0, -1, 0)), "find_intersects_ray() border intersection normal incorrect.");
  211. aabb_simple.find_intersects_ray(Vector3(0.5, 1, 0.5), Vector3(0, -1, 0), inside, &intersection_point, &intersection_normal);
  212. CHECK_MESSAGE(inside == false, "find_intersects_ray() should return outside on borders.");
  213. CHECK_MESSAGE(intersection_point.is_equal_approx(Vector3(0.5, 1, 0.5)), "find_intersects_ray() border intersection point incorrect.");
  214. CHECK_MESSAGE(intersection_normal.is_equal_approx(Vector3(0, 1, 0)), "find_intersects_ray() border intersection normal incorrect.");
  215. // Inside.
  216. aabb_simple.find_intersects_ray(Vector3(0.5, 0.1, 0.5), Vector3(0, 1, 0), inside, &intersection_point, &intersection_normal);
  217. CHECK_MESSAGE(inside == true, "find_intersects_ray() should return inside when inside.");
  218. CHECK_MESSAGE(intersection_point.is_equal_approx(Vector3(0.5, 0, 0.5)), "find_intersects_ray() inside backtracking intersection point incorrect.");
  219. CHECK_MESSAGE(intersection_normal.is_equal_approx(Vector3(0, -1, 0)), "find_intersects_ray() inside intersection normal incorrect.");
  220. // Zero sized AABB.
  221. const AABB aabb_zero = AABB(Vector3(), Vector3(1, 0, 1));
  222. aabb_zero.find_intersects_ray(Vector3(0.5, 0, 0.5), Vector3(0, 1, 0), inside, &intersection_point, &intersection_normal);
  223. CHECK_MESSAGE(inside == false, "find_intersects_ray() should return outside on borders of zero sized AABB.");
  224. CHECK_MESSAGE(intersection_point.is_equal_approx(Vector3(0.5, 0, 0.5)), "find_intersects_ray() border intersection point incorrect for zero sized AABB.");
  225. CHECK_MESSAGE(intersection_normal.is_equal_approx(Vector3(0, -1, 0)), "find_intersects_ray() border intersection normal incorrect for zero sized AABB.");
  226. aabb_zero.find_intersects_ray(Vector3(0.5, 0, 0.5), Vector3(0, -1, 0), inside, &intersection_point, &intersection_normal);
  227. CHECK_MESSAGE(inside == false, "find_intersects_ray() should return outside on borders of zero sized AABB.");
  228. CHECK_MESSAGE(intersection_point.is_equal_approx(Vector3(0.5, 0, 0.5)), "find_intersects_ray() border intersection point incorrect for zero sized AABB.");
  229. CHECK_MESSAGE(intersection_normal.is_equal_approx(Vector3(0, 1, 0)), "find_intersects_ray() border intersection normal incorrect for zero sized AABB.");
  230. aabb_zero.find_intersects_ray(Vector3(0.5, -1, 0.5), Vector3(0, 1, 0), inside, &intersection_point, &intersection_normal);
  231. CHECK_MESSAGE(inside == false, "find_intersects_ray() should return outside on borders of zero sized AABB.");
  232. CHECK_MESSAGE(intersection_point.is_equal_approx(Vector3(0.5, 0, 0.5)), "find_intersects_ray() border intersection point incorrect for zero sized AABB.");
  233. CHECK_MESSAGE(intersection_normal.is_equal_approx(Vector3(0, -1, 0)), "find_intersects_ray() border intersection normal incorrect for zero sized AABB.");
  234. }
  235. TEST_CASE("[AABB] Merging") {
  236. const AABB aabb_big = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  237. AABB aabb_small = AABB(Vector3(-1.5, 2, -2.5), Vector3(1, 1, 1));
  238. CHECK_MESSAGE(
  239. aabb_big.merge(aabb_small).is_equal_approx(aabb_big),
  240. "merge() with fully contained AABB (touching the edge) should return the expected result.");
  241. aabb_small = AABB(Vector3(0.5, 1.5, -2), Vector3(1, 1, 1));
  242. CHECK_MESSAGE(
  243. aabb_big.merge(aabb_small).is_equal_approx(AABB(Vector3(-1.5, 1.5, -2.5), Vector3(4, 5.5, 6))),
  244. "merge() with partially contained AABB (overflowing on Y axis) should return the expected result.");
  245. aabb_small = AABB(Vector3(10, -10, -10), Vector3(1, 1, 1));
  246. CHECK_MESSAGE(
  247. aabb_big.merge(aabb_small).is_equal_approx(AABB(Vector3(-1.5, -10, -10), Vector3(12.5, 17, 13.5))),
  248. "merge() with non-contained AABB should return the expected result.");
  249. }
  250. TEST_CASE("[AABB] Encloses") {
  251. const AABB aabb_big = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  252. CHECK_MESSAGE(
  253. aabb_big.encloses(aabb_big),
  254. "encloses() with itself should return the expected result.");
  255. AABB aabb_small = AABB(Vector3(-1.5, 2, -2.5), Vector3(1, 1, 1));
  256. CHECK_MESSAGE(
  257. aabb_big.encloses(aabb_small),
  258. "encloses() with fully contained AABB (touching the edge) should return the expected result.");
  259. aabb_small = AABB(Vector3(1.5, 6, 2.5), Vector3(1, 1, 1));
  260. CHECK_MESSAGE(
  261. aabb_big.encloses(aabb_small),
  262. "encloses() with fully contained AABB (touching the edge) should return the expected result.");
  263. aabb_small = AABB(Vector3(0.5, 1.5, -2), Vector3(1, 1, 1));
  264. CHECK_MESSAGE(
  265. !aabb_big.encloses(aabb_small),
  266. "encloses() with partially contained AABB (overflowing on Y axis) should return the expected result.");
  267. aabb_small = AABB(Vector3(10, -10, -10), Vector3(1, 1, 1));
  268. CHECK_MESSAGE(
  269. !aabb_big.encloses(aabb_small),
  270. "encloses() with non-contained AABB should return the expected result.");
  271. }
  272. TEST_CASE("[AABB] Get endpoints") {
  273. const AABB aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  274. CHECK_MESSAGE(
  275. aabb.get_endpoint(0).is_equal_approx(Vector3(-1.5, 2, -2.5)),
  276. "The endpoint at index 0 should match the expected value.");
  277. CHECK_MESSAGE(
  278. aabb.get_endpoint(1).is_equal_approx(Vector3(-1.5, 2, 3.5)),
  279. "The endpoint at index 1 should match the expected value.");
  280. CHECK_MESSAGE(
  281. aabb.get_endpoint(2).is_equal_approx(Vector3(-1.5, 7, -2.5)),
  282. "The endpoint at index 2 should match the expected value.");
  283. CHECK_MESSAGE(
  284. aabb.get_endpoint(3).is_equal_approx(Vector3(-1.5, 7, 3.5)),
  285. "The endpoint at index 3 should match the expected value.");
  286. CHECK_MESSAGE(
  287. aabb.get_endpoint(4).is_equal_approx(Vector3(2.5, 2, -2.5)),
  288. "The endpoint at index 4 should match the expected value.");
  289. CHECK_MESSAGE(
  290. aabb.get_endpoint(5).is_equal_approx(Vector3(2.5, 2, 3.5)),
  291. "The endpoint at index 5 should match the expected value.");
  292. CHECK_MESSAGE(
  293. aabb.get_endpoint(6).is_equal_approx(Vector3(2.5, 7, -2.5)),
  294. "The endpoint at index 6 should match the expected value.");
  295. CHECK_MESSAGE(
  296. aabb.get_endpoint(7).is_equal_approx(Vector3(2.5, 7, 3.5)),
  297. "The endpoint at index 7 should match the expected value.");
  298. ERR_PRINT_OFF;
  299. CHECK_MESSAGE(
  300. aabb.get_endpoint(8).is_equal_approx(Vector3()),
  301. "The endpoint at invalid index 8 should match the expected value.");
  302. CHECK_MESSAGE(
  303. aabb.get_endpoint(-1).is_equal_approx(Vector3()),
  304. "The endpoint at invalid index -1 should match the expected value.");
  305. ERR_PRINT_ON;
  306. }
  307. TEST_CASE("[AABB] Get longest/shortest axis") {
  308. const AABB aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  309. CHECK_MESSAGE(
  310. aabb.get_longest_axis() == Vector3(0, 0, 1),
  311. "get_longest_axis() should return the expected value.");
  312. CHECK_MESSAGE(
  313. aabb.get_longest_axis_index() == Vector3::AXIS_Z,
  314. "get_longest_axis_index() should return the expected value.");
  315. CHECK_MESSAGE(
  316. aabb.get_longest_axis_size() == 6,
  317. "get_longest_axis_size() should return the expected value.");
  318. CHECK_MESSAGE(
  319. aabb.get_shortest_axis() == Vector3(1, 0, 0),
  320. "get_shortest_axis() should return the expected value.");
  321. CHECK_MESSAGE(
  322. aabb.get_shortest_axis_index() == Vector3::AXIS_X,
  323. "get_shortest_axis_index() should return the expected value.");
  324. CHECK_MESSAGE(
  325. aabb.get_shortest_axis_size() == 4,
  326. "get_shortest_axis_size() should return the expected value.");
  327. }
  328. TEST_CASE("[AABB] Get support") {
  329. const AABB aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  330. CHECK_MESSAGE(
  331. aabb.get_support(Vector3(1, 0, 0)) == Vector3(2.5, 2, -2.5),
  332. "get_support() should return the expected value.");
  333. CHECK_MESSAGE(
  334. aabb.get_support(Vector3(0.5, 1, 1)) == Vector3(2.5, 7, 3.5),
  335. "get_support() should return the expected value.");
  336. CHECK_MESSAGE(
  337. aabb.get_support(Vector3(0.5, 1, -400)) == Vector3(2.5, 7, -2.5),
  338. "get_support() should return the expected value.");
  339. CHECK_MESSAGE(
  340. aabb.get_support(Vector3(0, -1, 0)) == Vector3(-1.5, 2, -2.5),
  341. "get_support() should return the expected value.");
  342. CHECK_MESSAGE(
  343. aabb.get_support(Vector3(0, -0.1, 0)) == Vector3(-1.5, 2, -2.5),
  344. "get_support() should return the expected value.");
  345. CHECK_MESSAGE(
  346. aabb.get_support(Vector3()) == Vector3(-1.5, 2, -2.5),
  347. "get_support() should return the AABB position when given a zero vector.");
  348. }
  349. TEST_CASE("[AABB] Grow") {
  350. const AABB aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  351. CHECK_MESSAGE(
  352. aabb.grow(0.25).is_equal_approx(AABB(Vector3(-1.75, 1.75, -2.75), Vector3(4.5, 5.5, 6.5))),
  353. "grow() with positive value should return the expected AABB.");
  354. CHECK_MESSAGE(
  355. aabb.grow(-0.25).is_equal_approx(AABB(Vector3(-1.25, 2.25, -2.25), Vector3(3.5, 4.5, 5.5))),
  356. "grow() with negative value should return the expected AABB.");
  357. CHECK_MESSAGE(
  358. aabb.grow(-10).is_equal_approx(AABB(Vector3(8.5, 12, 7.5), Vector3(-16, -15, -14))),
  359. "grow() with large negative value should return the expected AABB.");
  360. }
  361. TEST_CASE("[AABB] Has point") {
  362. const AABB aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  363. CHECK_MESSAGE(
  364. aabb.has_point(Vector3(-1, 3, 0)),
  365. "has_point() with contained point should return the expected value.");
  366. CHECK_MESSAGE(
  367. aabb.has_point(Vector3(2, 3, 0)),
  368. "has_point() with contained point should return the expected value.");
  369. CHECK_MESSAGE(
  370. !aabb.has_point(Vector3(-20, 0, 0)),
  371. "has_point() with non-contained point should return the expected value.");
  372. CHECK_MESSAGE(
  373. aabb.has_point(Vector3(-1.5, 3, 0)),
  374. "has_point() with positive size should include point on near face (X axis).");
  375. CHECK_MESSAGE(
  376. aabb.has_point(Vector3(2.5, 3, 0)),
  377. "has_point() with positive size should include point on far face (X axis).");
  378. CHECK_MESSAGE(
  379. aabb.has_point(Vector3(0, 2, 0)),
  380. "has_point() with positive size should include point on near face (Y axis).");
  381. CHECK_MESSAGE(
  382. aabb.has_point(Vector3(0, 7, 0)),
  383. "has_point() with positive size should include point on far face (Y axis).");
  384. CHECK_MESSAGE(
  385. aabb.has_point(Vector3(0, 3, -2.5)),
  386. "has_point() with positive size should include point on near face (Z axis).");
  387. CHECK_MESSAGE(
  388. aabb.has_point(Vector3(0, 3, 3.5)),
  389. "has_point() with positive size should include point on far face (Z axis).");
  390. }
  391. TEST_CASE("[AABB] Expanding") {
  392. const AABB aabb = AABB(Vector3(-1.5, 2, -2.5), Vector3(4, 5, 6));
  393. CHECK_MESSAGE(
  394. aabb.expand(Vector3(-1, 3, 0)).is_equal_approx(aabb),
  395. "expand() with contained point should return the expected AABB.");
  396. CHECK_MESSAGE(
  397. aabb.expand(Vector3(2, 3, 0)).is_equal_approx(aabb),
  398. "expand() with contained point should return the expected AABB.");
  399. CHECK_MESSAGE(
  400. aabb.expand(Vector3(-1.5, 3, 0)).is_equal_approx(aabb),
  401. "expand() with contained point on negative edge should return the expected AABB.");
  402. CHECK_MESSAGE(
  403. aabb.expand(Vector3(2.5, 3, 0)).is_equal_approx(aabb),
  404. "expand() with contained point on positive edge should return the expected AABB.");
  405. CHECK_MESSAGE(
  406. aabb.expand(Vector3(-20, 0, 0)).is_equal_approx(AABB(Vector3(-20, 0, -2.5), Vector3(22.5, 7, 6))),
  407. "expand() with non-contained point should return the expected AABB.");
  408. }
  409. TEST_CASE("[AABB] Finite number checks") {
  410. const Vector3 x(0, 1, 2);
  411. const Vector3 infinite(NAN, NAN, NAN);
  412. CHECK_MESSAGE(
  413. AABB(x, x).is_finite(),
  414. "AABB with all components finite should be finite");
  415. CHECK_FALSE_MESSAGE(
  416. AABB(infinite, x).is_finite(),
  417. "AABB with one component infinite should not be finite.");
  418. CHECK_FALSE_MESSAGE(
  419. AABB(x, infinite).is_finite(),
  420. "AABB with one component infinite should not be finite.");
  421. CHECK_FALSE_MESSAGE(
  422. AABB(infinite, infinite).is_finite(),
  423. "AABB with two components infinite should not be finite.");
  424. }
  425. } // namespace TestAABB
  426. #endif // TEST_AABB_H