aabb.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 "plane.h"
  33. #include "vector3.h"
  34. /**
  35. * AABB / AABB (Axis Aligned Bounding Box)
  36. * This is implemented by a point (pos) and the box size
  37. */
  38. class AABB {
  39. public:
  40. Vector3 pos;
  41. Vector3 size;
  42. float get_area() const; /// get area
  43. _FORCE_INLINE_ bool has_no_area() const {
  44. return (size.x <= CMP_EPSILON || size.y <= CMP_EPSILON || size.z <= CMP_EPSILON);
  45. }
  46. _FORCE_INLINE_ bool has_no_surface() const {
  47. return (size.x <= CMP_EPSILON && size.y <= CMP_EPSILON && size.z <= CMP_EPSILON);
  48. }
  49. const Vector3 &get_pos() const { return pos; }
  50. void set_pos(const Vector3 &p_pos) { pos = 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. _FORCE_INLINE_ bool intersects(const AABB &p_aabb) const; /// Both AABBs overlap
  56. _FORCE_INLINE_ bool intersects_inclusive(const AABB &p_aabb) const; /// Both AABBs (or their faces) overlap
  57. _FORCE_INLINE_ bool encloses(const AABB &p_aabb) const; /// p_aabb is completely inside this
  58. AABB merge(const AABB &p_with) const;
  59. void merge_with(const AABB &p_aabb); ///merge with another AABB
  60. AABB intersection(const AABB &p_aabb) const; ///get box where two intersect, empty if no intersection occurs
  61. bool intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vector3 *r_clip = NULL, Vector3 *r_normal = NULL) const;
  62. bool intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *r_clip = NULL, Vector3 *r_normal = NULL) const;
  63. _FORCE_INLINE_ bool smits_intersect_ray(const Vector3 &from, const Vector3 &p_dir, float t0, float t1) const;
  64. _FORCE_INLINE_ bool intersects_convex_shape(const Plane *p_plane, int p_plane_count) const;
  65. bool intersects_plane(const Plane &p_plane) const;
  66. _FORCE_INLINE_ bool has_point(const Vector3 &p_point) const;
  67. _FORCE_INLINE_ Vector3 get_support(const Vector3 &p_normal) const;
  68. Vector3 get_longest_axis() const;
  69. int get_longest_axis_index() const;
  70. _FORCE_INLINE_ real_t get_longest_axis_size() const;
  71. Vector3 get_shortest_axis() const;
  72. int get_shortest_axis_index() const;
  73. _FORCE_INLINE_ real_t get_shortest_axis_size() const;
  74. AABB grow(real_t p_by) const;
  75. _FORCE_INLINE_ void grow_by(real_t p_amount);
  76. void get_edge(int p_edge, Vector3 &r_from, Vector3 &r_to) const;
  77. _FORCE_INLINE_ Vector3 get_endpoint(int p_point) const;
  78. AABB expand(const Vector3 &p_vector) const;
  79. _FORCE_INLINE_ void project_range_in_plane(const Plane &p_plane, float &r_min, float &r_max) const;
  80. _FORCE_INLINE_ void expand_to(const Vector3 &p_vector); /** expand to contain a point if necesary */
  81. operator String() const;
  82. _FORCE_INLINE_ AABB() {}
  83. inline AABB(const Vector3 &p_pos, const Vector3 &p_size) {
  84. pos = p_pos;
  85. size = p_size;
  86. }
  87. };
  88. inline bool AABB::intersects(const AABB &p_aabb) const {
  89. if (pos.x >= (p_aabb.pos.x + p_aabb.size.x))
  90. return false;
  91. if ((pos.x + size.x) <= p_aabb.pos.x)
  92. return false;
  93. if (pos.y >= (p_aabb.pos.y + p_aabb.size.y))
  94. return false;
  95. if ((pos.y + size.y) <= p_aabb.pos.y)
  96. return false;
  97. if (pos.z >= (p_aabb.pos.z + p_aabb.size.z))
  98. return false;
  99. if ((pos.z + size.z) <= p_aabb.pos.z)
  100. return false;
  101. return true;
  102. }
  103. inline bool AABB::intersects_inclusive(const AABB &p_aabb) const {
  104. if (pos.x > (p_aabb.pos.x + p_aabb.size.x))
  105. return false;
  106. if ((pos.x + size.x) < p_aabb.pos.x)
  107. return false;
  108. if (pos.y > (p_aabb.pos.y + p_aabb.size.y))
  109. return false;
  110. if ((pos.y + size.y) < p_aabb.pos.y)
  111. return false;
  112. if (pos.z > (p_aabb.pos.z + p_aabb.size.z))
  113. return false;
  114. if ((pos.z + size.z) < p_aabb.pos.z)
  115. return false;
  116. return true;
  117. }
  118. inline bool AABB::encloses(const AABB &p_aabb) const {
  119. Vector3 src_min = pos;
  120. Vector3 src_max = pos + size;
  121. Vector3 dst_min = p_aabb.pos;
  122. Vector3 dst_max = p_aabb.pos + p_aabb.size;
  123. return (
  124. (src_min.x <= dst_min.x) &&
  125. (src_max.x > dst_max.x) &&
  126. (src_min.y <= dst_min.y) &&
  127. (src_max.y > dst_max.y) &&
  128. (src_min.z <= dst_min.z) &&
  129. (src_max.z > dst_max.z));
  130. }
  131. Vector3 AABB::get_support(const Vector3 &p_normal) const {
  132. Vector3 half_extents = size * 0.5;
  133. Vector3 ofs = pos + half_extents;
  134. return Vector3(
  135. (p_normal.x > 0) ? -half_extents.x : half_extents.x,
  136. (p_normal.y > 0) ? -half_extents.y : half_extents.y,
  137. (p_normal.z > 0) ? -half_extents.z : half_extents.z) +
  138. ofs;
  139. }
  140. Vector3 AABB::get_endpoint(int p_point) const {
  141. switch (p_point) {
  142. case 0: return Vector3(pos.x, pos.y, pos.z);
  143. case 1: return Vector3(pos.x, pos.y, pos.z + size.z);
  144. case 2: return Vector3(pos.x, pos.y + size.y, pos.z);
  145. case 3: return Vector3(pos.x, pos.y + size.y, pos.z + size.z);
  146. case 4: return Vector3(pos.x + size.x, pos.y, pos.z);
  147. case 5: return Vector3(pos.x + size.x, pos.y, pos.z + size.z);
  148. case 6: return Vector3(pos.x + size.x, pos.y + size.y, pos.z);
  149. case 7: return Vector3(pos.x + size.x, pos.y + size.y, pos.z + size.z);
  150. };
  151. ERR_FAIL_V(Vector3());
  152. }
  153. bool AABB::intersects_convex_shape(const Plane *p_planes, int p_plane_count) const {
  154. #if 1
  155. Vector3 half_extents = size * 0.5;
  156. Vector3 ofs = pos + half_extents;
  157. for (int i = 0; i < p_plane_count; i++) {
  158. const Plane &p = p_planes[i];
  159. Vector3 point(
  160. (p.normal.x > 0) ? -half_extents.x : half_extents.x,
  161. (p.normal.y > 0) ? -half_extents.y : half_extents.y,
  162. (p.normal.z > 0) ? -half_extents.z : half_extents.z);
  163. point += ofs;
  164. if (p.is_point_over(point))
  165. return false;
  166. }
  167. return true;
  168. #else
  169. //cache all points to check against!
  170. // #warning should be easy to optimize, just use the same as when taking the support and use only that point
  171. Vector3 points[8] = {
  172. Vector3(pos.x, pos.y, pos.z),
  173. Vector3(pos.x, pos.y, pos.z + size.z),
  174. Vector3(pos.x, pos.y + size.y, pos.z),
  175. Vector3(pos.x, pos.y + size.y, pos.z + size.z),
  176. Vector3(pos.x + size.x, pos.y, pos.z),
  177. Vector3(pos.x + size.x, pos.y, pos.z + size.z),
  178. Vector3(pos.x + size.x, pos.y + size.y, pos.z),
  179. Vector3(pos.x + size.x, pos.y + size.y, pos.z + size.z),
  180. };
  181. for (int i = 0; i < p_plane_count; i++) { //for each plane
  182. const Plane &plane = p_planes[i];
  183. bool all_points_over = true;
  184. //test if it has all points over!
  185. for (int j = 0; j < 8; j++) {
  186. if (!plane.is_point_over(points[j])) {
  187. all_points_over = false;
  188. break;
  189. }
  190. }
  191. if (all_points_over) {
  192. return false;
  193. }
  194. }
  195. return true;
  196. #endif
  197. }
  198. bool AABB::has_point(const Vector3 &p_point) const {
  199. if (p_point.x < pos.x)
  200. return false;
  201. if (p_point.y < pos.y)
  202. return false;
  203. if (p_point.z < pos.z)
  204. return false;
  205. if (p_point.x > pos.x + size.x)
  206. return false;
  207. if (p_point.y > pos.y + size.y)
  208. return false;
  209. if (p_point.z > pos.z + size.z)
  210. return false;
  211. return true;
  212. }
  213. inline void AABB::expand_to(const Vector3 &p_vector) {
  214. Vector3 begin = pos;
  215. Vector3 end = pos + size;
  216. if (p_vector.x < begin.x)
  217. begin.x = p_vector.x;
  218. if (p_vector.y < begin.y)
  219. begin.y = p_vector.y;
  220. if (p_vector.z < begin.z)
  221. begin.z = p_vector.z;
  222. if (p_vector.x > end.x)
  223. end.x = p_vector.x;
  224. if (p_vector.y > end.y)
  225. end.y = p_vector.y;
  226. if (p_vector.z > end.z)
  227. end.z = p_vector.z;
  228. pos = begin;
  229. size = end - begin;
  230. }
  231. void AABB::project_range_in_plane(const Plane &p_plane, float &r_min, float &r_max) const {
  232. Vector3 half_extents(size.x * 0.5, size.y * 0.5, size.z * 0.5);
  233. Vector3 center(pos.x + half_extents.x, pos.y + half_extents.y, pos.z + half_extents.z);
  234. float length = p_plane.normal.abs().dot(half_extents);
  235. float distance = p_plane.distance_to(center);
  236. r_min = distance - length;
  237. r_max = distance + length;
  238. }
  239. inline real_t AABB::get_longest_axis_size() const {
  240. real_t max_size = size.x;
  241. if (size.y > max_size) {
  242. max_size = size.y;
  243. }
  244. if (size.z > max_size) {
  245. max_size = size.z;
  246. }
  247. return max_size;
  248. }
  249. inline real_t AABB::get_shortest_axis_size() const {
  250. real_t max_size = size.x;
  251. if (size.y < max_size) {
  252. max_size = size.y;
  253. }
  254. if (size.z < max_size) {
  255. max_size = size.z;
  256. }
  257. return max_size;
  258. }
  259. bool AABB::smits_intersect_ray(const Vector3 &from, const Vector3 &dir, float t0, float t1) const {
  260. float divx = 1.0 / dir.x;
  261. float divy = 1.0 / dir.y;
  262. float divz = 1.0 / dir.z;
  263. Vector3 upbound = pos + size;
  264. float tmin, tmax, tymin, tymax, tzmin, tzmax;
  265. if (dir.x >= 0) {
  266. tmin = (pos.x - from.x) * divx;
  267. tmax = (upbound.x - from.x) * divx;
  268. } else {
  269. tmin = (upbound.x - from.x) * divx;
  270. tmax = (pos.x - from.x) * divx;
  271. }
  272. if (dir.y >= 0) {
  273. tymin = (pos.y - from.y) * divy;
  274. tymax = (upbound.y - from.y) * divy;
  275. } else {
  276. tymin = (upbound.y - from.y) * divy;
  277. tymax = (pos.y - from.y) * divy;
  278. }
  279. if ((tmin > tymax) || (tymin > tmax))
  280. return false;
  281. if (tymin > tmin)
  282. tmin = tymin;
  283. if (tymax < tmax)
  284. tmax = tymax;
  285. if (dir.z >= 0) {
  286. tzmin = (pos.z - from.z) * divz;
  287. tzmax = (upbound.z - from.z) * divz;
  288. } else {
  289. tzmin = (upbound.z - from.z) * divz;
  290. tzmax = (pos.z - from.z) * divz;
  291. }
  292. if ((tmin > tzmax) || (tzmin > tmax))
  293. return false;
  294. if (tzmin > tmin)
  295. tmin = tzmin;
  296. if (tzmax < tmax)
  297. tmax = tzmax;
  298. return ((tmin < t1) && (tmax > t0));
  299. }
  300. void AABB::grow_by(real_t p_amount) {
  301. pos.x -= p_amount;
  302. pos.y -= p_amount;
  303. pos.z -= p_amount;
  304. size.x += 2.0 * p_amount;
  305. size.y += 2.0 * p_amount;
  306. size.z += 2.0 * p_amount;
  307. }
  308. typedef AABB Rect3;
  309. #endif // AABB_H