rect2.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /*************************************************************************/
  2. /* rect2.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 RECT2_H
  31. #define RECT2_H
  32. #include "core/math/vector2.h" // also includes math_funcs and ustring
  33. struct Transform2D;
  34. struct Rect2 {
  35. Point2 position;
  36. Size2 size;
  37. const Vector2 &get_position() const { return position; }
  38. void set_position(const Vector2 &p_pos) { position = p_pos; }
  39. const Vector2 &get_size() const { return size; }
  40. void set_size(const Vector2 &p_size) { size = p_size; }
  41. real_t get_area() const { return size.width * size.height; }
  42. inline bool intersects(const Rect2 &p_rect, const bool p_include_borders = false) const {
  43. if (p_include_borders) {
  44. if (position.x > (p_rect.position.x + p_rect.size.width)) {
  45. return false;
  46. }
  47. if ((position.x + size.width) < p_rect.position.x) {
  48. return false;
  49. }
  50. if (position.y > (p_rect.position.y + p_rect.size.height)) {
  51. return false;
  52. }
  53. if ((position.y + size.height) < p_rect.position.y) {
  54. return false;
  55. }
  56. } else {
  57. if (position.x >= (p_rect.position.x + p_rect.size.width)) {
  58. return false;
  59. }
  60. if ((position.x + size.width) <= p_rect.position.x) {
  61. return false;
  62. }
  63. if (position.y >= (p_rect.position.y + p_rect.size.height)) {
  64. return false;
  65. }
  66. if ((position.y + size.height) <= p_rect.position.y) {
  67. return false;
  68. }
  69. }
  70. return true;
  71. }
  72. inline real_t distance_to(const Vector2 &p_point) const {
  73. real_t dist = 0.0;
  74. bool inside = true;
  75. if (p_point.x < position.x) {
  76. real_t d = position.x - p_point.x;
  77. dist = d;
  78. inside = false;
  79. }
  80. if (p_point.y < position.y) {
  81. real_t d = position.y - p_point.y;
  82. dist = inside ? d : MIN(dist, d);
  83. inside = false;
  84. }
  85. if (p_point.x >= (position.x + size.x)) {
  86. real_t d = p_point.x - (position.x + size.x);
  87. dist = inside ? d : MIN(dist, d);
  88. inside = false;
  89. }
  90. if (p_point.y >= (position.y + size.y)) {
  91. real_t d = p_point.y - (position.y + size.y);
  92. dist = inside ? d : MIN(dist, d);
  93. inside = false;
  94. }
  95. if (inside) {
  96. return 0;
  97. } else {
  98. return dist;
  99. }
  100. }
  101. bool intersects_transformed(const Transform2D &p_xform, const Rect2 &p_rect) const;
  102. bool intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2 *r_pos = nullptr, Point2 *r_normal = nullptr) const;
  103. inline bool encloses(const Rect2 &p_rect) const {
  104. return (p_rect.position.x >= position.x) && (p_rect.position.y >= position.y) &&
  105. ((p_rect.position.x + p_rect.size.x) <= (position.x + size.x)) &&
  106. ((p_rect.position.y + p_rect.size.y) <= (position.y + size.y));
  107. }
  108. _FORCE_INLINE_ bool has_no_area() const {
  109. return (size.x <= 0 || size.y <= 0);
  110. }
  111. // Returns the instersection between two Rect2s or an empty Rect2 if there is no intersection
  112. inline Rect2 intersection(const Rect2 &p_rect) const {
  113. Rect2 new_rect = p_rect;
  114. if (!intersects(new_rect)) {
  115. return Rect2();
  116. }
  117. new_rect.position.x = MAX(p_rect.position.x, position.x);
  118. new_rect.position.y = MAX(p_rect.position.y, position.y);
  119. Point2 p_rect_end = p_rect.position + p_rect.size;
  120. Point2 end = position + size;
  121. new_rect.size.x = MIN(p_rect_end.x, end.x) - new_rect.position.x;
  122. new_rect.size.y = MIN(p_rect_end.y, end.y) - new_rect.position.y;
  123. return new_rect;
  124. }
  125. inline Rect2 merge(const Rect2 &p_rect) const { ///< return a merged rect
  126. Rect2 new_rect;
  127. new_rect.position.x = MIN(p_rect.position.x, position.x);
  128. new_rect.position.y = MIN(p_rect.position.y, position.y);
  129. new_rect.size.x = MAX(p_rect.position.x + p_rect.size.x, position.x + size.x);
  130. new_rect.size.y = MAX(p_rect.position.y + p_rect.size.y, position.y + size.y);
  131. new_rect.size = new_rect.size - new_rect.position; //make relative again
  132. return new_rect;
  133. }
  134. inline bool has_point(const Point2 &p_point) const {
  135. if (p_point.x < position.x) {
  136. return false;
  137. }
  138. if (p_point.y < position.y) {
  139. return false;
  140. }
  141. if (p_point.x >= (position.x + size.x)) {
  142. return false;
  143. }
  144. if (p_point.y >= (position.y + size.y)) {
  145. return false;
  146. }
  147. return true;
  148. }
  149. bool is_equal_approx(const Rect2 &p_rect) const;
  150. bool operator==(const Rect2 &p_rect) const { return position == p_rect.position && size == p_rect.size; }
  151. bool operator!=(const Rect2 &p_rect) const { return position != p_rect.position || size != p_rect.size; }
  152. inline Rect2 grow(real_t p_amount) const {
  153. Rect2 g = *this;
  154. g.position.x -= p_amount;
  155. g.position.y -= p_amount;
  156. g.size.width += p_amount * 2;
  157. g.size.height += p_amount * 2;
  158. return g;
  159. }
  160. inline Rect2 grow_side(Side p_side, real_t p_amount) const {
  161. Rect2 g = *this;
  162. g = g.grow_individual((SIDE_LEFT == p_side) ? p_amount : 0,
  163. (SIDE_TOP == p_side) ? p_amount : 0,
  164. (SIDE_RIGHT == p_side) ? p_amount : 0,
  165. (SIDE_BOTTOM == p_side) ? p_amount : 0);
  166. return g;
  167. }
  168. inline Rect2 grow_side_bind(uint32_t p_side, real_t p_amount) const {
  169. return grow_side(Side(p_side), p_amount);
  170. }
  171. inline Rect2 grow_individual(real_t p_left, real_t p_top, real_t p_right, real_t p_bottom) const {
  172. Rect2 g = *this;
  173. g.position.x -= p_left;
  174. g.position.y -= p_top;
  175. g.size.width += p_left + p_right;
  176. g.size.height += p_top + p_bottom;
  177. return g;
  178. }
  179. _FORCE_INLINE_ Rect2 expand(const Vector2 &p_vector) const {
  180. Rect2 r = *this;
  181. r.expand_to(p_vector);
  182. return r;
  183. }
  184. inline void expand_to(const Vector2 &p_vector) { //in place function for speed
  185. Vector2 begin = position;
  186. Vector2 end = position + size;
  187. if (p_vector.x < begin.x) {
  188. begin.x = p_vector.x;
  189. }
  190. if (p_vector.y < begin.y) {
  191. begin.y = p_vector.y;
  192. }
  193. if (p_vector.x > end.x) {
  194. end.x = p_vector.x;
  195. }
  196. if (p_vector.y > end.y) {
  197. end.y = p_vector.y;
  198. }
  199. position = begin;
  200. size = end - begin;
  201. }
  202. _FORCE_INLINE_ Rect2 abs() const {
  203. return Rect2(Point2(position.x + MIN(size.x, 0), position.y + MIN(size.y, 0)), size.abs());
  204. }
  205. Vector2 get_support(const Vector2 &p_normal) const {
  206. Vector2 half_extents = size * 0.5;
  207. Vector2 ofs = position + half_extents;
  208. return Vector2(
  209. (p_normal.x > 0) ? -half_extents.x : half_extents.x,
  210. (p_normal.y > 0) ? -half_extents.y : half_extents.y) +
  211. ofs;
  212. }
  213. _FORCE_INLINE_ bool intersects_filled_polygon(const Vector2 *p_points, int p_point_count) const {
  214. Vector2 center = position + size * 0.5;
  215. int side_plus = 0;
  216. int side_minus = 0;
  217. Vector2 end = position + size;
  218. int i_f = p_point_count - 1;
  219. for (int i = 0; i < p_point_count; i++) {
  220. const Vector2 &a = p_points[i_f];
  221. const Vector2 &b = p_points[i];
  222. i_f = i;
  223. Vector2 r = (b - a);
  224. float l = r.length();
  225. if (l == 0.0) {
  226. continue;
  227. }
  228. //check inside
  229. Vector2 tg = r.orthogonal();
  230. float s = tg.dot(center) - tg.dot(a);
  231. if (s < 0.0) {
  232. side_plus++;
  233. } else {
  234. side_minus++;
  235. }
  236. //check ray box
  237. r /= l;
  238. Vector2 ir(1.0 / r.x, 1.0 / r.y);
  239. // lb is the corner of AABB with minimal coordinates - left bottom, rt is maximal corner
  240. // r.org is origin of ray
  241. Vector2 t13 = (position - a) * ir;
  242. Vector2 t24 = (end - a) * ir;
  243. float tmin = MAX(MIN(t13.x, t24.x), MIN(t13.y, t24.y));
  244. float tmax = MIN(MAX(t13.x, t24.x), MAX(t13.y, t24.y));
  245. // if tmax < 0, ray (line) is intersecting AABB, but the whole AABB is behind us
  246. if (tmax < 0 || tmin > tmax || tmin >= l) {
  247. continue;
  248. }
  249. return true;
  250. }
  251. if (side_plus * side_minus == 0) {
  252. return true; //all inside
  253. } else {
  254. return false;
  255. }
  256. }
  257. _FORCE_INLINE_ void set_end(const Vector2 &p_end) {
  258. size = p_end - position;
  259. }
  260. _FORCE_INLINE_ Vector2 get_end() const {
  261. return position + size;
  262. }
  263. operator String() const { return String(position) + ", " + String(size); }
  264. Rect2() {}
  265. Rect2(real_t p_x, real_t p_y, real_t p_width, real_t p_height) :
  266. position(Point2(p_x, p_y)),
  267. size(Size2(p_width, p_height)) {
  268. }
  269. Rect2(const Point2 &p_pos, const Size2 &p_size) :
  270. position(p_pos),
  271. size(p_size) {
  272. }
  273. };
  274. struct Rect2i {
  275. Point2i position;
  276. Size2i size;
  277. const Point2i &get_position() const { return position; }
  278. void set_position(const Point2i &p_position) { position = p_position; }
  279. const Size2i &get_size() const { return size; }
  280. void set_size(const Size2i &p_size) { size = p_size; }
  281. int get_area() const { return size.width * size.height; }
  282. inline bool intersects(const Rect2i &p_rect) const {
  283. if (position.x > (p_rect.position.x + p_rect.size.width)) {
  284. return false;
  285. }
  286. if ((position.x + size.width) < p_rect.position.x) {
  287. return false;
  288. }
  289. if (position.y > (p_rect.position.y + p_rect.size.height)) {
  290. return false;
  291. }
  292. if ((position.y + size.height) < p_rect.position.y) {
  293. return false;
  294. }
  295. return true;
  296. }
  297. inline bool encloses(const Rect2i &p_rect) const {
  298. return (p_rect.position.x >= position.x) && (p_rect.position.y >= position.y) &&
  299. ((p_rect.position.x + p_rect.size.x) < (position.x + size.x)) &&
  300. ((p_rect.position.y + p_rect.size.y) < (position.y + size.y));
  301. }
  302. _FORCE_INLINE_ bool has_no_area() const {
  303. return (size.x <= 0 || size.y <= 0);
  304. }
  305. // Returns the instersection between two Rect2is or an empty Rect2i if there is no intersection
  306. inline Rect2i intersection(const Rect2i &p_rect) const {
  307. Rect2i new_rect = p_rect;
  308. if (!intersects(new_rect)) {
  309. return Rect2i();
  310. }
  311. new_rect.position.x = MAX(p_rect.position.x, position.x);
  312. new_rect.position.y = MAX(p_rect.position.y, position.y);
  313. Point2i p_rect_end = p_rect.position + p_rect.size;
  314. Point2i end = position + size;
  315. new_rect.size.x = (int)(MIN(p_rect_end.x, end.x) - new_rect.position.x);
  316. new_rect.size.y = (int)(MIN(p_rect_end.y, end.y) - new_rect.position.y);
  317. return new_rect;
  318. }
  319. inline Rect2i merge(const Rect2i &p_rect) const { ///< return a merged rect
  320. Rect2i new_rect;
  321. new_rect.position.x = MIN(p_rect.position.x, position.x);
  322. new_rect.position.y = MIN(p_rect.position.y, position.y);
  323. new_rect.size.x = MAX(p_rect.position.x + p_rect.size.x, position.x + size.x);
  324. new_rect.size.y = MAX(p_rect.position.y + p_rect.size.y, position.y + size.y);
  325. new_rect.size = new_rect.size - new_rect.position; //make relative again
  326. return new_rect;
  327. }
  328. bool has_point(const Point2i &p_point) const {
  329. if (p_point.x < position.x) {
  330. return false;
  331. }
  332. if (p_point.y < position.y) {
  333. return false;
  334. }
  335. if (p_point.x >= (position.x + size.x)) {
  336. return false;
  337. }
  338. if (p_point.y >= (position.y + size.y)) {
  339. return false;
  340. }
  341. return true;
  342. }
  343. bool operator==(const Rect2i &p_rect) const { return position == p_rect.position && size == p_rect.size; }
  344. bool operator!=(const Rect2i &p_rect) const { return position != p_rect.position || size != p_rect.size; }
  345. Rect2i grow(int p_amount) const {
  346. Rect2i g = *this;
  347. g.position.x -= p_amount;
  348. g.position.y -= p_amount;
  349. g.size.width += p_amount * 2;
  350. g.size.height += p_amount * 2;
  351. return g;
  352. }
  353. inline Rect2i grow_side(Side p_side, int p_amount) const {
  354. Rect2i g = *this;
  355. g = g.grow_individual((SIDE_LEFT == p_side) ? p_amount : 0,
  356. (SIDE_TOP == p_side) ? p_amount : 0,
  357. (SIDE_RIGHT == p_side) ? p_amount : 0,
  358. (SIDE_BOTTOM == p_side) ? p_amount : 0);
  359. return g;
  360. }
  361. inline Rect2i grow_side_bind(uint32_t p_side, int p_amount) const {
  362. return grow_side(Side(p_side), p_amount);
  363. }
  364. inline Rect2i grow_individual(int p_left, int p_top, int p_right, int p_bottom) const {
  365. Rect2i g = *this;
  366. g.position.x -= p_left;
  367. g.position.y -= p_top;
  368. g.size.width += p_left + p_right;
  369. g.size.height += p_top + p_bottom;
  370. return g;
  371. }
  372. _FORCE_INLINE_ Rect2i expand(const Vector2i &p_vector) const {
  373. Rect2i r = *this;
  374. r.expand_to(p_vector);
  375. return r;
  376. }
  377. inline void expand_to(const Point2i &p_vector) {
  378. Point2i begin = position;
  379. Point2i end = position + size;
  380. if (p_vector.x < begin.x) {
  381. begin.x = p_vector.x;
  382. }
  383. if (p_vector.y < begin.y) {
  384. begin.y = p_vector.y;
  385. }
  386. if (p_vector.x > end.x) {
  387. end.x = p_vector.x;
  388. }
  389. if (p_vector.y > end.y) {
  390. end.y = p_vector.y;
  391. }
  392. position = begin;
  393. size = end - begin;
  394. }
  395. _FORCE_INLINE_ Rect2i abs() const {
  396. return Rect2i(Point2i(position.x + MIN(size.x, 0), position.y + MIN(size.y, 0)), size.abs());
  397. }
  398. _FORCE_INLINE_ void set_end(const Vector2i &p_end) {
  399. size = p_end - position;
  400. }
  401. _FORCE_INLINE_ Vector2i get_end() const {
  402. return position + size;
  403. }
  404. operator String() const { return String(position) + ", " + String(size); }
  405. operator Rect2() const { return Rect2(position, size); }
  406. Rect2i() {}
  407. Rect2i(const Rect2 &p_r2) :
  408. position(p_r2.position),
  409. size(p_r2.size) {
  410. }
  411. Rect2i(int p_x, int p_y, int p_width, int p_height) :
  412. position(Point2i(p_x, p_y)),
  413. size(Size2i(p_width, p_height)) {
  414. }
  415. Rect2i(const Point2i &p_pos, const Size2i &p_size) :
  416. position(p_pos),
  417. size(p_size) {
  418. }
  419. };
  420. #endif // RECT2_H