aabb.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*************************************************************************/
  2. /* aabb.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. #include "aabb.h"
  31. #include "core/string/print_string.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. 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. bool AABB::intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *r_clip, Vector3 *r_normal) const {
  99. #ifdef MATH_CHECKS
  100. if (unlikely(size.x < 0 || size.y < 0 || size.z < 0)) {
  101. ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size.");
  102. }
  103. #endif
  104. Vector3 c1, c2;
  105. Vector3 end = position + size;
  106. real_t near = -1e20;
  107. real_t far = 1e20;
  108. int axis = 0;
  109. for (int i = 0; i < 3; i++) {
  110. if (p_dir[i] == 0) {
  111. if ((p_from[i] < position[i]) || (p_from[i] > end[i])) {
  112. return false;
  113. }
  114. } else { // ray not parallel to planes in this direction
  115. c1[i] = (position[i] - p_from[i]) / p_dir[i];
  116. c2[i] = (end[i] - p_from[i]) / p_dir[i];
  117. if (c1[i] > c2[i]) {
  118. SWAP(c1, c2);
  119. }
  120. if (c1[i] > near) {
  121. near = c1[i];
  122. axis = i;
  123. }
  124. if (c2[i] < far) {
  125. far = c2[i];
  126. }
  127. if ((near > far) || (far < 0)) {
  128. return false;
  129. }
  130. }
  131. }
  132. if (r_clip) {
  133. *r_clip = c1;
  134. }
  135. if (r_normal) {
  136. *r_normal = Vector3();
  137. (*r_normal)[axis] = p_dir[axis] ? -1 : 1;
  138. }
  139. return true;
  140. }
  141. bool AABB::intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vector3 *r_clip, Vector3 *r_normal) const {
  142. #ifdef MATH_CHECKS
  143. if (unlikely(size.x < 0 || size.y < 0 || size.z < 0)) {
  144. ERR_PRINT("AABB size is negative, this is not supported. Use AABB.abs() to get an AABB with a positive size.");
  145. }
  146. #endif
  147. real_t min = 0, max = 1;
  148. int axis = 0;
  149. real_t sign = 0;
  150. for (int i = 0; i < 3; i++) {
  151. real_t seg_from = p_from[i];
  152. real_t seg_to = p_to[i];
  153. real_t box_begin = position[i];
  154. real_t box_end = box_begin + size[i];
  155. real_t cmin, cmax;
  156. real_t csign;
  157. if (seg_from < seg_to) {
  158. if (seg_from > box_end || seg_to < box_begin) {
  159. return false;
  160. }
  161. real_t length = seg_to - seg_from;
  162. cmin = (seg_from < box_begin) ? ((box_begin - seg_from) / length) : 0;
  163. cmax = (seg_to > box_end) ? ((box_end - seg_from) / length) : 1;
  164. csign = -1.0;
  165. } else {
  166. if (seg_to > box_end || seg_from < box_begin) {
  167. return false;
  168. }
  169. real_t length = seg_to - seg_from;
  170. cmin = (seg_from > box_end) ? (box_end - seg_from) / length : 0;
  171. cmax = (seg_to < box_begin) ? (box_begin - seg_from) / length : 1;
  172. csign = 1.0;
  173. }
  174. if (cmin > min) {
  175. min = cmin;
  176. axis = i;
  177. sign = csign;
  178. }
  179. if (cmax < max) {
  180. max = cmax;
  181. }
  182. if (max < min) {
  183. return false;
  184. }
  185. }
  186. Vector3 rel = p_to - p_from;
  187. if (r_normal) {
  188. Vector3 normal;
  189. normal[axis] = sign;
  190. *r_normal = normal;
  191. }
  192. if (r_clip) {
  193. *r_clip = p_from + rel * min;
  194. }
  195. return true;
  196. }
  197. bool AABB::intersects_plane(const Plane &p_plane) const {
  198. Vector3 points[8] = {
  199. Vector3(position.x, position.y, position.z),
  200. Vector3(position.x, position.y, position.z + size.z),
  201. Vector3(position.x, position.y + size.y, position.z),
  202. Vector3(position.x, position.y + size.y, position.z + size.z),
  203. Vector3(position.x + size.x, position.y, position.z),
  204. Vector3(position.x + size.x, position.y, position.z + size.z),
  205. Vector3(position.x + size.x, position.y + size.y, position.z),
  206. Vector3(position.x + size.x, position.y + size.y, position.z + size.z),
  207. };
  208. bool over = false;
  209. bool under = false;
  210. for (int i = 0; i < 8; i++) {
  211. if (p_plane.distance_to(points[i]) > 0) {
  212. over = true;
  213. } else {
  214. under = true;
  215. }
  216. }
  217. return under && over;
  218. }
  219. Vector3 AABB::get_longest_axis() const {
  220. Vector3 axis(1, 0, 0);
  221. real_t max_size = size.x;
  222. if (size.y > max_size) {
  223. axis = Vector3(0, 1, 0);
  224. max_size = size.y;
  225. }
  226. if (size.z > max_size) {
  227. axis = Vector3(0, 0, 1);
  228. }
  229. return axis;
  230. }
  231. int AABB::get_longest_axis_index() const {
  232. int axis = 0;
  233. real_t max_size = size.x;
  234. if (size.y > max_size) {
  235. axis = 1;
  236. max_size = size.y;
  237. }
  238. if (size.z > max_size) {
  239. axis = 2;
  240. }
  241. return axis;
  242. }
  243. Vector3 AABB::get_shortest_axis() const {
  244. Vector3 axis(1, 0, 0);
  245. real_t min_size = size.x;
  246. if (size.y < min_size) {
  247. axis = Vector3(0, 1, 0);
  248. min_size = size.y;
  249. }
  250. if (size.z < min_size) {
  251. axis = Vector3(0, 0, 1);
  252. }
  253. return axis;
  254. }
  255. int AABB::get_shortest_axis_index() const {
  256. int axis = 0;
  257. real_t min_size = size.x;
  258. if (size.y < min_size) {
  259. axis = 1;
  260. min_size = size.y;
  261. }
  262. if (size.z < min_size) {
  263. axis = 2;
  264. }
  265. return axis;
  266. }
  267. AABB AABB::merge(const AABB &p_with) const {
  268. AABB aabb = *this;
  269. aabb.merge_with(p_with);
  270. return aabb;
  271. }
  272. AABB AABB::expand(const Vector3 &p_vector) const {
  273. AABB aabb = *this;
  274. aabb.expand_to(p_vector);
  275. return aabb;
  276. }
  277. AABB AABB::grow(real_t p_by) const {
  278. AABB aabb = *this;
  279. aabb.grow_by(p_by);
  280. return aabb;
  281. }
  282. void AABB::get_edge(int p_edge, Vector3 &r_from, Vector3 &r_to) const {
  283. ERR_FAIL_INDEX(p_edge, 12);
  284. switch (p_edge) {
  285. case 0: {
  286. r_from = Vector3(position.x + size.x, position.y, position.z);
  287. r_to = Vector3(position.x, position.y, position.z);
  288. } break;
  289. case 1: {
  290. r_from = Vector3(position.x + size.x, position.y, position.z + size.z);
  291. r_to = Vector3(position.x + size.x, position.y, position.z);
  292. } break;
  293. case 2: {
  294. r_from = Vector3(position.x, position.y, position.z + size.z);
  295. r_to = Vector3(position.x + size.x, position.y, position.z + size.z);
  296. } break;
  297. case 3: {
  298. r_from = Vector3(position.x, position.y, position.z);
  299. r_to = Vector3(position.x, position.y, position.z + size.z);
  300. } break;
  301. case 4: {
  302. r_from = Vector3(position.x, position.y + size.y, position.z);
  303. r_to = Vector3(position.x + size.x, position.y + size.y, position.z);
  304. } break;
  305. case 5: {
  306. r_from = Vector3(position.x + size.x, position.y + size.y, position.z);
  307. r_to = Vector3(position.x + size.x, position.y + size.y, position.z + size.z);
  308. } break;
  309. case 6: {
  310. r_from = Vector3(position.x + size.x, position.y + size.y, position.z + size.z);
  311. r_to = Vector3(position.x, position.y + size.y, position.z + size.z);
  312. } break;
  313. case 7: {
  314. r_from = Vector3(position.x, position.y + size.y, position.z + size.z);
  315. r_to = Vector3(position.x, position.y + size.y, position.z);
  316. } break;
  317. case 8: {
  318. r_from = Vector3(position.x, position.y, position.z + size.z);
  319. r_to = Vector3(position.x, position.y + size.y, position.z + size.z);
  320. } break;
  321. case 9: {
  322. r_from = Vector3(position.x, position.y, position.z);
  323. r_to = Vector3(position.x, position.y + size.y, position.z);
  324. } break;
  325. case 10: {
  326. r_from = Vector3(position.x + size.x, position.y, position.z);
  327. r_to = Vector3(position.x + size.x, position.y + size.y, position.z);
  328. } break;
  329. case 11: {
  330. r_from = Vector3(position.x + size.x, position.y, position.z + size.z);
  331. r_to = Vector3(position.x + size.x, position.y + size.y, position.z + size.z);
  332. } break;
  333. }
  334. }
  335. Variant AABB::intersects_segment_bind(const Vector3 &p_from, const Vector3 &p_to) const {
  336. Vector3 inters;
  337. if (intersects_segment(p_from, p_to, &inters)) {
  338. return inters;
  339. }
  340. return Variant();
  341. }
  342. Variant AABB::intersects_ray_bind(const Vector3 &p_from, const Vector3 &p_dir) const {
  343. Vector3 inters;
  344. if (intersects_ray(p_from, p_dir, &inters)) {
  345. return inters;
  346. }
  347. return Variant();
  348. }
  349. AABB::operator String() const {
  350. return "[P: " + position.operator String() + ", S: " + size + "]";
  351. }