aabb.h 15 KB

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