GeographicLib  1.35
Geocentric.cpp
Go to the documentation of this file.
1 /**
2  * \file Geocentric.cpp
3  * \brief Implementation for GeographicLib::Geocentric class
4  *
5  * Copyright (c) Charles Karney (2008-2011) <charles@karney.com> and licensed
6  * under the MIT/X11 License. For more information, see
7  * http://geographiclib.sourceforge.net/
8  **********************************************************************/
9 
11 
12 namespace GeographicLib {
13 
14  using namespace std;
15 
16  Geocentric::Geocentric(real a, real f)
17  : _a(a)
18  , _f(f <= 1 ? f : 1/f)
19  , _e2(_f * (2 - _f))
20  , _e2m(Math::sq(1 - _f)) // 1 - _e2
21  , _e2a(abs(_e2))
22  , _e4a(Math::sq(_e2))
23  , _maxrad(2 * _a / numeric_limits<real>::epsilon())
24  {
25  if (!(Math::isfinite(_a) && _a > 0))
26  throw GeographicErr("Major radius is not positive");
27  if (!(Math::isfinite(_f) && _f < 1))
28  throw GeographicErr("Minor radius is not positive");
29  }
30 
31  const Geocentric Geocentric::WGS84(Constants::WGS84_a<real>(),
32  Constants::WGS84_f<real>());
33 
34  void Geocentric::IntForward(real lat, real lon, real h,
35  real& X, real& Y, real& Z,
36  real M[dim2_]) const throw() {
37  lon = Math::AngNormalize(lon);
38  real
39  phi = lat * Math::degree<real>(),
40  lam = lon * Math::degree<real>(),
41  sphi = sin(phi),
42  cphi = abs(lat) == 90 ? 0 : cos(phi),
43  n = _a/sqrt(1 - _e2 * Math::sq(sphi)),
44  slam = lon == -180 ? 0 : sin(lam),
45  clam = abs(lon) == 90 ? 0 : cos(lam);
46  Z = ( _e2m * n + h) * sphi;
47  X = (n + h) * cphi;
48  Y = X * slam;
49  X *= clam;
50  if (M)
51  Rotation(sphi, cphi, slam, clam, M);
52  }
53 
54  void Geocentric::IntReverse(real X, real Y, real Z,
55  real& lat, real& lon, real& h,
56  real M[dim2_]) const throw() {
57  real
58  R = Math::hypot(X, Y),
59  slam = R ? Y / R : 0,
60  clam = R ? X / R : 1;
61  h = Math::hypot(R, Z); // Distance to center of earth
62  real sphi, cphi;
63  if (h > _maxrad) {
64  // We really far away (> 12 million light years); treat the earth as a
65  // point and h, above, is an acceptable approximation to the height.
66  // This avoids overflow, e.g., in the computation of disc below. It's
67  // possible that h has overflowed to inf; but that's OK.
68  //
69  // Treat the case X, Y finite, but R overflows to +inf by scaling by 2.
70  R = Math::hypot(X/2, Y/2);
71  slam = R ? (Y/2) / R : 0;
72  clam = R ? (X/2) / R : 1;
73  real H = Math::hypot(Z/2, R);
74  sphi = (Z/2) / H;
75  cphi = R / H;
76  } else if (_e4a == 0) {
77  // Treat the spherical case. Dealing with underflow in the general case
78  // with _e2 = 0 is difficult. Origin maps to N pole same as with
79  // ellipsoid.
80  real H = Math::hypot(h == 0 ? 1 : Z, R);
81  sphi = (h == 0 ? 1 : Z) / H;
82  cphi = R / H;
83  h -= _a;
84  } else {
85  // Treat prolate spheroids by swapping R and Z here and by switching
86  // the arguments to phi = atan2(...) at the end.
87  real
88  p = Math::sq(R / _a),
89  q = _e2m * Math::sq(Z / _a),
90  r = (p + q - _e4a) / 6;
91  if (_f < 0) swap(p, q);
92  if ( !(_e4a * q == 0 && r <= 0) ) {
93  real
94  // Avoid possible division by zero when r = 0 by multiplying
95  // equations for s and t by r^3 and r, resp.
96  S = _e4a * p * q / 4, // S = r^3 * s
97  r2 = Math::sq(r),
98  r3 = r * r2,
99  disc = S * (2 * r3 + S);
100  real u = r;
101  if (disc >= 0) {
102  real T3 = S + r3;
103  // Pick the sign on the sqrt to maximize abs(T3). This minimizes
104  // loss of precision due to cancellation. The result is unchanged
105  // because of the way the T is used in definition of u.
106  T3 += T3 < 0 ? -sqrt(disc) : sqrt(disc); // T3 = (r * t)^3
107  // N.B. cbrt always returns the real root. cbrt(-8) = -2.
108  real T = Math::cbrt(T3); // T = r * t
109  // T can be zero; but then r2 / T -> 0.
110  u += T + (T != 0 ? r2 / T : 0);
111  } else {
112  // T is complex, but the way u is defined the result is real.
113  real ang = atan2(sqrt(-disc), -(S + r3));
114  // There are three possible cube roots. We choose the root which
115  // avoids cancellation. Note that disc < 0 implies that r < 0.
116  u += 2 * r * cos(ang / 3);
117  }
118  real
119  v = sqrt(Math::sq(u) + _e4a * q), // guaranteed positive
120  // Avoid loss of accuracy when u < 0. Underflow doesn't occur in
121  // e4 * q / (v - u) because u ~ e^4 when q is small and u < 0.
122  uv = u < 0 ? _e4a * q / (v - u) : u + v, // u+v, guaranteed positive
123  // Need to guard against w going negative due to roundoff in uv - q.
124  w = max(real(0), _e2a * (uv - q) / (2 * v)),
125  // Rearrange expression for k to avoid loss of accuracy due to
126  // subtraction. Division by 0 not possible because uv > 0, w >= 0.
127  k = uv / (sqrt(uv + Math::sq(w)) + w),
128  k1 = _f >= 0 ? k : k - _e2,
129  k2 = _f >= 0 ? k + _e2 : k,
130  d = k1 * R / k2,
131  H = Math::hypot(Z/k1, R/k2);
132  sphi = (Z/k1) / H;
133  cphi = (R/k2) / H;
134  h = (1 - _e2m/k1) * Math::hypot(d, Z);
135  } else { // e4 * q == 0 && r <= 0
136  // This leads to k = 0 (oblate, equatorial plane) and k + e^2 = 0
137  // (prolate, rotation axis) and the generation of 0/0 in the general
138  // formulas for phi and h. using the general formula and division by 0
139  // in formula for h. So handle this case by taking the limits:
140  // f > 0: z -> 0, k -> e2 * sqrt(q)/sqrt(e4 - p)
141  // f < 0: R -> 0, k + e2 -> - e2 * sqrt(q)/sqrt(e4 - p)
142  real
143  zz = sqrt((_f >= 0 ? _e4a - p : p) / _e2m),
144  xx = sqrt( _f < 0 ? _e4a - p : p ),
145  H = Math::hypot(zz, xx);
146  sphi = zz / H;
147  cphi = xx / H;
148  if (Z < 0) sphi = -sphi; // for tiny negative Z (not for prolate)
149  h = - _a * (_f >= 0 ? _e2m : 1) * H / _e2a;
150  }
151  }
152  lat = atan2(sphi, cphi) / Math::degree<real>();
153  // Negative signs return lon in [-180, 180).
154  lon = -atan2(-slam, clam) / Math::degree<real>();
155  if (M)
156  Rotation(sphi, cphi, slam, clam, M);
157  }
158 
159  void Geocentric::Rotation(real sphi, real cphi, real slam, real clam,
160  real M[dim2_]) throw() {
161  // This rotation matrix is given by the following quaternion operations
162  // qrot(lam, [0,0,1]) * qrot(phi, [0,-1,0]) * [1,1,1,1]/2
163  // or
164  // qrot(pi/2 + lam, [0,0,1]) * qrot(-pi/2 + phi , [-1,0,0])
165  // where
166  // qrot(t,v) = [cos(t/2), sin(t/2)*v[1], sin(t/2)*v[2], sin(t/2)*v[3]]
167 
168  // Local X axis (east) in geocentric coords
169  M[0] = -slam; M[3] = clam; M[6] = 0;
170  // Local Y axis (north) in geocentric coords
171  M[1] = -clam * sphi; M[4] = -slam * sphi; M[7] = cphi;
172  // Local Z axis (up) in geocentric coords
173  M[2] = clam * cphi; M[5] = slam * cphi; M[8] = sphi;
174  }
175 
176 } // namespace GeographicLib
static T AngNormalize(T x)
Definition: Math.hpp:388
GeographicLib::Math::real real
Definition: GeodSolve.cpp:40
static T cbrt(T x)
Definition: Math.hpp:340
static bool isfinite(T x)
Definition: Math.hpp:435
Mathematical functions needed by GeographicLib.
Definition: Math.hpp:73
Geocentric coordinates
Definition: Geocentric.hpp:61
static T hypot(T x, T y)
Definition: Math.hpp:165
static T sq(T x)
Definition: Math.hpp:153
static const Geocentric WGS84
Definition: Geocentric.hpp:270
Header for GeographicLib::Geocentric class.
Exception handling for GeographicLib.
Definition: Constants.hpp:320