rect3.cpp 10 KB

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