rect2.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /**************************************************************************/
  2. /* rect2.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 "rect2.h"
  31. #include "core/math/rect2i.h"
  32. #include "core/math/transform_2d.h"
  33. #include "core/string/ustring.h"
  34. bool Rect2::is_equal_approx(const Rect2 &p_rect) const {
  35. return position.is_equal_approx(p_rect.position) && size.is_equal_approx(p_rect.size);
  36. }
  37. bool Rect2::is_finite() const {
  38. return position.is_finite() && size.is_finite();
  39. }
  40. bool Rect2::intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2 *r_pos, Point2 *r_normal) const {
  41. #ifdef MATH_CHECKS
  42. if (unlikely(size.x < 0 || size.y < 0)) {
  43. ERR_PRINT("Rect2 size is negative, this is not supported. Use Rect2.abs() to get a Rect2 with a positive size.");
  44. }
  45. #endif
  46. real_t min = 0, max = 1;
  47. int axis = 0;
  48. real_t sign = 0;
  49. for (int i = 0; i < 2; i++) {
  50. real_t seg_from = p_from[i];
  51. real_t seg_to = p_to[i];
  52. real_t box_begin = position[i];
  53. real_t box_end = box_begin + size[i];
  54. real_t cmin, cmax;
  55. real_t csign;
  56. if (seg_from < seg_to) {
  57. if (seg_from > box_end || seg_to < box_begin) {
  58. return false;
  59. }
  60. real_t length = seg_to - seg_from;
  61. cmin = (seg_from < box_begin) ? ((box_begin - seg_from) / length) : 0;
  62. cmax = (seg_to > box_end) ? ((box_end - seg_from) / length) : 1;
  63. csign = -1.0;
  64. } else {
  65. if (seg_to > box_end || seg_from < box_begin) {
  66. return false;
  67. }
  68. real_t length = seg_to - seg_from;
  69. cmin = (seg_from > box_end) ? (box_end - seg_from) / length : 0;
  70. cmax = (seg_to < box_begin) ? (box_begin - seg_from) / length : 1;
  71. csign = 1.0;
  72. }
  73. if (cmin > min) {
  74. min = cmin;
  75. axis = i;
  76. sign = csign;
  77. }
  78. if (cmax < max) {
  79. max = cmax;
  80. }
  81. if (max < min) {
  82. return false;
  83. }
  84. }
  85. Vector2 rel = p_to - p_from;
  86. if (r_normal) {
  87. Vector2 normal;
  88. normal[axis] = sign;
  89. *r_normal = normal;
  90. }
  91. if (r_pos) {
  92. *r_pos = p_from + rel * min;
  93. }
  94. return true;
  95. }
  96. bool Rect2::intersects_transformed(const Transform2D &p_xform, const Rect2 &p_rect) const {
  97. #ifdef MATH_CHECKS
  98. if (unlikely(size.x < 0 || size.y < 0 || p_rect.size.x < 0 || p_rect.size.y < 0)) {
  99. ERR_PRINT("Rect2 size is negative, this is not supported. Use Rect2.abs() to get a Rect2 with a positive size.");
  100. }
  101. #endif
  102. //SAT intersection between local and transformed rect2
  103. Vector2 xf_points[4] = {
  104. p_xform.xform(p_rect.position),
  105. p_xform.xform(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y)),
  106. p_xform.xform(Vector2(p_rect.position.x, p_rect.position.y + p_rect.size.y)),
  107. p_xform.xform(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y + p_rect.size.y)),
  108. };
  109. real_t low_limit;
  110. //base rect2 first (faster)
  111. if (xf_points[0].y > position.y) {
  112. goto next1;
  113. }
  114. if (xf_points[1].y > position.y) {
  115. goto next1;
  116. }
  117. if (xf_points[2].y > position.y) {
  118. goto next1;
  119. }
  120. if (xf_points[3].y > position.y) {
  121. goto next1;
  122. }
  123. return false;
  124. next1:
  125. low_limit = position.y + size.y;
  126. if (xf_points[0].y < low_limit) {
  127. goto next2;
  128. }
  129. if (xf_points[1].y < low_limit) {
  130. goto next2;
  131. }
  132. if (xf_points[2].y < low_limit) {
  133. goto next2;
  134. }
  135. if (xf_points[3].y < low_limit) {
  136. goto next2;
  137. }
  138. return false;
  139. next2:
  140. if (xf_points[0].x > position.x) {
  141. goto next3;
  142. }
  143. if (xf_points[1].x > position.x) {
  144. goto next3;
  145. }
  146. if (xf_points[2].x > position.x) {
  147. goto next3;
  148. }
  149. if (xf_points[3].x > position.x) {
  150. goto next3;
  151. }
  152. return false;
  153. next3:
  154. low_limit = position.x + size.x;
  155. if (xf_points[0].x < low_limit) {
  156. goto next4;
  157. }
  158. if (xf_points[1].x < low_limit) {
  159. goto next4;
  160. }
  161. if (xf_points[2].x < low_limit) {
  162. goto next4;
  163. }
  164. if (xf_points[3].x < low_limit) {
  165. goto next4;
  166. }
  167. return false;
  168. next4:
  169. Vector2 xf_points2[4] = {
  170. position,
  171. Vector2(position.x + size.x, position.y),
  172. Vector2(position.x, position.y + size.y),
  173. Vector2(position.x + size.x, position.y + size.y),
  174. };
  175. real_t maxa = p_xform.columns[0].dot(xf_points2[0]);
  176. real_t mina = maxa;
  177. real_t dp = p_xform.columns[0].dot(xf_points2[1]);
  178. maxa = MAX(dp, maxa);
  179. mina = MIN(dp, mina);
  180. dp = p_xform.columns[0].dot(xf_points2[2]);
  181. maxa = MAX(dp, maxa);
  182. mina = MIN(dp, mina);
  183. dp = p_xform.columns[0].dot(xf_points2[3]);
  184. maxa = MAX(dp, maxa);
  185. mina = MIN(dp, mina);
  186. real_t maxb = p_xform.columns[0].dot(xf_points[0]);
  187. real_t minb = maxb;
  188. dp = p_xform.columns[0].dot(xf_points[1]);
  189. maxb = MAX(dp, maxb);
  190. minb = MIN(dp, minb);
  191. dp = p_xform.columns[0].dot(xf_points[2]);
  192. maxb = MAX(dp, maxb);
  193. minb = MIN(dp, minb);
  194. dp = p_xform.columns[0].dot(xf_points[3]);
  195. maxb = MAX(dp, maxb);
  196. minb = MIN(dp, minb);
  197. if (mina > maxb) {
  198. return false;
  199. }
  200. if (minb > maxa) {
  201. return false;
  202. }
  203. maxa = p_xform.columns[1].dot(xf_points2[0]);
  204. mina = maxa;
  205. dp = p_xform.columns[1].dot(xf_points2[1]);
  206. maxa = MAX(dp, maxa);
  207. mina = MIN(dp, mina);
  208. dp = p_xform.columns[1].dot(xf_points2[2]);
  209. maxa = MAX(dp, maxa);
  210. mina = MIN(dp, mina);
  211. dp = p_xform.columns[1].dot(xf_points2[3]);
  212. maxa = MAX(dp, maxa);
  213. mina = MIN(dp, mina);
  214. maxb = p_xform.columns[1].dot(xf_points[0]);
  215. minb = maxb;
  216. dp = p_xform.columns[1].dot(xf_points[1]);
  217. maxb = MAX(dp, maxb);
  218. minb = MIN(dp, minb);
  219. dp = p_xform.columns[1].dot(xf_points[2]);
  220. maxb = MAX(dp, maxb);
  221. minb = MIN(dp, minb);
  222. dp = p_xform.columns[1].dot(xf_points[3]);
  223. maxb = MAX(dp, maxb);
  224. minb = MIN(dp, minb);
  225. if (mina > maxb) {
  226. return false;
  227. }
  228. if (minb > maxa) {
  229. return false;
  230. }
  231. return true;
  232. }
  233. Rect2::operator String() const {
  234. return "[P: " + position.operator String() + ", S: " + size + "]";
  235. }
  236. Rect2::operator Rect2i() const {
  237. return Rect2i(position, size);
  238. }