2 Earth Centered Earth Fixed (ECEF) WGS84 Navigation Utility Copyright © 2013-2017 Infinite Delta Corp
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser 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.
9 This program 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.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <ecef-quaternion.h>
21 #include <ecef-wgs84.h>
25 static bool tst(const double i, const double j=0, const double t=0.0000001)
26 {if (i>=j ? i-j <= t : j-i <= t) return true;
27 std::cout << i << " " << j << " " << i-j << " " << t << " ";
31 #define ERR(e,m) {std::cout << "Test Error: \"" << m << "\" in " << __FILE__ << ":" << __LINE__ << std::endl; return e;}
33 #define LL_INC wgs84Const::maxNorth/1000.0
40 if (pos.x != 6378137.0) ERR(1, "Default X is not Wgs84 Radius");
41 if (pos.y != 0.0) ERR(1, "Default Y is not zero");
42 if (pos.z != 0.0) ERR(1, "Default Z is not zero");
43 // std::cout << pos.x << std::endl;
44 // std::cout << pos.y << std::endl;
45 // std::cout << pos.z << std::endl;
47 if (vec(1.0, 0.0, 0.0) + vec(0.0, 2.0, 0.0) + vec(0.0, 0.0, 3.0) !=
48 vec(1.0, 2.0, 3.0)) ERR(1, "Bad vector addition");
50 if (vec(1.0, 0.0, 0.0) - vec(2.0, 3.0, 0.0) - vec(0.0, 0.0, 4.0) !=
51 vec(-1.0, -3.0, -4.0)) ERR(1, "Bad vector subtraction");
53 if (vec(1.0, 2.0, 3.0).dot(vec(4.0, 5.0, 6.0)) != 32.0) ERR(1, "Bad vector dot product");
55 if (vec(1.0, 0.0, 0.0).cross(vec(0.0, 1.0, 0.0)) != vec(0.0, 0.0, 1.0))
56 ERR(1, "Bad vector cross product");
58 if (vec(0.0, 1.0, 0.0).cross(vec(0.0, 1.0, 0.0)) != vec(0.0, 0.0, 0.0))
59 ERR(1, "Bad vector cross product");
61 if (vec(0.0, 0.0, 1.0).cross(vec(0.0, 1.0, 0.0)) != vec(-1.0, 0.0, 0.0))
62 ERR(1, "Bad vector cross product");
64 // std::cout << pos.dist(vec(6378137.0, 0.0, 0.0)) << std::endl;
66 if (!(pos == vec(6378137.0, 0.0, 0.0))) ERR(1, "Default pos is not prime meridian");
67 if (pos != vec(6378137.0, 0.0, 0.0)) ERR(1, "Default pos is not prime meridian");
69 // std::cout << cent.dist(pos)- 6378137.0 << std::endl;
71 if (cent.dist(pos) != 6378137.0) ERR(1, "Equator Radius not Wgs84");
72 if (wgs84Const::c != 6356752.3142451792954l) ERR(1, "Minor axis not Wgs84");
74 // std::cout << wgs84(wgs84Const::maxNorth, 0.0).x << std::endl;
75 // std::cout << wgs84(wgs84Const::maxNorth, 0.0).y << std::endl;
76 // std::cout << wgs84(wgs84Const::maxNorth, 0.0).z << std::endl;
78 // std::cout << a * (1-1/fr) << std::endl;
80 if (wgs84(wgs84Const::maxNorth, 0.0).z != wgs84Const::c) ERR(1, "Bad North");
81 if (!tst(wgs84(wgs84Const::maxNorth, 0.0).z, 6356752.31424518l)) ERR(1, "Bad North");
83 for (double lon=wgs84Const::maxEast; lon > wgs84Const::maxWest; lon -= LL_INC)
85 if (!tst(cent.dist(wgs84(0.0, lon)), 6378137.0))
87 std::cout << lon << ": " << cent.dist(wgs84(0.0, lon)) - 6378137.0 << std::endl;
89 ERR(1, "World Equator Radius not Wgs84");
100 // line l1(vec(6378137.0/2,0,0));
101 // line l1(vec(6000000.0,0,0));
102 // line l2(count, l1);
104 // std::cout << count << std::endl;
105 // std::cout << l2.p1.x << std::endl;
106 // std::cout << l2.p1.y << std::endl;
107 // std::cout << l2.p1.z << std::endl;
108 // std::cout << l2.p2.x << std::endl;
109 // std::cout << l2.p2.y << std::endl;
110 // std::cout << l2.p2.z << std::endl;
112 if (line.trimToEllipsoid() != 2) ERR(2, "WGS84 Crossing count");
113 if (line.p[0] != vec(-6378137.0,0,0)) ERR(2, "WGS84 Crossing");
114 if (line.p[1] != vec( 6378137.0,0,0)) ERR(2, "WGS84 Crossing");
116 line = wgs84Line(vec(0,1,0));
117 if (line.trimToEllipsoid() != 2) ERR(2, "WGS84 Crossing count");
118 // std::cout << " " << line.p[0].x << std::endl;
119 // std::cout << " " << line.p[0].y << std::endl;
120 // std::cout << " " << line.p[0].z << std::endl;
121 // std::cout << std::endl;
122 if (line.p[0] != vec(0,-6378137.0,0)) ERR(2, "WGS84 Crossing");
123 if (line.p[1] != vec(0, 6378137.0,0)) ERR(2, "WGS84 Crossing");
125 line = wgs84Line(vec(0,0,1));
126 if (line.trimToEllipsoid() != 2) ERR(2, "WGS84 Crossing count");
127 if (line.p[0] != vec(0,0,-6356752.31424518)) ERR(2, "WGS84 Crossing");
128 if (line.p[1] != vec(0,0, 6356752.31424518)) ERR(2, "WGS84 Crossing");
130 for (double lon=wgs84Const::maxEast; lon > wgs84Const::maxWest; lon -= LL_INC)
132 for (double lat=wgs84Const::maxNorth; lat > wgs84Const::maxSouth; lat -= LL_INC)
134 wgs84Line l1(wgs84(lat,lon), wgs84(wgs84Const::maxNorth/2, wgs84Const::maxNorth));
136 int count = l2.trimToEllipsoid();
137 if (count > 0 && l1.p[0] != l2.p[0]) ERR(2, "WGS84 robust");
138 if (count > 1 && l1.p[1] != l2.p[1]) ERR(2, "WGS84 robust");
140 wgs84Line l3(wgs84(lat,lon));
141 wgs84Line l5(l3.p[1]/33.0);
142 l5.trimToEllipsoid();
144 if (l3.p[1]*-1 != l5.p[0]) ERR(2, "WGS84 trim robust");
145 if (l3.p[1] != l5.p[1]) ERR(2, "WGS84 trim robust");
148 vec p1(wgs84(lat,lon));
151 line l6(count, line(p1, p2));
154 std::cout << p1.x << std::endl;
155 std::cout << p1.y << std::endl;
156 std::cout << p1.z << std::endl;
157 std::cout << l6.p1.x << std::endl;
158 std::cout << l6.p1.y << std::endl;
159 std::cout << l6.p1.z << std::endl;
160 ERR(2, "WGS84 robust");
166 std::cout << "Total " << total << std::endl;
172 for (double lon=wgs84Const::maxEast; lon > wgs84Const::maxWest; lon -= LL_INC)
174 wgs84 npole(wgs84Const::maxNorth,lon);
175 if (!tst(0, npole.lon()*1800/M_PIl)) ERR(4, "Bad Longitude");
176 wgs84 spole(wgs84Const::maxSouth,lon);
177 if (!tst(0, spole.lon()*1800/M_PIl)) ERR(4, "Bad Longitude");
179 for (double lat=wgs84Const::maxNorth-LL_INC; lat >= wgs84Const::maxSouth+LL_INC; lat -= LL_INC)
182 if (!tst(lon*1800/M_PIl, t.lon()*1800/M_PIl)) ERR(4, "Bad Longitude");
192 if (!tst(acos(t.cos(vec(1,1,0)))*180/M_PIl, 45)) ERR(4, "Bad Cos");
193 if (!tst(acos(t.cos(vec(1,0,1)))*180/M_PIl, 45)) ERR(4, "Bad Cos");
194 if (!tst(acos(t.cos(vec(1,0,0)))*180/M_PIl, 0)) ERR(4, "Bad Cos");
195 if (!tst(acos(t.cos(vec(1,-1,0)))*180/M_PIl, 45)) ERR(4, "Bad Cos");
196 if (!tst(acos(t.cos(vec(1,0,-1)))*180/M_PIl, 45)) ERR(4, "Bad Cos");
197 if (!tst(acos(t.cos(vec(-1,0,0)))*180/M_PIl, 180)) ERR(4, "Bad Cos");
198 if (!tst(acos(t.cos(vec(0,1,0)))*180/M_PIl, 90)) ERR(4, "Bad Cos");
199 if (!tst(acos(t.cos(vec(0,0,1)))*180/M_PIl, 90)) ERR(4, "Bad Cos");
200 if (!tst(acos(t.cos(vec(0,-1,0)))*180/M_PIl, 90)) ERR(4, "Bad Cos");
201 if (!tst(acos(t.cos(vec(0,0,-1)))*180/M_PIl, 90)) ERR(4, "Bad Cos");
203 for (double lon=wgs84Const::maxEast; lon > wgs84Const::maxWest; lon -= LL_INC*100)
205 // std::cout << lon*1800/M_PIl << std::endl;
206 for (double lat=wgs84Const::maxNorth; lat >= wgs84Const::maxSouth; lat -= LL_INC)
208 wgs84Fast t(lat,lon), n(t.north());
209 if (!tst(lat*1800/M_PIl, t.lat()*1800/M_PIl)) ERR(4, "Bad Lat");
211 if (!tst(abs(t.sinLat()), n.distZ()/n.dist())) ERR(4, "Bad North vector");
213 wgs84Fast t2(lat,-85,10000);
214 if (!tst(lat*1800/M_PIl, t2.lat()*1800/M_PIl, 0.0035)) ERR(4, "Bad Lat at alt");
223 for (double lat=wgs84Const::maxNorth; lat > wgs84Const::maxSouth; lat -= LL_INC)
225 wgs84Fast pnt(lat,0);
226 // std::cout << " " << lat*1800/M_PIl;
227 // std::cout << pnt.x - FindX2D(pnt.z);
228 // std::cout << " " << Slope2D(pnt.z);
229 // std::cout << " " << tan(lat);
230 // std::cout << " " << tan(lat) - Slope2D(pnt.z);
231 // if (!tst(pnt.x, pnt.rotationRadius(), 0.00001)) ERR(4, "Bad radius");
232 if (!tst(pnt.x, pnt.distZ())) ERR(4, "Bad radius");
233 //tlz if (!tst(tan(lat), pnt.tanLat())) ERR(4, "Bad Tan");
234 //tlz if (!tst(pnt.x, tan(lat)*(pnt.z - TanZCrossing(pnt.z)), 0.000001)) ERR(4, "Bad Z0");
235 if (!tst(lat, asin(pnt.sinLat()), 0.0000001)) ERR(4, "Bad WGS84 Sin");
237 pnt = wgs84(lat,0,10000);
238 // std::cout << " " << lat*1800/M_PIl;
239 // std::cout << " " << pnt.x;
240 // std::cout << " " << pnt.y;
241 // std::cout << " " << pnt.z;
242 // std::cout << " " << TanZCrossing(pnt.z);
243 // std::cout << " " << tan(lat);
244 // std::cout << " " << (find2dX(pnt.z) - find2dX(pnt.z+0.1))/0.1;
245 // std::cout << " " << pnt.x / (pnt.z - TanZCrossing(pnt.z));
246 // std::cout << " " << pnt.z;
247 // std::cout << " " << WGS84EarthCenter(pnt.z);
248 // std::cout << std::endl;
249 if (!tst(lat, asin(pnt.sinLat()), 0.00001)) ERR(4, "Bad WGS84 Sin");
251 // if (lat > wgs84Const::maxNorth-0.2) continue; // Skip near polls TanZCrossing is better!
252 // if (lat < wgs84Const::maxSouth+0.2) continue; // Skip near polls TanZCrossing is better!
254 // if (!tst(pnt.x, tan(lat)*(pnt.z - TanZCrossing(pnt.z)), 0.1)) ERR(4, "Bad Z0");
264 q = quat(0, vec(0,0,0)).versor();
265 if (!tst(q.a, 0)) ERR(5, "Bad Quat Versor Init");
266 if (!tst(q.v.x, 0)) ERR(5, "Bad Quat Versor Init");
267 if (!tst(q.v.y, 0)) ERR(5, "Bad Quat Versor Init");
268 if (!tst(q.v.z, 1)) ERR(5, "Bad Quat Versor Init");
270 q = quat(3, vec(4,5,6)).conj();
271 if (!tst(q.a, 3)) ERR(5, "Bad Quat Conj Angle");
272 if (!tst(q.v.x, -4)) ERR(5, "Bad Quat Conj X");
273 if (!tst(q.v.y, -5)) ERR(5, "Bad Quat Conj Y");
274 if (!tst(q.v.z, -6)) ERR(5, "Bad Quat Conj Z");
275 if (!tst(q.mag(), sqrt(86.0))) ERR(5, "Bad Quat Magnitude");
276 if (!tst(q.versor().mag(), 1)) ERR(5, "Bad Quat Versor");
278 q = quat(0, zAxis) * quat(0, zAxis);
279 if (!tst(q.a, -1)) ERR(5, "Bad Quat Angle");
280 if (!tst(q.v.x, 0)) ERR(5, "Bad Quat X");
281 if (!tst(q.v.y, 0)) ERR(5, "Bad Quat Y");
282 if (!tst(q.v.z, 0)) ERR(5, "Bad Quat Z");
284 q = quat(0, zAxis) * quat(0, vec(1,0,0));
285 if (!tst(q.a, 0)) ERR(5, "Bad Quat Angle");
286 if (!tst(q.v.x, 0)) ERR(5, "Bad Quat X");
287 if (!tst(q.v.y, 1)) ERR(5, "Bad Quat Y");
288 if (!tst(q.v.z, 0)) ERR(5, "Bad Quat Z");
290 // q = quat(cos(M_PI_2l/2), zAxis) * quat(cos(M_PI_2l/2), zAxis);
291 // q = quat(cos(M_PI_2l/2), zAxis * sin(M_PI_2l/2));
293 q = quat(cos(M_PI_4l/2), zAxis).normVec();
296 if (!tst(q.a, cos(M_PI_4l))) ERR(5, "Bad Quat Hamilton Multiple Angle");
297 if (!tst(q.v.x, 0)) ERR(5, "Bad Quat Mult X");
298 if (!tst(q.v.y, 0)) ERR(5, "Bad Quat Mult Y");
299 if (!tst(q.v.norm().z, 1)) ERR(5, "Bad Quat Z");
301 q = quat(cos(M_PI_4l/4), zAxis).normVec() * quat(cos(M_PI_4l/2), zAxis).normVec();
302 if (!tst(q.a, cos(M_PI_4l * 3/4))) ERR(5, "Bad Quat Hamilton Multiple Angle");
303 if (!tst(q.v.x, 0)) ERR(5, "Bad Quat Mult X");
304 if (!tst(q.v.y, 0)) ERR(5, "Bad Quat Mult Y");
305 if (!tst(q.v.norm().z, 1)) ERR(5, "Bad Quat Z");
307 q = quat(cos(M_PI_2l), zAxis).normVec() * quat(cos(M_PI_2l), vec(0,1,0)).normVec();
308 if (!tst(q.a, cos(M_PI_2l))) ERR(5, "Bad Quat Hamilton Multiple Angle");
309 if (!tst(q.v.x, -1)) ERR(5, "Bad Quat Mult X");
310 if (!tst(q.v.y, 0)) ERR(5, "Bad Quat Mult Y");
311 if (!tst(q.v.z, 0)) ERR(5, "Bad Quat Mult Z");
313 q = quat(cos(M_PI_4l), zAxis).normVec() * quat(cos(M_PI_4l), vec(0,1,0)).normVec();
314 if (!tst(q.a, cos(M_PIl/3))) ERR(5, "Bad Quat Hamilton Multiple Angle");
315 if (!tst(q.v.x, -0.5)) ERR(5, "Bad Quat Mult X");
316 if (!tst(q.v.y, 0.5)) ERR(5, "Bad Quat Mult Y");
317 if (!tst(q.v.z, 0.5)) ERR(5, "Bad Quat Mult Z");
319 // std::cout << " " << q.a;
320 // std::cout << " " << acos(q.a) * 180 / M_PIl;
321 // std::cout << " " << q.v.x;
322 // std::cout << " " << q.v.y;
323 // std::cout << " " << q.v.z;
324 // std::cout << std::endl;
332 quat q(cos(M_PI_4l), zAxis);
336 if (!tst(v.x, 0)) ERR(5, "Bad Quat rotate X");
337 if (!tst(v.y, 1)) ERR(5, "Bad Quat rotate Y");
338 if (!tst(v.z, 0)) ERR(5, "Bad Quat rotate Z");
341 if (!tst(v.x, -1)) ERR(5, "Bad Quat rotate X");
342 if (!tst(v.y, 0)) ERR(5, "Bad Quat rotate Y");
343 if (!tst(v.z, 0)) ERR(5, "Bad Quat rotate Z");
346 if (!tst(v.x, 0)) ERR(5, "Bad Quat rotate X");
347 if (!tst(v.y, 0)) ERR(5, "Bad Quat rotate Y");
348 if (!tst(v.z, 1)) ERR(5, "Bad Quat rotate Z");
351 if (!tst(v.x, -1)) ERR(5, "Bad Quat rotate X");
352 if (!tst(v.y, 1)) ERR(5, "Bad Quat rotate Y");
353 if (!tst(v.z, 0)) ERR(5, "Bad Quat rotate Z");
355 q = quat(cos(M_PI_4l/2), zAxis).normVec();
357 if (!tst(v.x, sqrt(0.5))) ERR(5, "Bad Quat rotate X");
358 if (!tst(v.y, sqrt(0.5))) ERR(5, "Bad Quat rotate Y");
359 if (!tst(v.z, 0)) ERR(5, "Bad Quat rotate Z");
361 q = quat(cos(30.0 * M_PI_2l/180), zAxis).normVec();
363 //if (!tst(v.x, sqrt(0.5))) ERR(5, "Bad Quat rotate X");
364 if (!tst(v.y, 2.5)) ERR(5, "Bad Quat rotate Y");
365 if (!tst(v.z, 0)) ERR(5, "Bad Quat rotate Z");
368 std::cout << " " << v.x;
369 std::cout << " " << v.y;
370 std::cout << " " << v.z;
371 std::cout << std::endl;
377 int quaternionAroundWorldTest()
379 wgs84 start(42.89, 85.56), p; // 42.89°N 85.56°W
380 quat q = quat(cos(M_PI_2l/18000), zAxis).normVec(); // Around the world at this latitude.
383 for (int i=0; i<36000; ++i) p = q * p;
384 if ((start - p).dist() > 0.1) ERR(6, "Around world on latitude Quaternion test");
386 q = quat(cos(M_PI_2l/18000), zAxis.cross(start)).normVec(); // Polar around the world
388 for (int i=0; i<36000; ++i) p = q * p;
389 if ((start - p).dist() > 0.25) ERR(6, "Polar around world Quaternion test");
391 q = quat(cos(M_PI_2l/18000), wgs84(0,0).cross(start)).normVec(); // Around world through N0, E0
393 for (int i=0; i<36000; ++i) p = q * p;
394 if ((start - p).dist() > 0.25) ERR(6, "Around world through N0, W0 Quaternion test");
397 std::cout << " " << p.x;
398 std::cout << " " << p.y;
399 std::cout << " " << p.z;
400 std::cout << " " << (start - p).dist();
401 std::cout << std::endl;
414 std::cout.precision(14);
415 std::cout << std::scientific;
417 std::cout << "starting ecef-wgs84_test..." << std::endl;
419 err |= ecef::tstLon();
422 err |= ecef::tstCos();
425 err |= ecef::qvtst();
426 err |= ecef::quaternionAroundWorldTest();
428 std::cout << "finishing ecef-wgs84_test." << std::endl;