aabb.cpp 12 KB

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