-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgeometry.cpp
More file actions
279 lines (217 loc) · 5.62 KB
/
geometry.cpp
File metadata and controls
279 lines (217 loc) · 5.62 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "geometry.h"
#include "iostream"
#include <iomanip>
double interpolate(double x, double x1, double y1, double x2, double y2){
double m = (y2 - y1)/( x2 - x1 );
return y1 + m * (x-x1);
}
Point::Point(double x, double y) :
x(x),y(y){}
string Point::to_string() const {
stringstream ss;
ss << std::fixed << std::setprecision(2) << "(" << x << "," << std::setprecision(2) << y << ")";
return ss.str();
}
Point Point::operator -(Point rhs)
{
return Point(x-rhs.x,y-rhs.y);
}
string to_string(const Point &p) {
return p.to_string();
}
Angle Angle::degrees(double d) {
Angle a;
a.set_degrees(d);
return a;
}
Angle Angle::radians(double rad) {
Angle a;
a.theta = rad;
return a;
}
double Angle::radians() const {
return theta;
}
double Angle::degrees() const {
return theta * 180. / M_PI;
}
void Angle::set_degrees(double d) {
theta = d * M_PI/180.;
}
void Angle::set_radians(double theta_)
{
theta = theta_;
}
void Angle::standardize() {
theta = fmod(theta + 99*M_PI , 2.*M_PI) - M_PI;
}
const string Angle::to_string() {
stringstream s;
s << degrees() << "°";
return s.str();
}
bool Angle::operator ==(Angle &rhs) {
return theta == rhs.theta;
}
Angle &Angle::operator /=(double d) {
theta /= d;
return *this;
}
Angle Angle::operator /(double d) const {
Angle rv;
rv.theta = this->theta / d;
return rv;
}
Angle Angle::operator *(double d) const {
Angle rv;
rv.theta = this->theta * d;
return rv;
}
Angle Angle::operator +(const Angle &rhs) const {
Angle rv;
rv.theta = this->theta + rhs.theta;
return rv;
}
Angle &Angle::operator +=(const Angle &rhs) {
theta += rhs.theta;
return *this;
}
Angle Angle::operator -() {
return Angle::radians(-theta);
}
Angle Angle::operator -(const Angle &rhs) const {
Angle rv;
rv.theta = this->theta - rhs.theta;
rv.standardize();
return rv;
}
double degrees(double radians) {
return radians * 180 / M_PI;
}
double radians(double degrees) {
return degrees* M_PI / 180;
}
double standardized_radians(double theta) {
return fmod(theta + M_PI , 2.*M_PI) - M_PI;
}
double standardized_degrees(double theta) {
return fmod(theta + 180., 360) - 180.;
}
double degrees_diff(double theta1, double theta2) {
return standardized_degrees(theta2 - theta1);
}
double length(double x, double y) {
return sqrt(x*x+y*y);
}
vector<double> quadratic(double a, double b, double c) {
return {(-b +sqrt(b*b - 4.*a*c))/(2.*a) ,(-b -sqrt(b*b-4.*a*c))/(2.*a)};
}
double distance(double x1, double y1, double x2, double y2) {
return length(x2-x1,y2-y1);
}
double distance(Point p1, Point p2) {
return distance(p1.x,p1.y,p2.x,p2.y);
}
// returns distance of point p from segment from start to end
double distance_from_segment_to_pointt(Point start, Point end, Point p ) {
double dx = end.x - start.x;
double dy = end.y - start.y;
double drx = p.x - start.x;
double dry = p.y - start.y;
double progress = (drx * dx + dry * dy)/(dx * dx + dy * dy);
if(progress < 0)
return distance(start,p);
if(progress > 1)
return distance(end,p);
double l = length(dx,dy);
double cte = (dry * dx - drx * dy) / l;
return fabs(cte);
}
double velocity_at_time(double t, double a, double v0){
return v0 + a * t;
}
double velocity_at_position(double x, double a, double v0, double x0){
x = x-x0;
double t = time_at_position(x,a,v0);
return velocity_at_time(t,a,v0);
}
Point unit_vector(Point p) {
auto l=length(p.x,p.y);
return Point(p.x/l,p.y/l);
}
Angle angle_between(double x1, double y1, double x2, double y2) {
double dot = x1*x2 + y1*y2; // dot product
double det = x1*y2 - y1*x2; // determinant
return Angle::radians( atan2(det, dot) );
}
Angle angle_between(Point p1, Point p2) {
return angle_between(p1.x,p1.y,p2.x,p2.y);
}
Angle angle_to(Point p1, Point p2) {
return Angle::radians(atan2(p2.y-p1.y,p2.x-p1.x));
}
double clamp(double value, double min_value, double max_value) {
if(value < min_value)
return min_value;
if (value > max_value)
return max_value;
return value;
}
double acceleration_for_distance_and_velocities(double d, double v1, double v2) {
bool same_signs = (v2 >= 0) == (v1 >= 0);
double a;
if(same_signs) {
a = (v2*v2 - v1*v1) / (2*d);
} else {
a = (v2*v2 + v1*v1) / (2*d);
}
return a;
}
double time_at_position(double x, double a, double v0, double x0){
if(isnan(x)) {
return NAN;
}
x = x-x0;
if(a==0)
return x/v0;
auto t = quadratic(0.5*a,v0,-x);
if (t[0] < 0){
return t[1] > 0 ? t[1] : NAN;
}
if (t[1] < 0){
return t[0];
}
return min(t[0],t[1]);
}
Transform2d Transform2d::pose_to_world_transform(Pose2d pose) {
Transform2d t = world_to_pose_transform(pose);
t.t = t.t.inverse();
t.rotation = -t.rotation;
return t;
}
Transform2d Transform2d::world_to_pose_transform(Pose2d pose) {
Transform2d t;
t.t.setIdentity();
Eigen::Translation2d translation(-pose.position.x, -pose.position.y);
t.t *= Eigen::Rotation2Dd(-pose.heading);
t.t *= translation;
t.rotation = -pose.heading;
return t;
}
Point Transform2d::operator() (Point & point) {
Eigen::Vector2d v;
v << point.x, point.y;
Eigen::Vector2d vt = t * v;
return Point(vt[0], vt[1]);
}
Pose2d Transform2d::operator() (Pose2d & pose) {
Angle heading = pose.heading + rotation;
Eigen::Vector2d v;
v << pose.position.x, pose.position.y;
Eigen::Vector2d vt = t * v;
//double ddx = pose.position.x + dx;
//double ddy = pose.position.y + dy;
//double x = ddx * cos(rotation.radians() ) - ddy * sin(rotation.radians());
//double y = ddx * sin(rotation.radians() ) + ddy * cos(rotation.radians());
return Pose2d(heading, {vt[0],vt[1]});
}