rect2.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /**************************************************************************/
  2. /* rect2.h */
  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. #ifndef RECT2_H
  31. #define RECT2_H
  32. #include "core/error/error_macros.h"
  33. #include "core/math/vector2.h"
  34. class String;
  35. struct Rect2i;
  36. struct Transform2D;
  37. struct _NO_DISCARD_ Rect2 {
  38. Point2 position;
  39. Size2 size;
  40. const Vector2 &get_position() const { return position; }
  41. void set_position(const Vector2 &p_pos) { position = p_pos; }
  42. const Vector2 &get_size() const { return size; }
  43. void set_size(const Vector2 &p_size) { size = p_size; }
  44. real_t get_area() const { return size.width * size.height; }
  45. _FORCE_INLINE_ Vector2 get_center() const { return position + (size * 0.5f); }
  46. inline bool intersects(const Rect2 &p_rect, const bool p_include_borders = false) const {
  47. #ifdef MATH_CHECKS
  48. if (unlikely(size.x < 0 || size.y < 0 || p_rect.size.x < 0 || p_rect.size.y < 0)) {
  49. ERR_PRINT("Rect2 size is negative, this is not supported. Use Rect2.abs() to get a Rect2 with a positive size.");
  50. }
  51. #endif
  52. if (p_include_borders) {
  53. if (position.x > (p_rect.position.x + p_rect.size.width)) {
  54. return false;
  55. }
  56. if ((position.x + size.width) < p_rect.position.x) {
  57. return false;
  58. }
  59. if (position.y > (p_rect.position.y + p_rect.size.height)) {
  60. return false;
  61. }
  62. if ((position.y + size.height) < p_rect.position.y) {
  63. return false;
  64. }
  65. } else {
  66. if (position.x >= (p_rect.position.x + p_rect.size.width)) {
  67. return false;
  68. }
  69. if ((position.x + size.width) <= p_rect.position.x) {
  70. return false;
  71. }
  72. if (position.y >= (p_rect.position.y + p_rect.size.height)) {
  73. return false;
  74. }
  75. if ((position.y + size.height) <= p_rect.position.y) {
  76. return false;
  77. }
  78. }
  79. return true;
  80. }
  81. inline real_t distance_to(const Vector2 &p_point) const {
  82. #ifdef MATH_CHECKS
  83. if (unlikely(size.x < 0 || size.y < 0)) {
  84. ERR_PRINT("Rect2 size is negative, this is not supported. Use Rect2.abs() to get a Rect2 with a positive size.");
  85. }
  86. #endif
  87. real_t dist = 0.0;
  88. bool inside = true;
  89. if (p_point.x < position.x) {
  90. real_t d = position.x - p_point.x;
  91. dist = d;
  92. inside = false;
  93. }
  94. if (p_point.y < position.y) {
  95. real_t d = position.y - p_point.y;
  96. dist = inside ? d : MIN(dist, d);
  97. inside = false;
  98. }
  99. if (p_point.x >= (position.x + size.x)) {
  100. real_t d = p_point.x - (position.x + size.x);
  101. dist = inside ? d : MIN(dist, d);
  102. inside = false;
  103. }
  104. if (p_point.y >= (position.y + size.y)) {
  105. real_t d = p_point.y - (position.y + size.y);
  106. dist = inside ? d : MIN(dist, d);
  107. inside = false;
  108. }
  109. if (inside) {
  110. return 0;
  111. } else {
  112. return dist;
  113. }
  114. }
  115. bool intersects_transformed(const Transform2D &p_xform, const Rect2 &p_rect) const;
  116. bool intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2 *r_pos = nullptr, Point2 *r_normal = nullptr) const;
  117. inline bool encloses(const Rect2 &p_rect) const {
  118. #ifdef MATH_CHECKS
  119. if (unlikely(size.x < 0 || size.y < 0 || p_rect.size.x < 0 || p_rect.size.y < 0)) {
  120. ERR_PRINT("Rect2 size is negative, this is not supported. Use Rect2.abs() to get a Rect2 with a positive size.");
  121. }
  122. #endif
  123. return (p_rect.position.x >= position.x) && (p_rect.position.y >= position.y) &&
  124. ((p_rect.position.x + p_rect.size.x) <= (position.x + size.x)) &&
  125. ((p_rect.position.y + p_rect.size.y) <= (position.y + size.y));
  126. }
  127. _FORCE_INLINE_ bool has_area() const {
  128. return size.x > 0.0f && size.y > 0.0f;
  129. }
  130. // Returns the intersection between two Rect2s or an empty Rect2 if there is no intersection.
  131. inline Rect2 intersection(const Rect2 &p_rect) const {
  132. Rect2 new_rect = p_rect;
  133. if (!intersects(new_rect)) {
  134. return Rect2();
  135. }
  136. new_rect.position.x = MAX(p_rect.position.x, position.x);
  137. new_rect.position.y = MAX(p_rect.position.y, position.y);
  138. Point2 p_rect_end = p_rect.position + p_rect.size;
  139. Point2 end = position + size;
  140. new_rect.size.x = MIN(p_rect_end.x, end.x) - new_rect.position.x;
  141. new_rect.size.y = MIN(p_rect_end.y, end.y) - new_rect.position.y;
  142. return new_rect;
  143. }
  144. inline Rect2 merge(const Rect2 &p_rect) const { ///< return a merged rect
  145. #ifdef MATH_CHECKS
  146. if (unlikely(size.x < 0 || size.y < 0 || p_rect.size.x < 0 || p_rect.size.y < 0)) {
  147. ERR_PRINT("Rect2 size is negative, this is not supported. Use Rect2.abs() to get a Rect2 with a positive size.");
  148. }
  149. #endif
  150. Rect2 new_rect;
  151. new_rect.position.x = MIN(p_rect.position.x, position.x);
  152. new_rect.position.y = MIN(p_rect.position.y, position.y);
  153. new_rect.size.x = MAX(p_rect.position.x + p_rect.size.x, position.x + size.x);
  154. new_rect.size.y = MAX(p_rect.position.y + p_rect.size.y, position.y + size.y);
  155. new_rect.size = new_rect.size - new_rect.position; // Make relative again.
  156. return new_rect;
  157. }
  158. inline bool has_point(const Point2 &p_point) const {
  159. #ifdef MATH_CHECKS
  160. if (unlikely(size.x < 0 || size.y < 0)) {
  161. ERR_PRINT("Rect2 size is negative, this is not supported. Use Rect2.abs() to get a Rect2 with a positive size.");
  162. }
  163. #endif
  164. if (p_point.x < position.x) {
  165. return false;
  166. }
  167. if (p_point.y < position.y) {
  168. return false;
  169. }
  170. if (p_point.x >= (position.x + size.x)) {
  171. return false;
  172. }
  173. if (p_point.y >= (position.y + size.y)) {
  174. return false;
  175. }
  176. return true;
  177. }
  178. bool is_equal_approx(const Rect2 &p_rect) const;
  179. bool is_finite() const;
  180. bool operator==(const Rect2 &p_rect) const { return position == p_rect.position && size == p_rect.size; }
  181. bool operator!=(const Rect2 &p_rect) const { return position != p_rect.position || size != p_rect.size; }
  182. inline Rect2 grow(real_t p_amount) const {
  183. Rect2 g = *this;
  184. g.grow_by(p_amount);
  185. return g;
  186. }
  187. inline void grow_by(real_t p_amount) {
  188. position.x -= p_amount;
  189. position.y -= p_amount;
  190. size.width += p_amount * 2;
  191. size.height += p_amount * 2;
  192. }
  193. inline Rect2 grow_side(Side p_side, real_t p_amount) const {
  194. Rect2 g = *this;
  195. g = g.grow_individual((SIDE_LEFT == p_side) ? p_amount : 0,
  196. (SIDE_TOP == p_side) ? p_amount : 0,
  197. (SIDE_RIGHT == p_side) ? p_amount : 0,
  198. (SIDE_BOTTOM == p_side) ? p_amount : 0);
  199. return g;
  200. }
  201. inline Rect2 grow_side_bind(uint32_t p_side, real_t p_amount) const {
  202. return grow_side(Side(p_side), p_amount);
  203. }
  204. inline Rect2 grow_individual(real_t p_left, real_t p_top, real_t p_right, real_t p_bottom) const {
  205. Rect2 g = *this;
  206. g.position.x -= p_left;
  207. g.position.y -= p_top;
  208. g.size.width += p_left + p_right;
  209. g.size.height += p_top + p_bottom;
  210. return g;
  211. }
  212. _FORCE_INLINE_ Rect2 expand(const Vector2 &p_vector) const {
  213. Rect2 r = *this;
  214. r.expand_to(p_vector);
  215. return r;
  216. }
  217. inline void expand_to(const Vector2 &p_vector) { // In place function for speed.
  218. #ifdef MATH_CHECKS
  219. if (unlikely(size.x < 0 || size.y < 0)) {
  220. ERR_PRINT("Rect2 size is negative, this is not supported. Use Rect2.abs() to get a Rect2 with a positive size.");
  221. }
  222. #endif
  223. Vector2 begin = position;
  224. Vector2 end = position + size;
  225. if (p_vector.x < begin.x) {
  226. begin.x = p_vector.x;
  227. }
  228. if (p_vector.y < begin.y) {
  229. begin.y = p_vector.y;
  230. }
  231. if (p_vector.x > end.x) {
  232. end.x = p_vector.x;
  233. }
  234. if (p_vector.y > end.y) {
  235. end.y = p_vector.y;
  236. }
  237. position = begin;
  238. size = end - begin;
  239. }
  240. _FORCE_INLINE_ Rect2 abs() const {
  241. return Rect2(Point2(position.x + MIN(size.x, (real_t)0), position.y + MIN(size.y, (real_t)0)), size.abs());
  242. }
  243. Vector2 get_support(const Vector2 &p_normal) const {
  244. Vector2 half_extents = size * 0.5f;
  245. Vector2 ofs = position + half_extents;
  246. return Vector2(
  247. (p_normal.x > 0) ? -half_extents.x : half_extents.x,
  248. (p_normal.y > 0) ? -half_extents.y : half_extents.y) +
  249. ofs;
  250. }
  251. _FORCE_INLINE_ bool intersects_filled_polygon(const Vector2 *p_points, int p_point_count) const {
  252. Vector2 center = get_center();
  253. int side_plus = 0;
  254. int side_minus = 0;
  255. Vector2 end = position + size;
  256. int i_f = p_point_count - 1;
  257. for (int i = 0; i < p_point_count; i++) {
  258. const Vector2 &a = p_points[i_f];
  259. const Vector2 &b = p_points[i];
  260. i_f = i;
  261. Vector2 r = (b - a);
  262. float l = r.length();
  263. if (l == 0.0f) {
  264. continue;
  265. }
  266. // Check inside.
  267. Vector2 tg = r.orthogonal();
  268. float s = tg.dot(center) - tg.dot(a);
  269. if (s < 0.0f) {
  270. side_plus++;
  271. } else {
  272. side_minus++;
  273. }
  274. // Check ray box.
  275. r /= l;
  276. Vector2 ir(1.0f / r.x, 1.0f / r.y);
  277. // lb is the corner of AABB with minimal coordinates - left bottom, rt is maximal corner
  278. // r.org is origin of ray
  279. Vector2 t13 = (position - a) * ir;
  280. Vector2 t24 = (end - a) * ir;
  281. float tmin = MAX(MIN(t13.x, t24.x), MIN(t13.y, t24.y));
  282. float tmax = MIN(MAX(t13.x, t24.x), MAX(t13.y, t24.y));
  283. // if tmax < 0, ray (line) is intersecting AABB, but the whole AABB is behind us
  284. if (tmax < 0 || tmin > tmax || tmin >= l) {
  285. continue;
  286. }
  287. return true;
  288. }
  289. if (side_plus * side_minus == 0) {
  290. return true; // All inside.
  291. } else {
  292. return false;
  293. }
  294. }
  295. _FORCE_INLINE_ void set_end(const Vector2 &p_end) {
  296. size = p_end - position;
  297. }
  298. _FORCE_INLINE_ Vector2 get_end() const {
  299. return position + size;
  300. }
  301. operator String() const;
  302. operator Rect2i() const;
  303. Rect2() {}
  304. Rect2(real_t p_x, real_t p_y, real_t p_width, real_t p_height) :
  305. position(Point2(p_x, p_y)),
  306. size(Size2(p_width, p_height)) {
  307. }
  308. Rect2(const Point2 &p_pos, const Size2 &p_size) :
  309. position(p_pos),
  310. size(p_size) {
  311. }
  312. };
  313. #endif // RECT2_H