aabb.cpp 13 KB

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