rect2.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /**************************************************************************/
  2. /* rect2.hpp */
  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 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 intersection 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 = p_rect.position.max(position);
  138. Point2 p_rect_end = p_rect.position + p_rect.size;
  139. Point2 end = position + size;
  140. new_rect.size = p_rect_end.min(end) - new_rect.position;
  141. return new_rect;
  142. }
  143. inline Rect2 merge(const Rect2 &p_rect) const { ///< return a merged rect
  144. #ifdef MATH_CHECKS
  145. if (unlikely(size.x < 0 || size.y < 0 || p_rect.size.x < 0 || p_rect.size.y < 0)) {
  146. ERR_PRINT("Rect2 size is negative, this is not supported. Use Rect2.abs() to get a Rect2 with a positive size.");
  147. }
  148. #endif
  149. Rect2 new_rect;
  150. new_rect.position = p_rect.position.min(position);
  151. new_rect.size = (p_rect.position + p_rect.size).max(position + size);
  152. new_rect.size = new_rect.size - new_rect.position; // Make relative again.
  153. return new_rect;
  154. }
  155. inline bool has_point(const Point2 &p_point) const {
  156. #ifdef MATH_CHECKS
  157. if (unlikely(size.x < 0 || size.y < 0)) {
  158. ERR_PRINT("Rect2 size is negative, this is not supported. Use Rect2.abs() to get a Rect2 with a positive size.");
  159. }
  160. #endif
  161. if (p_point.x < position.x) {
  162. return false;
  163. }
  164. if (p_point.y < position.y) {
  165. return false;
  166. }
  167. if (p_point.x >= (position.x + size.x)) {
  168. return false;
  169. }
  170. if (p_point.y >= (position.y + size.y)) {
  171. return false;
  172. }
  173. return true;
  174. }
  175. bool is_equal_approx(const Rect2 &p_rect) const;
  176. bool is_finite() const;
  177. bool operator==(const Rect2 &p_rect) const { return position == p_rect.position && size == p_rect.size; }
  178. bool operator!=(const Rect2 &p_rect) const { return position != p_rect.position || size != p_rect.size; }
  179. inline Rect2 grow(real_t p_amount) const {
  180. Rect2 g = *this;
  181. g.grow_by(p_amount);
  182. return g;
  183. }
  184. inline void grow_by(real_t p_amount) {
  185. position.x -= p_amount;
  186. position.y -= p_amount;
  187. size.width += p_amount * 2;
  188. size.height += p_amount * 2;
  189. }
  190. inline Rect2 grow_side(Side p_side, real_t p_amount) const {
  191. Rect2 g = *this;
  192. g = g.grow_individual((SIDE_LEFT == p_side) ? p_amount : 0,
  193. (SIDE_TOP == p_side) ? p_amount : 0,
  194. (SIDE_RIGHT == p_side) ? p_amount : 0,
  195. (SIDE_BOTTOM == p_side) ? p_amount : 0);
  196. return g;
  197. }
  198. inline Rect2 grow_side_bind(uint32_t p_side, real_t p_amount) const {
  199. return grow_side(Side(p_side), p_amount);
  200. }
  201. inline Rect2 grow_individual(real_t p_left, real_t p_top, real_t p_right, real_t p_bottom) const {
  202. Rect2 g = *this;
  203. g.position.x -= p_left;
  204. g.position.y -= p_top;
  205. g.size.width += p_left + p_right;
  206. g.size.height += p_top + p_bottom;
  207. return g;
  208. }
  209. _FORCE_INLINE_ Rect2 expand(const Vector2 &p_vector) const {
  210. Rect2 r = *this;
  211. r.expand_to(p_vector);
  212. return r;
  213. }
  214. inline void expand_to(const Vector2 &p_vector) { // In place function for speed.
  215. #ifdef MATH_CHECKS
  216. if (unlikely(size.x < 0 || size.y < 0)) {
  217. ERR_PRINT("Rect2 size is negative, this is not supported. Use Rect2.abs() to get a Rect2 with a positive size.");
  218. }
  219. #endif
  220. Vector2 begin = position;
  221. Vector2 end = position + size;
  222. if (p_vector.x < begin.x) {
  223. begin.x = p_vector.x;
  224. }
  225. if (p_vector.y < begin.y) {
  226. begin.y = p_vector.y;
  227. }
  228. if (p_vector.x > end.x) {
  229. end.x = p_vector.x;
  230. }
  231. if (p_vector.y > end.y) {
  232. end.y = p_vector.y;
  233. }
  234. position = begin;
  235. size = end - begin;
  236. }
  237. _FORCE_INLINE_ Rect2 abs() const {
  238. return Rect2(position + size.minf(0), size.abs());
  239. }
  240. Vector2 get_support(const Vector2 &p_normal) const {
  241. Vector2 half_extents = size * 0.5f;
  242. Vector2 ofs = position + half_extents;
  243. return Vector2(
  244. (p_normal.x > 0) ? -half_extents.x : half_extents.x,
  245. (p_normal.y > 0) ? -half_extents.y : half_extents.y) +
  246. ofs;
  247. }
  248. _FORCE_INLINE_ bool intersects_filled_polygon(const Vector2 *p_points, int p_point_count) const {
  249. Vector2 center = get_center();
  250. int side_plus = 0;
  251. int side_minus = 0;
  252. Vector2 end = position + size;
  253. int i_f = p_point_count - 1;
  254. for (int i = 0; i < p_point_count; i++) {
  255. const Vector2 &a = p_points[i_f];
  256. const Vector2 &b = p_points[i];
  257. i_f = i;
  258. Vector2 r = (b - a);
  259. float l = r.length();
  260. if (l == 0.0f) {
  261. continue;
  262. }
  263. // Check inside.
  264. Vector2 tg = r.orthogonal();
  265. float s = tg.dot(center) - tg.dot(a);
  266. if (s < 0.0f) {
  267. side_plus++;
  268. } else {
  269. side_minus++;
  270. }
  271. // Check ray box.
  272. r /= l;
  273. Vector2 ir(1.0f / r.x, 1.0f / r.y);
  274. // lb is the corner of AABB with minimal coordinates - left bottom, rt is maximal corner
  275. // r.org is origin of ray
  276. Vector2 t13 = (position - a) * ir;
  277. Vector2 t24 = (end - a) * ir;
  278. float tmin = Math::max(Math::min(t13.x, t24.x), Math::min(t13.y, t24.y));
  279. float tmax = Math::min(Math::max(t13.x, t24.x), Math::max(t13.y, t24.y));
  280. // if tmax < 0, ray (line) is intersecting AABB, but the whole AABB is behind us
  281. if (tmax < 0 || tmin > tmax || tmin >= l) {
  282. continue;
  283. }
  284. return true;
  285. }
  286. if (side_plus * side_minus == 0) {
  287. return true; // All inside.
  288. } else {
  289. return false;
  290. }
  291. }
  292. _FORCE_INLINE_ void set_end(const Vector2 &p_end) {
  293. size = p_end - position;
  294. }
  295. _FORCE_INLINE_ Vector2 get_end() const {
  296. return position + size;
  297. }
  298. operator String() const;
  299. operator Rect2i() const;
  300. Rect2() {}
  301. Rect2(real_t p_x, real_t p_y, real_t p_width, real_t p_height) :
  302. position(Point2(p_x, p_y)),
  303. size(Size2(p_width, p_height)) {
  304. }
  305. Rect2(const Point2 &p_pos, const Size2 &p_size) :
  306. position(p_pos),
  307. size(p_size) {
  308. }
  309. };
  310. } // namespace godot
  311. #endif // GODOT_RECT2_HPP