Geocentric.cpp

Go to the documentation of this file.
00001 /**
00002  * \file Geocentric.cpp
00003  * \brief Implementation for GeographicLib::Geocentric class
00004  *
00005  * Copyright (c) Charles Karney (2008, 2009, 2010, 2011) <charles@karney.com>
00006  * and licensed under the LGPL.  For more information, see
00007  * http://geographiclib.sourceforge.net/
00008  **********************************************************************/
00009 
00010 #include "GeographicLib/Geocentric.hpp"
00011 
00012 #define GEOGRAPHICLIB_GEOCENTRIC_CPP "$Id: Geocentric.cpp 6952 2011-02-14 20:26:44Z karney $"
00013 
00014 RCSID_DECL(GEOGRAPHICLIB_GEOCENTRIC_CPP)
00015 RCSID_DECL(GEOGRAPHICLIB_GEOCENTRIC_HPP)
00016 
00017 #if defined(_MSC_VER)
00018 // Squelch warnings about unsafe use of copy
00019 #pragma warning (disable: 4996)
00020 #endif
00021 
00022 namespace GeographicLib {
00023 
00024   using namespace std;
00025 
00026   Geocentric::Geocentric(real a, real r)
00027     : _a(a)
00028     , _r(r)
00029     , _f(_r != 0 ? 1 / _r : 0)
00030     , _e2(_f * (2 - _f))
00031     , _e2m(sq(1 - _f))          // 1 - _e2
00032     , _e2a(abs(_e2))
00033     , _e4a(sq(_e2))
00034     , _maxrad(2 * _a / numeric_limits<real>::epsilon())
00035   {
00036     if (!(_a > 0))
00037       throw GeographicErr("Major radius is not positive");
00038     if (!(_f < 1))
00039       throw GeographicErr("Minor radius is not positive");
00040   }
00041 
00042   const Geocentric Geocentric::WGS84(Constants::WGS84_a<real>(),
00043                                      Constants::WGS84_r<real>());
00044   /*
00045   void Geocentric::Forward(real lat, real lon, real h,
00046                            real& x, real& y, real& z) const throw() {
00047     lon = lon >= 180 ? lon - 360 : lon < -180 ? lon + 360 : lon;
00048     real
00049       phi = lat * Math::degree<real>(),
00050       lam = lon * Math::degree<real>(),
00051       sphi = sin(phi),
00052       cphi = abs(lat) == 90 ? 0 : cos(phi),
00053       n = _a/sqrt(1 - _e2 * sq(sphi)),
00054       slam = lon == -180 ? 0 : sin(lam),
00055       clam = abs(lon) == 90 ? 0 : cos(lam);
00056     z = ( _e2m * n + h) * sphi;
00057     x = (n + h) * cphi;
00058     y = x * slam;
00059     x *= clam;
00060   }
00061 */
00062   void Geocentric::IntForward(real lat, real lon, real h,
00063                               real& x, real& y, real& z,
00064                               real M[dim2]) const throw() {
00065     lon = lon >= 180 ? lon - 360 : lon < -180 ? lon + 360 : lon;
00066     real
00067       phi = lat * Math::degree<real>(),
00068       lam = lon * Math::degree<real>(),
00069       sphi = sin(phi),
00070       cphi = abs(lat) == 90 ? 0 : cos(phi),
00071       n = _a/sqrt(1 - _e2 * sq(sphi)),
00072       slam = lon == -180 ? 0 : sin(lam),
00073       clam = abs(lon) == 90 ? 0 : cos(lam);
00074     z = ( _e2m * n + h) * sphi;
00075     x = (n + h) * cphi;
00076     y = x * slam;
00077     x *= clam;
00078     if (M)
00079       Rotation(sphi, cphi, slam, clam, M);
00080   }
00081 
00082   void Geocentric::IntReverse(real x, real y, real z,
00083                               real& lat, real& lon, real& h,
00084                               real M[dim2]) const throw() {
00085     real
00086       R = Math::hypot(x, y),
00087       slam = R ? y / R : 0,
00088       clam = R ? x / R : 1;
00089     h = Math::hypot(R, z);      // Distance to center of earth
00090     real sphi, cphi;
00091     if (h > _maxrad) {
00092       // We really far away (> 12 million light years); treat the earth as a
00093       // point and h, above, is an acceptable approximation to the height.
00094       // This avoids overflow, e.g., in the computation of disc below.  It's
00095       // possible that h has overflowed to inf; but that's OK.
00096       //
00097       // Treat the case x, y finite, but R overflows to +inf by scaling by 2.
00098       R = Math::hypot(x/2, y/2);
00099       slam = R ? (y/2) / R : 0;
00100       clam = R ? (x/2) / R : 1;
00101       real H = Math::hypot(z/2, R);
00102       sphi = (z/2) / H;
00103       cphi = R / H;
00104     } else if (_e4a == 0) {
00105       // Treat the spherical case.  Dealing with underflow in the general case
00106       // with _e2 = 0 is difficult.  Origin maps to N pole same as with
00107       // ellipsoid.
00108       real H = Math::hypot(h == 0 ? 1 : z, R);
00109       sphi = (h == 0 ? 1 : z) / H;
00110       cphi = R / H;
00111       h -= _a;
00112     } else {
00113       // Treat prolate spheroids by swapping R and z here and by switching
00114       // the arguments to phi = atan2(...) at the end.
00115       real
00116         p = sq(R / _a),
00117         q = _e2m * sq(z / _a),
00118         r = (p + q - _e4a) / 6;
00119       if (_f < 0) swap(p, q);
00120       if ( !(_e4a * q == 0 && r <= 0) ) {
00121         real
00122           // Avoid possible division by zero when r = 0 by multiplying
00123           // equations for s and t by r^3 and r, resp.
00124           S = _e4a * p * q / 4, // S = r^3 * s
00125           r2 = sq(r),
00126           r3 = r * r2,
00127           disc =  S * (2 * r3 + S);
00128         real u = r;
00129         if (disc >= 0) {
00130           real T3 = r3 + S;
00131           // Pick the sign on the sqrt to maximize abs(T3).  This minimizes
00132           // loss of precision due to cancellation.  The result is unchanged
00133           // because of the way the T is used in definition of u.
00134           T3 += T3 < 0 ? -sqrt(disc) : sqrt(disc); // T3 = (r * t)^3
00135           // N.B. cbrt always returns the real root.  cbrt(-8) = -2.
00136           real T = Math::cbrt(T3); // T = r * t
00137           // T can be zero; but then r2 / T -> 0.
00138           u += T + (T != 0 ? r2 / T : 0);
00139         } else {
00140           // T is complex, but the way u is defined the result is real.
00141           real ang = atan2(sqrt(-disc), -(S + r3));
00142           // There are three possible cube roots.  We choose the root which
00143           // avoids cancellation.  Note that disc < 0 implies that r < 0.
00144           u += 2 * r * cos(ang / 3);
00145         }
00146         real
00147           v = sqrt(sq(u) + _e4a * q), // guaranteed positive
00148           // Avoid loss of accuracy when u < 0.  Underflow doesn't occur in
00149           // e4 * q / (v - u) because u ~ e^4 when q is small and u < 0.
00150           uv = u < 0 ? _e4a * q / (v - u) : u + v, // u+v, guaranteed positive
00151           // Need to guard against w going negative due to roundoff in uv - q.
00152           w = max(real(0), _e2a * (uv - q) / (2 * v)),
00153           // Rearrange expression for k to avoid loss of accuracy due to
00154           // subtraction.  Division by 0 not possible because uv > 0, w >= 0.
00155           k = uv / (sqrt(uv + sq(w)) + w),
00156           k1 = _f >= 0 ? k : k - _e2,
00157           k2 = _f >= 0 ? k + _e2 : k,
00158           d = k1 * R / k2,
00159           H = Math::hypot(z/k1, R/k2);
00160         sphi = (z/k1) / H;
00161         cphi = (R/k2) / H;
00162         h = (1 - _e2m/k1) * Math::hypot(d, z);
00163       } else {                  // e4 * q == 0 && r <= 0
00164         // This leads to k = 0 (oblate, equatorial plane) and k + e^2 = 0
00165         // (prolate, rotation axis) and the generation of 0/0 in the general
00166         // formulas for phi and h.  using the general formula and division by 0
00167         // in formula for h.  So handle this case by taking the limits:
00168         // f > 0: z -> 0, k      ->   e2 * sqrt(q)/sqrt(e4 - p)
00169         // f < 0: R -> 0, k + e2 -> - e2 * sqrt(q)/sqrt(e4 - p)
00170         real
00171           zz = sqrt((_f >= 0 ? _e4a - p : p) / _e2m),
00172           xx = sqrt( _f <  0 ? _e4a - p : p        ),
00173           H = Math::hypot(zz, xx);
00174         sphi = zz / H;
00175         cphi = xx / H;
00176         if (z < 0) sphi = -sphi; // for tiny negative z (not for prolate)
00177         h = - _a * (_f >= 0 ? _e2m : 1) * H / _e2a;
00178       }
00179     }
00180     lat = atan2(sphi, cphi) / Math::degree<real>();
00181     // Negative signs return lon in [-180, 180).
00182     lon = -atan2(-slam, clam) / Math::degree<real>();
00183     if (M)
00184       Rotation(sphi, cphi, slam, clam, M);
00185   }
00186 
00187   void Geocentric::Rotation(real sphi, real cphi, real slam, real clam,
00188                              real M[dim2]) const throw() {
00189     // This rotation matrix is given by the following quaternion operations
00190     // qrot(lam, [0,0,1]) * qrot(phi, [0,-1,0]) * [1,1,1,1]/2
00191     // or
00192     // qrot(pi/2 + lam, [0,0,1]) * qrot(-pi/2 + phi , [-1,0,0])
00193     // where
00194     // qrot(t,v) = [cos(t/2), sin(t/2)*v[1], sin(t/2)*v[2], sin(t/2)*v[3]]
00195 
00196     // Local x axis (east) in geocentric coords
00197     M[0] = -slam;        M[3] =  clam;        M[6] = 0;
00198     // Local y axis (north) in geocentric coords
00199     M[1] = -clam * sphi; M[4] = -slam * sphi; M[7] = cphi;
00200     // Local z axis (up) in geocentric coords
00201     M[2] =  clam * cphi; M[5] =  slam * cphi; M[8] = sphi;
00202   }
00203 
00204 } // namespace GeographicLib