aabb.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*************************************************************************/
  2. /* aabb.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 AABB_H
  31. #define AABB_H
  32. #include "core/math/math_defs.h"
  33. #include "core/math/plane.h"
  34. #include "core/math/vector3.h"
  35. /**
  36. * AABB / AABB (Axis Aligned Bounding Box)
  37. * This is implemented by a point (position) and the box size
  38. */
  39. class AABB {
  40. public:
  41. Vector3 position;
  42. Vector3 size;
  43. real_t get_area() const; /// get area
  44. _FORCE_INLINE_ bool has_no_area() const {
  45. return (size.x <= 0 || size.y <= 0 || size.z <= 0);
  46. }
  47. _FORCE_INLINE_ bool has_no_surface() const {
  48. return (size.x <= 0 && size.y <= 0 && size.z <= 0);
  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. _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. operator String() const;
  85. _FORCE_INLINE_ AABB() {}
  86. inline AABB(const Vector3 &p_pos, const Vector3 &p_size) :
  87. position(p_pos),
  88. size(p_size) {
  89. }
  90. };
  91. inline bool AABB::intersects(const AABB &p_aabb) const {
  92. if (position.x >= (p_aabb.position.x + p_aabb.size.x)) {
  93. return false;
  94. }
  95. if ((position.x + size.x) <= p_aabb.position.x) {
  96. return false;
  97. }
  98. if (position.y >= (p_aabb.position.y + p_aabb.size.y)) {
  99. return false;
  100. }
  101. if ((position.y + size.y) <= p_aabb.position.y) {
  102. return false;
  103. }
  104. if (position.z >= (p_aabb.position.z + p_aabb.size.z)) {
  105. return false;
  106. }
  107. if ((position.z + size.z) <= p_aabb.position.z) {
  108. return false;
  109. }
  110. return true;
  111. }
  112. inline bool AABB::intersects_inclusive(const AABB &p_aabb) const {
  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::encloses(const AABB &p_aabb) const {
  134. Vector3 src_min = position;
  135. Vector3 src_max = position + size;
  136. Vector3 dst_min = p_aabb.position;
  137. Vector3 dst_max = p_aabb.position + p_aabb.size;
  138. return (
  139. (src_min.x <= dst_min.x) &&
  140. (src_max.x > dst_max.x) &&
  141. (src_min.y <= dst_min.y) &&
  142. (src_max.y > dst_max.y) &&
  143. (src_min.z <= dst_min.z) &&
  144. (src_max.z > dst_max.z));
  145. }
  146. Vector3 AABB::get_support(const Vector3 &p_normal) const {
  147. Vector3 half_extents = size * 0.5;
  148. Vector3 ofs = position + half_extents;
  149. return Vector3(
  150. (p_normal.x > 0) ? -half_extents.x : half_extents.x,
  151. (p_normal.y > 0) ? -half_extents.y : half_extents.y,
  152. (p_normal.z > 0) ? -half_extents.z : half_extents.z) +
  153. ofs;
  154. }
  155. Vector3 AABB::get_endpoint(int p_point) const {
  156. switch (p_point) {
  157. case 0:
  158. return Vector3(position.x, position.y, position.z);
  159. case 1:
  160. return Vector3(position.x, position.y, position.z + size.z);
  161. case 2:
  162. return Vector3(position.x, position.y + size.y, position.z);
  163. case 3:
  164. return Vector3(position.x, position.y + size.y, position.z + size.z);
  165. case 4:
  166. return Vector3(position.x + size.x, position.y, position.z);
  167. case 5:
  168. return Vector3(position.x + size.x, position.y, position.z + size.z);
  169. case 6:
  170. return Vector3(position.x + size.x, position.y + size.y, position.z);
  171. case 7:
  172. return Vector3(position.x + size.x, position.y + size.y, position.z + size.z);
  173. };
  174. ERR_FAIL_V(Vector3());
  175. }
  176. bool AABB::intersects_convex_shape(const Plane *p_planes, int p_plane_count, const Vector3 *p_points, int p_point_count) const {
  177. Vector3 half_extents = size * 0.5;
  178. Vector3 ofs = position + half_extents;
  179. for (int i = 0; i < p_plane_count; i++) {
  180. const Plane &p = p_planes[i];
  181. Vector3 point(
  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. point += ofs;
  186. if (p.is_point_over(point)) {
  187. return false;
  188. }
  189. }
  190. // Make sure all points in the shape aren't fully separated from the AABB on
  191. // each axis.
  192. int bad_point_counts_positive[3] = { 0 };
  193. int bad_point_counts_negative[3] = { 0 };
  194. for (int k = 0; k < 3; k++) {
  195. for (int i = 0; i < p_point_count; i++) {
  196. if (p_points[i].coord[k] > ofs.coord[k] + half_extents.coord[k]) {
  197. bad_point_counts_positive[k]++;
  198. }
  199. if (p_points[i].coord[k] < ofs.coord[k] - half_extents.coord[k]) {
  200. bad_point_counts_negative[k]++;
  201. }
  202. }
  203. if (bad_point_counts_negative[k] == p_point_count) {
  204. return false;
  205. }
  206. if (bad_point_counts_positive[k] == p_point_count) {
  207. return false;
  208. }
  209. }
  210. return true;
  211. }
  212. bool AABB::inside_convex_shape(const Plane *p_planes, int p_plane_count) const {
  213. Vector3 half_extents = size * 0.5;
  214. Vector3 ofs = position + half_extents;
  215. for (int i = 0; i < p_plane_count; i++) {
  216. const Plane &p = p_planes[i];
  217. Vector3 point(
  218. (p.normal.x < 0) ? -half_extents.x : half_extents.x,
  219. (p.normal.y < 0) ? -half_extents.y : half_extents.y,
  220. (p.normal.z < 0) ? -half_extents.z : half_extents.z);
  221. point += ofs;
  222. if (p.is_point_over(point)) {
  223. return false;
  224. }
  225. }
  226. return true;
  227. }
  228. bool AABB::has_point(const Vector3 &p_point) const {
  229. if (p_point.x < position.x) {
  230. return false;
  231. }
  232. if (p_point.y < position.y) {
  233. return false;
  234. }
  235. if (p_point.z < position.z) {
  236. return false;
  237. }
  238. if (p_point.x > position.x + size.x) {
  239. return false;
  240. }
  241. if (p_point.y > position.y + size.y) {
  242. return false;
  243. }
  244. if (p_point.z > position.z + size.z) {
  245. return false;
  246. }
  247. return true;
  248. }
  249. inline void AABB::expand_to(const Vector3 &p_vector) {
  250. Vector3 begin = position;
  251. Vector3 end = position + size;
  252. if (p_vector.x < begin.x) {
  253. begin.x = p_vector.x;
  254. }
  255. if (p_vector.y < begin.y) {
  256. begin.y = p_vector.y;
  257. }
  258. if (p_vector.z < begin.z) {
  259. begin.z = p_vector.z;
  260. }
  261. if (p_vector.x > end.x) {
  262. end.x = p_vector.x;
  263. }
  264. if (p_vector.y > end.y) {
  265. end.y = p_vector.y;
  266. }
  267. if (p_vector.z > end.z) {
  268. end.z = p_vector.z;
  269. }
  270. position = begin;
  271. size = end - begin;
  272. }
  273. void AABB::project_range_in_plane(const Plane &p_plane, real_t &r_min, real_t &r_max) const {
  274. Vector3 half_extents(size.x * 0.5, size.y * 0.5, size.z * 0.5);
  275. Vector3 center(position.x + half_extents.x, position.y + half_extents.y, position.z + half_extents.z);
  276. real_t length = p_plane.normal.abs().dot(half_extents);
  277. real_t distance = p_plane.distance_to(center);
  278. r_min = distance - length;
  279. r_max = distance + length;
  280. }
  281. inline real_t AABB::get_longest_axis_size() const {
  282. real_t max_size = size.x;
  283. if (size.y > max_size) {
  284. max_size = size.y;
  285. }
  286. if (size.z > max_size) {
  287. max_size = size.z;
  288. }
  289. return max_size;
  290. }
  291. inline real_t AABB::get_shortest_axis_size() const {
  292. real_t max_size = size.x;
  293. if (size.y < max_size) {
  294. max_size = size.y;
  295. }
  296. if (size.z < max_size) {
  297. max_size = size.z;
  298. }
  299. return max_size;
  300. }
  301. bool AABB::smits_intersect_ray(const Vector3 &p_from, const Vector3 &p_dir, real_t t0, real_t t1) const {
  302. real_t divx = 1.0 / p_dir.x;
  303. real_t divy = 1.0 / p_dir.y;
  304. real_t divz = 1.0 / p_dir.z;
  305. Vector3 upbound = position + size;
  306. real_t tmin, tmax, tymin, tymax, tzmin, tzmax;
  307. if (p_dir.x >= 0) {
  308. tmin = (position.x - p_from.x) * divx;
  309. tmax = (upbound.x - p_from.x) * divx;
  310. } else {
  311. tmin = (upbound.x - p_from.x) * divx;
  312. tmax = (position.x - p_from.x) * divx;
  313. }
  314. if (p_dir.y >= 0) {
  315. tymin = (position.y - p_from.y) * divy;
  316. tymax = (upbound.y - p_from.y) * divy;
  317. } else {
  318. tymin = (upbound.y - p_from.y) * divy;
  319. tymax = (position.y - p_from.y) * divy;
  320. }
  321. if ((tmin > tymax) || (tymin > tmax)) {
  322. return false;
  323. }
  324. if (tymin > tmin) {
  325. tmin = tymin;
  326. }
  327. if (tymax < tmax) {
  328. tmax = tymax;
  329. }
  330. if (p_dir.z >= 0) {
  331. tzmin = (position.z - p_from.z) * divz;
  332. tzmax = (upbound.z - p_from.z) * divz;
  333. } else {
  334. tzmin = (upbound.z - p_from.z) * divz;
  335. tzmax = (position.z - p_from.z) * divz;
  336. }
  337. if ((tmin > tzmax) || (tzmin > tmax)) {
  338. return false;
  339. }
  340. if (tzmin > tmin) {
  341. tmin = tzmin;
  342. }
  343. if (tzmax < tmax) {
  344. tmax = tzmax;
  345. }
  346. return ((tmin < t1) && (tmax > t0));
  347. }
  348. void AABB::grow_by(real_t p_amount) {
  349. position.x -= p_amount;
  350. position.y -= p_amount;
  351. position.z -= p_amount;
  352. size.x += 2.0 * p_amount;
  353. size.y += 2.0 * p_amount;
  354. size.z += 2.0 * p_amount;
  355. }
  356. #endif // AABB_H