2
0

rect2.h 15 KB

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