forked from kektobiologist/motion-simulation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geometry.h
262 lines (227 loc) · 6.75 KB
/
geometry.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/* This file contains the template class Vector2D which implements
* all major functions for point type objects with two fields x and y.
* It also implements overloading of the following operators : -,+,*,/
* for these objects.
*/
#ifndef GEOMETRY_H
#define GEOMETRY_H
#include <cmath>
#include <pose.h>
#include "constants.h"
#define PI (3.14159265358979323f)
#define INF (9999999)
// Vector2D can also be called as Point2D
#define Point2D Vector2D
using namespace Constants;
// Function definitions
template <class T>
class Vector2D
{
public:
T x;
T y;
bool valid(void) const
{
return (x != -INF && y != -INF && x != INF && y != INF);
}
inline Vector2D() :
x(-INF),
y(-INF)
{ }
inline Vector2D(T x, T y) :
x(x),
y(y)
{ }
// Copy constructor
inline Vector2D(const Vector2D<T>& v) :
x(v.x),
y(v.y)
{ }
// Get Invalid Vector2D
static inline Vector2D<T> InvalidVector(void)
{
return Vector2D<T>();
}
// Sets the vector using polar coordinates
static inline const Vector2D<T> fromPolar(float r, float theta)
{
Vector2D<T> v;
v.x = r * cos(theta);
v.y = r * sin(theta);
return v;
}
// Returns the angle made by the vector (head - tail) in the range -pi to pi
static inline float angle(const Vector2D<T>& head, const Vector2D<T>& tail)
{
return atan2((float)head.y - tail.y, (float)head.x - tail.x);
}
// Returns the Eucledian distance between the 2 vectors
static inline float dist(const Vector2D<T>& v1, const Vector2D<T>& v2)
{
return sqrt((float)(v1.x - v2.x) * (v1.x - v2.x) + (v1.y - v2.y) * (v1.y - v2.y));
}
static inline float dist(const Vector2D<T>& v1, const int x, const int y) {
return sqrt((float)(v1.x - x) * (v1.x - x) + (v1.y - y) * (v1.y - y));
}
// Returns the squared Eucledian distane between 2 vectors. Prefer it over dist() for speed improvements
static inline T distSq(const Vector2D<T>& v1, const Vector2D<T>& v2)
{
return ((v1.x - v2.x) * (v1.x - v2.x) + (v1.y - v2.y) * (v1.y - v2.y));
}
// Returns the absolute value of the vector
inline float abs(void) const
{
return sqrt((float)x * x + y * y);
}
// Returns the squared absolute value of the vector. Prefer if over abs() for speed improvements
inline T absSq(void) const
{
return (x * x + y * y);
}
inline Vector2D<T>& operator += (const Vector2D<T>& rhs)
{
x += rhs.x;
y += rhs.y;
return *this;
}
inline Vector2D<T>& operator -= (const Vector2D<T>& rhs)
{
x -= rhs.x;
y -= rhs.y;
return *this;
}
inline T dot(const Vector2D<T>& rhs)
{
return x * rhs.x + y * rhs.y;
}
};
// Binary minus operator overloading
template <class T>
inline Vector2D<T> operator - (const Vector2D<T>& lhs, const Vector2D<T>& rhs)
{
Vector2D<T> result = lhs;
result.x = result.x - rhs.x;
result.y = result.y - rhs.y;
return result;
}
// Binary plus operator overloading
template <class T>
inline Vector2D<T> operator + (const Vector2D<T>& lhs, const Vector2D<T>& rhs)
{
Vector2D<T> result = lhs;
result.x = result.x + rhs.x;
result.y = result.y + rhs.y;
return result;
}
// Binary * (for scaler product) operator overloading
template <class T>
inline Vector2D<T> operator * (float scale, const Vector2D<T>& rhs)
{
Vector2D<T> result;
result.x = scale * rhs.x;
result.y = scale * rhs.y;
return result;
}
// Binary * (for scaler product) operator overloading
template <class T>
inline Vector2D<T> operator * (const Vector2D<T>& lhs, float scale)
{
Vector2D<T> result;
result.x = scale * lhs.x;
result.y = scale * lhs.y;
return result;
}
// Binary / (for scaler division) operator overloading
template <class T>
inline Vector2D<T> operator / (const Vector2D<T>& lhs, float scaleInv)
{
Vector2D<T> result;
result.x = (1.0f / scaleInv) * lhs.x;
result.y = (1.0f / scaleInv) * lhs.y;
return result;
}
// Binary == (equality) operator overloading
template <class T>
inline bool operator == (const Vector2D<T>& lhs, const Vector2D<T>& rhs)
{
return (lhs.x == rhs.x && lhs.y == rhs.y);
}
// Binary != (equality) operator overloading
template <class T>
inline bool operator != (const Vector2D<T>& lhs, const Vector2D<T>& rhs)
{
return (lhs.x != rhs.x || lhs.y != rhs.y);
}
// Normalizes the angle (in radians) to be in the range (-pi, pi]
inline double normalizeAngle(double angle)
{
while(angle > PI) angle -= 2*PI;
while(angle <= -PI) angle += 2*PI;
return angle;
}
// Detects if a point is within a circle or not
template <class T>
inline bool intersects(const Point2D<int>& point,
const Vector2D<T>& center,
T radius)
{
return (Vector2D<T>::distSq(point, center) < radius * radius);
}
// Detects if a line segment intersects a circle or not
template <class T>
inline bool intersects(const Point2D<T>& point1,
const Point2D<T>& point2,
const Point2D<T>& center,
T radius)
{
/* Source of algorithm used: http://stackoverflow.com/questions/1073336/circle-line-collision-detection */
Vector2D<int> d = point2 - point1;
Vector2D<int> f = point1 - center;
float a = d.dot(d);
float b = 2 * f.dot(d);
float c = f.dot(f) - radius * radius;
float dis = b * b - 4 * a * c;
if (dis >= 0)
{
dis = sqrt(dis);
float t1 = (-b + dis) / (2 * a);
float t2 = (-b - dis) / (2 * a);
if (t1 >= 0 && t1 <= 1)
return true;
if (t2 >= 0 && t2 <= 1)
return true;
}
return false;
}
inline float distance_line_point(const Vector2D<int>& start,
const Vector2D<float>& slope,
const Vector2D<int>& point) {
Vector2D<float> joinLine((point-start).x, (point-start).y);
float theta = Vector2D<float>::angle(slope, joinLine);
return sin(theta) * Vector2D<int>::dist(point, start);
}
inline bool get_perpendicular_base(const Vector2D<int>& start,
const Vector2D<float>& slope,
const Vector2D<int>& point,
Vector2D<int> &perpendicularBase,
float &alpha) {
alpha = (slope.x*(point.x - start.x) + slope.y*(point.y - start.y)) / slope.absSq();
perpendicularBase = Vector2D<int>(start.x + (alpha * slope.x), start.y + (alpha * slope.y));
return alpha >= 0;
}
inline float firaNormalizeAngle(float angle)
{
angle = fmod(angle,2*PI);
if (angle > PI)
angle = angle - 2 * PI;
else if (angle <= -PI)
angle = angle + 2 * PI;
if(angle > PI/2) angle = angle-PI;
else if(angle <= -PI/2) angle = angle + PI;
return angle;
}
inline bool isPointinField(const Vector2D<int>& point){
if(abs(point.x) <= HALF_FIELD_MAXX && abs(point.y) <= HALF_FIELD_MAXY)return 1;
else return 0;
}
#endif // GEOMETRY_H