1 /*************************************************************************/
2 /*  rect2.cpp                                                            */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 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 
31 #include "core/math/transform_2d.h" // Includes rect2.h but Rect2 needs Transform2D
32 
is_equal_approx(const Rect2 & p_rect) const33 bool Rect2::is_equal_approx(const Rect2 &p_rect) const {
34 
35 	return position.is_equal_approx(p_rect.position) && size.is_equal_approx(p_rect.size);
36 }
37 
intersects_segment(const Point2 & p_from,const Point2 & p_to,Point2 * r_pos,Point2 * r_normal) const38 bool Rect2::intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2 *r_pos, Point2 *r_normal) const {
39 
40 	real_t min = 0, max = 1;
41 	int axis = 0;
42 	real_t sign = 0;
43 
44 	for (int i = 0; i < 2; i++) {
45 		real_t seg_from = p_from[i];
46 		real_t seg_to = p_to[i];
47 		real_t box_begin = position[i];
48 		real_t box_end = box_begin + size[i];
49 		real_t cmin, cmax;
50 		real_t csign;
51 
52 		if (seg_from < seg_to) {
53 
54 			if (seg_from > box_end || seg_to < box_begin)
55 				return false;
56 			real_t length = seg_to - seg_from;
57 			cmin = (seg_from < box_begin) ? ((box_begin - seg_from) / length) : 0;
58 			cmax = (seg_to > box_end) ? ((box_end - seg_from) / length) : 1;
59 			csign = -1.0;
60 
61 		} else {
62 
63 			if (seg_to > box_end || seg_from < box_begin)
64 				return false;
65 			real_t length = seg_to - seg_from;
66 			cmin = (seg_from > box_end) ? (box_end - seg_from) / length : 0;
67 			cmax = (seg_to < box_begin) ? (box_begin - seg_from) / length : 1;
68 			csign = 1.0;
69 		}
70 
71 		if (cmin > min) {
72 			min = cmin;
73 			axis = i;
74 			sign = csign;
75 		}
76 		if (cmax < max)
77 			max = cmax;
78 		if (max < min)
79 			return false;
80 	}
81 
82 	Vector2 rel = p_to - p_from;
83 
84 	if (r_normal) {
85 		Vector2 normal;
86 		normal[axis] = sign;
87 		*r_normal = normal;
88 	}
89 
90 	if (r_pos)
91 		*r_pos = p_from + rel * min;
92 
93 	return true;
94 }
95 
intersects_transformed(const Transform2D & p_xform,const Rect2 & p_rect) const96 bool Rect2::intersects_transformed(const Transform2D &p_xform, const Rect2 &p_rect) const {
97 
98 	//SAT intersection between local and transformed rect2
99 
100 	Vector2 xf_points[4] = {
101 		p_xform.xform(p_rect.position),
102 		p_xform.xform(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y)),
103 		p_xform.xform(Vector2(p_rect.position.x, p_rect.position.y + p_rect.size.y)),
104 		p_xform.xform(Vector2(p_rect.position.x + p_rect.size.x, p_rect.position.y + p_rect.size.y)),
105 	};
106 
107 	real_t low_limit;
108 
109 	//base rect2 first (faster)
110 
111 	if (xf_points[0].y > position.y)
112 		goto next1;
113 	if (xf_points[1].y > position.y)
114 		goto next1;
115 	if (xf_points[2].y > position.y)
116 		goto next1;
117 	if (xf_points[3].y > position.y)
118 		goto next1;
119 
120 	return false;
121 
122 next1:
123 
124 	low_limit = position.y + size.y;
125 
126 	if (xf_points[0].y < low_limit)
127 		goto next2;
128 	if (xf_points[1].y < low_limit)
129 		goto next2;
130 	if (xf_points[2].y < low_limit)
131 		goto next2;
132 	if (xf_points[3].y < low_limit)
133 		goto next2;
134 
135 	return false;
136 
137 next2:
138 
139 	if (xf_points[0].x > position.x)
140 		goto next3;
141 	if (xf_points[1].x > position.x)
142 		goto next3;
143 	if (xf_points[2].x > position.x)
144 		goto next3;
145 	if (xf_points[3].x > position.x)
146 		goto next3;
147 
148 	return false;
149 
150 next3:
151 
152 	low_limit = position.x + size.x;
153 
154 	if (xf_points[0].x < low_limit)
155 		goto next4;
156 	if (xf_points[1].x < low_limit)
157 		goto next4;
158 	if (xf_points[2].x < low_limit)
159 		goto next4;
160 	if (xf_points[3].x < low_limit)
161 		goto next4;
162 
163 	return false;
164 
165 next4:
166 
167 	Vector2 xf_points2[4] = {
168 		position,
169 		Vector2(position.x + size.x, position.y),
170 		Vector2(position.x, position.y + size.y),
171 		Vector2(position.x + size.x, position.y + size.y),
172 	};
173 
174 	real_t maxa = p_xform.elements[0].dot(xf_points2[0]);
175 	real_t mina = maxa;
176 
177 	real_t dp = p_xform.elements[0].dot(xf_points2[1]);
178 	maxa = MAX(dp, maxa);
179 	mina = MIN(dp, mina);
180 
181 	dp = p_xform.elements[0].dot(xf_points2[2]);
182 	maxa = MAX(dp, maxa);
183 	mina = MIN(dp, mina);
184 
185 	dp = p_xform.elements[0].dot(xf_points2[3]);
186 	maxa = MAX(dp, maxa);
187 	mina = MIN(dp, mina);
188 
189 	real_t maxb = p_xform.elements[0].dot(xf_points[0]);
190 	real_t minb = maxb;
191 
192 	dp = p_xform.elements[0].dot(xf_points[1]);
193 	maxb = MAX(dp, maxb);
194 	minb = MIN(dp, minb);
195 
196 	dp = p_xform.elements[0].dot(xf_points[2]);
197 	maxb = MAX(dp, maxb);
198 	minb = MIN(dp, minb);
199 
200 	dp = p_xform.elements[0].dot(xf_points[3]);
201 	maxb = MAX(dp, maxb);
202 	minb = MIN(dp, minb);
203 
204 	if (mina > maxb)
205 		return false;
206 	if (minb > maxa)
207 		return false;
208 
209 	maxa = p_xform.elements[1].dot(xf_points2[0]);
210 	mina = maxa;
211 
212 	dp = p_xform.elements[1].dot(xf_points2[1]);
213 	maxa = MAX(dp, maxa);
214 	mina = MIN(dp, mina);
215 
216 	dp = p_xform.elements[1].dot(xf_points2[2]);
217 	maxa = MAX(dp, maxa);
218 	mina = MIN(dp, mina);
219 
220 	dp = p_xform.elements[1].dot(xf_points2[3]);
221 	maxa = MAX(dp, maxa);
222 	mina = MIN(dp, mina);
223 
224 	maxb = p_xform.elements[1].dot(xf_points[0]);
225 	minb = maxb;
226 
227 	dp = p_xform.elements[1].dot(xf_points[1]);
228 	maxb = MAX(dp, maxb);
229 	minb = MIN(dp, minb);
230 
231 	dp = p_xform.elements[1].dot(xf_points[2]);
232 	maxb = MAX(dp, maxb);
233 	minb = MIN(dp, minb);
234 
235 	dp = p_xform.elements[1].dot(xf_points[3]);
236 	maxb = MAX(dp, maxb);
237 	minb = MIN(dp, minb);
238 
239 	if (mina > maxb)
240 		return false;
241 	if (minb > maxa)
242 		return false;
243 
244 	return true;
245 }
246