aabb.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /**************************************************************************/
  2. /* aabb.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #include "aabb.h"
  31. #include "core/string/ustring.h"
  32. #include "core/variant/variant.h"
  33. real_t AABB::get_volume() const {
  34. return size.x * size.y * size.z;
  35. }
  36. bool AABB::operator==(const AABB &p_rval) const {
  37. return ((position == p_rval.position) && (size == p_rval.size));
  38. }
  39. bool AABB::operator!=(const AABB &p_rval) const {
  40. return ((position != p_rval.position) || (size != p_rval.size));
  41. }
  42. void AABB::merge_with(const AABB &p_aabb) {
  43. #ifdef MATH_CHECKS
  44. if (unlikely(size.x < 0 || size.y < 0 || size.z < 0 || p_aabb.size.x < 0 || p_aabb.size.y < 0 || p_aabb.size.z < 0)) {
  45. ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size.");
  46. }
  47. #endif
  48. Vector3 beg_1, beg_2;
  49. Vector3 end_1, end_2;
  50. Vector3 min, max;
  51. beg_1 = position;
  52. beg_2 = p_aabb.position;
  53. end_1 = size + beg_1;
  54. end_2 = p_aabb.size + beg_2;
  55. min.x = (beg_1.x < beg_2.x) ? beg_1.x : beg_2.x;
  56. min.y = (beg_1.y < beg_2.y) ? beg_1.y : beg_2.y;
  57. min.z = (beg_1.z < beg_2.z) ? beg_1.z : beg_2.z;
  58. max.x = (end_1.x > end_2.x) ? end_1.x : end_2.x;
  59. max.y = (end_1.y > end_2.y) ? end_1.y : end_2.y;
  60. max.z = (end_1.z > end_2.z) ? end_1.z : end_2.z;
  61. position = min;
  62. size = max - min;
  63. }
  64. bool AABB::is_equal_approx(const AABB &p_aabb) const {
  65. return position.is_equal_approx(p_aabb.position) && size.is_equal_approx(p_aabb.size);
  66. }
  67. bool AABB::is_finite() const {
  68. return position.is_finite() && size.is_finite();
  69. }
  70. AABB AABB::intersection(const AABB &p_aabb) const {
  71. #ifdef MATH_CHECKS
  72. if (unlikely(size.x < 0 || size.y < 0 || size.z < 0 || p_aabb.size.x < 0 || p_aabb.size.y < 0 || p_aabb.size.z < 0)) {
  73. ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size.");
  74. }
  75. #endif
  76. Vector3 src_min = position;
  77. Vector3 src_max = position + size;
  78. Vector3 dst_min = p_aabb.position;
  79. Vector3 dst_max = p_aabb.position + p_aabb.size;
  80. Vector3 min, max;
  81. if (src_min.x > dst_max.x || src_max.x < dst_min.x) {
  82. return AABB();
  83. } else {
  84. min.x = (src_min.x > dst_min.x) ? src_min.x : dst_min.x;
  85. max.x = (src_max.x < dst_max.x) ? src_max.x : dst_max.x;
  86. }
  87. if (src_min.y > dst_max.y || src_max.y < dst_min.y) {
  88. return AABB();
  89. } else {
  90. min.y = (src_min.y > dst_min.y) ? src_min.y : dst_min.y;
  91. max.y = (src_max.y < dst_max.y) ? src_max.y : dst_max.y;
  92. }
  93. if (src_min.z > dst_max.z || src_max.z < dst_min.z) {
  94. return AABB();
  95. } else {
  96. min.z = (src_min.z > dst_min.z) ? src_min.z : dst_min.z;
  97. max.z = (src_max.z < dst_max.z) ? src_max.z : dst_max.z;
  98. }
  99. return AABB(min, max - min);
  100. }
  101. // Note that this routine returns the BACKTRACKED (i.e. behind the ray origin)
  102. // intersection point + normal if INSIDE the AABB.
  103. // The caller can therefore decide when INSIDE whether to use the
  104. // backtracked intersection, or use p_from as the intersection, and
  105. // carry on progressing without e.g. reflecting against the normal.
  106. bool AABB::find_intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, bool &r_inside, Vector3 *r_intersection_point, Vector3 *r_normal) const {
  107. #ifdef MATH_CHECKS
  108. if (unlikely(size.x < 0 || size.y < 0 || size.z < 0)) {
  109. ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size.");
  110. }
  111. #endif
  112. Vector3 end = position + size;
  113. real_t tmin = -1e20;
  114. real_t tmax = 1e20;
  115. int axis = 0;
  116. // Make sure r_inside is always initialized,
  117. // to prevent reading uninitialized data in the client code.
  118. r_inside = false;
  119. for (int i = 0; i < 3; i++) {
  120. if (p_dir[i] == 0) {
  121. if ((p_from[i] < position[i]) || (p_from[i] > end[i])) {
  122. return false;
  123. }
  124. } else { // ray not parallel to planes in this direction
  125. real_t t1 = (position[i] - p_from[i]) / p_dir[i];
  126. real_t t2 = (end[i] - p_from[i]) / p_dir[i];
  127. if (t1 > t2) {
  128. SWAP(t1, t2);
  129. }
  130. if (t1 >= tmin) {
  131. tmin = t1;
  132. axis = i;
  133. }
  134. if (t2 < tmax) {
  135. if (t2 < 0) {
  136. return false;
  137. }
  138. tmax = t2;
  139. }
  140. if (tmin > tmax) {
  141. return false;
  142. }
  143. }
  144. }
  145. // Did the ray start from inside the box?
  146. // In which case the intersection returned is the point of entry
  147. // (behind the ray start) or the calling routine can use the ray origin as intersection point.
  148. r_inside = tmin < 0;
  149. if (r_intersection_point) {
  150. *r_intersection_point = p_from + p_dir * tmin;
  151. // Prevent float error by making sure the point is exactly
  152. // on the AABB border on the relevant axis.
  153. r_intersection_point->coord[axis] = (p_dir[axis] >= 0) ? position.coord[axis] : end.coord[axis];
  154. }
  155. if (r_normal) {
  156. *r_normal = Vector3();
  157. (*r_normal)[axis] = (p_dir[axis] >= 0) ? -1 : 1;
  158. }
  159. return true;
  160. }
  161. bool AABB::intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vector3 *r_intersection_point, Vector3 *r_normal) const {
  162. #ifdef MATH_CHECKS
  163. if (unlikely(size.x < 0 || size.y < 0 || size.z < 0)) {
  164. ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size.");
  165. }
  166. #endif
  167. real_t min = 0, max = 1;
  168. int axis = 0;
  169. real_t sign = 0;
  170. for (int i = 0; i < 3; i++) {
  171. real_t seg_from = p_from[i];
  172. real_t seg_to = p_to[i];
  173. real_t box_begin = position[i];
  174. real_t box_end = box_begin + size[i];
  175. real_t cmin, cmax;
  176. real_t csign;
  177. if (seg_from < seg_to) {
  178. if (seg_from > box_end || seg_to < box_begin) {
  179. return false;
  180. }
  181. real_t length = seg_to - seg_from;
  182. cmin = (seg_from < box_begin) ? ((box_begin - seg_from) / length) : 0;
  183. cmax = (seg_to > box_end) ? ((box_end - seg_from) / length) : 1;
  184. csign = -1.0;
  185. } else {
  186. if (seg_to > box_end || seg_from < box_begin) {
  187. return false;
  188. }
  189. real_t length = seg_to - seg_from;
  190. cmin = (seg_from > box_end) ? (box_end - seg_from) / length : 0;
  191. cmax = (seg_to < box_begin) ? (box_begin - seg_from) / length : 1;
  192. csign = 1.0;
  193. }
  194. if (cmin > min) {
  195. min = cmin;
  196. axis = i;
  197. sign = csign;
  198. }
  199. if (cmax < max) {
  200. max = cmax;
  201. }
  202. if (max < min) {
  203. return false;
  204. }
  205. }
  206. Vector3 rel = p_to - p_from;
  207. if (r_normal) {
  208. Vector3 normal;
  209. normal[axis] = sign;
  210. *r_normal = normal;
  211. }
  212. if (r_intersection_point) {
  213. *r_intersection_point = p_from + rel * min;
  214. }
  215. return true;
  216. }
  217. bool AABB::intersects_plane(const Plane &p_plane) const {
  218. Vector3 points[8] = {
  219. Vector3(position.x, position.y, position.z),
  220. Vector3(position.x, position.y, position.z + size.z),
  221. Vector3(position.x, position.y + size.y, position.z),
  222. Vector3(position.x, position.y + size.y, position.z + size.z),
  223. Vector3(position.x + size.x, position.y, position.z),
  224. Vector3(position.x + size.x, position.y, position.z + size.z),
  225. Vector3(position.x + size.x, position.y + size.y, position.z),
  226. Vector3(position.x + size.x, position.y + size.y, position.z + size.z),
  227. };
  228. bool over = false;
  229. bool under = false;
  230. for (int i = 0; i < 8; i++) {
  231. if (p_plane.distance_to(points[i]) > 0) {
  232. over = true;
  233. } else {
  234. under = true;
  235. }
  236. }
  237. return under && over;
  238. }
  239. Vector3 AABB::get_longest_axis() const {
  240. Vector3 axis(1, 0, 0);
  241. real_t max_size = size.x;
  242. if (size.y > max_size) {
  243. axis = Vector3(0, 1, 0);
  244. max_size = size.y;
  245. }
  246. if (size.z > max_size) {
  247. axis = Vector3(0, 0, 1);
  248. }
  249. return axis;
  250. }
  251. int AABB::get_longest_axis_index() const {
  252. int axis = 0;
  253. real_t max_size = size.x;
  254. if (size.y > max_size) {
  255. axis = 1;
  256. max_size = size.y;
  257. }
  258. if (size.z > max_size) {
  259. axis = 2;
  260. }
  261. return axis;
  262. }
  263. Vector3 AABB::get_shortest_axis() const {
  264. Vector3 axis(1, 0, 0);
  265. real_t min_size = size.x;
  266. if (size.y < min_size) {
  267. axis = Vector3(0, 1, 0);
  268. min_size = size.y;
  269. }
  270. if (size.z < min_size) {
  271. axis = Vector3(0, 0, 1);
  272. }
  273. return axis;
  274. }
  275. int AABB::get_shortest_axis_index() const {
  276. int axis = 0;
  277. real_t min_size = size.x;
  278. if (size.y < min_size) {
  279. axis = 1;
  280. min_size = size.y;
  281. }
  282. if (size.z < min_size) {
  283. axis = 2;
  284. }
  285. return axis;
  286. }
  287. AABB AABB::merge(const AABB &p_with) const {
  288. AABB aabb = *this;
  289. aabb.merge_with(p_with);
  290. return aabb;
  291. }
  292. AABB AABB::expand(const Vector3 &p_vector) const {
  293. AABB aabb = *this;
  294. aabb.expand_to(p_vector);
  295. return aabb;
  296. }
  297. AABB AABB::grow(real_t p_by) const {
  298. AABB aabb = *this;
  299. aabb.grow_by(p_by);
  300. return aabb;
  301. }
  302. void AABB::get_edge(int p_edge, Vector3 &r_from, Vector3 &r_to) const {
  303. ERR_FAIL_INDEX(p_edge, 12);
  304. switch (p_edge) {
  305. case 0: {
  306. r_from = Vector3(position.x + size.x, position.y, position.z);
  307. r_to = Vector3(position.x, position.y, position.z);
  308. } break;
  309. case 1: {
  310. r_from = Vector3(position.x + size.x, position.y, position.z + size.z);
  311. r_to = Vector3(position.x + size.x, position.y, position.z);
  312. } break;
  313. case 2: {
  314. r_from = Vector3(position.x, position.y, position.z + size.z);
  315. r_to = Vector3(position.x + size.x, position.y, position.z + size.z);
  316. } break;
  317. case 3: {
  318. r_from = Vector3(position.x, position.y, position.z);
  319. r_to = Vector3(position.x, position.y, position.z + size.z);
  320. } break;
  321. case 4: {
  322. r_from = Vector3(position.x, position.y + size.y, position.z);
  323. r_to = Vector3(position.x + size.x, position.y + size.y, position.z);
  324. } break;
  325. case 5: {
  326. r_from = Vector3(position.x + size.x, position.y + size.y, position.z);
  327. r_to = Vector3(position.x + size.x, position.y + size.y, position.z + size.z);
  328. } break;
  329. case 6: {
  330. r_from = Vector3(position.x + size.x, position.y + size.y, position.z + size.z);
  331. r_to = Vector3(position.x, position.y + size.y, position.z + size.z);
  332. } break;
  333. case 7: {
  334. r_from = Vector3(position.x, position.y + size.y, position.z + size.z);
  335. r_to = Vector3(position.x, position.y + size.y, position.z);
  336. } break;
  337. case 8: {
  338. r_from = Vector3(position.x, position.y, position.z + size.z);
  339. r_to = Vector3(position.x, position.y + size.y, position.z + size.z);
  340. } break;
  341. case 9: {
  342. r_from = Vector3(position.x, position.y, position.z);
  343. r_to = Vector3(position.x, position.y + size.y, position.z);
  344. } break;
  345. case 10: {
  346. r_from = Vector3(position.x + size.x, position.y, position.z);
  347. r_to = Vector3(position.x + size.x, position.y + size.y, position.z);
  348. } break;
  349. case 11: {
  350. r_from = Vector3(position.x + size.x, position.y, position.z + size.z);
  351. r_to = Vector3(position.x + size.x, position.y + size.y, position.z + size.z);
  352. } break;
  353. }
  354. }
  355. Variant AABB::intersects_segment_bind(const Vector3 &p_from, const Vector3 &p_to) const {
  356. Vector3 inters;
  357. if (intersects_segment(p_from, p_to, &inters)) {
  358. return inters;
  359. }
  360. return Variant();
  361. }
  362. Variant AABB::intersects_ray_bind(const Vector3 &p_from, const Vector3 &p_dir) const {
  363. Vector3 inters;
  364. bool inside = false;
  365. if (find_intersects_ray(p_from, p_dir, inside, &inters)) {
  366. // When inside the intersection point may be BEHIND the ray,
  367. // so for general use we return the ray origin.
  368. if (inside) {
  369. return p_from;
  370. }
  371. return inters;
  372. }
  373. return Variant();
  374. }
  375. AABB::operator String() const {
  376. return "[P: " + position.operator String() + ", S: " + size + "]";
  377. }