GeographicLib  1.46
GeodesicExact.cpp
Go to the documentation of this file.
1 /**
2  * \file GeodesicExact.cpp
3  * \brief Implementation for GeographicLib::GeodesicExact class
4  *
5  * Copyright (c) Charles Karney (2012-2016) <charles@karney.com> and licensed
6  * under the MIT/X11 License. For more information, see
7  * http://geographiclib.sourceforge.net/
8  *
9  * This is a reformulation of the geodesic problem. The notation is as
10  * follows:
11  * - at a general point (no suffix or 1 or 2 as suffix)
12  * - phi = latitude
13  * - beta = latitude on auxiliary sphere
14  * - omega = longitude on auxiliary sphere
15  * - lambda = longitude
16  * - alpha = azimuth of great circle
17  * - sigma = arc length along great circle
18  * - s = distance
19  * - tau = scaled distance (= sigma at multiples of pi/2)
20  * - at northwards equator crossing
21  * - beta = phi = 0
22  * - omega = lambda = 0
23  * - alpha = alpha0
24  * - sigma = s = 0
25  * - a 12 suffix means a difference, e.g., s12 = s2 - s1.
26  * - s and c prefixes mean sin and cos
27  **********************************************************************/
28 
31 
32 #if defined(_MSC_VER)
33 // Squelch warnings about potentially uninitialized local variables and
34 // constant conditional expressions
35 # pragma warning (disable: 4701 4127)
36 #endif
37 
38 namespace GeographicLib {
39 
40  using namespace std;
41 
43  : maxit2_(maxit1_ + Math::digits() + 10)
44  // Underflow guard. We require
45  // tiny_ * epsilon() > 0
46  // tiny_ + epsilon() == epsilon()
47  , tiny_(sqrt(numeric_limits<real>::min()))
48  , tol0_(numeric_limits<real>::epsilon())
49  // Increase multiplier in defn of tol1_ from 100 to 200 to fix inverse
50  // case 52.784459512564 0 -52.784459512563990912 179.634407464943777557
51  // which otherwise failed for Visual Studio 10 (Release and Debug)
52  , tol1_(200 * tol0_)
53  , tol2_(sqrt(tol0_))
54  , tolb_(tol0_ * tol2_) // Check on bisection interval
55  , xthresh_(1000 * tol2_)
56  , _a(a)
57  , _f(f)
58  , _f1(1 - _f)
59  , _e2(_f * (2 - _f))
60  , _ep2(_e2 / Math::sq(_f1)) // e2 / (1 - e2)
61  , _n(_f / ( 2 - _f))
62  , _b(_a * _f1)
63  // The Geodesic class substitutes atanh(sqrt(e2)) for asinh(sqrt(ep2)) in
64  // the definition of _c2. The latter is more accurate for very oblate
65  // ellipsoids (which the Geodesic class does not attempt to handle). Of
66  // course, the area calculation in GeodesicExact is still based on a
67  // series and so only holds for moderately oblate (or prolate)
68  // ellipsoids.
69  , _c2((Math::sq(_a) + Math::sq(_b) *
70  (_f == 0 ? 1 :
71  (_f > 0 ? Math::asinh(sqrt(_ep2)) : atan(sqrt(-_e2))) /
72  sqrt(abs(_e2))))/2) // authalic radius squared
73  // The sig12 threshold for "really short". Using the auxiliary sphere
74  // solution with dnm computed at (bet1 + bet2) / 2, the relative error in
75  // the azimuth consistency check is sig12^2 * abs(f) * min(1, 1-f/2) / 2.
76  // (Error measured for 1/100 < b/a < 100 and abs(f) >= 1/1000. For a
77  // given f and sig12, the max error occurs for lines near the pole. If
78  // the old rule for computing dnm = (dn1 + dn2)/2 is used, then the error
79  // increases by a factor of 2.) Setting this equal to epsilon gives
80  // sig12 = etol2. Here 0.1 is a safety factor (error decreased by 100)
81  // and max(0.001, abs(f)) stops etol2 getting too large in the nearly
82  // spherical case.
83  , _etol2(0.1 * tol2_ /
84  sqrt( max(real(0.001), abs(_f)) * min(real(1), 1 - _f/2) / 2 ))
85  {
86  if (!(Math::isfinite(_a) && _a > 0))
87  throw GeographicErr("Major radius is not positive");
88  if (!(Math::isfinite(_b) && _b > 0))
89  throw GeographicErr("Minor radius is not positive");
90  C4coeff();
91  }
92 
94  static const GeodesicExact wgs84(Constants::WGS84_a(),
96  return wgs84;
97  }
98 
99  Math::real GeodesicExact::CosSeries(real sinx, real cosx,
100  const real c[], int n) {
101  // Evaluate
102  // y = sum(c[i] * cos((2*i+1) * x), i, 0, n-1)
103  // using Clenshaw summation.
104  // Approx operation count = (n + 5) mult and (2 * n + 2) add
105  c += n ; // Point to one beyond last element
106  real
107  ar = 2 * (cosx - sinx) * (cosx + sinx), // 2 * cos(2 * x)
108  y0 = n & 1 ? *--c : 0, y1 = 0; // accumulators for sum
109  // Now n is even
110  n /= 2;
111  while (n--) {
112  // Unroll loop x 2, so accumulators return to their original role
113  y1 = ar * y0 - y1 + *--c;
114  y0 = ar * y1 - y0 + *--c;
115  }
116  return cosx * (y0 - y1); // cos(x) * (y0 - y1)
117  }
118 
119  GeodesicLineExact GeodesicExact::Line(real lat1, real lon1, real azi1,
120  unsigned caps) const {
121  return GeodesicLineExact(*this, lat1, lon1, azi1, caps);
122  }
123 
124  Math::real GeodesicExact::GenDirect(real lat1, real lon1, real azi1,
125  bool arcmode, real s12_a12,
126  unsigned outmask,
127  real& lat2, real& lon2, real& azi2,
128  real& s12, real& m12,
129  real& M12, real& M21,
130  real& S12) const {
131  // Automatically supply DISTANCE_IN if necessary
132  if (!arcmode) outmask |= DISTANCE_IN;
133  return GeodesicLineExact(*this, lat1, lon1, azi1, outmask)
134  . // Note the dot!
135  GenPosition(arcmode, s12_a12, outmask,
136  lat2, lon2, azi2, s12, m12, M12, M21, S12);
137  }
138 
140  real azi1,
141  bool arcmode, real s12_a12,
142  unsigned caps) const {
143  azi1 = Math::AngNormalize(azi1);
144  real salp1, calp1;
145  // Guard against underflow in salp0. Also -0 is converted to +0.
146  Math::sincosd(Math::AngRound(azi1), salp1, calp1);
147  // Automatically supply DISTANCE_IN if necessary
148  if (!arcmode) caps |= DISTANCE_IN;
149  return GeodesicLineExact(*this, lat1, lon1, azi1, salp1, calp1,
150  caps, arcmode, s12_a12);
151  }
152 
154  real azi1, real s12,
155  unsigned caps) const {
156  return GenDirectLine(lat1, lon1, azi1, false, s12, caps);
157  }
158 
160  real azi1, real a12,
161  unsigned caps) const {
162  return GenDirectLine(lat1, lon1, azi1, true, a12, caps);
163  }
164 
165  Math::real GeodesicExact::GenInverse(real lat1, real lon1,
166  real lat2, real lon2,
167  unsigned outmask, real& s12,
168  real& salp1, real& calp1,
169  real& salp2, real& calp2,
170  real& m12, real& M12, real& M21,
171  real& S12) const {
172  // Compute longitude difference (AngDiff does this carefully). Result is
173  // in [-180, 180] but -180 is only for west-going geodesics. 180 is for
174  // east-going and meridional geodesics.
175  real lon12s, lon12 = Math::AngDiff(lon1, lon2, lon12s);
176  // Make longitude difference positive.
177  int lonsign = lon12 >= 0 ? 1 : -1;
178  // If very close to being on the same half-meridian, then make it so.
179  lon12 = lonsign * Math::AngRound(lon12);
180  lon12s = Math::AngRound((180 - lon12) - lonsign * lon12s);
181  real
182  lam12 = lon12 * Math::degree(),
183  slam12, clam12;
184  if (lon12 > 90) {
185  Math::sincosd(lon12s, slam12, clam12);
186  clam12 = -clam12;
187  } else
188  Math::sincosd(lon12, slam12, clam12);
189 
190  // If really close to the equator, treat as on equator.
191  lat1 = Math::AngRound(Math::LatFix(lat1));
192  lat2 = Math::AngRound(Math::LatFix(lat2));
193  // Swap points so that point with higher (abs) latitude is point 1
194  // If one latitude is a nan, then it becomes lat1.
195  int swapp = abs(lat1) < abs(lat2) ? -1 : 1;
196  if (swapp < 0) {
197  lonsign *= -1;
198  swap(lat1, lat2);
199  }
200  // Make lat1 <= 0
201  int latsign = lat1 < 0 ? 1 : -1;
202  lat1 *= latsign;
203  lat2 *= latsign;
204  // Now we have
205  //
206  // 0 <= lon12 <= 180
207  // -90 <= lat1 <= 0
208  // lat1 <= lat2 <= -lat1
209  //
210  // longsign, swapp, latsign register the transformation to bring the
211  // coordinates to this canonical form. In all cases, 1 means no change was
212  // made. We make these transformations so that there are few cases to
213  // check, e.g., on verifying quadrants in atan2. In addition, this
214  // enforces some symmetries in the results returned.
215 
216  real sbet1, cbet1, sbet2, cbet2, s12x, m12x;
217  // Initialize for the meridian. No longitude calculation is done in this
218  // case to let the parameter default to 0.
219  EllipticFunction E(-_ep2);
220 
221  Math::sincosd(lat1, sbet1, cbet1); sbet1 *= _f1;
222  // Ensure cbet1 = +epsilon at poles; doing the fix on beta means that sig12
223  // will be <= 2*tiny for two points at the same pole.
224  Math::norm(sbet1, cbet1); cbet1 = max(tiny_, cbet1);
225 
226  Math::sincosd(lat2, sbet2, cbet2); sbet2 *= _f1;
227  // Ensure cbet2 = +epsilon at poles
228  Math::norm(sbet2, cbet2); cbet2 = max(tiny_, cbet2);
229 
230  // If cbet1 < -sbet1, then cbet2 - cbet1 is a sensitive measure of the
231  // |bet1| - |bet2|. Alternatively (cbet1 >= -sbet1), abs(sbet2) + sbet1 is
232  // a better measure. This logic is used in assigning calp2 in Lambda12.
233  // Sometimes these quantities vanish and in that case we force bet2 = +/-
234  // bet1 exactly. An example where is is necessary is the inverse problem
235  // 48.522876735459 0 -48.52287673545898293 179.599720456223079643
236  // which failed with Visual Studio 10 (Release and Debug)
237 
238  if (cbet1 < -sbet1) {
239  if (cbet2 == cbet1)
240  sbet2 = sbet2 < 0 ? sbet1 : -sbet1;
241  } else {
242  if (abs(sbet2) == -sbet1)
243  cbet2 = cbet1;
244  }
245 
246  real
247  dn1 = (_f >= 0 ? sqrt(1 + _ep2 * Math::sq(sbet1)) :
248  sqrt(1 - _e2 * Math::sq(cbet1)) / _f1),
249  dn2 = (_f >= 0 ? sqrt(1 + _ep2 * Math::sq(sbet2)) :
250  sqrt(1 - _e2 * Math::sq(cbet2)) / _f1);
251 
252  real a12, sig12;
253 
254  bool meridian = lat1 == -90 || slam12 == 0;
255 
256  if (meridian) {
257 
258  // Endpoints are on a single full meridian, so the geodesic might lie on
259  // a meridian.
260 
261  calp1 = clam12; salp1 = slam12; // Head to the target longitude
262  calp2 = 1; salp2 = 0; // At the target we're heading north
263 
264  real
265  // tan(bet) = tan(sig) * cos(alp)
266  ssig1 = sbet1, csig1 = calp1 * cbet1,
267  ssig2 = sbet2, csig2 = calp2 * cbet2;
268 
269  // sig12 = sig2 - sig1
270  sig12 = atan2(max(real(0), csig1 * ssig2 - ssig1 * csig2),
271  csig1 * csig2 + ssig1 * ssig2);
272  {
273  real dummy;
274  Lengths(E, sig12, ssig1, csig1, dn1, ssig2, csig2, dn2,
275  cbet1, cbet2, outmask | REDUCEDLENGTH,
276  s12x, m12x, dummy, M12, M21);
277  }
278  // Add the check for sig12 since zero length geodesics might yield m12 <
279  // 0. Test case was
280  //
281  // echo 20.001 0 20.001 0 | GeodSolve -i
282  //
283  // In fact, we will have sig12 > pi/2 for meridional geodesic which is
284  // not a shortest path.
285  if (sig12 < 1 || m12x >= 0) {
286  // Need at least 2, to handle 90 0 90 180
287  if (sig12 < 3 * tiny_)
288  sig12 = m12x = s12x = 0;
289  m12x *= _b;
290  s12x *= _b;
291  a12 = sig12 / Math::degree();
292  } else
293  // m12 < 0, i.e., prolate and too close to anti-podal
294  meridian = false;
295  }
296 
297  // somg12 > 1 marks that it needs to be calculated
298  real omg12 = 0, somg12 = 2, comg12 = 0;
299  if (!meridian &&
300  sbet1 == 0 && // and sbet2 == 0
301  (_f <= 0 || lon12s >= _f * 180)) {
302 
303  // Geodesic runs along equator
304  calp1 = calp2 = 0; salp1 = salp2 = 1;
305  s12x = _a * lam12;
306  sig12 = omg12 = lam12 / _f1;
307  m12x = _b * sin(sig12);
308  if (outmask & GEODESICSCALE)
309  M12 = M21 = cos(sig12);
310  a12 = lon12 / _f1;
311 
312  } else if (!meridian) {
313 
314  // Now point1 and point2 belong within a hemisphere bounded by a
315  // meridian and geodesic is neither meridional or equatorial.
316 
317  // Figure a starting point for Newton's method
318  real dnm;
319  sig12 = InverseStart(E, sbet1, cbet1, dn1, sbet2, cbet2, dn2,
320  lam12, slam12, clam12,
321  salp1, calp1, salp2, calp2, dnm);
322 
323  if (sig12 >= 0) {
324  // Short lines (InverseStart sets salp2, calp2, dnm)
325  s12x = sig12 * _b * dnm;
326  m12x = Math::sq(dnm) * _b * sin(sig12 / dnm);
327  if (outmask & GEODESICSCALE)
328  M12 = M21 = cos(sig12 / dnm);
329  a12 = sig12 / Math::degree();
330  omg12 = lam12 / (_f1 * dnm);
331  } else {
332 
333  // Newton's method. This is a straightforward solution of f(alp1) =
334  // lambda12(alp1) - lam12 = 0 with one wrinkle. f(alp) has exactly one
335  // root in the interval (0, pi) and its derivative is positive at the
336  // root. Thus f(alp) is positive for alp > alp1 and negative for alp <
337  // alp1. During the course of the iteration, a range (alp1a, alp1b) is
338  // maintained which brackets the root and with each evaluation of
339  // f(alp) the range is shrunk, if possible. Newton's method is
340  // restarted whenever the derivative of f is negative (because the new
341  // value of alp1 is then further from the solution) or if the new
342  // estimate of alp1 lies outside (0,pi); in this case, the new starting
343  // guess is taken to be (alp1a + alp1b) / 2.
344  //
345  // initial values to suppress warnings (if loop is executed 0 times)
346  real ssig1 = 0, csig1 = 0, ssig2 = 0, csig2 = 0;
347  unsigned numit = 0;
348  // Bracketing range
349  real salp1a = tiny_, calp1a = 1, salp1b = tiny_, calp1b = -1;
350  for (bool tripn = false, tripb = false;
351  numit < maxit2_ || GEOGRAPHICLIB_PANIC;
352  ++numit) {
353  // 1/4 meridan = 10e6 m and random input. max err is estimated max
354  // error in nm (checking solution of inverse problem by direct
355  // solution). iter is mean and sd of number of iterations
356  //
357  // max iter
358  // log2(b/a) err mean sd
359  // -7 387 5.33 3.68
360  // -6 345 5.19 3.43
361  // -5 269 5.00 3.05
362  // -4 210 4.76 2.44
363  // -3 115 4.55 1.87
364  // -2 69 4.35 1.38
365  // -1 36 4.05 1.03
366  // 0 15 0.01 0.13
367  // 1 25 5.10 1.53
368  // 2 96 5.61 2.09
369  // 3 318 6.02 2.74
370  // 4 985 6.24 3.22
371  // 5 2352 6.32 3.44
372  // 6 6008 6.30 3.45
373  // 7 19024 6.19 3.30
374  real dv;
375  real v = Lambda12(sbet1, cbet1, dn1, sbet2, cbet2, dn2, salp1, calp1,
376  slam12, clam12,
377  salp2, calp2, sig12, ssig1, csig1, ssig2, csig2,
378  E, somg12, comg12, numit < maxit1_, dv);
379  // Reversed test to allow escape with NaNs
380  if (tripb || !(abs(v) >= (tripn ? 8 : 1) * tol0_)) break;
381  // Update bracketing values
382  if (v > 0 && (numit > maxit1_ || calp1/salp1 > calp1b/salp1b))
383  { salp1b = salp1; calp1b = calp1; }
384  else if (v < 0 && (numit > maxit