forked from gazebosim/gz-math
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSphericalCoordinates.cc
559 lines (468 loc) · 17.3 KB
/
SphericalCoordinates.cc
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/*
* Copyright (C) 2012 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <string>
#include "ignition/math/Matrix3.hh"
#include "ignition/math/SphericalCoordinates.hh"
using namespace ignition;
using namespace math;
// Parameters for EARTH_WGS84 model
// wikipedia: World_Geodetic_System#A_new_World_Geodetic_System:_WGS_84
// a: Equatorial radius. Semi-major axis of the WGS84 spheroid (meters).
const double g_EarthWGS84AxisEquatorial = 6378137.0;
// b: Polar radius. Semi-minor axis of the wgs84 spheroid (meters).
const double g_EarthWGS84AxisPolar = 6356752.314245;
// if: WGS84 inverse flattening parameter (no units)
const double g_EarthWGS84Flattening = 1.0/298.257223563;
// Radius of the Earth (meters).
const double g_EarthRadius = 6371000.0;
// Private data for the SphericalCoordinates class.
class ignition::math::SphericalCoordinatesPrivate
{
/// \brief Type of surface being used.
public: SphericalCoordinates::SurfaceType surfaceType;
/// \brief Latitude of reference point.
public: ignition::math::Angle latitudeReference;
/// \brief Longitude of reference point.
public: ignition::math::Angle longitudeReference;
/// \brief Elevation of reference point relative to sea level in meters.
public: double elevationReference;
/// \brief Heading offset, expressed as angle from East to
/// gazebo x-axis, or equivalently from North to gazebo y-axis.
public: ignition::math::Angle headingOffset;
/// \brief Semi-major axis ellipse parameter
public: double ellA;
/// \brief Semi-minor axis ellipse parameter
public: double ellB;
/// \brief Flattening ellipse parameter
public: double ellF;
/// \brief First eccentricity ellipse parameter
public: double ellE;
/// \brief Second eccentricity ellipse parameter
public: double ellP;
/// \brief Rotation matrix that moves ECEF to GLOBAL
public: ignition::math::Matrix3d rotECEFToGlobal;
/// \brief Rotation matrix that moves GLOBAL to ECEF
public: ignition::math::Matrix3d rotGlobalToECEF;
/// \brief Cache the ECEF position of the the origin
public: ignition::math::Vector3d origin;
/// \brief Cache cosine head transform
public: double cosHea;
/// \brief Cache sine head transform
public: double sinHea;
};
//////////////////////////////////////////////////
SphericalCoordinates::SurfaceType SphericalCoordinates::Convert(
const std::string &_str)
{
if ("EARTH_WGS84" == _str)
return EARTH_WGS84;
std::cerr << "SurfaceType string not recognized, "
<< "EARTH_WGS84 returned by default" << std::endl;
return EARTH_WGS84;
}
//////////////////////////////////////////////////
SphericalCoordinates::SphericalCoordinates()
: dataPtr(new SphericalCoordinatesPrivate)
{
this->SetSurface(EARTH_WGS84);
this->SetElevationReference(0.0);
}
//////////////////////////////////////////////////
SphericalCoordinates::SphericalCoordinates(const SurfaceType _type)
: dataPtr(new SphericalCoordinatesPrivate)
{
this->SetSurface(_type);
this->SetElevationReference(0.0);
}
//////////////////////////////////////////////////
SphericalCoordinates::SphericalCoordinates(const SurfaceType _type,
const ignition::math::Angle &_latitude,
const ignition::math::Angle &_longitude,
const double _elevation,
const ignition::math::Angle &_heading)
: dataPtr(new SphericalCoordinatesPrivate)
{
// Set the reference and calculate ellipse parameters
this->SetSurface(_type);
// Set the coordinate transform parameters
this->dataPtr->latitudeReference = _latitude;
this->dataPtr->longitudeReference = _longitude;
this->dataPtr->elevationReference = _elevation;
this->dataPtr->headingOffset = _heading;
// Generate transformation matrix
this->UpdateTransformationMatrix();
}
//////////////////////////////////////////////////
SphericalCoordinates::SphericalCoordinates(const SphericalCoordinates &_sc)
: SphericalCoordinates()
{
(*this) = _sc;
}
//////////////////////////////////////////////////
SphericalCoordinates::~SphericalCoordinates()
{
}
//////////////////////////////////////////////////
SphericalCoordinates::SurfaceType SphericalCoordinates::Surface() const
{
return this->dataPtr->surfaceType;
}
//////////////////////////////////////////////////
ignition::math::Angle SphericalCoordinates::LatitudeReference() const
{
return this->dataPtr->latitudeReference;
}
//////////////////////////////////////////////////
ignition::math::Angle SphericalCoordinates::LongitudeReference() const
{
return this->dataPtr->longitudeReference;
}
//////////////////////////////////////////////////
double SphericalCoordinates::ElevationReference() const
{
return this->dataPtr->elevationReference;
}
//////////////////////////////////////////////////
ignition::math::Angle SphericalCoordinates::HeadingOffset() const
{
return this->dataPtr->headingOffset;
}
//////////////////////////////////////////////////
void SphericalCoordinates::SetSurface(const SurfaceType &_type)
{
this->dataPtr->surfaceType = _type;
switch (this->dataPtr->surfaceType)
{
case EARTH_WGS84:
{
// Set the semi-major axis
this->dataPtr->ellA = g_EarthWGS84AxisEquatorial;
// Set the semi-minor axis
this->dataPtr->ellB = g_EarthWGS84AxisPolar;
// Set the flattening parameter
this->dataPtr->ellF = g_EarthWGS84Flattening;
// Set the first eccentricity ellipse parameter
// https://en.wikipedia.org/wiki/Eccentricity_(mathematics)#Ellipses
this->dataPtr->ellE = sqrt(1.0 -
std::pow(this->dataPtr->ellB, 2) / std::pow(this->dataPtr->ellA, 2));
// Set the second eccentricity ellipse parameter
// https://en.wikipedia.org/wiki/Eccentricity_(mathematics)#Ellipses
this->dataPtr->ellP = sqrt(
std::pow(this->dataPtr->ellA, 2) / std::pow(this->dataPtr->ellB, 2) -
1.0);
break;
}
default:
{
std::cerr << "Unknown surface type["
<< this->dataPtr->surfaceType << "]\n";
break;
}
}
}
//////////////////////////////////////////////////
void SphericalCoordinates::SetLatitudeReference(
const ignition::math::Angle &_angle)
{
this->dataPtr->latitudeReference = _angle;
this->UpdateTransformationMatrix();
}
//////////////////////////////////////////////////
void SphericalCoordinates::SetLongitudeReference(
const ignition::math::Angle &_angle)
{
this->dataPtr->longitudeReference = _angle;
this->UpdateTransformationMatrix();
}
//////////////////////////////////////////////////
void SphericalCoordinates::SetElevationReference(const double _elevation)
{
this->dataPtr->elevationReference = _elevation;
this->UpdateTransformationMatrix();
}
//////////////////////////////////////////////////
void SphericalCoordinates::SetHeadingOffset(const ignition::math::Angle &_angle)
{
this->dataPtr->headingOffset.Radian(_angle.Radian());
this->UpdateTransformationMatrix();
}
//////////////////////////////////////////////////
ignition::math::Vector3d SphericalCoordinates::SphericalFromLocalPosition(
const ignition::math::Vector3d &_xyz) const
{
ignition::math::Vector3d result =
this->PositionTransform(_xyz, LOCAL, SPHERICAL);
result.X(IGN_RTOD(result.X()));
result.Y(IGN_RTOD(result.Y()));
return result;
}
//////////////////////////////////////////////////
ignition::math::Vector3d SphericalCoordinates::LocalFromSphericalPosition(
const ignition::math::Vector3d &_xyz) const
{
ignition::math::Vector3d result = _xyz;
result.X(IGN_DTOR(result.X()));
result.Y(IGN_DTOR(result.Y()));
return this->PositionTransform(result, SPHERICAL, LOCAL);
}
//////////////////////////////////////////////////
ignition::math::Vector3d SphericalCoordinates::GlobalFromLocalVelocity(
const ignition::math::Vector3d &_xyz) const
{
return this->VelocityTransform(_xyz, LOCAL, GLOBAL);
}
//////////////////////////////////////////////////
ignition::math::Vector3d SphericalCoordinates::LocalFromGlobalVelocity(
const ignition::math::Vector3d &_xyz) const
{
return this->VelocityTransform(_xyz, GLOBAL, LOCAL);
}
//////////////////////////////////////////////////
/// Based on Haversine formula (http://en.wikipedia.org/wiki/Haversine_formula).
double SphericalCoordinates::Distance(const ignition::math::Angle &_latA,
const ignition::math::Angle &_lonA,
const ignition::math::Angle &_latB,
const ignition::math::Angle &_lonB)
{
ignition::math::Angle dLat = _latB - _latA;
ignition::math::Angle dLon = _lonB - _lonA;
double a = sin(dLat.Radian() / 2) * sin(dLat.Radian() / 2) +
sin(dLon.Radian() / 2) * sin(dLon.Radian() / 2) *
cos(_latA.Radian()) * cos(_latB.Radian());
double c = 2 * atan2(sqrt(a), sqrt(1 - a));
double d = g_EarthRadius * c;
return d;
}
//////////////////////////////////////////////////
void SphericalCoordinates::UpdateTransformationMatrix()
{
// Cache trig results
double cosLat = cos(this->dataPtr->latitudeReference.Radian());
double sinLat = sin(this->dataPtr->latitudeReference.Radian());
double cosLon = cos(this->dataPtr->longitudeReference.Radian());
double sinLon = sin(this->dataPtr->longitudeReference.Radian());
// Create a rotation matrix that moves ECEF to GLOBAL
// http://www.navipedia.net/index.php/
// Transformations_between_ECEF_and_ENU_coordinates
this->dataPtr->rotECEFToGlobal = ignition::math::Matrix3d(
-sinLon, cosLon, 0.0,
-cosLon * sinLat, -sinLon * sinLat, cosLat,
cosLon * cosLat, sinLon * cosLat, sinLat);
// Create a rotation matrix that moves GLOBAL to ECEF
// http://www.navipedia.net/index.php/
// Transformations_between_ECEF_and_ENU_coordinates
this->dataPtr->rotGlobalToECEF = ignition::math::Matrix3d(
-sinLon, -cosLon * sinLat, cosLon * cosLat,
cosLon, -sinLon * sinLat, sinLon * cosLat,
0, cosLat, sinLat);
// Cache heading transforms -- note that we have to negate the heading in
// order to preserve backward compatibility. ie. Gazebo has traditionally
// expressed positive angle as a CLOCKWISE rotation that takes the GLOBAL
// frame to the LOCAL frame. However, right hand coordinate systems require
// this to be expressed as an ANTI-CLOCKWISE rotation. So, we negate it.
this->dataPtr->cosHea = cos(-this->dataPtr->headingOffset.Radian());
this->dataPtr->sinHea = sin(-this->dataPtr->headingOffset.Radian());
// Cache the ECEF coordinate of the origin
this->dataPtr->origin = ignition::math::Vector3d(
this->dataPtr->latitudeReference.Radian(),
this->dataPtr->longitudeReference.Radian(),
this->dataPtr->elevationReference);
this->dataPtr->origin =
this->PositionTransform(this->dataPtr->origin, SPHERICAL, ECEF);
}
/////////////////////////////////////////////////
ignition::math::Vector3d SphericalCoordinates::PositionTransform(
const ignition::math::Vector3d &_pos,
const CoordinateType &_in, const CoordinateType &_out) const
{
ignition::math::Vector3d tmp = _pos;
// Cache trig results
double cosLat = cos(_pos.X());
double sinLat = sin(_pos.X());
double cosLon = cos(_pos.Y());
double sinLon = sin(_pos.Y());
// Radius of planet curvature (meters)
double curvature = 1.0 -
this->dataPtr->ellE * this->dataPtr->ellE * sinLat * sinLat;
curvature = this->dataPtr->ellA / sqrt(curvature);
// Convert whatever arrives to a more flexible ECEF coordinate
switch (_in)
{
// East, North, Up (ENU), note no break at end of case
case LOCAL:
{
tmp.X(-_pos.X() * this->dataPtr->cosHea + _pos.Y() *
this->dataPtr->sinHea);
tmp.Y(-_pos.X() * this->dataPtr->sinHea - _pos.Y() *
this->dataPtr->cosHea);
}
/* Falls through. */
case GLOBAL:
{
tmp = this->dataPtr->origin + this->dataPtr->rotGlobalToECEF * tmp;
break;
}
case SPHERICAL:
{
tmp.X((_pos.Z() + curvature) * cosLat * cosLon);
tmp.Y((_pos.Z() + curvature) * cosLat * sinLon);
tmp.Z(((this->dataPtr->ellB * this->dataPtr->ellB)/
(this->dataPtr->ellA * this->dataPtr->ellA) *
curvature + _pos.Z()) * sinLat);
break;
}
// Do nothing
case ECEF:
break;
default:
{
std::cerr << "Invalid coordinate type[" << _in << "]\n";
return _pos;
}
}
// Convert ECEF to the requested output coordinate system
switch (_out)
{
case SPHERICAL:
{
// Convert from ECEF to SPHERICAL
double p = sqrt(tmp.X() * tmp.X() + tmp.Y() * tmp.Y());
double theta = atan((tmp.Z() * this->dataPtr->ellA) /
(p * this->dataPtr->ellB));
// Calculate latitude and longitude
double lat = atan(
(tmp.Z() + std::pow(this->dataPtr->ellP, 2) * this->dataPtr->ellB *
std::pow(sin(theta), 3)) /
(p - std::pow(this->dataPtr->ellE, 2) *
this->dataPtr->ellA * std::pow(cos(theta), 3)));
double lon = atan2(tmp.Y(), tmp.X());
// Recalculate radius of planet curvature at the current latitude.
double nCurvature = 1.0 - std::pow(this->dataPtr->ellE, 2) *
std::pow(sin(lat), 2);
nCurvature = this->dataPtr->ellA / sqrt(nCurvature);
tmp.X(lat);
tmp.Y(lon);
// Now calculate Z
tmp.Z(p/cos(lat) - nCurvature);
break;
}
// Convert from ECEF TO GLOBAL
case GLOBAL:
tmp = this->dataPtr->rotECEFToGlobal * (tmp - this->dataPtr->origin);
break;
// Convert from ECEF TO LOCAL
case LOCAL:
tmp = this->dataPtr->rotECEFToGlobal * (tmp - this->dataPtr->origin);
tmp = ignition::math::Vector3d(
tmp.X() * this->dataPtr->cosHea - tmp.Y() * this->dataPtr->sinHea,
tmp.X() * this->dataPtr->sinHea + tmp.Y() * this->dataPtr->cosHea,
tmp.Z());
break;
// Return ECEF (do nothing)
case ECEF:
break;
default:
std::cerr << "Unknown coordinate type[" << _out << "]\n";
return _pos;
}
return tmp;
}
//////////////////////////////////////////////////
ignition::math::Vector3d SphericalCoordinates::VelocityTransform(
const ignition::math::Vector3d &_vel,
const CoordinateType &_in, const CoordinateType &_out) const
{
// Sanity check -- velocity should not be expressed in spherical coordinates
if (_in == SPHERICAL || _out == SPHERICAL)
{
return _vel;
}
// Intermediate data type
ignition::math::Vector3d tmp = _vel;
// First, convert to an ECEF vector
switch (_in)
{
// ENU (note no break at end of case)
case LOCAL:
tmp.X(-_vel.X() * this->dataPtr->cosHea + _vel.Y() *
this->dataPtr->sinHea);
tmp.Y(-_vel.X() * this->dataPtr->sinHea - _vel.Y() *
this->dataPtr->cosHea);
/* Falls through. */
// spherical
case GLOBAL:
tmp = this->dataPtr->rotGlobalToECEF * tmp;
break;
// Do nothing
case ECEF:
tmp = _vel;
break;
default:
std::cerr << "Unknown coordinate type[" << _in << "]\n";
return _vel;
}
// Then, convert to the request coordinate type
switch (_out)
{
// ECEF, do nothing
case ECEF:
break;
// Convert from ECEF to global
case GLOBAL:
tmp = this->dataPtr->rotECEFToGlobal * tmp;
break;
// Convert from ECEF to local
case LOCAL:
tmp = this->dataPtr->rotECEFToGlobal * tmp;
tmp = ignition::math::Vector3d(
tmp.X() * this->dataPtr->cosHea - tmp.Y() * this->dataPtr->sinHea,
tmp.X() * this->dataPtr->sinHea + tmp.Y() * this->dataPtr->cosHea,
tmp.Z());
break;
default:
std::cerr << "Unknown coordinate type[" << _out << "]\n";
return _vel;
}
return tmp;
}
//////////////////////////////////////////////////
bool SphericalCoordinates::operator==(const SphericalCoordinates &_sc) const
{
return this->Surface() == _sc.Surface() &&
this->LatitudeReference() == _sc.LatitudeReference() &&
this->LongitudeReference() == _sc.LongitudeReference() &&
math::equal(this->ElevationReference(), _sc.ElevationReference()) &&
this->HeadingOffset() == _sc.HeadingOffset();
}
//////////////////////////////////////////////////
bool SphericalCoordinates::operator!=(const SphericalCoordinates &_sc) const
{
return !(*this == _sc);
}
//////////////////////////////////////////////////
SphericalCoordinates &SphericalCoordinates::operator=(
const SphericalCoordinates &_sc)
{
this->SetSurface(_sc.Surface());
this->SetLatitudeReference(_sc.LatitudeReference());
this->SetLongitudeReference(_sc.LongitudeReference());
this->SetElevationReference(_sc.ElevationReference());
this->SetHeadingOffset(_sc.HeadingOffset());
// Generate transformation matrix
this->UpdateTransformationMatrix();
return *this;
}