Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
Geometry.h
Go to the documentation of this file.
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#ifndef INCLUDED_HELPER_GEOMETRY
19#define INCLUDED_HELPER_GEOMETRY
20
21/**
22 * @file
23 * Helper functions related to geometry algorithms
24 */
25
26#include "maths/Fixed.h"
27#include "maths/FixedVector2D.h"
28
29#include <utility>
30
31namespace Geometry
32{
33
34/**
35 * Checks if a point is inside the given rotated rectangle.
36 * Points precisely on an edge are considered to be inside.
37 *
38 * The rectangle is defined by the four vertexes
39 * (+/-u*halfSize.X +/-v*halfSize.Y)
40 *
41 * The @p u and @p v vectors must be perpendicular.
42 */
43inline bool PointIsInSquare(const CFixedVector2D& point, const CFixedVector2D& u, const CFixedVector2D& v, const CFixedVector2D& halfSize)
44{
45 return point.Dot(u).Absolute() <= halfSize.X && point.Dot(v).Absolute() <= halfSize.Y;
46}
47
48/**
49 * Returns a vector (bx,by) such that every point inside
50 * the given rotated rectangle has coordinates
51 * (x,y) with -bx <= x <= bx, -by <= y < by.
52 *
53 * The rectangle is defined by the four vertexes
54 * (+/-u*halfSize.X +/-v*halfSize.Y).
55 */
57
58/**
59 * Returns the minimum Euclidean distance from the given point to
60 * any point on the boundary of the given rotated rectangle.
61 *
62 * If @p countInsideAsZero is true, and the point is inside the rectangle,
63 * it will return 0.
64 * If @p countInsideAsZero is false, the (positive) distance to the boundary
65 * will be returned regardless of where the point is.
66 *
67 * The rectangle is defined by the four vertexes
68 * (+/-u*halfSize.X +/-v*halfSize.Y).
69 *
70 * The @p u and @p v vectors must be perpendicular and unit length.
71 */
73 const CFixedVector2D& u, const CFixedVector2D& v, const CFixedVector2D& halfSize,
74 bool countInsideAsZero = false);
75
76/**
77 * Similar to above but never uses sqrt, so it returns the squared distance.
78 */
80 const CFixedVector2D& u, const CFixedVector2D& v, const CFixedVector2D& halfSize,
81 bool countInsideAsZero = false);
82/**
83 * Returns a point on the boundary of the given rotated rectangle
84 * that is closest (or equally closest) to the given point
85 * in Euclidean distance.
86 *
87 * The rectangle is defined by the four vertexes
88 * (+/-u*halfSize.X +/-v*halfSize.Y).
89 *
90 * The @p u and @p v vectors must be perpendicular and unit length.
91 */
93 const CFixedVector2D& u, const CFixedVector2D& v, const CFixedVector2D& halfSize);
94
95/**
96 * Returns the shortest distance between two squares.
97 */
99 const CFixedVector2D& u1, const CFixedVector2D& v1, const CFixedVector2D& halfSize1,
100 const CFixedVector2D& u2, const CFixedVector2D& v2, const CFixedVector2D& halfSize2);
101
102/**
103 * Returns the greatest straight line distance from a point to a square.
104 *
105 * If @p countInsideAsZero is true, and the point is inside the rectangle,
106 * it will return 0.
107 * If @p countInsideAsZero is false, the greatest (positive) distance to the boundary
108 * will be returned regardless of where the point is.
109 */
111 const CFixedVector2D& u, const CFixedVector2D& v, const CFixedVector2D& halfSize,
112 bool countInsideAsZero = false);
113
114/**
115 * Return the greatest straight line distance between two squares.
116 */
118 const CFixedVector2D& u1, const CFixedVector2D& v1, const CFixedVector2D& halfSize1,
119 const CFixedVector2D& u2, const CFixedVector2D& v2, const CFixedVector2D& halfSize2);
120
121bool TestRaySquare(const CFixedVector2D& a, const CFixedVector2D& b, const CFixedVector2D& u, const CFixedVector2D& v, const CFixedVector2D& halfSize);
122
123bool TestRayAASquare(const CFixedVector2D& a, const CFixedVector2D& b, const CFixedVector2D& halfSize);
124
126 const CFixedVector2D& c0, const CFixedVector2D& u0, const CFixedVector2D& v0, const CFixedVector2D& halfSize0,
127 const CFixedVector2D& c1, const CFixedVector2D& u1, const CFixedVector2D& v1, const CFixedVector2D& halfSize1);
128
129/**
130 * Used in Footprint when spawning units:
131 * Given a grid point (x, y) on the rectangle [-x_max, x_max] x [-y_max, y_max],
132 * this returns the distance travelled in moving from (x_max, 0) to the the point while
133 * walking counter-clockwise along the perimeter of the rectangle.
134 */
135int GetPerimeterDistance(int x_max, int y_max, int x, int y);
136
137/**
138 * Used in Footprint when spawning units:
139 * This returns the grid point on the rectangle [-x_max, x_max] x [-y_max, y_max]
140 * reached after starting at (x_max, 0) and walking a distance k
141 * counter-clockwise along the perimeter of the rectangle.
142 */
143std::pair<int, int> GetPerimeterCoordinates(int x_max, int y_max, int k);
144
145/**
146 * Returns the minimum Euclidean distance from the given point to
147 * any point on the given segment.
148 *
149 * @a and @b represents segment's points.
150 *
151 */
153 const CFixedVector2D& point, const CFixedVector2D& a, const CFixedVector2D& b);
154
155} // namespace Geometry
156
157#endif // INCLUDED_HELPER_GEOMETRY
Definition: FixedVector2D.h:25
fixed X
Definition: FixedVector2D.h:27
fixed Dot(const CFixedVector2D &v) const
Compute the dot product of this vector with another.
Definition: FixedVector2D.h:209
fixed Y
Definition: FixedVector2D.h:27
A simple fixed-point number class.
Definition: Fixed.h:129
constexpr CFixed Absolute() const
Definition: Fixed.h:325
Definition: Geometry.cpp:25
fixed DistanceSquareToSquare(const CFixedVector2D &relativePos, const CFixedVector2D &u1, const CFixedVector2D &v1, const CFixedVector2D &halfSize1, const CFixedVector2D &u2, const CFixedVector2D &v2, const CFixedVector2D &halfSize2)
Returns the shortest distance between two squares.
Definition: Geometry.cpp:192
bool TestRayAASquare(const CFixedVector2D &a, const CFixedVector2D &b, const CFixedVector2D &halfSize)
Definition: Geometry.cpp:308
std::pair< int, int > GetPerimeterCoordinates(int x_max, int y_max, int k)
Used in Footprint when spawning units: This returns the grid point on the rectangle [-x_max,...
Definition: Geometry.cpp:410
bool TestRaySquare(const CFixedVector2D &a, const CFixedVector2D &b, const CFixedVector2D &u, const CFixedVector2D &v, const CFixedVector2D &halfSize)
Definition: Geometry.cpp:259
CFixedVector2D NearestPointOnSquare(const CFixedVector2D &point, const CFixedVector2D &u, const CFixedVector2D &v, const CFixedVector2D &halfSize)
Returns a point on the boundary of the given rotated rectangle that is closest (or equally closest) t...
Definition: Geometry.cpp:118
fixed DistanceToSquare(const CFixedVector2D &point, const CFixedVector2D &u, const CFixedVector2D &v, const CFixedVector2D &halfSize, bool countInsideAsZero)
Returns the minimum Euclidean distance from the given point to any point on the boundary of the given...
Definition: Geometry.cpp:37
fixed MaxDistanceToSquare(const CFixedVector2D &point, const CFixedVector2D &u, const CFixedVector2D &v, const CFixedVector2D &halfSize, bool countInsideAsZero)
Returns the greatest straight line distance from a point to a square.
Definition: Geometry.cpp:223
fixed DistanceToSegment(const CFixedVector2D &point, const CFixedVector2D &a, const CFixedVector2D &b)
Returns the minimum Euclidean distance from the given point to any point on the given segment.
Definition: Geometry.cpp:431
CFixedVector2D GetHalfBoundingBox(const CFixedVector2D &u, const CFixedVector2D &v, const CFixedVector2D &halfSize)
Returns a vector (bx,by) such that every point inside the given rotated rectangle has coordinates (x,...
Definition: Geometry.cpp:29
int GetPerimeterDistance(int x_max, int y_max, int x, int y)
Used in Footprint when spawning units: Given a grid point (x, y) on the rectangle [-x_max,...
Definition: Geometry.cpp:391
fixed MaxDistanceSquareToSquare(const CFixedVector2D &relativePos, const CFixedVector2D &u1, const CFixedVector2D &v1, const CFixedVector2D &halfSize1, const CFixedVector2D &u2, const CFixedVector2D &v2, const CFixedVector2D &halfSize2)
Return the greatest straight line distance between two squares.
Definition: Geometry.cpp:243
bool TestSquareSquare(const CFixedVector2D &c0, const CFixedVector2D &u0, const CFixedVector2D &v0, const CFixedVector2D &halfSize0, const CFixedVector2D &c1, const CFixedVector2D &u1, const CFixedVector2D &v1, const CFixedVector2D &halfSize1)
Definition: Geometry.cpp:359
fixed DistanceToSquareSquared(const CFixedVector2D &point, const CFixedVector2D &u, const CFixedVector2D &v, const CFixedVector2D &halfSize, bool countInsideAsZero)
Similar to above but never uses sqrt, so it returns the squared distance.
Definition: Geometry.cpp:93
bool PointIsInSquare(const CFixedVector2D &point, const CFixedVector2D &u, const CFixedVector2D &v, const CFixedVector2D &halfSize)
Checks if a point is inside the given rotated rectangle.
Definition: Geometry.h:43