aabb.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /**************************************************************************/
  2. /* 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 AABB_H
  31. #define AABB_H
  32. #include "core/math/plane.h"
  33. #include "core/math/vector3.h"
  34. /**
  35. * AABB (Axis Aligned Bounding Box)
  36. * This is implemented by a point (position) and the box size.
  37. */
  38. class Variant;
  39. struct [[nodiscard]] AABB {
  40. Vector3 position;
  41. Vector3 size;
  42. real_t get_volume() const;
  43. _FORCE_INLINE_ bool has_volume() const {
  44. return size.x > 0.0f && size.y > 0.0f && size.z > 0.0f;
  45. }
  46. _FORCE_INLINE_ bool has_surface() const {
  47. return size.x > 0.0f || size.y > 0.0f || size.z > 0.0f;
  48. }
  49. const Vector3 &get_position() const { return position; }
  50. void set_position(const Vector3 &p_pos) { position = p_pos; }
  51. const Vector3 &get_size() const { return size; }
  52. void set_size(const Vector3 &p_size) { size = p_size; }
  53. bool operator==(const AABB &p_rval) const;
  54. bool operator!=(const AABB &p_rval) const;
  55. bool is_equal_approx(const AABB &p_aabb) const;
  56. bool is_finite() const;
  57. _FORCE_INLINE_ bool intersects(const AABB &p_aabb) const; /// Both AABBs overlap
  58. _FORCE_INLINE_ bool intersects_inclusive(const AABB &p_aabb) const; /// Both AABBs (or their faces) overlap
  59. _FORCE_INLINE_ bool encloses(const AABB &p_aabb) const; /// p_aabb is completely inside this
  60. AABB merge(const AABB &p_with) const;
  61. void merge_with(const AABB &p_aabb); ///merge with another AABB
  62. AABB intersection(const AABB &p_aabb) const; ///get box where two intersect, empty if no intersection occurs
  63. _FORCE_INLINE_ bool smits_intersect_ray(const Vector3 &p_from, const Vector3 &p_dir, real_t p_t0, real_t p_t1) const;
  64. bool intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vector3 *r_intersection_point = nullptr, Vector3 *r_normal = nullptr) const;
  65. bool intersects_ray(const Vector3 &p_from, const Vector3 &p_dir) const {
  66. bool inside;
  67. return find_intersects_ray(p_from, p_dir, inside);
  68. }
  69. bool find_intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, bool &r_inside, Vector3 *r_intersection_point = nullptr, Vector3 *r_normal = nullptr) const;
  70. _FORCE_INLINE_ bool intersects_convex_shape(const Plane *p_planes, int p_plane_count, const Vector3 *p_points, int p_point_count) const;
  71. _FORCE_INLINE_ bool inside_convex_shape(const Plane *p_planes, int p_plane_count) const;
  72. bool intersects_plane(const Plane &p_plane) const;
  73. _FORCE_INLINE_ bool has_point(const Vector3 &p_point) const;
  74. _FORCE_INLINE_ Vector3 get_support(const Vector3 &p_direction) const;
  75. Vector3 get_longest_axis() const;
  76. int get_longest_axis_index() const;
  77. _FORCE_INLINE_ real_t get_longest_axis_size() const;
  78. Vector3 get_shortest_axis() const;
  79. int get_shortest_axis_index() const;
  80. _FORCE_INLINE_ real_t get_shortest_axis_size() const;
  81. AABB grow(real_t p_by) const;
  82. _FORCE_INLINE_ void grow_by(real_t p_amount);
  83. void get_edge(int p_edge, Vector3 &r_from, Vector3 &r_to) const;
  84. _FORCE_INLINE_ Vector3 get_endpoint(int p_point) const;
  85. AABB expand(const Vector3 &p_vector) const;
  86. _FORCE_INLINE_ void project_range_in_plane(const Plane &p_plane, real_t &r_min, real_t &r_max) const;
  87. _FORCE_INLINE_ void expand_to(const Vector3 &p_vector); /** expand to contain a point if necessary */
  88. _FORCE_INLINE_ AABB abs() const {
  89. return AABB(position + size.minf(0), size.abs());
  90. }
  91. Variant intersects_segment_bind(const Vector3 &p_from, const Vector3 &p_to) const;
  92. Variant intersects_ray_bind(const Vector3 &p_from, const Vector3 &p_dir) const;
  93. _FORCE_INLINE_ void quantize(real_t p_unit);
  94. _FORCE_INLINE_ AABB quantized(real_t p_unit) const;
  95. _FORCE_INLINE_ void set_end(const Vector3 &p_end) {
  96. size = p_end - position;
  97. }
  98. _FORCE_INLINE_ Vector3 get_end() const {
  99. return position + size;
  100. }
  101. _FORCE_INLINE_ Vector3 get_center() const {
  102. return position + (size * 0.5f);
  103. }
  104. operator String() const;
  105. _FORCE_INLINE_ AABB() {}
  106. inline AABB(const Vector3 &p_pos, const Vector3 &p_size) :
  107. position(p_pos),
  108. size(p_size) {
  109. }
  110. };
  111. inline bool AABB::intersects(const AABB &p_aabb) const {
  112. #ifdef MATH_CHECKS
  113. if (unlikely(size.x < 0 || size.y < 0 || size.z < 0 || p_aabb.size.x < 0 || p_aabb.size.y < 0 || p_aabb.size.z < 0)) {
  114. ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size.");
  115. }
  116. #endif
  117. if (position.x >= (p_aabb.position.x + p_aabb.size.x)) {
  118. return false;
  119. }
  120. if ((position.x + size.x) <= p_aabb.position.x) {
  121. return false;
  122. }
  123. if (position.y >= (p_aabb.position.y + p_aabb.size.y)) {
  124. return false;
  125. }
  126. if ((position.y + size.y) <= p_aabb.position.y) {
  127. return false;
  128. }
  129. if (position.z >= (p_aabb.position.z + p_aabb.size.z)) {
  130. return false;
  131. }
  132. if ((position.z + size.z) <= p_aabb.position.z) {
  133. return false;
  134. }
  135. return true;
  136. }
  137. inline bool AABB::intersects_inclusive(const AABB &p_aabb) const {
  138. #ifdef MATH_CHECKS
  139. if (unlikely(size.x < 0 || size.y < 0 || size.z < 0 || p_aabb.size.x < 0 || p_aabb.size.y < 0 || p_aabb.size.z < 0)) {
  140. ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size.");
  141. }
  142. #endif
  143. if (position.x > (p_aabb.position.x + p_aabb.size.x)) {
  144. return false;
  145. }
  146. if ((position.x + size.x) < p_aabb.position.x) {
  147. return false;
  148. }
  149. if (position.y > (p_aabb.position.y + p_aabb.size.y)) {
  150. return false;
  151. }
  152. if ((position.y + size.y) < p_aabb.position.y) {
  153. return false;
  154. }
  155. if (position.z > (p_aabb.position.z + p_aabb.size.z)) {
  156. return false;
  157. }
  158. if ((position.z + size.z) < p_aabb.position.z) {
  159. return false;
  160. }
  161. return true;
  162. }
  163. inline bool AABB::encloses(const AABB &p_aabb) const {
  164. #ifdef MATH_CHECKS
  165. if (unlikely(size.x < 0 || size.y < 0 || size.z < 0 || p_aabb.size.x < 0 || p_aabb.size.y < 0 || p_aabb.size.z < 0)) {
  166. ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size.");
  167. }
  168. #endif
  169. Vector3 src_min = position;
  170. Vector3 src_max = position + size;
  171. Vector3 dst_min = p_aabb.position;
  172. Vector3 dst_max = p_aabb.position + p_aabb.size;
  173. return (
  174. (src_min.x <= dst_min.x) &&
  175. (src_max.x >= dst_max.x) &&
  176. (src_min.y <= dst_min.y) &&
  177. (src_max.y >= dst_max.y) &&
  178. (src_min.z <= dst_min.z) &&
  179. (src_max.z >= dst_max.z));
  180. }
  181. Vector3 AABB::get_support(const Vector3 &p_direction) const {
  182. Vector3 support = position;
  183. if (p_direction.x > 0.0f) {
  184. support.x += size.x;
  185. }
  186. if (p_direction.y > 0.0f) {
  187. support.y += size.y;
  188. }
  189. if (p_direction.z > 0.0f) {
  190. support.z += size.z;
  191. }
  192. return support;
  193. }
  194. Vector3 AABB::get_endpoint(int p_point) const {
  195. switch (p_point) {
  196. case 0:
  197. return Vector3(position.x, position.y, position.z);
  198. case 1:
  199. return Vector3(position.x, position.y, position.z + size.z);
  200. case 2:
  201. return Vector3(position.x, position.y + size.y, position.z);
  202. case 3:
  203. return Vector3(position.x, position.y + size.y, position.z + size.z);
  204. case 4:
  205. return Vector3(position.x + size.x, position.y, position.z);
  206. case 5:
  207. return Vector3(position.x + size.x, position.y, position.z + size.z);
  208. case 6:
  209. return Vector3(position.x + size.x, position.y + size.y, position.z);
  210. case 7:
  211. return Vector3(position.x + size.x, position.y + size.y, position.z + size.z);
  212. }
  213. ERR_FAIL_V(Vector3());
  214. }
  215. bool AABB::intersects_convex_shape(const Plane *p_planes, int p_plane_count, const Vector3 *p_points, int p_point_count) const {
  216. Vector3 half_extents = size * 0.5f;
  217. Vector3 ofs = position + half_extents;
  218. for (int i = 0; i < p_plane_count; i++) {
  219. const Plane &p = p_planes[i];
  220. Vector3 point(
  221. (p.normal.x > 0) ? -half_extents.x : half_extents.x,
  222. (p.normal.y > 0) ? -half_extents.y : half_extents.y,
  223. (p.normal.z > 0) ? -half_extents.z : half_extents.z);
  224. point += ofs;
  225. if (p.is_point_over(point)) {
  226. return false;
  227. }
  228. }
  229. // Make sure all points in the shape aren't fully separated from the AABB on
  230. // each axis.
  231. int bad_point_counts_positive[3] = { 0 };
  232. int bad_point_counts_negative[3] = { 0 };
  233. for (int k = 0; k < 3; k++) {
  234. for (int i = 0; i < p_point_count; i++) {
  235. if (p_points[i].coord[k] > ofs.coord[k] + half_extents.coord[k]) {
  236. bad_point_counts_positive[k]++;
  237. }
  238. if (p_points[i].coord[k] < ofs.coord[k] - half_extents.coord[k]) {
  239. bad_point_counts_negative[k]++;
  240. }
  241. }
  242. if (bad_point_counts_negative[k] == p_point_count) {
  243. return false;
  244. }
  245. if (bad_point_counts_positive[k] == p_point_count) {
  246. return false;
  247. }
  248. }
  249. return true;
  250. }
  251. bool AABB::inside_convex_shape(const Plane *p_planes, int p_plane_count) const {
  252. Vector3 half_extents = size * 0.5f;
  253. Vector3 ofs = position + half_extents;
  254. for (int i = 0; i < p_plane_count; i++) {
  255. const Plane &p = p_planes[i];
  256. Vector3 point(
  257. (p.normal.x < 0) ? -half_extents.x : half_extents.x,
  258. (p.normal.y < 0) ? -half_extents.y : half_extents.y,
  259. (p.normal.z < 0) ? -half_extents.z : half_extents.z);
  260. point += ofs;
  261. if (p.is_point_over(point)) {
  262. return false;
  263. }
  264. }
  265. return true;
  266. }
  267. bool AABB::has_point(const Vector3 &p_point) const {
  268. #ifdef MATH_CHECKS
  269. if (unlikely(size.x < 0 || size.y < 0 || size.z < 0)) {
  270. ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size.");
  271. }
  272. #endif
  273. if (p_point.x < position.x) {
  274. return false;
  275. }
  276. if (p_point.y < position.y) {
  277. return false;
  278. }
  279. if (p_point.z < position.z) {
  280. return false;
  281. }
  282. if (p_point.x > position.x + size.x) {
  283. return false;
  284. }
  285. if (p_point.y > position.y + size.y) {
  286. return false;
  287. }
  288. if (p_point.z > position.z + size.z) {
  289. return false;
  290. }
  291. return true;
  292. }
  293. inline void AABB::expand_to(const Vector3 &p_vector) {
  294. #ifdef MATH_CHECKS
  295. if (unlikely(size.x < 0 || size.y < 0 || size.z < 0)) {
  296. ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size.");
  297. }
  298. #endif
  299. Vector3 begin = position;
  300. Vector3 end = position + size;
  301. if (p_vector.x < begin.x) {
  302. begin.x = p_vector.x;
  303. }
  304. if (p_vector.y < begin.y) {
  305. begin.y = p_vector.y;
  306. }
  307. if (p_vector.z < begin.z) {
  308. begin.z = p_vector.z;
  309. }
  310. if (p_vector.x > end.x) {
  311. end.x = p_vector.x;
  312. }
  313. if (p_vector.y > end.y) {
  314. end.y = p_vector.y;
  315. }
  316. if (p_vector.z > end.z) {
  317. end.z = p_vector.z;
  318. }
  319. position = begin;
  320. size = end - begin;
  321. }
  322. void AABB::project_range_in_plane(const Plane &p_plane, real_t &r_min, real_t &r_max) const {
  323. Vector3 half_extents(size.x * 0.5f, size.y * 0.5f, size.z * 0.5f);
  324. Vector3 center(position.x + half_extents.x, position.y + half_extents.y, position.z + half_extents.z);
  325. real_t length = p_plane.normal.abs().dot(half_extents);
  326. real_t distance = p_plane.distance_to(center);
  327. r_min = distance - length;
  328. r_max = distance + length;
  329. }
  330. inline real_t AABB::get_longest_axis_size() const {
  331. real_t max_size = size.x;
  332. if (size.y > max_size) {
  333. max_size = size.y;
  334. }
  335. if (size.z > max_size) {
  336. max_size = size.z;
  337. }
  338. return max_size;
  339. }
  340. inline real_t AABB::get_shortest_axis_size() const {
  341. real_t max_size = size.x;
  342. if (size.y < max_size) {
  343. max_size = size.y;
  344. }
  345. if (size.z < max_size) {
  346. max_size = size.z;
  347. }
  348. return max_size;
  349. }
  350. bool AABB::smits_intersect_ray(const Vector3 &p_from, const Vector3 &p_dir, real_t p_t0, real_t p_t1) const {
  351. #ifdef MATH_CHECKS
  352. if (unlikely(size.x < 0 || size.y < 0 || size.z < 0)) {
  353. ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size.");
  354. }
  355. #endif
  356. real_t divx = 1.0f / p_dir.x;
  357. real_t divy = 1.0f / p_dir.y;
  358. real_t divz = 1.0f / p_dir.z;
  359. Vector3 upbound = position + size;
  360. real_t tmin, tmax, tymin, tymax, tzmin, tzmax;
  361. if (p_dir.x >= 0) {
  362. tmin = (position.x - p_from.x) * divx;
  363. tmax = (upbound.x - p_from.x) * divx;
  364. } else {
  365. tmin = (upbound.x - p_from.x) * divx;
  366. tmax = (position.x - p_from.x) * divx;
  367. }
  368. if (p_dir.y >= 0) {
  369. tymin = (position.y - p_from.y) * divy;
  370. tymax = (upbound.y - p_from.y) * divy;
  371. } else {
  372. tymin = (upbound.y - p_from.y) * divy;
  373. tymax = (position.y - p_from.y) * divy;
  374. }
  375. if ((tmin > tymax) || (tymin > tmax)) {
  376. return false;
  377. }
  378. if (tymin > tmin) {
  379. tmin = tymin;
  380. }
  381. if (tymax < tmax) {
  382. tmax = tymax;
  383. }
  384. if (p_dir.z >= 0) {
  385. tzmin = (position.z - p_from.z) * divz;
  386. tzmax = (upbound.z - p_from.z) * divz;
  387. } else {
  388. tzmin = (upbound.z - p_from.z) * divz;
  389. tzmax = (position.z - p_from.z) * divz;
  390. }
  391. if ((tmin > tzmax) || (tzmin > tmax)) {
  392. return false;
  393. }
  394. if (tzmin > tmin) {
  395. tmin = tzmin;
  396. }
  397. if (tzmax < tmax) {
  398. tmax = tzmax;
  399. }
  400. return ((tmin < p_t1) && (tmax > p_t0));
  401. }
  402. void AABB::grow_by(real_t p_amount) {
  403. position.x -= p_amount;
  404. position.y -= p_amount;
  405. position.z -= p_amount;
  406. size.x += 2.0f * p_amount;
  407. size.y += 2.0f * p_amount;
  408. size.z += 2.0f * p_amount;
  409. }
  410. void AABB::quantize(real_t p_unit) {
  411. size += position;
  412. position.x -= Math::fposmodp(position.x, p_unit);
  413. position.y -= Math::fposmodp(position.y, p_unit);
  414. position.z -= Math::fposmodp(position.z, p_unit);
  415. size.x -= Math::fposmodp(size.x, p_unit);
  416. size.y -= Math::fposmodp(size.y, p_unit);
  417. size.z -= Math::fposmodp(size.z, p_unit);
  418. size.x += p_unit;
  419. size.y += p_unit;
  420. size.z += p_unit;
  421. size -= position;
  422. }
  423. AABB AABB::quantized(real_t p_unit) const {
  424. AABB ret = *this;
  425. ret.quantize(p_unit);
  426. return ret;
  427. }
  428. #endif // AABB_H