aabb.cpp 11 KB

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