aabb.hpp 15 KB

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