aabb.hpp 11 KB

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