Pyrogenesis  trunk
Geometry.h
Go to the documentation of this file.
1 /* Copyright (C) 2012 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 #include "maths/MathUtil.h"
29 
30 namespace Geometry
31 {
32 
33 /**
34  * Checks if a point is inside the given rotated rectangle.
35  * Points precisely on an edge are considered to be inside.
36  *
37  * The rectangle is defined by the four vertexes
38  * (+/-u*halfSize.X +/-v*halfSize.Y)
39  *
40  * The @p u and @p v vectors must be perpendicular.
41  */
42 inline bool PointIsInSquare(const CFixedVector2D& point, const CFixedVector2D& u, const CFixedVector2D& v, const CFixedVector2D& halfSize)
43 {
44  return point.Dot(u).Absolute() <= halfSize.X && point.Dot(v).Absolute() <= halfSize.Y;
45 }
46 
47 /**
48  * Returns a vector (bx,by) such that every point inside
49  * the given rotated rectangle has coordinates
50  * (x,y) with -bx <= x <= bx, -by <= y < by.
51  *
52  * The rectangle is defined by the four vertexes
53  * (+/-u*halfSize.X +/-v*halfSize.Y).
54  */
56 
57 /**
58  * Returns the minimum Euclidean distance from the given point to
59  * any point on the boundary of the given rotated rectangle.
60  *
61  * If @p countInsideAsZero is true, and the point is inside the rectangle,
62  * it will return 0.
63  * If @p countInsideAsZero is false, the (positive) distance to the boundary
64  * will be returned regardless of where the point is.
65  *
66  * The rectangle is defined by the four vertexes
67  * (+/-u*halfSize.X +/-v*halfSize.Y).
68  *
69  * The @p u and @p v vectors must be perpendicular and unit length.
70  */
72  const CFixedVector2D& u, const CFixedVector2D& v, const CFixedVector2D& halfSize,
73  bool countInsideAsZero = false);
74 
75 /**
76  * Similar to above but never uses sqrt, so it returns the squared distance.
77  */
79  const CFixedVector2D& u, const CFixedVector2D& v, const CFixedVector2D& halfSize,
80  bool countInsideAsZero = false);
81 /**
82  * Returns a point on the boundary of the given rotated rectangle
83  * that is closest (or equally closest) to the given point
84  * in Euclidean distance.
85  *
86  * The rectangle is defined by the four vertexes
87  * (+/-u*halfSize.X +/-v*halfSize.Y).
88  *
89  * The @p u and @p v vectors must be perpendicular and unit length.
90  */
92  const CFixedVector2D& u, const CFixedVector2D& v, const CFixedVector2D& halfSize);
93 
94 /**
95  * Given a circle of radius @p radius, and a chord of length @p chordLength
96  * on this circle, computes the central angle formed by
97  * connecting the chord's endpoints to the center of the circle.
98  *
99  * @param radius Radius of the circle; must be strictly positive.
100  */
101 float ChordToCentralAngle(const float chordLength, const float radius);
102 
103 bool TestRaySquare(const CFixedVector2D& a, const CFixedVector2D& b, const CFixedVector2D& u, const CFixedVector2D& v, const CFixedVector2D& halfSize);
104 
105 bool TestRayAASquare(const CFixedVector2D& a, const CFixedVector2D& b, const CFixedVector2D& halfSize);
106 
107 bool TestSquareSquare(
108  const CFixedVector2D& c0, const CFixedVector2D& u0, const CFixedVector2D& v0, const CFixedVector2D& halfSize0,
109  const CFixedVector2D& c1, const CFixedVector2D& u1, const CFixedVector2D& v1, const CFixedVector2D& halfSize1);
110 
111 } // namespace
112 
113 #endif // INCLUDED_HELPER_GEOMETRY
A simple fixed-point number class.
Definition: Fixed.h:115
fixed Dot(const CFixedVector2D &v) const
Compute the dot product of this vector with another.
Definition: FixedVector2D.h:201
Definition: FixedVector2D.h:24
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:120
CFixed Absolute() const
Definition: Fixed.h:305
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:294
fixed DistanceToSquare(const CFixedVector2D &point, const CFixedVector2D &u, const CFixedVector2D &v, const CFixedVector2D &halfSize, bool countInsideAsZero=false)
Returns the minimum Euclidean distance from the given point to any point on the boundary of the given...
Definition: Geometry.cpp:39
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:26
bool TestRayAASquare(const CFixedVector2D &a, const CFixedVector2D &b, const CFixedVector2D &halfSize)
Definition: Geometry.cpp:243
fixed Y
Definition: FixedVector2D.h:27
fixed DistanceToSquareSquared(const CFixedVector2D &point, const CFixedVector2D &u, const CFixedVector2D &v, const CFixedVector2D &halfSize, bool countInsideAsZero=false)
Similar to above but never uses sqrt, so it returns the squared distance.
Definition: Geometry.cpp:95
bool TestRaySquare(const CFixedVector2D &a, const CFixedVector2D &b, const CFixedVector2D &u, const CFixedVector2D &v, const CFixedVector2D &halfSize)
Definition: Geometry.cpp:194
fixed X
Definition: FixedVector2D.h:27
float ChordToCentralAngle(const float chordLength, const float radius)
Given a circle of radius radius, and a chord of length chordLength on this circle, computes the central angle formed by connecting the chord&#39;s endpoints to the center of the circle.
Definition: Geometry.cpp:34
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:42
Definition: Geometry.h:30