aabb.hpp 13 KB

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