aabb.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*************************************************************************/
  2. /* aabb.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. if (r_normal) {
  119. *r_normal = Vector3();
  120. (*r_normal)[axis] = p_dir[axis] ? -1 : 1;
  121. }
  122. return true;
  123. }
  124. bool AABB::intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vector3 *r_clip, Vector3 *r_normal) const {
  125. real_t min = 0, max = 1;
  126. int axis = 0;
  127. real_t sign = 0;
  128. for (int i = 0; i < 3; i++) {
  129. real_t seg_from = p_from[i];
  130. real_t seg_to = p_to[i];
  131. real_t box_begin = position[i];
  132. real_t box_end = box_begin + size[i];
  133. real_t cmin, cmax;
  134. real_t csign;
  135. if (seg_from < seg_to) {
  136. if (seg_from > box_end || seg_to < box_begin)
  137. return false;
  138. real_t length = seg_to - seg_from;
  139. cmin = (seg_from < box_begin) ? ((box_begin - seg_from) / length) : 0;
  140. cmax = (seg_to > box_end) ? ((box_end - seg_from) / length) : 1;
  141. csign = -1.0;
  142. } else {
  143. if (seg_to > box_end || seg_from < box_begin)
  144. return false;
  145. real_t length = seg_to - seg_from;
  146. cmin = (seg_from > box_end) ? (box_end - seg_from) / length : 0;
  147. cmax = (seg_to < box_begin) ? (box_begin - seg_from) / length : 1;
  148. csign = 1.0;
  149. }
  150. if (cmin > min) {
  151. min = cmin;
  152. axis = i;
  153. sign = csign;
  154. }
  155. if (cmax < max)
  156. max = cmax;
  157. if (max < min)
  158. return false;
  159. }
  160. Vector3 rel = p_to - p_from;
  161. if (r_normal) {
  162. Vector3 normal;
  163. normal[axis] = sign;
  164. *r_normal = normal;
  165. }
  166. if (r_clip)
  167. *r_clip = p_from + rel * min;
  168. return true;
  169. }
  170. bool AABB::intersects_plane(const Plane &p_plane) const {
  171. Vector3 points[8] = {
  172. Vector3(position.x, position.y, position.z),
  173. Vector3(position.x, position.y, position.z + size.z),
  174. Vector3(position.x, position.y + size.y, position.z),
  175. Vector3(position.x, position.y + size.y, position.z + size.z),
  176. Vector3(position.x + size.x, position.y, position.z),
  177. Vector3(position.x + size.x, position.y, position.z + size.z),
  178. Vector3(position.x + size.x, position.y + size.y, position.z),
  179. Vector3(position.x + size.x, position.y + size.y, position.z + size.z),
  180. };
  181. bool over = false;
  182. bool under = false;
  183. for (int i = 0; i < 8; i++) {
  184. if (p_plane.distance_to(points[i]) > 0)
  185. over = true;
  186. else
  187. under = true;
  188. }
  189. return under && over;
  190. }
  191. Vector3 AABB::get_longest_axis() const {
  192. Vector3 axis(1, 0, 0);
  193. real_t max_size = size.x;
  194. if (size.y > max_size) {
  195. axis = Vector3(0, 1, 0);
  196. max_size = size.y;
  197. }
  198. if (size.z > max_size) {
  199. axis = Vector3(0, 0, 1);
  200. }
  201. return axis;
  202. }
  203. int AABB::get_longest_axis_index() const {
  204. int axis = 0;
  205. real_t max_size = size.x;
  206. if (size.y > max_size) {
  207. axis = 1;
  208. max_size = size.y;
  209. }
  210. if (size.z > max_size) {
  211. axis = 2;
  212. }
  213. return axis;
  214. }
  215. Vector3 AABB::get_shortest_axis() const {
  216. Vector3 axis(1, 0, 0);
  217. real_t max_size = size.x;
  218. if (size.y < max_size) {
  219. axis = Vector3(0, 1, 0);
  220. max_size = size.y;
  221. }
  222. if (size.z < max_size) {
  223. axis = Vector3(0, 0, 1);
  224. }
  225. return axis;
  226. }
  227. int AABB::get_shortest_axis_index() const {
  228. int axis = 0;
  229. real_t max_size = size.x;
  230. if (size.y < max_size) {
  231. axis = 1;
  232. max_size = size.y;
  233. }
  234. if (size.z < max_size) {
  235. axis = 2;
  236. }
  237. return axis;
  238. }
  239. AABB AABB::merge(const AABB &p_with) const {
  240. AABB aabb = *this;
  241. aabb.merge_with(p_with);
  242. return aabb;
  243. }
  244. AABB AABB::expand(const Vector3 &p_vector) const {
  245. AABB aabb = *this;
  246. aabb.expand_to(p_vector);
  247. return aabb;
  248. }
  249. AABB AABB::grow(real_t p_by) const {
  250. AABB aabb = *this;
  251. aabb.grow_by(p_by);
  252. return aabb;
  253. }
  254. void AABB::get_edge(int p_edge, Vector3 &r_from, Vector3 &r_to) const {
  255. ERR_FAIL_INDEX(p_edge, 12);
  256. switch (p_edge) {
  257. case 0: {
  258. r_from = Vector3(position.x + size.x, position.y, position.z);
  259. r_to = Vector3(position.x, position.y, position.z);
  260. } break;
  261. case 1: {
  262. r_from = Vector3(position.x + size.x, position.y, position.z + size.z);
  263. r_to = Vector3(position.x + size.x, position.y, position.z);
  264. } break;
  265. case 2: {
  266. r_from = Vector3(position.x, position.y, position.z + size.z);
  267. r_to = Vector3(position.x + size.x, position.y, position.z + size.z);
  268. } break;
  269. case 3: {
  270. r_from = Vector3(position.x, position.y, position.z);
  271. r_to = Vector3(position.x, position.y, position.z + size.z);
  272. } break;
  273. case 4: {
  274. r_from = Vector3(position.x, position.y + size.y, position.z);
  275. r_to = Vector3(position.x + size.x, position.y + size.y, position.z);
  276. } break;
  277. case 5: {
  278. r_from = Vector3(position.x + size.x, position.y + size.y, position.z);
  279. r_to = Vector3(position.x + size.x, position.y + size.y, position.z + size.z);
  280. } break;
  281. case 6: {
  282. r_from = Vector3(position.x + size.x, position.y + size.y, position.z + size.z);
  283. r_to = Vector3(position.x, position.y + size.y, position.z + size.z);
  284. } break;
  285. case 7: {
  286. r_from = Vector3(position.x, position.y + size.y, position.z + size.z);
  287. r_to = Vector3(position.x, position.y + size.y, position.z);
  288. } break;
  289. case 8: {
  290. r_from = Vector3(position.x, position.y, position.z + size.z);
  291. r_to = Vector3(position.x, position.y + size.y, position.z + size.z);
  292. } break;
  293. case 9: {
  294. r_from = Vector3(position.x, position.y, position.z);
  295. r_to = Vector3(position.x, position.y + size.y, position.z);
  296. } break;
  297. case 10: {
  298. r_from = Vector3(position.x + size.x, position.y, position.z);
  299. r_to = Vector3(position.x + size.x, position.y + size.y, position.z);
  300. } break;
  301. case 11: {
  302. r_from = Vector3(position.x + size.x, position.y, position.z + size.z);
  303. r_to = Vector3(position.x + size.x, position.y + size.y, position.z + size.z);
  304. } break;
  305. }
  306. }
  307. AABB::operator String() const {
  308. return String() + position + " - " + size;
  309. }