aabb.h 15 KB

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