aabb.cpp 12 KB

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