Line data Source code
1 : /* Copyright (C) 2025 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
4 : * 0 A.D. is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 2 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * 0 A.D. is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "precompiled.h"
19 :
20 : #include "Geometry.h"
21 :
22 : #include <algorithm>
23 :
24 : namespace Geometry
25 : {
26 :
27 : // TODO: all of these things could be optimised quite easily
28 :
29 30 : CFixedVector2D GetHalfBoundingBox(const CFixedVector2D& u, const CFixedVector2D& v, const CFixedVector2D& halfSize)
30 : {
31 30 : return CFixedVector2D(
32 30 : u.X.Multiply(halfSize.X).Absolute() + v.X.Multiply(halfSize.Y).Absolute(),
33 30 : u.Y.Multiply(halfSize.X).Absolute() + v.Y.Multiply(halfSize.Y).Absolute()
34 30 : );
35 : }
36 :
37 174328 : fixed DistanceToSquare(const CFixedVector2D& point, const CFixedVector2D& u, const CFixedVector2D& v, const CFixedVector2D& halfSize, bool countInsideAsZero)
38 : {
39 : /*
40 : * Relative to its own coordinate system, we have a square like:
41 : *
42 : * A : B : C
43 : * : :
44 : * - - ########### - -
45 : * # #
46 : * # I #
47 : * D # 0 # E v
48 : * # # ^
49 : * # # |
50 : * - - ########### - - -->u
51 : * : :
52 : * F : G : H
53 : *
54 : * where 0 is the center, u and v are unit axes,
55 : * and the square is hw*2 by hh*2 units in size.
56 : *
57 : * Points in the BIG regions should check distance to horizontal edges.
58 : * Points in the DIE regions should check distance to vertical edges.
59 : * Points in the ACFH regions should check distance to the corresponding corner.
60 : *
61 : * So we just need to check all of the regions to work out which calculations to apply.
62 : *
63 : */
64 :
65 : // By symmetry (taking absolute values), we work only in the 0-B-C-E quadrant
66 : // du, dv are the location of the point in the square's coordinate system
67 174328 : fixed du = point.Dot(u).Absolute();
68 174328 : fixed dv = point.Dot(v).Absolute();
69 :
70 174328 : fixed hw = halfSize.X;
71 174328 : fixed hh = halfSize.Y;
72 :
73 174328 : if (du < hw) // regions B, I, G
74 : {
75 72799 : if (dv < hh) // region I
76 314 : return countInsideAsZero ? fixed::Zero() : std::min(hw - du, hh - dv);
77 : else
78 72642 : return dv - hh;
79 : }
80 101529 : else if (dv < hh) // regions D, E
81 : {
82 92 : return du - hw; // vertical edges
83 : }
84 : else // regions A, C, F, H
85 : {
86 101437 : CFixedVector2D distance(du - hw, dv - hh);
87 101437 : return distance.Length();
88 : }
89 : }
90 :
91 : // Same as above except it does not use Length
92 : // For explanations refer to DistanceToSquare
93 0 : fixed DistanceToSquareSquared(const CFixedVector2D& point, const CFixedVector2D& u, const CFixedVector2D& v, const CFixedVector2D& halfSize, bool countInsideAsZero)
94 : {
95 0 : fixed du = point.Dot(u).Absolute();
96 0 : fixed dv = point.Dot(v).Absolute();
97 :
98 0 : fixed hw = halfSize.X;
99 0 : fixed hh = halfSize.Y;
100 :
101 0 : if (du < hw) // regions B, I, G
102 : {
103 0 : if (dv < hh) // region I
104 0 : return countInsideAsZero ? fixed::Zero() : std::min((hw - du).Square(), (hh - dv).Square());
105 : else
106 0 : return (dv - hh).Square(); // horizontal edges
107 : }
108 0 : else if (dv < hh) // regions D, E
109 : {
110 0 : return (du - hw).Square(); // vertical edges
111 : }
112 : else // regions A, C, F, H
113 : {
114 0 : return (du - hw).Square() + (dv - hh).Square();
115 : }
116 : }
117 :
118 344 : CFixedVector2D NearestPointOnSquare(const CFixedVector2D& point, const CFixedVector2D& u, const CFixedVector2D& v, const CFixedVector2D& halfSize)
119 : {
120 : /*
121 : * Relative to its own coordinate system, we have a square like:
122 : *
123 : * A : : C
124 : * : :
125 : * - - #### B #### - -
126 : * #\ /#
127 : * # \ / #
128 : * D --0-- E v
129 : * # / \ # ^
130 : * #/ \# |
131 : * - - #### G #### - - -->u
132 : * : :
133 : * F : : H
134 : *
135 : * where 0 is the center, u and v are unit axes,
136 : * and the square is hw*2 by hh*2 units in size.
137 : *
138 : * Points in the BDEG regions are nearest to the corresponding edge.
139 : * Points in the ACFH regions are nearest to the corresponding corner.
140 : *
141 : * So we just need to check all of the regions to work out which calculations to apply.
142 : *
143 : */
144 :
145 : // du, dv are the location of the point in the square's coordinate system
146 344 : fixed du = point.Dot(u);
147 344 : fixed dv = point.Dot(v);
148 :
149 344 : fixed hw = halfSize.X;
150 344 : fixed hh = halfSize.Y;
151 :
152 344 : if (-hw < du && du < hw) // regions B, G; or regions D, E inside the square
153 : {
154 343 : if (-hh < dv && dv < hh && (du.Absolute() - hw).Absolute() < (dv.Absolute() - hh).Absolute()) // regions D, E
155 : {
156 0 : if (du >= fixed::Zero()) // E
157 0 : return u.Multiply(hw) + v.Multiply(dv);
158 : else // D
159 0 : return -u.Multiply(hw) + v.Multiply(dv);
160 : }
161 : else // B, G
162 : {
163 343 : if (dv >= fixed::Zero()) // B
164 0 : return v.Multiply(hh) + u.Multiply(du);
165 : else // G
166 343 : return -v.Multiply(hh) + u.Multiply(du);
167 : }
168 : }
169 1 : else if (-hh < dv && dv < hh) // regions D, E outside the square
170 : {
171 0 : if (du >= fixed::Zero()) // E
172 0 : return u.Multiply(hw) + v.Multiply(dv);
173 : else // D
174 0 : return -u.Multiply(hw) + v.Multiply(dv);
175 : }
176 : else // regions A, C, F, H
177 : {
178 1 : CFixedVector2D corner;
179 1 : if (du < fixed::Zero()) // A, F
180 1 : corner -= u.Multiply(hw);
181 : else // C, H
182 0 : corner += u.Multiply(hw);
183 1 : if (dv < fixed::Zero()) // F, H
184 1 : corner -= v.Multiply(hh);
185 : else // A, C
186 0 : corner += v.Multiply(hh);
187 :
188 1 : return corner;
189 : }
190 : }
191 :
192 21324 : fixed DistanceSquareToSquare(const CFixedVector2D& relativePos, const CFixedVector2D& u1, const CFixedVector2D& v1, const CFixedVector2D& halfSize1, const CFixedVector2D& u2, const CFixedVector2D& v2, const CFixedVector2D& halfSize2)
193 : {
194 : /*
195 : * The shortest distance between two non colliding squares equals the distance between a corner
196 : * and other square. Thus calculating all 8 those distances and taking the smallest.
197 : * For colliding squares we simply return 0. When one of the points is inside the other square
198 : * we depend on DistanceToSquare's countInsideAsZero. When no point is inside the other square,
199 : * it is enough to check that two adjacent edges of one square does not collide with the other square.
200 : */
201 21324 : fixed hw1 = halfSize1.X;
202 21324 : fixed hh1 = halfSize1.Y;
203 21324 : fixed hw2 = halfSize2.X;
204 21324 : fixed hh2 = halfSize2.Y;
205 21324 : if (TestRaySquare(relativePos + u1.Multiply(hw1) + v1.Multiply(hh1), relativePos - u1.Multiply(hw1) + v1.Multiply(hh1), u2, v2, halfSize2) ||
206 21317 : TestRaySquare(relativePos + u1.Multiply(hw1) + v1.Multiply(hh1), relativePos + u1.Multiply(hw1) - v1.Multiply(hh1), u2, v2, halfSize2))
207 51 : return fixed::Zero();
208 :
209 110057 : return std::min(std::min(std::min(
210 21273 : DistanceToSquare(relativePos + u1.Multiply(hw1) + v1.Multiply(hh1), u2, v2, halfSize2, true),
211 21273 : DistanceToSquare(relativePos + u1.Multiply(hw1) - v1.Multiply(hh1), u2, v2, halfSize2, true)),
212 : std::min(
213 21273 : DistanceToSquare(relativePos - u1.Multiply(hw1) + v1.Multiply(hh1), u2, v2, halfSize2, true),
214 21273 : DistanceToSquare(relativePos - u1.Multiply(hw1) - v1.Multiply(hh1), u2, v2, halfSize2, true))),
215 : std::min(std::min(
216 21273 : DistanceToSquare(relativePos + u2.Multiply(hw2) + v2.Multiply(hh2), u1, v1, halfSize1, true),
217 21273 : DistanceToSquare(relativePos + u2.Multiply(hw2) - v2.Multiply(hh2), u1, v1, halfSize1, true)),
218 : std::min(
219 21273 : DistanceToSquare(relativePos - u2.Multiply(hw2) + v2.Multiply(hh2), u1, v1, halfSize1, true),
220 42546 : DistanceToSquare(relativePos - u2.Multiply(hw2) - v2.Multiply(hh2), u1, v1, halfSize1, true))));
221 : }
222 :
223 38 : fixed MaxDistanceToSquare(const CFixedVector2D& point, const CFixedVector2D& u, const CFixedVector2D& v, const CFixedVector2D& halfSize, bool countInsideAsZero)
224 : {
225 38 : fixed hw = halfSize.X;
226 38 : fixed hh = halfSize.Y;
227 :
228 38 : if (point.Dot(u).Absolute() < hw && point.Dot(v).Absolute() < hh && countInsideAsZero)
229 0 : return fixed::Zero();
230 :
231 : /*
232 : * The maximum distance from a point to an edge of a square equals the greatest distance
233 : * from the point to the a corner. Thus calculating all and taking the greatest.
234 : */
235 114 : return std::max(std::max(
236 38 : (point + u.Multiply(hw) + v.Multiply(hh)).Length(),
237 38 : (point + u.Multiply(hw) - v.Multiply(hh)).Length()),
238 : std::max(
239 38 : (point - u.Multiply(hw) + v.Multiply(hh)).Length(),
240 76 : (point - u.Multiply(hw) - v.Multiply(hh)).Length()));
241 : }
242 :
243 9 : fixed MaxDistanceSquareToSquare(const CFixedVector2D& relativePos, const CFixedVector2D& u1, const CFixedVector2D& v1, const CFixedVector2D& halfSize1, const CFixedVector2D& u2, const CFixedVector2D& v2, const CFixedVector2D& halfSize2)
244 : {
245 : /*
246 : * The maximum distance from an edge of a square to the edge of another square
247 : * equals the greatest distance from the any of the 16 corner corner distances.
248 : */
249 9 : fixed hw1 = halfSize1.X;
250 9 : fixed hh1 = halfSize1.Y;
251 :
252 27 : return std::max(std::max(
253 9 : MaxDistanceToSquare(relativePos + u1.Multiply(hw1) + v1.Multiply(hh1), u2, v2, halfSize2, true),
254 9 : MaxDistanceToSquare(relativePos + u1.Multiply(hw1) - v1.Multiply(hh1), u2, v2, halfSize2, true)),
255 9 : std::max(MaxDistanceToSquare(relativePos - u1.Multiply(hw1) + v1.Multiply(hh1), u2, v2, halfSize2, true),
256 18 : MaxDistanceToSquare(relativePos - u1.Multiply(hw1) - v1.Multiply(hh1), u2, v2, halfSize2, true)));
257 : }
258 :
259 42641 : bool TestRaySquare(const CFixedVector2D& a, const CFixedVector2D& b, const CFixedVector2D& u, const CFixedVector2D& v, const CFixedVector2D& halfSize)
260 : {
261 : /*
262 : * We only consider collisions to be when the ray goes from outside to inside the shape (and possibly out again).
263 : * Various cases to consider:
264 : * 'a' inside, 'b' inside -> no collision
265 : * 'a' inside, 'b' outside -> no collision
266 : * 'a' outside, 'b' inside -> collision
267 : * 'a' outside, 'b' outside -> depends; use separating axis theorem:
268 : * if the ray's bounding box is outside the square -> no collision
269 : * if the whole square is on the same side of the ray -> no collision
270 : * otherwise -> collision
271 : * (Points on the edge are considered 'inside'.)
272 : */
273 :
274 42641 : fixed hw = halfSize.X;
275 42641 : fixed hh = halfSize.Y;
276 :
277 42641 : fixed au = a.Dot(u);
278 42641 : fixed av = a.Dot(v);
279 :
280 42641 : if (-hw <= au && au <= hw && -hh <= av && av <= hh)
281 : return false; // a is inside
282 :
283 42531 : fixed bu = b.Dot(u);
284 42531 : fixed bv = b.Dot(v);
285 :
286 42531 : if (-hw <= bu && bu <= hw && -hh <= bv && bv <= hh) // TODO: isn't this subsumed by the next checks?
287 : return true; // a is outside, b is inside
288 :
289 42487 : if ((au < -hw && bu < -hw) || (au > hw && bu > hw) || (av < -hh && bv < -hh) || (av > hh && bv > hh))
290 : return false; // ab is entirely above/below/side the square
291 :
292 7 : CFixedVector2D abp = (b - a).Perpendicular();
293 7 : fixed s0 = abp.Dot((u.Multiply(hw) + v.Multiply(hh)) - a);
294 7 : fixed s1 = abp.Dot((u.Multiply(hw) - v.Multiply(hh)) - a);
295 7 : fixed s2 = abp.Dot((-u.Multiply(hw) - v.Multiply(hh)) - a);
296 7 : fixed s3 = abp.Dot((-u.Multiply(hw) + v.Multiply(hh)) - a);
297 7 : if (s0.IsZero() || s1.IsZero() || s2.IsZero() || s3.IsZero())
298 : return true; // ray intersects the corner
299 :
300 0 : bool sign = (s0 < fixed::Zero());
301 0 : if ((s1 < fixed::Zero()) != sign || (s2 < fixed::Zero()) != sign || (s3 < fixed::Zero()) != sign)
302 : return true; // ray cuts through the square
303 :
304 : return false;
305 : }
306 :
307 : // Exactly like TestRaySquare with u=(1,0), v=(0,1)
308 1731 : bool TestRayAASquare(const CFixedVector2D& a, const CFixedVector2D& b, const CFixedVector2D& halfSize)
309 : {
310 1731 : fixed hw = halfSize.X;
311 1731 : fixed hh = halfSize.Y;
312 :
313 1731 : if (-hw <= a.X && a.X <= hw && -hh <= a.Y && a.Y <= hh)
314 : return false; // a is inside
315 :
316 1731 : if (-hw <= b.X && b.X <= hw && -hh <= b.Y && b.Y <= hh) // TODO: isn't this subsumed by the next checks?
317 : return true; // a is outside, b is inside
318 :
319 1731 : if ((a.X < -hw && b.X < -hw) || (a.X > hw && b.X > hw) || (a.Y < -hh && b.Y < -hh) || (a.Y > hh && b.Y > hh))
320 : return false; // ab is entirely above/below/side the square
321 :
322 0 : CFixedVector2D abp = (b - a).Perpendicular();
323 0 : fixed s0 = abp.Dot(CFixedVector2D(hw, hh) - a);
324 0 : fixed s1 = abp.Dot(CFixedVector2D(hw, -hh) - a);
325 0 : fixed s2 = abp.Dot(CFixedVector2D(-hw, -hh) - a);
326 0 : fixed s3 = abp.Dot(CFixedVector2D(-hw, hh) - a);
327 0 : if (s0.IsZero() || s1.IsZero() || s2.IsZero() || s3.IsZero())
328 : return true; // ray intersects the corner
329 :
330 0 : bool sign = (s0 < fixed::Zero());
331 0 : if ((s1 < fixed::Zero()) != sign || (s2 < fixed::Zero()) != sign || (s3 < fixed::Zero()) != sign)
332 : return true; // ray cuts through the square
333 :
334 : return false;
335 : }
336 :
337 : /**
338 : * Separating axis test; returns true if the square defined by u/v/halfSize at the origin
339 : * is not entirely on the clockwise side of a line in direction 'axis' passing through 'a'
340 : */
341 37 : static bool SquareSAT(const CFixedVector2D& a, const CFixedVector2D& axis, const CFixedVector2D& u, const CFixedVector2D& v, const CFixedVector2D& halfSize)
342 : {
343 37 : fixed hw = halfSize.X;
344 37 : fixed hh = halfSize.Y;
345 :
346 37 : CFixedVector2D p = axis.Perpendicular();
347 37 : if (p.RelativeOrientation(u.Multiply(hw) + v.Multiply(hh) - a) <= 0)
348 : return true;
349 12 : if (p.RelativeOrientation(u.Multiply(hw) - v.Multiply(hh) - a) <= 0)
350 : return true;
351 8 : if (p.RelativeOrientation(-u.Multiply(hw) - v.Multiply(hh) - a) <= 0)
352 : return true;
353 3 : if (p.RelativeOrientation(-u.Multiply(hw) + v.Multiply(hh) - a) <= 0)
354 0 : return true;
355 :
356 : return false;
357 : }
358 :
359 7 : bool TestSquareSquare(
360 : const CFixedVector2D& c0, const CFixedVector2D& u0, const CFixedVector2D& v0, const CFixedVector2D& halfSize0,
361 : const CFixedVector2D& c1, const CFixedVector2D& u1, const CFixedVector2D& v1, const CFixedVector2D& halfSize1)
362 : {
363 : // TODO: need to test this carefully
364 :
365 7 : CFixedVector2D corner0a = c0 + u0.Multiply(halfSize0.X) + v0.Multiply(halfSize0.Y);
366 7 : CFixedVector2D corner0b = c0 - u0.Multiply(halfSize0.X) - v0.Multiply(halfSize0.Y);
367 7 : CFixedVector2D corner1a = c1 + u1.Multiply(halfSize1.X) + v1.Multiply(halfSize1.Y);
368 7 : CFixedVector2D corner1b = c1 - u1.Multiply(halfSize1.X) - v1.Multiply(halfSize1.Y);
369 :
370 : // Do a SAT test for each square vs each edge of the other square
371 7 : if (!SquareSAT(corner0a - c1, -u0, u1, v1, halfSize1))
372 : return false;
373 5 : if (!SquareSAT(corner0a - c1, v0, u1, v1, halfSize1))
374 : return false;
375 5 : if (!SquareSAT(corner0b - c1, u0, u1, v1, halfSize1))
376 : return false;
377 4 : if (!SquareSAT(corner0b - c1, -v0, u1, v1, halfSize1))
378 : return false;
379 4 : if (!SquareSAT(corner1a - c0, -u1, u0, v0, halfSize0))
380 : return false;
381 4 : if (!SquareSAT(corner1a - c0, v1, u0, v0, halfSize0))
382 : return false;
383 4 : if (!SquareSAT(corner1b - c0, u1, u0, v0, halfSize0))
384 : return false;
385 4 : if (!SquareSAT(corner1b - c0, -v1, u0, v0, halfSize0))
386 : return false;
387 :
388 : return true;
389 : }
390 :
391 0 : int GetPerimeterDistance(int x_max, int y_max, int x, int y)
392 : {
393 0 : if (x_max <= 0 || y_max <= 0)
394 : return 0;
395 :
396 0 : int quarter = x_max + y_max;
397 0 : if (x == x_max && y >= 0)
398 : return y;
399 0 : if (y == y_max)
400 0 : return quarter - x;
401 0 : if (x == -x_max)
402 0 : return 2 * quarter - y;
403 0 : if (y == -y_max)
404 0 : return 3 * quarter + x;
405 0 : if (x == x_max)
406 0 : return 4 * quarter + y;
407 : return 0;
408 : }
409 :
410 0 : std::pair<int, int> GetPerimeterCoordinates(int x_max, int y_max, int k)
411 : {
412 0 : if (x_max <= 0 || y_max <= 0)
413 0 : return std::pair<int, int>(0, 0);
414 :
415 0 : int quarter = x_max + y_max;
416 0 : k %= 4 * quarter;
417 0 : if (k < 0)
418 0 : k += 4 * quarter;
419 :
420 0 : if (k < y_max)
421 0 : return std::pair<int, int>(x_max, k);
422 0 : if (k < quarter + x_max)
423 0 : return std::pair<int, int>(quarter - k, y_max);
424 0 : if (k < 2 * quarter + y_max)
425 0 : return std::pair<int, int>(-x_max, 2 * quarter - k);
426 0 : if (k < 3 * quarter + x_max)
427 0 : return std::pair<int, int>(k - 3 * quarter, -y_max);
428 0 : return std::pair<int, int>(x_max, k - 4 * quarter);
429 : }
430 :
431 0 : fixed DistanceToSegment(
432 : const CFixedVector2D& point, const CFixedVector2D& a, const CFixedVector2D& b)
433 : {
434 : // First we need to figure out from which part of the segment we should
435 : // calculate distance.
436 : // We split 2D space in three spaces:
437 : // | |
438 : // 1 | 2 | 3
439 : // A--------------------------------B
440 : // Here we need | Between A and B we need to | Here we need
441 : // distance to A | calculate distance to the line | distance to B
442 : //
443 0 : const CFixedVector2D dir = b - a;
444 : // We project the point, point A, and point B upon the direction of the
445 : // segment to figure out in which space the point is.
446 0 : const fixed pointDot = dir.Dot(point);
447 0 : const fixed aDot = dir.Dot(a);
448 : // The point is lying in space #1.
449 0 : if (pointDot <= aDot)
450 0 : return (point - a).Length();
451 0 : const fixed bDot = dir.Dot(b);
452 : // The point is lying in space #3.
453 0 : if (pointDot >= bDot)
454 0 : return (point - b).Length();
455 : // The point is lying in space #2.
456 0 : CFixedVector2D normal = dir.Perpendicular();
457 0 : normal.Normalize();
458 0 : return (normal.Dot(a) - normal.Dot(point)).Absolute();
459 : }
460 :
461 : } // namespace Geometry
|