Debian Codegraph @

core/math/rect2.h

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