-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeoMath.h
317 lines (256 loc) · 6.29 KB
/
GeoMath.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
This is a library written for the Wt32-AIO project for AgOpenGPS
Written by Miguel Cebrian, November 30th, 2023.
Based on Theejs development.
This library coordinates all other libraries.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef GEOMATH_H
#define GEOMATH_H
#include <cmath>
class Vector2{
public:
Vector2(){
Vector2(0,0);
}
Vector2(double _x, double _y){
x=_x;
y=_y;
}
double x=0, y=0;
double angle(){
// computes the angle in radians with respect to the positive x-axis
return std::atan2( - y, - x ) + 3.14159265;
}
Vector2& add(Vector2 v){
x += v.x;
y += v.y;
return *this;
}
Vector2 clone(){
return Vector2(x,y);
}
void copy(Vector2 v){
x = v.x;
y = v.y;
}
double distanceTo(Vector2 v){
return std::sqrt(distanceToSquared(v));
}
double distanceToSquared(Vector2 v){
double dx = x-v.x;
double dy = y-v.y;
return dx*dx+dy*dy;
}
Vector2& divideScalar(double v){
return multiplyScalar(1/v);
}
double length(){
return std::sqrt(x*x+y*y);
}
Vector2& lerp(Vector2 v, double a){
x+=(v.x-x)*a;
y+=(v.y-y)*a;
return *this;
}
Vector2& multiplyScalar(double v){
x *=v;
y *=v;
return *this;
}
Vector2& normalize(){
double l = length()? length() : 1;
return divideScalar(l);
}
Vector2& rotateAround(Vector2 center, double angle){
double c = std::cos(angle);
double s = std::sin(angle);
double xx = x-center.x;
double yy = y-center.y;
x = xx*c-yy*s+center.x;
y = xx*s+yy*c+center.y;
return *this;
}
Vector2& set(double _x, double _y){
x = _x;
y = _y;
return *this;
}
Vector2& setLength(double length){
return normalize().multiplyScalar(length);
}
Vector2& sub(Vector2& v){
x -= v.x;
y -= v.y;
return *this;
}
};
class Quaternion{
public:
Quaternion(double _x=0, double _y=0, double _z=0, double _w=0){
x = _x;
y = _y;
z = _z;
w = _w;
}
double x=0, y=0, z=0, w=0;
Quaternion& setFromEuler(double _x, double _y, double _z, const char* _order="XYZ"){
// http://www.mathworks.com/matlabcentral/fileexchange/
// 20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/
// content/SpinCalc.m
double c1 = std::cos(_x/2);
double c2 = std::cos(_y/2);
double c3 = std::cos(_z/2);
double s1 = std::sin(_x/2);
double s2 = std::sin(_y/2);
double s3 = std::sin(_z/2);
if(strcmp(_order,"XYZ") == 0){
x = s1 * c2 * c3 + c1 * s2 * s3;
y = c1 * s2 * c3 - s1 * c2 * s3;
z = c1 * c2 * s3 + s1 * s2 * c3;
w = c1 * c2 * c3 - s1 * s2 * s3;
}else if(strcmp(_order,"YXZ") == 0){
x = s1 * c2 * c3 + c1 * s2 * s3;
y = c1 * s2 * c3 - s1 * c2 * s3;
z = c1 * c2 * s3 - s1 * s2 * c3;
w = c1 * c2 * c3 + s1 * s2 * s3;
}else if(strcmp(_order,"ZXY") == 0){
x = s1 * c2 * c3 - c1 * s2 * s3;
y = c1 * s2 * c3 + s1 * c2 * s3;
z = c1 * c2 * s3 + s1 * s2 * c3;
w = c1 * c2 * c3 - s1 * s2 * s3;
}else if(strcmp(_order,"ZYX") == 0){
x = s1 * c2 * c3 - c1 * s2 * s3;
y = c1 * s2 * c3 + s1 * c2 * s3;
z = c1 * c2 * s3 - s1 * s2 * c3;
w = c1 * c2 * c3 + s1 * s2 * s3;
}else if(strcmp(_order,"YZX") == 0){
x = s1 * c2 * c3 + c1 * s2 * s3;
y = c1 * s2 * c3 + s1 * c2 * s3;
z = c1 * c2 * s3 - s1 * s2 * c3;
w = c1 * c2 * c3 - s1 * s2 * s3;
}else if(strcmp(_order,"XZY") == 0){
x = s1 * c2 * c3 - c1 * s2 * s3;
y = c1 * s2 * c3 - s1 * c2 * s3;
z = c1 * c2 * s3 + s1 * s2 * c3;
w = c1 * c2 * c3 + s1 * s2 * s3;
}
return *this;
}
};
class Vector3{
public:
Vector3(double _x=0.0, double _y=0.0, double _z=0.0){
x=_x;
y=_y;
z=_z;
}
double x=0, y=0, z=0;
Vector3& add(Vector3 v){
x += v.x;
y += v.y;
z += v.z;
return *this;
}
Vector3& applyQuaternion(Quaternion q){
double xx = x;
double yy = y;
double zz = z;
double qx = q.x;
double qy = q.y;
double qz = q.z;
double qw = q.w;
//calculate quat*vector
double ix = qw*xx+qy*zz-qz*yy;
double iy = qw*yy+qz*xx-qx*zz;
double iz = qw*zz+qx*yy-qy*xx;
double iw = -qx*xx-qy*yy-qz*zz;
//calculate result*inverse quat
x = ix*qw+iw*-qx+iy*-qz-iz*-qy;
y = iy*qw+iw*-qy+iz*-qx-ix*-qz;
z = iz*qw+iw*-qz+ix*-qy-iy*-qx;
return *this;
}
Vector3 clone(){
return Vector3(x,y,z);
}
double distanceTo(Vector3 v){
return std::sqrt(distanceToSquared(v));
}
double distanceToSquared(Vector3 v){
double dx = x-v.x;
double dy = y-v.y;
double dz = z-v.z;
return dx*dx+dy*dy+dz*dz;
}
double dot(Vector3 v){
return x*v.x+y*v.y+z*v.z;
}
Vector3& multiply(Vector3 v){
x *= v.x;
y *= v.y;
z *= v.z;
return *this;
}
Vector3& multiplyScalar(double v){
x *=v;
y *=v;
z *=v;
return *this;
}
Vector3& set(double _x, double _y, double _z){
x = _x;
y = _y;
z = _z;
return *this;
}
Vector3& sub(Vector3 v){
x -= v.x;
y -= v.y;
z -= v.z;
return *this;
}
Vector3& subVectors(Vector3 a, Vector3 b){
x = a.x-b.x;
y = a.y-b.y;
z = a.z-b.z;
return *this;
}
};
class Line3{
public:
Line3():start(0,0,0), end(0,0,0){
Line3(start, end);
}
Line3(Vector3 _start, Vector3 _end):start(_start.x,_start.y,_start.z),end(_end.x,_end.y,_end.z){
Vector3 _startP(0,0,0);
Vector3 _startEnd(0,0,0);
}
Vector3 start, end;
void closestPointToPoint(Vector3 point, bool clampToLine, Vector3& target){
double t = closestPointToPointParameter(point, clampToLine);
delta(target).multiplyScalar(t).add(start);
}
double closestPointToPointParameter(Vector3 point, bool clampToLine){
_startP.subVectors(point, start);
_startEnd.subVectors(end, start);
double startEnd2 = _startEnd.dot(_startEnd);
double startEnd_startP = _startEnd.dot(_startP);
double t = startEnd_startP/startEnd2;
if(clampToLine){
t = std::max(0.0,std::min(1.0,t));
}
return t;
}
Vector3& delta(Vector3& target){
return target.subVectors(end, start);
}
private:
Vector3 _startP, _startEnd;
};
#endif