aabb.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*************************************************************************/
  2. /* aabb.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/print_string.h"
  32. real_t AABB::get_area() const {
  33. return size.x * size.y * size.z;
  34. }
  35. bool AABB::operator==(const AABB &p_rval) const {
  36. return ((position == p_rval.position) && (size == p_rval.size));
  37. }
  38. bool AABB::operator!=(const AABB &p_rval) const {
  39. return ((position != p_rval.position) || (size != p_rval.size));
  40. }
  41. void AABB::merge_with(const AABB &p_aabb) {
  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 = Vector3(size.x, size.y, size.z) + beg_1;
  48. end_2 = Vector3(p_aabb.size.x, p_aabb.size.y, p_aabb.size.z) + 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. AABB AABB::intersection(const AABB &p_aabb) const {
  62. Vector3 src_min = position;
  63. Vector3 src_max = position + size;
  64. Vector3 dst_min = p_aabb.position;
  65. Vector3 dst_max = p_aabb.position + p_aabb.size;
  66. Vector3 min, max;
  67. if (src_min.x > dst_max.x || src_max.x < dst_min.x) {
  68. return AABB();
  69. } else {
  70. min.x = (src_min.x > dst_min.x) ? src_min.x : dst_min.x;
  71. max.x = (src_max.x < dst_max.x) ? src_max.x : dst_max.x;
  72. }
  73. if (src_min.y > dst_max.y || src_max.y < dst_min.y) {
  74. return AABB();
  75. } else {
  76. min.y = (src_min.y > dst_min.y) ? src_min.y : dst_min.y;
  77. max.y = (src_max.y < dst_max.y) ? src_max.y : dst_max.y;
  78. }
  79. if (src_min.z > dst_max.z || src_max.z < dst_min.z) {
  80. return AABB();
  81. } else {
  82. min.z = (src_min.z > dst_min.z) ? src_min.z : dst_min.z;
  83. max.z = (src_max.z < dst_max.z) ? src_max.z : dst_max.z;
  84. }
  85. return AABB(min, max - min);
  86. }
  87. bool AABB::intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *r_clip, Vector3 *r_normal) const {
  88. Vector3 c1, c2;
  89. Vector3 end = position + size;
  90. real_t near = -1e20;
  91. real_t far = 1e20;
  92. int axis = 0;
  93. for (int i = 0; i < 3; i++) {
  94. if (p_dir[i] == 0) {
  95. if ((p_from[i] < position[i]) || (p_from[i] > end[i])) {
  96. return false;
  97. }
  98. } else { // ray not parallel to planes in this direction
  99. c1[i] = (position[i] - p_from[i]) / p_dir[i];
  100. c2[i] = (end[i] - p_from[i]) / p_dir[i];
  101. if (c1[i] > c2[i]) {
  102. SWAP(c1, c2);
  103. }
  104. if (c1[i] > near) {
  105. near = c1[i];
  106. axis = i;
  107. }
  108. if (c2[i] < far) {
  109. far = c2[i];
  110. }
  111. if ((near > far) || (far < 0)) {
  112. return false;
  113. }
  114. }
  115. }
  116. if (r_clip) {
  117. *r_clip = c1;
  118. }
  119. if (r_normal) {
  120. *r_normal = Vector3();
  121. (*r_normal)[axis] = p_dir[axis] ? -1 : 1;
  122. }
  123. return true;
  124. }
  125. bool AABB::intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vector3 *r_clip, Vector3 *r_normal) const {
  126. real_t min = 0, max = 1;
  127. int axis = 0;
  128. real_t sign = 0;
  129. for (int i = 0; i < 3; i++) {
  130. real_t seg_from = p_from[i];
  131. real_t seg_to = p_to[i];
  132. real_t box_begin = position[i];
  133. real_t box_end = box_begin + size[i];
  134. real_t cmin, cmax;
  135. real_t csign;
  136. if (seg_from < seg_to) {
  137. if (seg_from > box_end || seg_to < box_begin) {
  138. return false;
  139. }
  140. real_t length = seg_to - seg_from;
  141. cmin = (seg_from < box_begin) ? ((box_begin - seg_from) / length) : 0;
  142. cmax = (seg_to > box_end) ? ((box_end - seg_from) / length) : 1;
  143. csign = -1.0;
  144. } else {
  145. if (seg_to > box_end || seg_from < box_begin) {
  146. return false;
  147. }
  148. real_t length = seg_to - seg_from;
  149. cmin = (seg_from > box_end) ? (box_end - seg_from) / length : 0;
  150. cmax = (seg_to < box_begin) ? (box_begin - seg_from) / length : 1;
  151. csign = 1.0;
  152. }
  153. if (cmin > min) {
  154. min = cmin;
  155. axis = i;
  156. sign = csign;
  157. }
  158. if (cmax < max) {
  159. max = cmax;
  160. }
  161. if (max < min) {
  162. return false;
  163. }
  164. }
  165. Vector3 rel = p_to - p_from;
  166. if (r_normal) {
  167. Vector3 normal;
  168. normal[axis] = sign;
  169. *r_normal = normal;
  170. }
  171. if (r_clip) {
  172. *r_clip = p_from + rel * min;
  173. }
  174. return true;
  175. }
  176. bool AABB::intersects_plane(const Plane &p_plane) const {
  177. Vector3 points[8] = {
  178. Vector3(position.x, position.y, position.z),
  179. Vector3(position.x, position.y, position.z + size.z),
  180. Vector3(position.x, position.y + size.y, position.z),
  181. Vector3(position.x, position.y + size.y, position.z + size.z),
  182. Vector3(position.x + size.x, position.y, position.z),
  183. Vector3(position.x + size.x, position.y, position.z + size.z),
  184. Vector3(position.x + size.x, position.y + size.y, position.z),
  185. Vector3(position.x + size.x, position.y + size.y, position.z + size.z),
  186. };
  187. bool over = false;
  188. bool under = false;
  189. for (int i = 0; i < 8; i++) {
  190. if (p_plane.distance_to(points[i]) > 0) {
  191. over = true;
  192. } else {
  193. under = true;
  194. }
  195. }
  196. return under && over;
  197. }
  198. Vector3 AABB::get_longest_axis() const {
  199. Vector3 axis(1, 0, 0);
  200. real_t max_size = size.x;
  201. if (size.y > max_size) {
  202. axis = Vector3(0, 1, 0);
  203. max_size = size.y;
  204. }
  205. if (size.z > max_size) {
  206. axis = Vector3(0, 0, 1);
  207. }
  208. return axis;
  209. }
  210. int AABB::get_longest_axis_index() const {
  211. int axis = 0;
  212. real_t max_size = size.x;
  213. if (size.y > max_size) {
  214. axis = 1;
  215. max_size = size.y;
  216. }
  217. if (size.z > max_size) {
  218. axis = 2;
  219. }
  220. return axis;
  221. }
  222. Vector3 AABB::get_shortest_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_shortest_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. AABB AABB::merge(const AABB &p_with) const {
  247. AABB aabb = *this;
  248. aabb.merge_with(p_with);
  249. return aabb;
  250. }
  251. AABB AABB::expand(const Vector3 &p_vector) const {
  252. AABB aabb = *this;
  253. aabb.expand_to(p_vector);
  254. return aabb;
  255. }
  256. AABB AABB::grow(real_t p_by) const {
  257. AABB aabb = *this;
  258. aabb.grow_by(p_by);
  259. return aabb;
  260. }
  261. void AABB::get_edge(int p_edge, Vector3 &r_from, Vector3 &r_to) const {
  262. ERR_FAIL_INDEX(p_edge, 12);
  263. switch (p_edge) {
  264. case 0: {
  265. r_from = Vector3(position.x + size.x, position.y, position.z);
  266. r_to = Vector3(position.x, position.y, position.z);
  267. } break;
  268. case 1: {
  269. r_from = Vector3(position.x + size.x, position.y, position.z + size.z);
  270. r_to = Vector3(position.x + size.x, position.y, position.z);
  271. } break;
  272. case 2: {
  273. r_from = Vector3(position.x, position.y, position.z + size.z);
  274. r_to = Vector3(position.x + size.x, position.y, position.z + size.z);
  275. } break;
  276. case 3: {
  277. r_from = Vector3(position.x, position.y, position.z);
  278. r_to = Vector3(position.x, position.y, position.z + size.z);
  279. } break;
  280. case 4: {
  281. r_from = Vector3(position.x, position.y + size.y, position.z);
  282. r_to = Vector3(position.x + size.x, position.y + size.y, position.z);
  283. } break;
  284. case 5: {
  285. r_from = Vector3(position.x + size.x, position.y + size.y, position.z);
  286. r_to = Vector3(position.x + size.x, position.y + size.y, position.z + size.z);
  287. } break;
  288. case 6: {
  289. r_from = Vector3(position.x + size.x, position.y + size.y, position.z + size.z);
  290. r_to = Vector3(position.x, position.y + size.y, position.z + size.z);
  291. } break;
  292. case 7: {
  293. r_from = Vector3(position.x, position.y + size.y, position.z + size.z);
  294. r_to = Vector3(position.x, position.y + size.y, position.z);
  295. } break;
  296. case 8: {
  297. r_from = Vector3(position.x, position.y, position.z + size.z);
  298. r_to = Vector3(position.x, position.y + size.y, position.z + size.z);
  299. } break;
  300. case 9: {
  301. r_from = Vector3(position.x, position.y, position.z);
  302. r_to = Vector3(position.x, position.y + size.y, position.z);
  303. } break;
  304. case 10: {
  305. r_from = Vector3(position.x + size.x, position.y, position.z);
  306. r_to = Vector3(position.x + size.x, position.y + size.y, position.z);
  307. } break;
  308. case 11: {
  309. r_from = Vector3(position.x + size.x, position.y, position.z + size.z);
  310. r_to = Vector3(position.x + size.x, position.y + size.y, position.z + size.z);
  311. } break;
  312. }
  313. }
  314. AABB::operator String() const {
  315. return String() + position + " - " + size;
  316. }