aabb.hpp 13 KB

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