aabb.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*************************************************************************/
  2. /* aabb.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "math_defs.h"
  33. #include "plane.h"
  34. #include "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. _FORCE_INLINE_ bool intersects(const AABB &p_aabb) const; /// Both AABBs overlap
  57. _FORCE_INLINE_ bool intersects_inclusive(const AABB &p_aabb) const; /// Both AABBs (or their faces) overlap
  58. _FORCE_INLINE_ bool encloses(const AABB &p_aabb) const; /// p_aabb is completely inside this
  59. AABB merge(const AABB &p_with) const;
  60. void merge_with(const AABB &p_aabb); ///merge with another AABB
  61. AABB intersection(const AABB &p_aabb) const; ///get box where two intersect, empty if no intersection occurs
  62. bool intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vector3 *r_clip = NULL, Vector3 *r_normal = NULL) const;
  63. bool intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *r_clip = NULL, Vector3 *r_normal = NULL) const;
  64. _FORCE_INLINE_ bool smits_intersect_ray(const Vector3 &p_from, const Vector3 &p_dir, real_t t0, real_t t1) const;
  65. _FORCE_INLINE_ bool intersects_convex_shape(const Plane *p_planes, int p_plane_count) const;
  66. bool intersects_plane(const Plane &p_plane) const;
  67. _FORCE_INLINE_ bool has_point(const Vector3 &p_point) const;
  68. _FORCE_INLINE_ Vector3 get_support(const Vector3 &p_normal) const;
  69. Vector3 get_longest_axis() const;
  70. int get_longest_axis_index() const;
  71. _FORCE_INLINE_ real_t get_longest_axis_size() const;
  72. Vector3 get_shortest_axis() const;
  73. int get_shortest_axis_index() const;
  74. _FORCE_INLINE_ real_t get_shortest_axis_size() const;
  75. AABB grow(real_t p_by) const;
  76. _FORCE_INLINE_ void grow_by(real_t p_amount);
  77. void get_edge(int p_edge, Vector3 &r_from, Vector3 &r_to) const;
  78. _FORCE_INLINE_ Vector3 get_endpoint(int p_point) const;
  79. AABB expand(const Vector3 &p_vector) const;
  80. _FORCE_INLINE_ void project_range_in_plane(const Plane &p_plane, real_t &r_min, real_t &r_max) const;
  81. _FORCE_INLINE_ void expand_to(const Vector3 &p_vector); /** expand to contain a point if necessary */
  82. operator String() const;
  83. _FORCE_INLINE_ AABB() {}
  84. inline AABB(const Vector3 &p_pos, const Vector3 &p_size) :
  85. position(p_pos),
  86. size(p_size) {
  87. }
  88. };
  89. inline bool AABB::intersects(const AABB &p_aabb) const {
  90. if (position.x >= (p_aabb.position.x + p_aabb.size.x))
  91. return false;
  92. if ((position.x + size.x) <= p_aabb.position.x)
  93. return false;
  94. if (position.y >= (p_aabb.position.y + p_aabb.size.y))
  95. return false;
  96. if ((position.y + size.y) <= p_aabb.position.y)
  97. return false;
  98. if (position.z >= (p_aabb.position.z + p_aabb.size.z))
  99. return false;
  100. if ((position.z + size.z) <= p_aabb.position.z)
  101. return false;
  102. return true;
  103. }
  104. inline bool AABB::intersects_inclusive(const AABB &p_aabb) const {
  105. if (position.x > (p_aabb.position.x + p_aabb.size.x))
  106. return false;
  107. if ((position.x + size.x) < p_aabb.position.x)
  108. return false;
  109. if (position.y > (p_aabb.position.y + p_aabb.size.y))
  110. return false;
  111. if ((position.y + size.y) < p_aabb.position.y)
  112. return false;
  113. if (position.z > (p_aabb.position.z + p_aabb.size.z))
  114. return false;
  115. if ((position.z + size.z) < p_aabb.position.z)
  116. return false;
  117. return true;
  118. }
  119. inline bool AABB::encloses(const AABB &p_aabb) const {
  120. Vector3 src_min = position;
  121. Vector3 src_max = position + size;
  122. Vector3 dst_min = p_aabb.position;
  123. Vector3 dst_max = p_aabb.position + p_aabb.size;
  124. return (
  125. (src_min.x <= dst_min.x) &&
  126. (src_max.x > dst_max.x) &&
  127. (src_min.y <= dst_min.y) &&
  128. (src_max.y > dst_max.y) &&
  129. (src_min.z <= dst_min.z) &&
  130. (src_max.z > dst_max.z));
  131. }
  132. Vector3 AABB::get_support(const Vector3 &p_normal) const {
  133. Vector3 half_extents = size * 0.5;
  134. Vector3 ofs = position + half_extents;
  135. return Vector3(
  136. (p_normal.x > 0) ? -half_extents.x : half_extents.x,
  137. (p_normal.y > 0) ? -half_extents.y : half_extents.y,
  138. (p_normal.z > 0) ? -half_extents.z : half_extents.z) +
  139. ofs;
  140. }
  141. Vector3 AABB::get_endpoint(int p_point) const {
  142. switch (p_point) {
  143. case 0: return Vector3(position.x, position.y, position.z);
  144. case 1: return Vector3(position.x, position.y, position.z + size.z);
  145. case 2: return Vector3(position.x, position.y + size.y, position.z);
  146. case 3: return Vector3(position.x, position.y + size.y, position.z + size.z);
  147. case 4: return Vector3(position.x + size.x, position.y, position.z);
  148. case 5: return Vector3(position.x + size.x, position.y, position.z + size.z);
  149. case 6: return Vector3(position.x + size.x, position.y + size.y, position.z);
  150. case 7: return Vector3(position.x + size.x, position.y + size.y, position.z + size.z);
  151. };
  152. ERR_FAIL_V(Vector3());
  153. }
  154. bool AABB::intersects_convex_shape(const Plane *p_planes, int p_plane_count) const {
  155. Vector3 half_extents = size * 0.5;
  156. Vector3 ofs = position + 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. }
  169. bool AABB::has_point(const Vector3 &p_point) const {
  170. if (p_point.x < position.x)
  171. return false;
  172. if (p_point.y < position.y)
  173. return false;
  174. if (p_point.z < position.z)
  175. return false;
  176. if (p_point.x > position.x + size.x)
  177. return false;
  178. if (p_point.y > position.y + size.y)
  179. return false;
  180. if (p_point.z > position.z + size.z)
  181. return false;
  182. return true;
  183. }
  184. inline void AABB::expand_to(const Vector3 &p_vector) {
  185. Vector3 begin = position;
  186. Vector3 end = position + size;
  187. if (p_vector.x < begin.x)
  188. begin.x = p_vector.x;
  189. if (p_vector.y < begin.y)
  190. begin.y = p_vector.y;
  191. if (p_vector.z < begin.z)
  192. begin.z = p_vector.z;
  193. if (p_vector.x > end.x)
  194. end.x = p_vector.x;
  195. if (p_vector.y > end.y)
  196. end.y = p_vector.y;
  197. if (p_vector.z > end.z)
  198. end.z = p_vector.z;
  199. position = begin;
  200. size = end - begin;
  201. }
  202. void AABB::project_range_in_plane(const Plane &p_plane, real_t &r_min, real_t &r_max) const {
  203. Vector3 half_extents(size.x * 0.5, size.y * 0.5, size.z * 0.5);
  204. Vector3 center(position.x + half_extents.x, position.y + half_extents.y, position.z + half_extents.z);
  205. real_t length = p_plane.normal.abs().dot(half_extents);
  206. real_t distance = p_plane.distance_to(center);
  207. r_min = distance - length;
  208. r_max = distance + length;
  209. }
  210. inline real_t AABB::get_longest_axis_size() const {
  211. real_t max_size = size.x;
  212. if (size.y > max_size) {
  213. max_size = size.y;
  214. }
  215. if (size.z > max_size) {
  216. max_size = size.z;
  217. }
  218. return max_size;
  219. }
  220. inline real_t AABB::get_shortest_axis_size() const {
  221. real_t max_size = size.x;
  222. if (size.y < max_size) {
  223. max_size = size.y;
  224. }
  225. if (size.z < max_size) {
  226. max_size = size.z;
  227. }
  228. return max_size;
  229. }
  230. bool AABB::smits_intersect_ray(const Vector3 &p_from, const Vector3 &p_dir, real_t t0, real_t t1) const {
  231. real_t divx = 1.0 / p_dir.x;
  232. real_t divy = 1.0 / p_dir.y;
  233. real_t divz = 1.0 / p_dir.z;
  234. Vector3 upbound = position + size;
  235. real_t tmin, tmax, tymin, tymax, tzmin, tzmax;
  236. if (p_dir.x >= 0) {
  237. tmin = (position.x - p_from.x) * divx;
  238. tmax = (upbound.x - p_from.x) * divx;
  239. } else {
  240. tmin = (upbound.x - p_from.x) * divx;
  241. tmax = (position.x - p_from.x) * divx;
  242. }
  243. if (p_dir.y >= 0) {
  244. tymin = (position.y - p_from.y) * divy;
  245. tymax = (upbound.y - p_from.y) * divy;
  246. } else {
  247. tymin = (upbound.y - p_from.y) * divy;
  248. tymax = (position.y - p_from.y) * divy;
  249. }
  250. if ((tmin > tymax) || (tymin > tmax))
  251. return false;
  252. if (tymin > tmin)
  253. tmin = tymin;
  254. if (tymax < tmax)
  255. tmax = tymax;
  256. if (p_dir.z >= 0) {
  257. tzmin = (position.z - p_from.z) * divz;
  258. tzmax = (upbound.z - p_from.z) * divz;
  259. } else {
  260. tzmin = (upbound.z - p_from.z) * divz;
  261. tzmax = (position.z - p_from.z) * divz;
  262. }
  263. if ((tmin > tzmax) || (tzmin > tmax))
  264. return false;
  265. if (tzmin > tmin)
  266. tmin = tzmin;
  267. if (tzmax < tmax)
  268. tmax = tzmax;
  269. return ((tmin < t1) && (tmax > t0));
  270. }
  271. void AABB::grow_by(real_t p_amount) {
  272. position.x -= p_amount;
  273. position.y -= p_amount;
  274. position.z -= p_amount;
  275. size.x += 2.0 * p_amount;
  276. size.y += 2.0 * p_amount;
  277. size.z += 2.0 * p_amount;
  278. }
  279. #endif // AABB_H