aabb.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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/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. bool AABB::create_from_points(const Vector<Vector3> &p_points) {
  42. if (!p_points.size()) {
  43. return false;
  44. }
  45. Vector3 begin = p_points[0];
  46. Vector3 end = begin;
  47. for (int n = 1; n < p_points.size(); n++) {
  48. const Vector3 &pt = p_points[n];
  49. if (pt.x < begin.x) {
  50. begin.x = pt.x;
  51. }
  52. if (pt.y < begin.y) {
  53. begin.y = pt.y;
  54. }
  55. if (pt.z < begin.z) {
  56. begin.z = pt.z;
  57. }
  58. if (pt.x > end.x) {
  59. end.x = pt.x;
  60. }
  61. if (pt.y > end.y) {
  62. end.y = pt.y;
  63. }
  64. if (pt.z > end.z) {
  65. end.z = pt.z;
  66. }
  67. }
  68. position = begin;
  69. size = end - begin;
  70. return true;
  71. }
  72. void AABB::merge_with(const AABB &p_aabb) {
  73. Vector3 beg_1, beg_2;
  74. Vector3 end_1, end_2;
  75. Vector3 min, max;
  76. beg_1 = position;
  77. beg_2 = p_aabb.position;
  78. end_1 = Vector3(size.x, size.y, size.z) + beg_1;
  79. end_2 = Vector3(p_aabb.size.x, p_aabb.size.y, p_aabb.size.z) + beg_2;
  80. min.x = (beg_1.x < beg_2.x) ? beg_1.x : beg_2.x;
  81. min.y = (beg_1.y < beg_2.y) ? beg_1.y : beg_2.y;
  82. min.z = (beg_1.z < beg_2.z) ? beg_1.z : beg_2.z;
  83. max.x = (end_1.x > end_2.x) ? end_1.x : end_2.x;
  84. max.y = (end_1.y > end_2.y) ? end_1.y : end_2.y;
  85. max.z = (end_1.z > end_2.z) ? end_1.z : end_2.z;
  86. position = min;
  87. size = max - min;
  88. }
  89. bool AABB::is_equal_approx(const AABB &p_aabb) const {
  90. return position.is_equal_approx(p_aabb.position) && size.is_equal_approx(p_aabb.size);
  91. }
  92. AABB AABB::intersection(const AABB &p_aabb) const {
  93. Vector3 src_min = position;
  94. Vector3 src_max = position + size;
  95. Vector3 dst_min = p_aabb.position;
  96. Vector3 dst_max = p_aabb.position + p_aabb.size;
  97. Vector3 min, max;
  98. if (src_min.x > dst_max.x || src_max.x < dst_min.x) {
  99. return AABB();
  100. } else {
  101. min.x = (src_min.x > dst_min.x) ? src_min.x : dst_min.x;
  102. max.x = (src_max.x < dst_max.x) ? src_max.x : dst_max.x;
  103. }
  104. if (src_min.y > dst_max.y || src_max.y < dst_min.y) {
  105. return AABB();
  106. } else {
  107. min.y = (src_min.y > dst_min.y) ? src_min.y : dst_min.y;
  108. max.y = (src_max.y < dst_max.y) ? src_max.y : dst_max.y;
  109. }
  110. if (src_min.z > dst_max.z || src_max.z < dst_min.z) {
  111. return AABB();
  112. } else {
  113. min.z = (src_min.z > dst_min.z) ? src_min.z : dst_min.z;
  114. max.z = (src_max.z < dst_max.z) ? src_max.z : dst_max.z;
  115. }
  116. return AABB(min, max - min);
  117. }
  118. bool AABB::intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *r_clip, Vector3 *r_normal) const {
  119. Vector3 c1, c2;
  120. Vector3 end = position + size;
  121. real_t near = -1e20;
  122. real_t far = 1e20;
  123. int axis = 0;
  124. for (int i = 0; i < 3; i++) {
  125. if (p_dir[i] == 0) {
  126. if ((p_from[i] < position[i]) || (p_from[i] > end[i])) {
  127. return false;
  128. }
  129. } else { // ray not parallel to planes in this direction
  130. c1[i] = (position[i] - p_from[i]) / p_dir[i];
  131. c2[i] = (end[i] - p_from[i]) / p_dir[i];
  132. if (c1[i] > c2[i]) {
  133. SWAP(c1, c2);
  134. }
  135. if (c1[i] > near) {
  136. near = c1[i];
  137. axis = i;
  138. }
  139. if (c2[i] < far) {
  140. far = c2[i];
  141. }
  142. if ((near > far) || (far < 0)) {
  143. return false;
  144. }
  145. }
  146. }
  147. if (r_clip) {
  148. *r_clip = c1;
  149. }
  150. if (r_normal) {
  151. *r_normal = Vector3();
  152. (*r_normal)[axis] = p_dir[axis] ? -1 : 1;
  153. }
  154. return true;
  155. }
  156. bool AABB::intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vector3 *r_clip, Vector3 *r_normal) const {
  157. real_t min = 0, max = 1;
  158. int axis = 0;
  159. real_t sign = 0;
  160. for (int i = 0; i < 3; i++) {
  161. real_t seg_from = p_from[i];
  162. real_t seg_to = p_to[i];
  163. real_t box_begin = position[i];
  164. real_t box_end = box_begin + size[i];
  165. real_t cmin, cmax;
  166. real_t csign;
  167. if (seg_from < seg_to) {
  168. if (seg_from > box_end || seg_to < box_begin) {
  169. return false;
  170. }
  171. real_t length = seg_to - seg_from;
  172. cmin = (seg_from < box_begin) ? ((box_begin - seg_from) / length) : 0;
  173. cmax = (seg_to > box_end) ? ((box_end - seg_from) / length) : 1;
  174. csign = -1.0;
  175. } else {
  176. if (seg_to > box_end || seg_from < box_begin) {
  177. return false;
  178. }
  179. real_t length = seg_to - seg_from;
  180. cmin = (seg_from > box_end) ? (box_end - seg_from) / length : 0;
  181. cmax = (seg_to < box_begin) ? (box_begin - seg_from) / length : 1;
  182. csign = 1.0;
  183. }
  184. if (cmin > min) {
  185. min = cmin;
  186. axis = i;
  187. sign = csign;
  188. }
  189. if (cmax < max) {
  190. max = cmax;
  191. }
  192. if (max < min) {
  193. return false;
  194. }
  195. }
  196. Vector3 rel = p_to - p_from;
  197. if (r_normal) {
  198. Vector3 normal;
  199. normal[axis] = sign;
  200. *r_normal = normal;
  201. }
  202. if (r_clip) {
  203. *r_clip = p_from + rel * min;
  204. }
  205. return true;
  206. }
  207. bool AABB::intersects_plane(const Plane &p_plane) const {
  208. Vector3 points[8] = {
  209. Vector3(position.x, position.y, position.z),
  210. Vector3(position.x, position.y, position.z + size.z),
  211. Vector3(position.x, position.y + size.y, position.z),
  212. Vector3(position.x, position.y + size.y, position.z + size.z),
  213. Vector3(position.x + size.x, position.y, position.z),
  214. Vector3(position.x + size.x, position.y, position.z + size.z),
  215. Vector3(position.x + size.x, position.y + size.y, position.z),
  216. Vector3(position.x + size.x, position.y + size.y, position.z + size.z),
  217. };
  218. bool over = false;
  219. bool under = false;
  220. for (int i = 0; i < 8; i++) {
  221. if (p_plane.distance_to(points[i]) > 0) {
  222. over = true;
  223. } else {
  224. under = true;
  225. }
  226. }
  227. return under && over;
  228. }
  229. Vector3 AABB::get_longest_axis() const {
  230. Vector3 axis(1, 0, 0);
  231. real_t max_size = size.x;
  232. if (size.y > max_size) {
  233. axis = Vector3(0, 1, 0);
  234. max_size = size.y;
  235. }
  236. if (size.z > max_size) {
  237. axis = Vector3(0, 0, 1);
  238. }
  239. return axis;
  240. }
  241. int AABB::get_longest_axis_index() const {
  242. int axis = 0;
  243. real_t max_size = size.x;
  244. if (size.y > max_size) {
  245. axis = 1;
  246. max_size = size.y;
  247. }
  248. if (size.z > max_size) {
  249. axis = 2;
  250. }
  251. return axis;
  252. }
  253. Vector3 AABB::get_shortest_axis() const {
  254. Vector3 axis(1, 0, 0);
  255. real_t max_size = size.x;
  256. if (size.y < max_size) {
  257. axis = Vector3(0, 1, 0);
  258. max_size = size.y;
  259. }
  260. if (size.z < max_size) {
  261. axis = Vector3(0, 0, 1);
  262. }
  263. return axis;
  264. }
  265. int AABB::get_shortest_axis_index() const {
  266. int axis = 0;
  267. real_t max_size = size.x;
  268. if (size.y < max_size) {
  269. axis = 1;
  270. max_size = size.y;
  271. }
  272. if (size.z < max_size) {
  273. axis = 2;
  274. }
  275. return axis;
  276. }
  277. AABB AABB::merge(const AABB &p_with) const {
  278. AABB aabb = *this;
  279. aabb.merge_with(p_with);
  280. return aabb;
  281. }
  282. AABB AABB::expand(const Vector3 &p_vector) const {
  283. AABB aabb = *this;
  284. aabb.expand_to(p_vector);
  285. return aabb;
  286. }
  287. AABB AABB::grow(real_t p_by) const {
  288. AABB aabb = *this;
  289. aabb.grow_by(p_by);
  290. return aabb;
  291. }
  292. void AABB::get_edge(int p_edge, Vector3 &r_from, Vector3 &r_to) const {
  293. ERR_FAIL_INDEX(p_edge, 12);
  294. switch (p_edge) {
  295. case 0: {
  296. r_from = Vector3(position.x + size.x, position.y, position.z);
  297. r_to = Vector3(position.x, position.y, position.z);
  298. } break;
  299. case 1: {
  300. r_from = Vector3(position.x + size.x, position.y, position.z + size.z);
  301. r_to = Vector3(position.x + size.x, position.y, position.z);
  302. } break;
  303. case 2: {
  304. r_from = Vector3(position.x, position.y, position.z + size.z);
  305. r_to = Vector3(position.x + size.x, position.y, position.z + size.z);
  306. } break;
  307. case 3: {
  308. r_from = Vector3(position.x, position.y, position.z);
  309. r_to = Vector3(position.x, position.y, position.z + size.z);
  310. } break;
  311. case 4: {
  312. r_from = Vector3(position.x, position.y + size.y, position.z);
  313. r_to = Vector3(position.x + size.x, position.y + size.y, position.z);
  314. } break;
  315. case 5: {
  316. r_from = Vector3(position.x + size.x, position.y + size.y, position.z);
  317. r_to = Vector3(position.x + size.x, position.y + size.y, position.z + size.z);
  318. } break;
  319. case 6: {
  320. r_from = Vector3(position.x + size.x, position.y + size.y, position.z + size.z);
  321. r_to = Vector3(position.x, position.y + size.y, position.z + size.z);
  322. } break;
  323. case 7: {
  324. r_from = Vector3(position.x, position.y + size.y, position.z + size.z);
  325. r_to = Vector3(position.x, position.y + size.y, position.z);
  326. } break;
  327. case 8: {
  328. r_from = Vector3(position.x, position.y, position.z + size.z);
  329. r_to = Vector3(position.x, position.y + size.y, position.z + size.z);
  330. } break;
  331. case 9: {
  332. r_from = Vector3(position.x, position.y, position.z);
  333. r_to = Vector3(position.x, position.y + size.y, position.z);
  334. } break;
  335. case 10: {
  336. r_from = Vector3(position.x + size.x, position.y, position.z);
  337. r_to = Vector3(position.x + size.x, position.y + size.y, position.z);
  338. } break;
  339. case 11: {
  340. r_from = Vector3(position.x + size.x, position.y, position.z + size.z);
  341. r_to = Vector3(position.x + size.x, position.y + size.y, position.z + size.z);
  342. } break;
  343. }
  344. }
  345. AABB::operator String() const {
  346. return String() + position + " - " + size;
  347. }