rect2.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*************************************************************************/
  2. /* rect2.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. #ifndef GODOT_RECT2_HPP
  31. #define GODOT_RECT2_HPP
  32. #include <godot_cpp/classes/global_constants.hpp>
  33. #include <godot_cpp/variant/vector2.hpp>
  34. namespace godot {
  35. class String;
  36. struct Rect2i;
  37. struct Transform2D;
  38. struct _NO_DISCARD_ Rect2 {
  39. Point2 position;
  40. Size2 size;
  41. const Vector2 &get_position() const { return position; }
  42. void set_position(const Vector2 &p_pos) { position = p_pos; }
  43. const Vector2 &get_size() const { return size; }
  44. void set_size(const Vector2 &p_size) { size = p_size; }
  45. real_t get_area() const { return size.width * size.height; }
  46. _FORCE_INLINE_ Vector2 get_center() const { return position + (size * 0.5f); }
  47. inline bool intersects(const Rect2 &p_rect, const bool p_include_borders = false) const {
  48. #ifdef MATH_CHECKS
  49. if (unlikely(size.x < 0 || size.y < 0 || p_rect.size.x < 0 || p_rect.size.y < 0)) {
  50. ERR_PRINT("Rect2 size is negative, this is not supported. Use Rect2.abs() to get a Rect2 with a positive size.");
  51. }
  52. #endif
  53. if (p_include_borders) {
  54. if (position.x > (p_rect.position.x + p_rect.size.width)) {
  55. return false;
  56. }
  57. if ((position.x + size.width) < p_rect.position.x) {
  58. return false;
  59. }
  60. if (position.y > (p_rect.position.y + p_rect.size.height)) {
  61. return false;
  62. }
  63. if ((position.y + size.height) < p_rect.position.y) {
  64. return false;
  65. }
  66. } else {
  67. if (position.x >= (p_rect.position.x + p_rect.size.width)) {
  68. return false;
  69. }
  70. if ((position.x + size.width) <= p_rect.position.x) {
  71. return false;
  72. }
  73. if (position.y >= (p_rect.position.y + p_rect.size.height)) {
  74. return false;
  75. }
  76. if ((position.y + size.height) <= p_rect.position.y) {
  77. return false;
  78. }
  79. }
  80. return true;
  81. }
  82. inline real_t distance_to(const Vector2 &p_point) const {
  83. #ifdef MATH_CHECKS
  84. if (unlikely(size.x < 0 || size.y < 0)) {
  85. ERR_PRINT("Rect2 size is negative, this is not supported. Use Rect2.abs() to get a Rect2 with a positive size.");
  86. }
  87. #endif
  88. real_t dist = 0.0;
  89. bool inside = true;
  90. if (p_point.x < position.x) {
  91. real_t d = position.x - p_point.x;
  92. dist = d;
  93. inside = false;
  94. }
  95. if (p_point.y < position.y) {
  96. real_t d = position.y - p_point.y;
  97. dist = inside ? d : Math::min(dist, d);
  98. inside = false;
  99. }
  100. if (p_point.x >= (position.x + size.x)) {
  101. real_t d = p_point.x - (position.x + size.x);
  102. dist = inside ? d : Math::min(dist, d);
  103. inside = false;
  104. }
  105. if (p_point.y >= (position.y + size.y)) {
  106. real_t d = p_point.y - (position.y + size.y);
  107. dist = inside ? d : Math::min(dist, d);
  108. inside = false;
  109. }
  110. if (inside) {
  111. return 0;
  112. } else {
  113. return dist;
  114. }
  115. }
  116. bool intersects_transformed(const Transform2D &p_xform, const Rect2 &p_rect) const;
  117. bool intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2 *r_pos = nullptr, Point2 *r_normal = nullptr) const;
  118. inline bool encloses(const Rect2 &p_rect) const {
  119. #ifdef MATH_CHECKS
  120. if (unlikely(size.x < 0 || size.y < 0 || p_rect.size.x < 0 || p_rect.size.y < 0)) {
  121. ERR_PRINT("Rect2 size is negative, this is not supported. Use Rect2.abs() to get a Rect2 with a positive size.");
  122. }
  123. #endif
  124. return (p_rect.position.x >= position.x) && (p_rect.position.y >= position.y) &&
  125. ((p_rect.position.x + p_rect.size.x) <= (position.x + size.x)) &&
  126. ((p_rect.position.y + p_rect.size.y) <= (position.y + size.y));
  127. }
  128. _FORCE_INLINE_ bool has_area() const {
  129. return size.x > 0.0f && size.y > 0.0f;
  130. }
  131. // Returns the instersection between two Rect2s or an empty Rect2 if there is no intersection
  132. inline Rect2 intersection(const Rect2 &p_rect) const {
  133. Rect2 new_rect = p_rect;
  134. if (!intersects(new_rect)) {
  135. return Rect2();
  136. }
  137. new_rect.position.x = Math::max(p_rect.position.x, position.x);
  138. new_rect.position.y = Math::max(p_rect.position.y, position.y);
  139. Point2 p_rect_end = p_rect.position + p_rect.size;
  140. Point2 end = position + size;
  141. new_rect.size.x = Math::min(p_rect_end.x, end.x) - new_rect.position.x;
  142. new_rect.size.y = Math::min(p_rect_end.y, end.y) - new_rect.position.y;
  143. return new_rect;
  144. }
  145. inline Rect2 merge(const Rect2 &p_rect) const { ///< return a merged rect
  146. #ifdef MATH_CHECKS
  147. if (unlikely(size.x < 0 || size.y < 0 || p_rect.size.x < 0 || p_rect.size.y < 0)) {
  148. ERR_PRINT("Rect2 size is negative, this is not supported. Use Rect2.abs() to get a Rect2 with a positive size.");
  149. }
  150. #endif
  151. Rect2 new_rect;
  152. new_rect.position.x = Math::min(p_rect.position.x, position.x);
  153. new_rect.position.y = Math::min(p_rect.position.y, position.y);
  154. new_rect.size.x = Math::max(p_rect.position.x + p_rect.size.x, position.x + size.x);
  155. new_rect.size.y = Math::max(p_rect.position.y + p_rect.size.y, position.y + size.y);
  156. new_rect.size = new_rect.size - new_rect.position; // Make relative again.
  157. return new_rect;
  158. }
  159. inline bool has_point(const Point2 &p_point) const {
  160. #ifdef MATH_CHECKS
  161. if (unlikely(size.x < 0 || size.y < 0)) {
  162. ERR_PRINT("Rect2 size is negative, this is not supported. Use Rect2.abs() to get a Rect2 with a positive size.");
  163. }
  164. #endif
  165. if (p_point.x < position.x) {
  166. return false;
  167. }
  168. if (p_point.y < position.y) {
  169. return false;
  170. }
  171. if (p_point.x >= (position.x + size.x)) {
  172. return false;
  173. }
  174. if (p_point.y >= (position.y + size.y)) {
  175. return false;
  176. }
  177. return true;
  178. }
  179. bool is_equal_approx(const Rect2 &p_rect) 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 + Math::min(size.x, (real_t)0), position.y + Math::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 = Math::max(Math::min(t13.x, t24.x), Math::min(t13.y, t24.y));
  282. float tmax = Math::min(Math::max(t13.x, t24.x), Math::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. } // namespace godot
  314. #endif // GODOT_RECT2_HPP