Debian Codegraph @

core/math/rect2.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* rect2.cpp */
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 #include "rect2.h"
32
33 #include "core/math/rect2i.h"
34 #include "core/math/transform_2d.h"
35 #include "core/string/ustring.h"
36
37 bool Rect2::is_equal_approx(const Rect2 &p_rect) const {
38 return position.is_equal_approx(p_rect.position) && size.is_equal_approx(p_rect.size);
39 }
40
41 bool Rect2::is_same(const Rect2 &p_rect) const {
42 return position.is_same(p_rect.position) && size.is_same(p_rect.size);
43 }
44
45 bool Rect2::is_finite() const {
46 return position.is_finite() && size.is_finite();
47 }
48
49 bool Rect2::intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2 *r_pos, Point2 *r_normal) const {
50 #ifdef MATH_CHECKS
51 if (unlikely(size.x < 0 || size.y < 0)) {
52 ERR_PRINT("Rect2 size is negative, this is not supported. Use Rect2.abs() to get a Rect2 with a positive size.");
53 }
54 #endif
55 real_t min = 0, max = 1;
56 int axis = 0;
57 real_t sign = 0;
58
59 for (int i = 0; i < 2; i++) {
60 real_t seg_from = p_from[i];
61 real_t seg_to = p_to[i];
62 real_t box_begin = position[i];
63 real_t box_end = box_begin + size[i];
64 real_t cmin, cmax;
65 real_t csign;
66
67 if (seg_from < seg_to) {
68 if (seg_from > box_end || seg_to < box_begin) {
69 return false;
70 }
71 real_t length = seg_to - seg_from;
72 cmin = (seg_from < box_begin) ? ((box_begin - seg_from) / length) : 0;
73 cmax = (seg_to > box_end) ? ((box_end - seg_from) / length) : 1;
74 csign = -1.0;
75
76 } else {
77 if (seg_to > box_end || seg_from < box_begin) {
78 return false;
79 }
80 real_t length = seg_to - seg_from;
81 cmin = (seg_from > box_end) ? (box_end - seg_from) / length : 0;
82 cmax = (seg_to < box_begin) ? (box_begin - seg_from) / length : 1;
83 csign = 1.0;
84 }
85
86 if (cmin > min) {
87 min = cmin;
88 axis = i;
89 sign = csign;
90 }
91 if (cmax < max) {
92 max = cmax;
93 }
94 if (max < min) {
95 return false;
96 }
97 }
98
99 Vector2 rel = p_to - p_from;
100
101 if (r_normal) {
102 Vector2 normal;
103 normal[axis] = sign;
104 *r_normal = normal;
105 }
106
107 if (r_pos) {
108 *r_pos = p_from + rel * min;
109 }
110
111 return true;
112 }
113
114 bool Rect2::intersects_transformed(const Transform2D &p_xform, const Rect2 &p_rect) const {
115 #ifdef MATH_CHECKS
116 if (unlikely(size.x < 0 || size.y < 0 || p_rect.size.x < 0 || p_rect.size.y < 0)) {
117 ERR_PRINT("Rect2 size is negative, this is not supported. Use Rect2.abs() to get a Rect2 with a positive size.");
118 }
119 #endif
120 //SAT intersection between local and transformed rect2
121
122 Vector2 xf_points[4] = {
123 p_xform.xform(p_rect.position),
124 p_xform.xform(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y)),
125 p_xform.xform(Vector2(p_rect.position.x, p_rect.position.y + p_rect.size.y)),
126 p_xform.xform(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y + p_rect.size.y)),
127 };
128
129 real_t low_limit;
130
131 //base rect2 first (faster)
132
133 if (xf_points[0].y > position.y) {
134 goto next1;
135 }
136 if (xf_points[1].y > position.y) {
137 goto next1;
138 }
139 if (xf_points[2].y > position.y) {
140 goto next1;
141 }
142 if (xf_points[3].y > position.y) {
143 goto next1;
144 }
145
146 return false;
147
148 next1:
149
150 low_limit = position.y + size.y;
151
152 if (xf_points[0].y < low_limit) {
153 goto next2;
154 }
155 if (xf_points[1].y < low_limit) {
156 goto next2;
157 }
158 if (xf_points[2].y < low_limit) {
159 goto next2;
160 }
161 if (xf_points[3].y < low_limit) {
162 goto next2;
163 }
164
165 return false;
166
167 next2:
168
169 if (xf_points[0].x > position.x) {
170 goto next3;
171 }
172 if (xf_points[1].x > position.x) {
173 goto next3;
174 }
175 if (xf_points[2].x > position.x) {
176 goto next3;
177 }
178 if (xf_points[3].x > position.x) {
179 goto next3;
180 }
181
182 return false;
183
184 next3:
185
186 low_limit = position.x + size.x;
187
188 if (xf_points[0].x < low_limit) {
189 goto next4;
190 }
191 if (xf_points[1].x < low_limit) {
192 goto next4;
193 }
194 if (xf_points[2].x < low_limit) {
195 goto next4;
196 }
197 if (xf_points[3].x < low_limit) {
198 goto next4;
199 }
200
201 return false;
202
203 next4:
204
205 Vector2 xf_points2[4] = {
206 position,
207 Vector2(position.x + size.x, position.y),
208 Vector2(position.x, position.y + size.y),
209 Vector2(position.x + size.x, position.y + size.y),
210 };
211
212 real_t maxa = p_xform.columns[0].dot(xf_points2[0]);
213 real_t mina = maxa;
214
215 real_t dp = p_xform.columns[0].dot(xf_points2[1]);
216 maxa = MAX(dp, maxa);
217 mina = MIN(dp, mina);
218
219 dp = p_xform.columns[0].dot(xf_points2[2]);
220 maxa = MAX(dp, maxa);
221 mina = MIN(dp, mina);
222
223 dp = p_xform.columns[0].dot(xf_points2[3]);
224 maxa = MAX(dp, maxa);
225 mina = MIN(dp, mina);
226
227 real_t maxb = p_xform.columns[0].dot(xf_points[0]);
228 real_t minb = maxb;
229
230 dp = p_xform.columns[0].dot(xf_points[1]);
231 maxb = MAX(dp, maxb);
232 minb = MIN(dp, minb);
233
234 dp = p_xform.columns[0].dot(xf_points[2]);
235 maxb = MAX(dp, maxb);
236 minb = MIN(dp, minb);
237
238 dp = p_xform.columns[0].dot(xf_points[3]);
239 maxb = MAX(dp, maxb);
240 minb = MIN(dp, minb);
241
242 if (mina > maxb) {
243 return false;
244 }
245 if (minb > maxa) {
246 return false;
247 }
248
249 maxa = p_xform.columns[1].dot(xf_points2[0]);
250 mina = maxa;
251
252 dp = p_xform.columns[1].dot(xf_points2[1]);
253 maxa = MAX(dp, maxa);
254 mina = MIN(dp, mina);
255
256 dp = p_xform.columns[1].dot(xf_points2[2]);
257 maxa = MAX(dp, maxa);
258 mina = MIN(dp, mina);
259
260 dp = p_xform.columns[1].dot(xf_points2[3]);
261 maxa = MAX(dp, maxa);
262 mina = MIN(dp, mina);
263
264 maxb = p_xform.columns[1].dot(xf_points[0]);
265 minb = maxb;
266
267 dp = p_xform.columns[1].dot(xf_points[1]);
268 maxb = MAX(dp, maxb);
269 minb = MIN(dp, minb);
270
271 dp = p_xform.columns[1].dot(xf_points[2]);
272 maxb = MAX(dp, maxb);
273 minb = MIN(dp, minb);
274
275 dp = p_xform.columns[1].dot(xf_points[3]);
276 maxb = MAX(dp, maxb);
277 minb = MIN(dp, minb);
278
279 if (mina > maxb) {
280 return false;
281 }
282 if (minb > maxa) {
283 return false;
284 }
285
286 return true;
287 }
288
289 Rect2::operator String() const {
290 return "[P: " + position.operator String() + ", S: " + size.operator String() + "]";
291 }
292
293 Rect2::operator Rect2i() const {
294 return Rect2i(position, size);
295 }