GeographicLib  1.38
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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-2014) <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  // Check on bisection interval
55  , tolb_(tol0_ * tol2_)
56  , xthresh_(1000 * tol2_)
57  , _a(a)
58  , _f(f <= 1 ? f : 1/f)
59  , _f1(1 - _f)
60  , _e2(_f * (2 - _f))
61  , _ep2(_e2 / Math::sq(_f1)) // e2 / (1 - e2)
62  , _n(_f / ( 2 - _f))
63  , _b(_a * _f1)
64  , _c2((Math::sq(_a) + Math::sq(_b) *
65  (_e2 == 0 ? 1 :
66  (_e2 > 0 ? Math::atanh(sqrt(_e2)) : atan(sqrt(-_e2))) /
67  sqrt(abs(_e2))))/2) // authalic radius squared
68  // The sig12 threshold for "really short". Using the auxiliary sphere
69  // solution with dnm computed at (bet1 + bet2) / 2, the relative error in
70  // the azimuth consistency check is sig12^2 * abs(f) * min(1, 1-f/2) / 2.
71  // (Error measured for 1/100 < b/a < 100 and abs(f) >= 1/1000. For a
72  // given f and sig12, the max error occurs for lines near the pole. If
73  // the old rule for computing dnm = (dn1 + dn2)/2 is used, then the error
74  // increases by a factor of 2.) Setting this equal to epsilon gives
75  // sig12 = etol2. Here 0.1 is a safety factor (error decreased by 100)
76  // and max(0.001, abs(f)) stops etol2 getting too large in the nearly
77  // spherical case.
78  , _etol2(0.1 * tol2_ /
79  sqrt( max(real(0.001), abs(_f)) * min(real(1), 1 - _f/2) / 2 ))
80  {
81  if (!(Math::isfinite(_a) && _a > 0))
82  throw GeographicErr("Major radius is not positive");
83  if (!(Math::isfinite(_b) && _b > 0))
84  throw GeographicErr("Minor radius is not positive");
85  C4coeff();
86  }
87 
89  static const GeodesicExact wgs84(Constants::WGS84_a(),
91  return wgs84;
92  }
93 
94  Math::real GeodesicExact::CosSeries(real sinx, real cosx,
95  const real c[], int n) {
96  // Evaluate
97  // y = sum(c[i] * cos((2*i+1) * x), i, 0, n-1)
98  // using Clenshaw summation.
99  // Approx operation count = (n + 5) mult and (2 * n + 2) add
100  c += n ; // Point to one beyond last element
101  real
102  ar = 2 * (cosx - sinx) * (cosx + sinx), // 2 * cos(2 * x)
103  y0 = n & 1 ? *--c : 0, y1 = 0; // accumulators for sum
104  // Now n is even
105  n /= 2;
106  while (n--) {
107  // Unroll loop x 2, so accumulators return to their original role
108  y1 = ar * y0 - y1 + *--c;
109  y0 = ar * y1 - y0 + *--c;
110  }
111  return cosx * (y0 - y1); // cos(x) * (y0 - y1)
112  }
113 
114  GeodesicLineExact GeodesicExact::Line(real lat1, real lon1, real azi1,
115  unsigned caps) const {
116  return GeodesicLineExact(*this, lat1, lon1, azi1, caps);
117  }
118 
119  Math::real GeodesicExact::GenDirect(real lat1, real lon1, real azi1,
120  bool arcmode, real s12_a12,
121  unsigned outmask,
122  real& lat2, real& lon2, real& azi2,
123  real& s12, real& m12,
124  real& M12, real& M21,
125  real& S12) const {
126  return GeodesicLineExact(*this, lat1, lon1, azi1,
127  // Automatically supply DISTANCE_IN if necessary
128  outmask | (arcmode ? NONE : DISTANCE_IN))
129  . // Note the dot!
130  GenPosition(arcmode, s12_a12, outmask,
131  lat2, lon2, azi2, s12, m12, M12, M21, S12);
132  }
133 
134  Math::real GeodesicExact::GenInverse(real lat1, real lon1,
135  real lat2, real lon2,
136  unsigned outmask,
137  real& s12, real& azi1, real& azi2,
138  real& m12, real& M12, real& M21,
139  real& S12) const {
140  outmask &= OUT_ALL;
141  // Compute longitude difference (AngDiff does this carefully). Result is
142  // in [-180, 180] but -180 is only for west-going geodesics. 180 is for
143  // east-going and meridional geodesics.
144  real lon12 = Math::AngDiff(Math::AngNormalize(lon1),
145  Math::AngNormalize(lon2));
146  // If very close to being on the same half-meridian, then make it so.
147  lon12 = AngRound(lon12);
148  // Make longitude difference positive.
149  int lonsign = lon12 >= 0 ? 1 : -1;
150  lon12 *= lonsign;
151  // If really close to the equator, treat as on equator.
152  lat1 = AngRound(lat1);
153  lat2 = AngRound(lat2);
154  // Swap points so that point with higher (abs) latitude is point 1
155  int swapp = abs(lat1) >= abs(lat2) ? 1 : -1;
156  if (swapp < 0) {
157  lonsign *= -1;
158  swap(lat1, lat2);
159  }
160  // Make lat1 <= 0
161  int latsign = lat1 < 0 ? 1 : -1;
162  lat1 *= latsign;
163  lat2 *= latsign;
164  // Now we have
165  //
166  // 0 <= lon12 <= 180
167  // -90 <= lat1 <= 0
168  // lat1 <= lat2 <= -lat1
169  //
170  // longsign, swapp, latsign register the transformation to bring the
171  // coordinates to this canonical form. In all cases, 1 means no change was
172  // made. We make these transformations so that there are few cases to
173  // check, e.g., on verifying quadrants in atan2. In addition, this
174  // enforces some symmetries in the results returned.
175 
176  real phi, sbet1, cbet1, sbet2, cbet2, s12x, m12x;
177  // Initialize for the meridian. No longitude calculation is done in this
178  // case to let the parameter default to 0.
179  EllipticFunction E(-_ep2);
180 
181  phi = lat1 * Math::degree();
182  // Ensure cbet1 = +epsilon at poles
183  sbet1 = _f1 * sin(phi);
184  cbet1 = lat1 == -90 ? tiny_ : cos(phi);
185  SinCosNorm(sbet1, cbet1);
186 
187  phi = lat2 * Math::degree();
188  // Ensure cbet2 = +epsilon at poles
189  sbet2 = _f1 * sin(phi);
190  cbet2 = abs(lat2) == 90 ? tiny_ : cos(phi);
191  SinCosNorm(sbet2, cbet2);
192 
193  // If cbet1 < -sbet1, then cbet2 - cbet1 is a sensitive measure of the
194  // |bet1| - |bet2|. Alternatively (cbet1 >= -sbet1), abs(sbet2) + sbet1 is
195  // a better measure. This logic is used in assigning calp2 in Lambda12.
196  // Sometimes these quantities vanish and in that case we force bet2 = +/-
197  // bet1 exactly. An example where is is necessary is the inverse problem
198  // 48.522876735459 0 -48.52287673545898293 179.599720456223079643
199  // which failed with Visual Studio 10 (Release and Debug)
200 
201  if (cbet1 < -sbet1) {
202  if (cbet2 == cbet1)
203  sbet2 = sbet2 < 0 ? sbet1 : -sbet1;
204  } else {
205  if (abs(sbet2) == -sbet1)
206  cbet2 = cbet1;
207  }
208 
209  real
210  dn1 = (_f >= 0 ? sqrt(1 + _ep2 * Math::sq(sbet1)) :
211  sqrt(1 - _e2 * Math::sq(cbet1)) / _f1),
212  dn2 = (_f >= 0 ? sqrt(1 + _ep2 * Math::sq(sbet2)) :
213  sqrt(1 - _e2 * Math::sq(cbet2)) / _f1);
214 
215  real
216  lam12 = lon12 * Math::degree(),
217  slam12 = abs(lon12) == 180 ? 0 : sin(lam12),
218  clam12 = cos(lam12); // lon12 == 90 isn't interesting
219 
220  // initial values to suppress warning
221  real a12, sig12, calp1, salp1, calp2 = 0, salp2 = 0;
222 
223  bool meridian = lat1 == -90 || slam12 == 0;
224 
225  if (meridian) {
226 
227  // Endpoints are on a single full meridian, so the geodesic might lie on
228  // a meridian.
229 
230  calp1 = clam12; salp1 = slam12; // Head to the target longitude
231  calp2 = 1; salp2 = 0; // At the target we're heading north
232 
233  real
234  // tan(bet) = tan(sig) * cos(alp)
235  ssig1 = sbet1, csig1 = calp1 * cbet1,
236  ssig2 = sbet2, csig2 = calp2 * cbet2;
237 
238  // sig12 = sig2 - sig1
239  sig12 = atan2(max(csig1 * ssig2 - ssig1 * csig2, real(0)),
240  csig1 * csig2 + ssig1 * ssig2);
241  {
242  real dummy;
243  Lengths(E, sig12, ssig1, csig1, dn1, ssig2, csig2, dn2,
244  cbet1, cbet2, s12x, m12x, dummy,
245  (outmask & GEODESICSCALE) != 0U, M12, M21);
246  }
247  // Add the check for sig12 since zero length geodesics might yield m12 <
248  // 0. Test case was
249  //
250  // echo 20.001 0 20.001 0 | GeodSolve -i
251  //
252  // In fact, we will have sig12 > pi/2 for meridional geodesic which is
253  // not a shortest path.
254  if (sig12 < 1 || m12x >= 0) {
255  m12x *= _b;
256  s12x *= _b;
257  a12 = sig12 / Math::degree();
258  } else
259  // m12 < 0, i.e., prolate and too close to anti-podal
260  meridian = false;
261  }
262 
263  real omg12 = 0; // initial value to suppress warning
264  if (!meridian &&
265  sbet1 == 0 && // and sbet2 == 0
266  // Mimic the way Lambda12 works with calp1 = 0
267  (_f <= 0 || lam12 <= Math::pi() - _f * Math::pi())) {
268 
269  // Geodesic runs along equator
270  calp1 = calp2 = 0; salp1 = salp2 = 1;
271  s12x = _a * lam12;
272  sig12 = omg12 = lam12 / _f1;
273  m12x = _b * sin(sig12);
274  if (outmask & GEODESICSCALE)
275  M12 = M21 = cos(sig12);
276  a12 = lon12 / _f1;
277 
278  } else if (!meridian) {
279 
280  // Now point1 and point2 belong within a hemisphere bounded by a
281  // meridian and geodesic is neither meridional or equatorial.
282 
283  // Figure a starting point for Newton's method
284  real dnm;
285  sig12 = InverseStart(E, sbet1, cbet1, dn1, sbet2, cbet2, dn2,
286  lam12,
287  salp1, calp1, salp2, calp2, dnm);
288 
289  if (sig12 >= 0) {
290  // Short lines (InverseStart sets salp2, calp2, dnm)
291  s12x = sig12 * _b * dnm;
292  m12x = Math::sq(dnm) * _b * sin(sig12 / dnm);
293  if (outmask & GEODESICSCALE)
294  M12 = M21 = cos(sig12 / dnm);
295  a12 = sig12 / Math::degree();
296  omg12 = lam12 / (_f1 * dnm);
297  } else {
298 
299  // Newton's method. This is a straightforward solution of f(alp1) =
300  // lambda12(alp1) - lam12 = 0 with one wrinkle. f(alp) has exactly one
301  // root in the interval (0, pi) and its derivative is positive at the
302  // root. Thus f(alp) is positive for alp > alp1 and negative for alp <
303  // alp1. During the course of the iteration, a range (alp1a, alp1b) is
304  // maintained which brackets the root and with each evaluation of
305  // f(alp) the range is shrunk, if possible. Newton's method is
306  // restarted whenever the derivative of f is negative (because the new
307  // value of alp1 is then further from the solution) or if the new
308  // estimate of alp1 lies outside (0,pi); in this case, the new starting
309  // guess is taken to be (alp1a + alp1b) / 2.
310  //
311  // initial values to suppress warnings (if loop is executed 0 times)
312  real ssig1 = 0, csig1 = 0, ssig2 = 0, csig2 = 0;
313  unsigned numit = 0;
314  // Bracketing range
315  real salp1a = tiny_, calp1a = 1, salp1b = tiny_, calp1b = -1;
316  for (bool tripn = false, tripb = false;
317  numit < maxit2_ || GEOGRAPHICLIB_PANIC;
318  ++numit) {
319  // 1/4 meridan = 10e6 m and random input. max err is estimated max
320  // error in nm (checking solution of inverse problem by direct
321  // solution). iter is mean and sd of number of iterations
322  //
323  // max iter
324  // log2(b/a) err mean sd
325  // -7 387 5.33 3.68
326  // -6 345 5.19 3.43
327  // -5 269 5.00 3.05
328  // -4 210 4.76 2.44
329  // -3 115 4.55 1.87
330  // -2 69 4.35 1.38
331  // -1 36 4.05 1.03
332  // 0 15 0.01 0.13
333  // 1 25 5.10 1.53
334  // 2 96 5.61 2.09
335  // 3 318 6.02 2.74
336  // 4 985 6.24 3.22
337  // 5 2352 6.32 3.44
338  // 6 6008 6.30 3.45
339  // 7 19024 6.19 3.30
340  real dv;
341  real v = Lambda12(sbet1, cbet1, dn1, sbet2, cbet2, dn2, salp1, calp1,
342  salp2, calp2, sig12, ssig1, csig1, ssig2, csig2,
343  E, omg12, numit < maxit1_, dv) - lam12;
344  // 2 * tol0 is approximately 1 ulp for a number in [0, pi].
345  // Reversed test to allow escape with NaNs
346  if (tripb || !(abs(v) >= (tripn ? 8 : 2) * tol0_)) break;
347  // Update bracketing values
348  if (v > 0 && (numit > maxit1_ || calp1/salp1 > calp1b/salp1b))
349  { salp1b = salp1; calp1b = calp1; }
350  else if (v < 0 && (numit > maxit1_ || calp1/salp1 < calp1a/salp1a))
351  { salp1a = salp1; calp1a = calp1; }
352  if (numit < maxit1_ && dv > 0) {
353  real
354  dalp1 = -v/dv;
355  real
356  sdalp1 = sin(dalp1), cdalp1 = cos(dalp1),
357  nsalp1 = salp1 * cdalp1 + calp1 * sdalp1;
358  if (nsalp1 > 0 && abs(dalp1) < Math::pi()) {
359  calp1 = calp1 * cdalp1 - salp1 * sdalp1;
360  salp1 = nsalp1;
361  SinCosNorm(salp1, calp1);
362  // In some regimes we don't get quadratic convergence because
363  // slope -> 0. So use convergence conditions based on epsilon
364  // instead of sqrt(epsilon).
365  tripn = abs(v) <= 16 * tol0_;
366  continue;
367  }
368  }
369  // Either dv was not postive or updated value was outside legal
370  // range. Use the midpoint of the bracket as the next estimate.
371  // This mechanism is not needed for the WGS84 ellipsoid, but it does
372  // catch problems with more eccentric ellipsoids. Its efficacy is
373  // such for the WGS84 test set with the starting guess set to alp1 =
374  // 90deg:
375  // the WGS84 test set: mean = 5.21, sd = 3.93, max = 24
376  // WGS84 and random input: mean = 4.74, sd = 0.99
377  salp1 = (salp1a + salp1b)/2;
378  calp1 = (calp1a + calp1b)/2;
379  SinCosNorm(salp1, calp1);
380  tripn = false;
381  tripb = (abs(salp1a - salp1) + (calp1a - calp1) < tolb_ ||
382  abs(salp1 - salp1b) + (calp1 - calp1b) < tolb_);
383  }
384  {
385  real dummy;
386  Lengths(E, sig12, ssig1, csig1, dn1, ssig2, csig2, dn2,
387  cbet1, cbet2, s12x, m12x, dummy,
388  (outmask & GEODESICSCALE) != 0U, M12, M21);
389  }
390  m12x *= _b;
391  s12x *= _b;
392  a12 = sig12 / Math::degree();
393  }
394  }
395 
396  if (outmask & DISTANCE)
397  s12 = 0 + s12x; // Convert -0 to 0
398 
399  if (outmask & REDUCEDLENGTH)
400  m12 = 0 + m12x; // Convert -0 to 0
401 
402  if (outmask & AREA) {
403  real
404  // From Lambda12: sin(alp1) * cos(bet1) = sin(alp0)
405  salp0 = salp1 * cbet1,
406  calp0 = Math::hypot(calp1, salp1 * sbet1); // calp0 > 0
407  real alp12;
408  if (calp0 != 0 && salp0 != 0) {
409  real
410  // From Lambda12: tan(bet) = tan(sig) * cos(alp)
411  ssig1 = sbet1, csig1 = calp1 * cbet1,
412  ssig2 = sbet2, csig2 = calp2 * cbet2,
413  k2 = Math::sq(calp0) * _ep2,
414  eps = k2 / (2 * (1 + sqrt(1 + k2)) + k2),
415  // Multiplier = a^2 * e^2 * cos(alpha0) * sin(alpha0).
416  A4 = Math::sq(_a) * calp0 * salp0 * _e2;
417  SinCosNorm(ssig1, csig1);
418  SinCosNorm(ssig2, csig2);
419  real C4a[nC4_];
420  C4f(eps, C4a);
421  real
422  B41 = CosSeries(ssig1, csig1, C4a, nC4_),
423  B42 = CosSeries(ssig2, csig2, C4a, nC4_);
424  S12 = A4 * (B42 - B41);
425  } else
426  // Avoid problems with indeterminate sig1, sig2 on equator
427  S12 = 0;
428 
429  if (!meridian &&
430  omg12 < real(0.75) * Math::pi() && // Long difference too big
431  sbet2 - sbet1 < real(1.75)) { // Lat difference too big
432  // Use tan(Gamma/2) = tan(omg12/2)
433  // * (tan(bet1/2)+tan(bet2/2))/(1+tan(bet1/2)*tan(bet2/2))
434  // with tan(x/2) = sin(x)/(1+cos(x))
435  real
436  somg12 = sin(omg12), domg12 = 1 + cos(omg12),
437  dbet1 = 1 + cbet1, dbet2 = 1 + cbet2;
438  alp12 = 2 * atan2( somg12 * ( sbet1 * dbet2 + sbet2 * dbet1 ),
439  domg12 * ( sbet1 * sbet2 + dbet1 * dbet2 ) );
440  } else {
441  // alp12 = alp2 - alp1, used in atan2 so no need to normalize
442  real
443  salp12 = salp2 * calp1 - calp2 * salp1,
444  calp12 = calp2 * calp1 + salp2 * salp1;
445  // The right thing appears to happen if alp1 = +/-180 and alp2 = 0, viz
446  // salp12 = -0 and alp12 = -180. However this depends on the sign
447  // being attached to 0 correctly. The following ensures the correct
448  // behavior.
449  if (salp12 == 0 && calp12 < 0) {
450  salp12 = tiny_ * calp1;
451  calp12 = -1;
452  }
453  alp12 = atan2(salp12, calp12);
454  }
455  S12 += _c2 * alp12;
456  S12 *= swapp * lonsign * latsign;
457  // Convert -0 to 0
458  S12 += 0;
459  }
460 
461  // Convert calp, salp to azimuth accounting for lonsign, swapp, latsign.
462  if (swapp < 0) {
463  swap(salp1, salp2);
464  swap(calp1, calp2);
465  if (outmask & GEODESICSCALE)
466  swap(M12, M21);
467  }
468 
469  salp1 *= swapp * lonsign; calp1 *= swapp * latsign;
470  salp2 *= swapp * lonsign; calp2 *= swapp * latsign;
471 
472  if (outmask & AZIMUTH) {
473  // minus signs give range [-180, 180). 0- converts -0 to +0.
474  azi1 = 0 - atan2(-salp1, calp1) / Math::degree();
475  azi2 = 0 - atan2(-salp2, calp2) / Math::degree();
476  }
477 
478  // Returned value in [0, 180]
479  return a12;
480  }
481 
482  void GeodesicExact::Lengths(const EllipticFunction& E,
483  real sig12,
484  real ssig1, real csig1, real dn1,
485  real ssig2, real csig2, real dn2,
486  real cbet1, real cbet2,
487  real& s12b, real& m12b, real& m0,
488  bool scalep, real& M12, real& M21) const {
489  // Return m12b = (reduced length)/_b; also calculate s12b = distance/_b,
490  // and m0 = coefficient of secular term in expression for reduced length.
491 
492  // It's OK to have repeated dummy arguments,
493  // e.g., s12b = m0 = M12 = M21 = dummy
494  m0 = - E.k2() * E.D() / (Math::pi() / 2);
495  real J12 = m0 *
496  (sig12 + E.deltaD(ssig2, csig2, dn2) - E.deltaD(ssig1, csig1, dn1));
497  // Missing a factor of _b.
498  // Add parens around (csig1 * ssig2) and (ssig1 * csig2) to ensure accurate
499  // cancellation in the case of coincident points.
500  m12b = dn2 * (csig1 * ssig2) - dn1 * (ssig1 * csig2) - csig1 * csig2 * J12;
501  // Missing a factor of _b
502  s12b = E.E() / (Math::pi() / 2) *
503  (sig12 + E.deltaE(ssig2, csig2, dn2) - E.deltaE(ssig1, csig1, dn1));
504  if (scalep) {
505  real csig12 = csig1 * csig2 + ssig1 * ssig2;
506  real t = _ep2 * (cbet1 - cbet2) * (cbet1 + cbet2) / (dn1 + dn2);
507  M12 = csig12 + (t * ssig2 - csig2 * J12) * ssig1 / dn1;
508  M21 = csig12 - (t * ssig1 - csig1 * J12) * ssig2 / dn2;
509  }
510  }
511 
512  Math::real GeodesicExact::Astroid(real x, real y) {
513  // Solve k^4+2*k^3-(x^2+y^2-1)*k^2-2*y^2*k-y^2 = 0 for positive root k.
514  // This solution is adapted from Geocentric::Reverse.
515  real k;
516  real
517  p = Math::sq(x),
518  q = Math::sq(y),
519  r = (p + q - 1) / 6;
520  if ( !(q == 0 && r <= 0) ) {
521  real
522  // Avoid possible division by zero when r = 0 by multiplying equations
523  // for s and t by r^3 and r, resp.
524  S = p * q / 4, // S = r^3 * s
525  r2 = Math::sq(r),
526  r3 = r * r2,
527  // The discrimant of the quadratic equation for T3. This is zero on
528  // the evolute curve p^(1/3)+q^(1/3) = 1
529  disc = S * (S + 2 * r3);
530  real u = r;
531  if (disc >= 0) {
532  real T3 = S + r3;
533  // Pick the sign on the sqrt to maximize abs(T3). This minimizes loss
534  // of precision due to cancellation. The result is unchanged because
535  // of the way the T is used in definition of u.
536  T3 += T3 < 0 ? -sqrt(disc) : sqrt(disc); // T3 = (r * t)^3
537  // N.B. cbrt always returns the real root. cbrt(-8) = -2.
538  real T = Math::cbrt(T3); // T = r * t
539  // T can be zero; but then r2 / T -> 0.
540  u += T + (T ? r2 / T : 0);
541  } else {
542  // T is complex, but the way u is defined the result is real.
543  real ang = atan2(sqrt(-disc), -(S + r3));
544  // There are three possible cube roots. We choose the root which
545  // avoids cancellation. Note that disc < 0 implies that r < 0.
546  u += 2 * r * cos(ang / 3);
547  }
548  real
549  v = sqrt(Math::sq(u) + q), // guaranteed positive
550  // Avoid loss of accuracy when u < 0.
551  uv = u < 0 ? q / (v - u) : u + v, // u+v, guaranteed positive
552  w = (uv - q) / (2 * v); // positive?
553  // Rearrange expression for k to avoid loss of accuracy due to
554  // subtraction. Division by 0 not possible because uv > 0, w >= 0.
555  k = uv / (sqrt(uv + Math::sq(w)) + w); // guaranteed positive
556  } else { // q == 0 && r <= 0
557  // y = 0 with |x| <= 1. Handle this case directly.
558  // for y small, positive root is k = abs(y)/sqrt(1-x^2)
559  k = 0;
560  }
561  return k;
562  }
563 
564  Math::real GeodesicExact::InverseStart(EllipticFunction& E,
565  real sbet1, real cbet1, real dn1,
566  real sbet2, real cbet2, real dn2,
567  real lam12,
568  real& salp1, real& calp1,
569  // Only updated if return val >= 0
570  real& salp2, real& calp2,
571  // Only updated for short lines
572  real& dnm)
573  const {
574  // Return a starting point for Newton's method in salp1 and calp1 (function
575  // value is -1). If Newton's method doesn't need to be used, return also
576  // salp2 and calp2 and function value is sig12.
577  real
578  sig12 = -1, // Return value
579  // bet12 = bet2 - bet1 in [0, pi); bet12a = bet2 + bet1 in (-pi, 0]
580  sbet12 = sbet2 * cbet1 - cbet2 * sbet1,
581  cbet12 = cbet2 * cbet1 + sbet2 * sbet1;
582 #if defined(__GNUC__) && __GNUC__ == 4 && \
583  (__GNUC_MINOR__ < 6 || defined(__MINGW32__))
584  // Volatile declaration needed to fix inverse cases
585  // 88.202499451857 0 -88.202499451857 179.981022032992859592
586  // 89.262080389218 0 -89.262080389218 179.992207982775375662
587  // 89.333123580033 0 -89.333123580032997687 179.99295812360148422
588  // which otherwise fail with g++ 4.4.4 x86 -O3 (Linux)
589  // and g++ 4.4.0 (mingw) and g++ 4.6.1 (tdm mingw).
590  real sbet12a;
591  {
592  GEOGRAPHICLIB_VOLATILE real xx1 = sbet2 * cbet1;
593  GEOGRAPHICLIB_VOLATILE real xx2 = cbet2 * sbet1;
594  sbet12a = xx1 + xx2;
595  }
596 #else
597  real sbet12a = sbet2 * cbet1 + cbet2 * sbet1;
598 #endif
599  bool shortline = cbet12 >= 0 && sbet12 < real(0.5) &&
600  cbet2 * lam12 < real(0.5);
601  real omg12 = lam12;
602  if (shortline) {
603  real sbetm2 = Math::sq(sbet1 + sbet2);
604  // sin((bet1+bet2)/2)^2
605  // = (sbet1 + sbet2)^2 / ((sbet1 + sbet2)^2 + (cbet1 + cbet2)^2)
606  sbetm2 /= sbetm2 + Math::sq(cbet1 + cbet2);
607  dnm = sqrt(1 + _ep2 * sbetm2);
608  omg12 /= _f1 * dnm;
609  }
610  real somg12 = sin(omg12), comg12 = cos(omg12);
611 
612  salp1 = cbet2 * somg12;
613  calp1 = comg12 >= 0 ?
614  sbet12 + cbet2 * sbet1 * Math::sq(somg12) / (1 + comg12) :
615  sbet12a - cbet2 * sbet1 * Math::sq(somg12) / (1 - comg12);
616 
617  real
618  ssig12 = Math::hypot(salp1, calp1),
619  csig12 = sbet1 * sbet2 + cbet1 * cbet2 * comg12;
620 
621  if (shortline && ssig12 < _etol2) {
622  // really short lines
623  salp2 = cbet1 * somg12;
624  calp2 = sbet12 - cbet1 * sbet2 *
625  (comg12 >= 0 ? Math::sq(somg12) / (1 + comg12) : 1 - comg12);
626  SinCosNorm(salp2, calp2);
627  // Set return value
628  sig12 = atan2(ssig12, csig12);
629  } else if (abs(_n) > real(0.1) || // Skip astroid calc if too eccentric
630  csig12 >= 0 ||
631  ssig12 >= 6 * abs(_n) * Math::pi() * Math::sq(cbet1)) {
632  // Nothing to do, zeroth order spherical approximation is OK
633  } else {
634  // Scale lam12 and bet2 to x, y coordinate system where antipodal point
635  // is at origin and singular point is at y = 0, x = -1.
636  real y, lamscale, betscale;
637  // Volatile declaration needed to fix inverse case
638  // 56.320923501171 0 -56.320923501171 179.664747671772880215
639  // which otherwise fails with g++ 4.4.4 x86 -O3
641  if (_f >= 0) { // In fact f == 0 does not get here
642  // x = dlong, y = dlat
643  {
644  real k2 = Math::sq(sbet1) * _ep2;
645  E.Reset(-k2, -_ep2, 1 + k2, 1 + _ep2);
646  lamscale = _e2/_f1 * cbet1 * 2 * E.H();
647  }
648  betscale = lamscale * cbet1;
649 
650  x = (lam12 - Math::pi()) / lamscale;
651  y = sbet12a / betscale;
652  } else { // _f < 0
653  // x = dlat, y = dlong
654  real
655  cbet12a = cbet2 * cbet1 - sbet2 * sbet1,
656  bet12a = atan2(sbet12a, cbet12a);
657  real m12b, m0, dummy;
658  // In the case of lon12 = 180, this repeats a calculation made in
659  // Inverse.
660  Lengths(E, Math::pi() + bet12a,
661  sbet1, -cbet1, dn1, sbet2, cbet2, dn2,
662  cbet1, cbet2, dummy, m12b, m0, false,
663  dummy, dummy);
664  x = -1 + m12b / (cbet1 * cbet2 * m0 * Math::pi());
665  betscale = x < -real(0.01) ? sbet12a / x :
666  -_f * Math::sq(cbet1) * Math::pi();
667  lamscale = betscale / cbet1;
668  y = (lam12 - Math::pi()) / lamscale;
669  }
670 
671  if (y > -tol1_ && x > -1 - xthresh_) {
672  // strip near cut
673  // Need real(x) here to cast away the volatility of x for min/max
674  if (_f >= 0) {
675  salp1 = min(real(1), -real(x)); calp1 = - sqrt(1 - Math::sq(salp1));
676  } else {
677  calp1 = max(real(x > -tol1_ ? 0 : -1), real(x));
678  salp1 = sqrt(1 - Math::sq(calp1));
679  }
680  } else {
681  // Estimate alp1, by solving the astroid problem.
682  //
683  // Could estimate alpha1 = theta + pi/2, directly, i.e.,
684  // calp1 = y/k; salp1 = -x/(1+k); for _f >= 0
685  // calp1 = x/(1+k); salp1 = -y/k; for _f < 0 (need to check)
686  //
687  // However, it's better to estimate omg12 from astroid and use
688  // spherical formula to compute alp1. This reduces the mean number of
689  // Newton iterations for astroid cases from 2.24 (min 0, max 6) to 2.12
690  // (min 0 max 5). The changes in the number of iterations are as
691  // follows:
692  //
693  // change percent
694  // 1 5
695  // 0 78
696  // -1 16
697  // -2 0.6
698  // -3 0.04
699  // -4 0.002
700  //
701  // The histogram of iterations is (m = number of iterations estimating
702  // alp1 directly, n = number of iterations estimating via omg12, total
703  // number of trials = 148605):
704  //
705  // iter m n
706  // 0 148 186
707  // 1 13046 13845
708  // 2 93315 102225
709  // 3 36189 32341
710  // 4 5396 7
711  // 5 455 1
712  // 6 56 0
713  //
714  // Because omg12 is near pi, estimate work with omg12a = pi - omg12
715  real k = Astroid(x, y);
716  real
717  omg12a = lamscale * ( _f >= 0 ? -x * k/(1 + k) : -y * (1 + k)/k );
718  somg12 = sin(omg12a); comg12 = -cos(omg12a);
719  // Update spherical estimate of alp1 using omg12 instead of lam12
720  salp1 = cbet2 * somg12;
721  calp1 = sbet12a - cbet2 * sbet1 * Math::sq(somg12) / (1 - comg12);
722  }
723  }
724  if (salp1 > 0) // Sanity check on starting guess
725  SinCosNorm(salp1, calp1);
726  else {
727  salp1 = 1; calp1 = 0;
728  }
729  return sig12;
730  }
731 
732  Math::real GeodesicExact::Lambda12(real sbet1, real cbet1, real dn1,
733  real sbet2, real cbet2, real dn2,
734  real salp1, real calp1,
735  real& salp2, real& calp2,
736  real& sig12,
737  real& ssig1, real& csig1,
738  real& ssig2, real& csig2,
739  EllipticFunction& E,
740  real& omg12,
741  bool diffp, real& dlam12) const
742  {
743 
744  if (sbet1 == 0 && calp1 == 0)
745  // Break degeneracy of equatorial line. This case has already been
746  // handled.
747  calp1 = -tiny_;
748 
749  real
750  // sin(alp1) * cos(bet1) = sin(alp0)
751  salp0 = salp1 * cbet1,
752  calp0 = Math::hypot(calp1, salp1 * sbet1); // calp0 > 0
753 
754  real somg1, comg1, somg2, comg2, cchi1, cchi2, lam12;
755  // tan(bet1) = tan(sig1) * cos(alp1)
756  // tan(omg1) = sin(alp0) * tan(sig1) = tan(omg1)=tan(alp1)*sin(bet1)
757  ssig1 = sbet1; somg1 = salp0 * sbet1;
758  csig1 = comg1 = calp1 * cbet1;
759  // Without normalization we have schi1 = somg1.
760  cchi1 = _f1 * dn1 * comg1;
761  SinCosNorm(ssig1, csig1);
762  // SinCosNorm(somg1, comg1); -- don't need to normalize!
763  // SinCosNorm(schi1, cchi1); -- don't need to normalize!
764 
765  // Enforce symmetries in the case abs(bet2) = -bet1. Need to be careful
766  // about this case, since this can yield singularities in the Newton
767  // iteration.
768  // sin(alp2) * cos(bet2) = sin(alp0)
769  salp2 = cbet2 != cbet1 ? salp0 / cbet2 : salp1;
770  // calp2 = sqrt(1 - sq(salp2))
771  // = sqrt(sq(calp0) - sq(sbet2)) / cbet2
772  // and subst for calp0 and rearrange to give (choose positive sqrt
773  // to give alp2 in [0, pi/2]).
774  calp2 = cbet2 != cbet1 || abs(sbet2) != -sbet1 ?
775  sqrt(Math::sq(calp1 * cbet1) +
776  (cbet1 < -sbet1 ?
777  (cbet2 - cbet1) * (cbet1 + cbet2) :
778  (sbet1 - sbet2) * (sbet1 + sbet2))) / cbet2 :
779  abs(calp1);
780  // tan(bet2) = tan(sig2) * cos(alp2)
781  // tan(omg2) = sin(alp0) * tan(sig2).
782  ssig2 = sbet2; somg2 = salp0 * sbet2;
783  csig2 = comg2 = calp2 * cbet2;
784  // Without normalization we have schi2 = somg2.
785  cchi2 = _f1 * dn2 * comg2;
786  SinCosNorm(ssig2, csig2);
787  // SinCosNorm(somg2, comg2); -- don't need to normalize!
788  // SinCosNorm(schi2, cchi2); -- don't need to normalize!
789 
790  // sig12 = sig2 - sig1, limit to [0, pi]
791  sig12 = atan2(max(csig1 * ssig2 - ssig1 * csig2, real(0)),
792  csig1 * csig2 + ssig1 * ssig2);
793 
794  // omg12 = omg2 - omg1, limit to [0, pi]
795  omg12 = atan2(max(comg1 * somg2 - somg1 * comg2, real(0)),
796  comg1 * comg2 + somg1 * somg2);
797  real k2 = Math::sq(calp0) * _ep2;
798  E.Reset(-k2, -_ep2, 1 + k2, 1 + _ep2);
799  real chi12 = atan2(max(cchi1 * somg2 - somg1 * cchi2, real(0)),
800  cchi1 * cchi2 + somg1 * somg2);
801  lam12 = chi12 -
802  _e2/_f1 * salp0 * E.H() / (Math::pi() / 2) *
803  (sig12 + E.deltaH(ssig2, csig2, dn2) - E.deltaH(ssig1, csig1, dn1) );
804 
805  if (diffp) {
806  if (calp2 == 0)
807  dlam12 = - 2 * _f1 * dn1 / sbet1;
808  else {
809  real dummy;
810  Lengths(E, sig12, ssig1, csig1, dn1, ssig2, csig2, dn2,
811  cbet1, cbet2, dummy, dlam12, dummy,
812  false, dummy, dummy);
813  dlam12 *= _f1 / (calp2 * cbet2);
814  }
815  }
816 
817  return lam12;
818  }
819 
820  void GeodesicExact::C4f(real eps, real c[]) const {
821  // Evaluate C4 coeffs by Horner's method
822  // Elements c[0] thru c[nC4_ - 1] are set
823  for (int j = nC4x_, k = nC4_; k; ) {
824  real t = 0;
825  for (int i = nC4_ - k + 1; i; --i)
826  t = eps * t + _C4x[--j];
827  c[--k] = t;
828  }
829 
830  real mult = 1;
831  for (int k = 1; k < nC4_; ) {
832  mult *= eps;
833  c[k++] *= mult;
834  }
835  }
836 
837  // Geodesic.cpp contains explicit expressions for _C4x[l]. However this
838  // results in extraordinarily long compiler times with real = quad (7 mins)
839  // or mpreal (15 mins). So instead we evaluate _C4x[l] by using the Horner
840  // recursion with the coefficient stored in an array by rawC4coeff.
841 
842  void GeodesicExact::C4coeff() {
843  const real* cc = rawC4coeff();
844  // Coefficients for C[4,m]
845  for (int m = 0, k = 0, h = 0; m < nC4_; ++m) {
846  // eps^j coefficient
847  for (int j = m; j < nC4_; ++j) {
848  real t = 0;
849  // n^l coefficient
850  for (int l = nC4_ - j; l--;)
851  t = _n * t + cc[h++];
852  _C4x[k++] = t/cc[h++];
853  }
854  }
855  return;
856  }
857 
858 } // namespace GeographicLib