GeographicLib  1.42
Geodesic.cpp
Go to the documentation of this file.
1 /**
2  * \file Geodesic.cpp
3  * \brief Implementation for GeographicLib::Geodesic class
4  *
5  * Copyright (c) Charles Karney (2009-2015) <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 
42  Geodesic::Geodesic(real a, real f)
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  Math::eatanhe(real(1), (_f < 0 ? -1 : 1) * sqrt(abs(_e2))) / _e2)
66  / 2) // authalic radius squared
67  // The sig12 threshold for "really short". Using the auxiliary sphere
68  // solution with dnm computed at (bet1 + bet2) / 2, the relative error in
69  // the azimuth consistency check is sig12^2 * abs(f) * min(1, 1-f/2) / 2.
70  // (Error measured for 1/100 < b/a < 100 and abs(f) >= 1/1000. For a
71  // given f and sig12, the max error occurs for lines near the pole. If
72  // the old rule for computing dnm = (dn1 + dn2)/2 is used, then the error
73  // increases by a factor of 2.) Setting this equal to epsilon gives
74  // sig12 = etol2. Here 0.1 is a safety factor (error decreased by 100)
75  // and max(0.001, abs(f)) stops etol2 getting too large in the nearly
76  // spherical case.
77  , _etol2(0.1 * tol2_ /
78  sqrt( max(real(0.001), abs(_f)) * min(real(1), 1 - _f/2) / 2 ))
79  {
80  if (!(Math::isfinite(_a) && _a > 0))
81  throw GeographicErr("Major radius is not positive");
82  if (!(Math::isfinite(_b) && _b > 0))
83  throw GeographicErr("Minor radius is not positive");
84  A3coeff();
85  C3coeff();
86  C4coeff();
87  }
88 
90  static const Geodesic wgs84(Constants::WGS84_a(), Constants::WGS84_f());
91  return wgs84;
92  }
93 
94  Math::real Geodesic::SinCosSeries(bool sinp,
95  real sinx, real cosx,
96  const real c[], int n) {
97  // Evaluate
98  // y = sinp ? sum(c[i] * sin( 2*i * x), i, 1, n) :
99  // sum(c[i] * cos((2*i+1) * x), i, 0, n-1)
100  // using Clenshaw summation. N.B. c[0] is unused for sin series
101  // Approx operation count = (n + 5) mult and (2 * n + 2) add
102  c += (n + sinp); // Point to one beyond last element
103  real
104  ar = 2 * (cosx - sinx) * (cosx + sinx), // 2 * cos(2 * x)
105  y0 = n & 1 ? *--c : 0, y1 = 0; // accumulators for sum
106  // Now n is even
107  n /= 2;
108  while (n--) {
109  // Unroll loop x 2, so accumulators return to their original role
110  y1 = ar * y0 - y1 + *--c;
111  y0 = ar * y1 - y0 + *--c;
112  }
113  return sinp
114  ? 2 * sinx * cosx * y0 // sin(2 * x) * y0
115  : cosx * (y0 - y1); // cos(x) * (y0 - y1)
116  }
117 
118  GeodesicLine Geodesic::Line(real lat1, real lon1, real azi1, unsigned caps)
119  const {
120  return GeodesicLine(*this, lat1, lon1, azi1, caps);
121  }
122 
123  Math::real Geodesic::GenDirect(real lat1, real lon1, real azi1,
124  bool arcmode, real s12_a12, unsigned outmask,
125  real& lat2, real& lon2, real& azi2,
126  real& s12, real& m12, real& M12, real& M21,
127  real& S12) const {
128  return GeodesicLine(*this, lat1, lon1, azi1,
129  // Automatically supply DISTANCE_IN if necessary
130  outmask | (arcmode ? NONE : DISTANCE_IN))
131  . // Note the dot!
132  GenPosition(arcmode, s12_a12, outmask,
133  lat2, lon2, azi2, s12, m12, M12, M21, S12);
134  }
135 
136  Math::real Geodesic::GenInverse(real lat1, real lon1, real lat2, real lon2,
137  unsigned outmask,
138  real& s12, real& azi1, real& azi2,
139  real& m12, real& M12, real& M21, real& S12)
140  const {
141  outmask &= OUT_MASK;
142  // Compute longitude difference (AngDiff does this carefully). Result is
143  // in [-180, 180] but -180 is only for west-going geodesics. 180 is for
144  // east-going and meridional geodesics.
145  real lon12 = Math::AngDiff(Math::AngNormalize(lon1),
146  Math::AngNormalize(lon2));
147  // If very close to being on the same half-meridian, then make it so.
148  lon12 = Math::AngRound(lon12);
149  // Make longitude difference positive.
150  int lonsign = lon12 >= 0 ? 1 : -1;
151  lon12 *= lonsign;
152  // If really close to the equator, treat as on equator.
153  lat1 = Math::AngRound(lat1);
154  lat2 = Math::AngRound(lat2);
155  // Swap points so that point with higher (abs) latitude is point 1
156  int swapp = abs(lat1) >= abs(lat2) ? 1 : -1;
157  if (swapp < 0) {
158  lonsign *= -1;
159  swap(lat1, lat2);
160  }
161  // Make lat1 <= 0
162  int latsign = lat1 < 0 ? 1 : -1;
163  lat1 *= latsign;
164  lat2 *= latsign;
165  // Now we have
166  //
167  // 0 <= lon12 <= 180
168  // -90 <= lat1 <= 0
169  // lat1 <= lat2 <= -lat1
170  //
171  // longsign, swapp, latsign register the transformation to bring the
172  // coordinates to this canonical form. In all cases, 1 means no change was
173  // made. We make these transformations so that there are few cases to
174  // check, e.g., on verifying quadrants in atan2. In addition, this
175  // enforces some symmetries in the results returned.
176 
177  real phi, sbet1, cbet1, sbet2, cbet2, s12x, m12x;
178 
179  phi = lat1 * Math::degree();
180  // Ensure cbet1 = +epsilon at poles
181  sbet1 = _f1 * sin(phi);
182  cbet1 = lat1 == -90 ? tiny_ : cos(phi);
183  Math::norm(sbet1, cbet1);
184 
185  phi = lat2 * Math::degree();
186  // Ensure cbet2 = +epsilon at poles
187  sbet2 = _f1 * sin(phi);
188  cbet2 = abs(lat2) == 90 ? tiny_ : cos(phi);
189  Math::norm(sbet2, cbet2);
190 
191  // If cbet1 < -sbet1, then cbet2 - cbet1 is a sensitive measure of the
192  // |bet1| - |bet2|. Alternatively (cbet1 >= -sbet1), abs(sbet2) + sbet1 is
193  // a better measure. This logic is used in assigning calp2 in Lambda12.
194  // Sometimes these quantities vanish and in that case we force bet2 = +/-
195  // bet1 exactly. An example where is is necessary is the inverse problem
196  // 48.522876735459 0 -48.52287673545898293 179.599720456223079643
197  // which failed with Visual Studio 10 (Release and Debug)
198 
199  if (cbet1 < -sbet1) {
200  if (cbet2 == cbet1)
201  sbet2 = sbet2 < 0 ? sbet1 : -sbet1;
202  } else {
203  if (abs(sbet2) == -sbet1)
204  cbet2 = cbet1;
205  }
206 
207  real
208  dn1 = sqrt(1 + _ep2 * Math::sq(sbet1)),
209  dn2 = sqrt(1 + _ep2 * Math::sq(sbet2));
210 
211  real
212  lam12 = lon12 * Math::degree(),
213  slam12 = abs(lon12) == 180 ? 0 : sin(lam12),
214  clam12 = cos(lam12); // lon12 == 90 isn't interesting
215 
216  // initial values to suppress warning
217  real a12, sig12, calp1, salp1, calp2 = 0, salp2 = 0;
218  // index zero elements of these arrays are unused
219  real C1a[nC1_ + 1], C2a[nC2_ + 1], C3a[nC3_];
220 
221  bool meridian = lat1 == -90 || slam12 == 0;
222 
223  if (meridian) {
224 
225  // Endpoints are on a single full meridian, so the geodesic might lie on
226  // a meridian.
227 
228  calp1 = clam12; salp1 = slam12; // Head to the target longitude
229  calp2 = 1; salp2 = 0; // At the target we're heading north
230 
231  real
232  // tan(bet) = tan(sig) * cos(alp)
233  ssig1 = sbet1, csig1 = calp1 * cbet1,
234  ssig2 = sbet2, csig2 = calp2 * cbet2;
235 
236  // sig12 = sig2 - sig1
237  sig12 = atan2(max(csig1 * ssig2 - ssig1 * csig2, real(0)),
238  csig1 * csig2 + ssig1 * ssig2);
239  {
240  real dummy;
241  Lengths(_n, sig12, ssig1, csig1, dn1, ssig2, csig2, dn2,
242  cbet1, cbet2, s12x, m12x, dummy,
243  (outmask & GEODESICSCALE) != 0U, M12, M21, C1a, C2a);
244  }
245  // Add the check for sig12 since zero length geodesics might yield m12 <
246  // 0. Test case was
247  //
248  // echo 20.001 0 20.001 0 | GeodSolve -i
249  //
250  // In fact, we will have sig12 > pi/2 for meridional geodesic which is
251  // not a shortest path.
252  if (sig12 < 1 || m12x >= 0) {
253  m12x *= _b;
254  s12x *= _b;
255  a12 = sig12 / Math::degree();
256  } else
257  // m12 < 0, i.e., prolate and too close to anti-podal
258  meridian = false;
259  }
260 
261  real omg12 = 0; // initial value to suppress warning
262  if (!meridian &&
263  sbet1 == 0 && // and sbet2 == 0
264  // Mimic the way Lambda12 works with calp1 = 0
265  (_f <= 0 || lam12 <= Math::pi() - _f * Math::pi())) {
266 
267  // Geodesic runs along equator
268  calp1 = calp2 = 0; salp1 = salp2 = 1;
269  s12x = _a * lam12;
270  sig12 = omg12 = lam12 / _f1;
271  m12x = _b * sin(sig12);
272  if (outmask & GEODESICSCALE)
273  M12 = M21 = cos(sig12);
274  a12 = lon12 / _f1;
275 
276  } else if (!meridian) {
277 
278  // Now point1 and point2 belong within a hemisphere bounded by a
279  // meridian and geodesic is neither meridional or equatorial.
280 
281  // Figure a starting point for Newton's method
282  real dnm;
283  sig12 = InverseStart(sbet1, cbet1, dn1, sbet2, cbet2, dn2,
284  lam12,
285  salp1, calp1, salp2, calp2, dnm,
286  C1a, C2a);
287 
288  if (sig12 >= 0) {
289  // Short lines (InverseStart sets salp2, calp2, dnm)
290  s12x = sig12 * _b * dnm;
291  m12x = Math::sq(dnm) * _b * sin(sig12 / dnm);
292  if (outmask & GEODESICSCALE)
293  M12 = M21 = cos(sig12 / dnm);
294  a12 = sig12 / Math::degree();
295  omg12 = lam12 / (_f1 * dnm);
296  } else {
297 
298  // Newton's method. This is a straightforward solution of f(alp1) =
299  // lambda12(alp1) - lam12 = 0 with one wrinkle. f(alp) has exactly one
300  // root in the interval (0, pi) and its derivative is positive at the
301  // root. Thus f(alp) is positive for alp > alp1 and negative for alp <
302  // alp1. During the course of the iteration, a range (alp1a, alp1b) is
303  // maintained which brackets the root and with each evaluation of
304  // f(alp) the range is shrunk, if possible. Newton's method is
305  // restarted whenever the derivative of f is negative (because the new
306  // value of alp1 is then further from the solution) or if the new
307  // estimate of alp1 lies outside (0,pi); in this case, the new starting
308  // guess is taken to be (alp1a + alp1b) / 2.
309  //
310  // initial values to suppress warnings (if loop is executed 0 times)
311  real ssig1 = 0, csig1 = 0, ssig2 = 0, csig2 = 0, eps = 0;
312  unsigned numit = 0;
313  // Bracketing range
314  real salp1a = tiny_, calp1a = 1, salp1b = tiny_, calp1b = -1;
315  for (bool tripn = false, tripb = false;
316  numit < maxit2_ || GEOGRAPHICLIB_PANIC;
317  ++numit) {
318  // the WGS84 test set: mean = 1.47, sd = 1.25, max = 16
319  // WGS84 and random input: mean = 2.85, sd = 0.60
320  real dv;
321  real v = Lambda12(sbet1, cbet1, dn1, sbet2, cbet2, dn2, salp1, calp1,
322  salp2, calp2, sig12, ssig1, csig1, ssig2, csig2,
323  eps, omg12, numit < maxit1_, dv, C1a, C2a, C3a)
324  - lam12;
325  // 2 * tol0 is approximately 1 ulp for a number in [0, pi].
326  // Reversed test to allow escape with NaNs
327  if (tripb || !(abs(v) >= (tripn ? 8 : 2) * tol0_)) break;
328  // Update bracketing values
329  if (v > 0 && (numit > maxit1_ || calp1/salp1 > calp1b/salp1b))
330  { salp1b = salp1; calp1b = calp1; }
331  else if (v < 0 && (numit > maxit1_ || calp1/salp1 < calp1a/salp1a))
332  { salp1a = salp1; calp1a = calp1; }
333  if (numit < maxit1_ && dv > 0) {
334  real
335  dalp1 = -v/dv;
336  real
337  sdalp1 = sin(dalp1), cdalp1 = cos(dalp1),
338  nsalp1 = salp1 * cdalp1 + calp1 * sdalp1;
339  if (nsalp1 > 0 && abs(dalp1) < Math::pi()) {
340  calp1 = calp1 * cdalp1 - salp1 * sdalp1;
341  salp1 = nsalp1;
342  Math::norm(salp1, calp1);
343  // In some regimes we don't get quadratic convergence because
344  // slope -> 0. So use convergence conditions based on epsilon
345  // instead of sqrt(epsilon).
346  tripn = abs(v) <= 16 * tol0_;
347  continue;
348  }
349  }
350  // Either dv was not postive or updated value was outside legal
351  // range. Use the midpoint of the bracket as the next estimate.
352  // This mechanism is not needed for the WGS84 ellipsoid, but it does
353  // catch problems with more eccentric ellipsoids. Its efficacy is
354  // such for the WGS84 test set with the starting guess set to alp1 =
355  // 90deg:
356  // the WGS84 test set: mean = 5.21, sd = 3.93, max = 24
357  // WGS84 and random input: mean = 4.74, sd = 0.99
358  salp1 = (salp1a + salp1b)/2;
359  calp1 = (calp1a + calp1b)/2;
360  Math::norm(salp1, calp1);
361  tripn = false;
362  tripb = (abs(salp1a - salp1) + (calp1a - calp1) < tolb_ ||
363  abs(salp1 - salp1b) + (calp1 - calp1b) < tolb_);
364  }
365  {
366  real dummy;
367  Lengths(eps, sig12, ssig1, csig1, dn1, ssig2, csig2, dn2,
368  cbet1, cbet2, s12x, m12x, dummy,
369  (outmask & GEODESICSCALE) != 0U, M12, M21, C1a, C2a);
370  }
371  m12x *= _b;
372  s12x *= _b;
373  a12 = sig12 / Math::degree();
374  omg12 = lam12 - omg12;
375  }
376  }
377 
378  if (outmask & DISTANCE)
379  s12 = 0 + s12x; // Convert -0 to 0
380 
381  if (outmask & REDUCEDLENGTH)
382  m12 = 0 + m12x; // Convert -0 to 0
383 
384  if (outmask & AREA) {
385  real
386  // From Lambda12: sin(alp1) * cos(bet1) = sin(alp0)
387  salp0 = salp1 * cbet1,
388  calp0 = Math::hypot(calp1, salp1 * sbet1); // calp0 > 0
389  real alp12;
390  if (calp0 != 0 && salp0 != 0) {
391  real
392  // From Lambda12: tan(bet) = tan(sig) * cos(alp)
393  ssig1 = sbet1, csig1 = calp1 * cbet1,
394  ssig2 = sbet2, csig2 = calp2 * cbet2,
395  k2 = Math::sq(calp0) * _ep2,
396  eps = k2 / (2 * (1 + sqrt(1 + k2)) + k2),
397  // Multiplier = a^2 * e^2 * cos(alpha0) * sin(alpha0).
398  A4 = Math::sq(_a) * calp0 * salp0 * _e2;
399  Math::norm(ssig1, csig1);
400  Math::norm(ssig2, csig2);
401  real C4a[nC4_];
402  C4f(eps, C4a);
403  real
404  B41 = SinCosSeries(false, ssig1, csig1, C4a, nC4_),
405  B42 = SinCosSeries(false, ssig2, csig2, C4a, nC4_);
406  S12 = A4 * (B42 - B41);
407  } else
408  // Avoid problems with indeterminate sig1, sig2 on equator
409  S12 = 0;
410 
411  if (!meridian &&
412  omg12 < real(0.75) * Math::pi() && // Long difference too big
413  sbet2 - sbet1 < real(1.75)) { // Lat difference too big
414  // Use tan(Gamma/2) = tan(omg12/2)
415  // * (tan(bet1/2)+tan(bet2/2))/(1+tan(bet1/2)*tan(bet2/2))
416  // with tan(x/2) = sin(x)/(1+cos(x))
417  real
418  somg12 = sin(omg12), domg12 = 1 + cos(omg12),
419  dbet1 = 1 + cbet1, dbet2 = 1 + cbet2;
420  alp12 = 2 * atan2( somg12 * ( sbet1 * dbet2 + sbet2 * dbet1 ),
421  domg12 * ( sbet1 * sbet2 + dbet1 * dbet2 ) );
422  } else {
423  // alp12 = alp2 - alp1, used in atan2 so no need to normalize
424  real
425  salp12 = salp2 * calp1 - calp2 * salp1,
426  calp12 = calp2 * calp1 + salp2 * salp1;
427  // The right thing appears to happen if alp1 = +/-180 and alp2 = 0, viz
428  // salp12 = -0 and alp12 = -180. However this depends on the sign
429  // being attached to 0 correctly. The following ensures the correct
430  // behavior.
431  if (salp12 == 0 && calp12 < 0) {
432  salp12 = tiny_ * calp1;
433  calp12 = -1;
434  }
435  alp12 = atan2(salp12, calp12);
436  }
437  S12 += _c2 * alp12;
438  S12 *= swapp * lonsign * latsign;
439  // Convert -0 to 0
440  S12 += 0;
441  }
442 
443  // Convert calp, salp to azimuth accounting for lonsign, swapp, latsign.
444  if (swapp < 0) {
445  swap(salp1, salp2);
446  swap(calp1, calp2);
447  if (outmask & GEODESICSCALE)
448  swap(M12, M21);
449  }
450 
451  salp1 *= swapp * lonsign; calp1 *= swapp * latsign;
452  salp2 *= swapp * lonsign; calp2 *= swapp * latsign;
453 
454  if (outmask & AZIMUTH) {
455  // minus signs give range [-180, 180). 0- converts -0 to +0.
456  azi1 = Math::atan2d(salp1, calp1);
457  azi2 = Math::atan2d(salp2, calp2);
458  }
459 
460  // Returned value in [0, 180]
461  return a12;
462  }
463 
464  void Geodesic::Lengths(real eps, real sig12,
465  real ssig1, real csig1, real dn1,
466  real ssig2, real csig2, real dn2,
467  real cbet1, real cbet2,
468  real& s12b, real& m12b, real& m0,
469  bool scalep, real& M12, real& M21,
470  // Scratch areas of the right size
471  real C1a[], real C2a[]) const {
472  // Return m12b = (reduced length)/_b; also calculate s12b = distance/_b,
473  // and m0 = coefficient of secular term in expression for reduced length.
474  C1f(eps, C1a);
475  C2f(eps, C2a);
476  real
477  A1m1 = A1m1f(eps),
478  AB1 = (1 + A1m1) * (SinCosSeries(true, ssig2, csig2, C1a, nC1_) -
479  SinCosSeries(true, ssig1, csig1, C1a, nC1_)),
480  A2m1 = A2m1f(eps),
481  AB2 = (1 + A2m1) * (SinCosSeries(true, ssig2, csig2, C2a, nC2_) -
482  SinCosSeries(true, ssig1, csig1, C2a, nC2_));
483  m0 = A1m1 - A2m1;
484  real J12 = m0 * sig12 + (AB1 - AB2);
485  // Missing a factor of _b.
486  // Add parens around (csig1 * ssig2) and (ssig1 * csig2) to ensure accurate
487  // cancellation in the case of coincident points.
488  m12b = dn2 * (csig1 * ssig2) - dn1 * (ssig1 * csig2) - csig1 * csig2 * J12;
489  // Missing a factor of _b
490  s12b = (1 + A1m1) * sig12 + AB1;
491  if (scalep) {
492  real csig12 = csig1 * csig2 + ssig1 * ssig2;
493  real t = _ep2 * (cbet1 - cbet2) * (cbet1 + cbet2) / (dn1 + dn2);
494  M12 = csig12 + (t * ssig2 - csig2 * J12) * ssig1 / dn1;
495  M21 = csig12 - (t * ssig1 - csig1 * J12) * ssig2 / dn2;
496  }
497  }
498 
499  Math::real Geodesic::Astroid(real x, real y) {
500  // Solve k^4+2*k^3-(x^2+y^2-1)*k^2-2*y^2*k-y^2 = 0 for positive root k.
501  // This solution is adapted from Geocentric::Reverse.
502  real k;
503  real
504  p = Math::sq(x),
505  q = Math::sq(y),
506  r = (p + q - 1) / 6;
507  if ( !(q == 0 && r <= 0) ) {
508  real
509  // Avoid possible division by zero when r = 0 by multiplying equations
510  // for s and t by r^3 and r, resp.
511  S = p * q / 4, // S = r^3 * s
512  r2 = Math::sq(r),
513  r3 = r * r2,
514  // The discrimant of the quadratic equation for T3. This is zero on
515  // the evolute curve p^(1/3)+q^(1/3) = 1
516  disc = S * (S + 2 * r3);
517  real u = r;
518  if (disc >= 0) {
519  real T3 = S + r3;
520  // Pick the sign on the sqrt to maximize abs(T3). This minimizes loss
521  // of precision due to cancellation. The result is unchanged because
522  // of the way the T is used in definition of u.
523  T3 += T3 < 0 ? -sqrt(disc) : sqrt(disc); // T3 = (r * t)^3
524  // N.B. cbrt always returns the real root. cbrt(-8) = -2.
525  real T = Math::cbrt(T3); // T = r * t
526  // T can be zero; but then r2 / T -> 0.
527  u += T + (T ? r2 / T : 0);
528  } else {
529  // T is complex, but the way u is defined the result is real.
530  real ang = atan2(sqrt(-disc), -(S + r3));
531  // There are three possible cube roots. We choose the root which
532  // avoids cancellation. Note that disc < 0 implies that r < 0.
533  u += 2 * r * cos(ang / 3);
534  }
535  real
536  v = sqrt(Math::sq(u) + q), // guaranteed positive
537  // Avoid loss of accuracy when u < 0.
538  uv = u < 0 ? q / (v - u) : u + v, // u+v, guaranteed positive
539  w = (uv - q) / (2 * v); // positive?
540  // Rearrange expression for k to avoid loss of accuracy due to
541  // subtraction. Division by 0 not possible because uv > 0, w >= 0.
542  k = uv / (sqrt(uv + Math::sq(w)) + w); // guaranteed positive
543  } else { // q == 0 && r <= 0
544  // y = 0 with |x| <= 1. Handle this case directly.
545  // for y small, positive root is k = abs(y)/sqrt(1-x^2)
546  k = 0;
547  }
548  return k;
549  }
550 
551  Math::real Geodesic::InverseStart(real sbet1, real cbet1, real dn1,
552  real sbet2, real cbet2, real dn2,
553  real lam12,
554  real& salp1, real& calp1,
555  // Only updated if return val >= 0
556  real& salp2, real& calp2,
557  // Only updated for short lines
558  real& dnm,
559  // Scratch areas of the right size
560  real C1a[], real C2a[]) const {
561  // Return a starting point for Newton's method in salp1 and calp1 (function
562  // value is -1). If Newton's method doesn't need to be used, return also
563  // salp2 and calp2 and function value is sig12.
564  real
565  sig12 = -1, // Return value
566  // bet12 = bet2 - bet1 in [0, pi); bet12a = bet2 + bet1 in (-pi, 0]
567  sbet12 = sbet2 * cbet1 - cbet2 * sbet1,
568  cbet12 = cbet2 * cbet1 + sbet2 * sbet1;
569 #if defined(__GNUC__) && __GNUC__ == 4 && \
570  (__GNUC_MINOR__ < 6 || defined(__MINGW32__))
571  // Volatile declaration needed to fix inverse cases
572  // 88.202499451857 0 -88.202499451857 179.981022032992859592
573  // 89.262080389218 0 -89.262080389218 179.992207982775375662
574  // 89.333123580033 0 -89.333123580032997687 179.99295812360148422
575  // which otherwise fail with g++ 4.4.4 x86 -O3 (Linux)
576  // and g++ 4.4.0 (mingw) and g++ 4.6.1 (tdm mingw).
577  real sbet12a;
578  {
579  GEOGRAPHICLIB_VOLATILE real xx1 = sbet2 * cbet1;
580  GEOGRAPHICLIB_VOLATILE real xx2 = cbet2 * sbet1;
581  sbet12a = xx1 + xx2;
582  }
583 #else
584  real sbet12a = sbet2 * cbet1 + cbet2 * sbet1;
585 #endif
586  bool shortline = cbet12 >= 0 && sbet12 < real(0.5) &&
587  cbet2 * lam12 < real(0.5);
588  real omg12 = lam12;
589  if (shortline) {
590  real sbetm2 = Math::sq(sbet1 + sbet2);
591  // sin((bet1+bet2)/2)^2
592  // = (sbet1 + sbet2)^2 / ((sbet1 + sbet2)^2 + (cbet1 + cbet2)^2)
593  sbetm2 /= sbetm2 + Math::sq(cbet1 + cbet2);
594  dnm = sqrt(1 + _ep2 * sbetm2);
595  omg12 /= _f1 * dnm;
596  }
597  real somg12 = sin(omg12), comg12 = cos(omg12);
598 
599  salp1 = cbet2 * somg12;
600  calp1 = comg12 >= 0 ?
601  sbet12 + cbet2 * sbet1 * Math::sq(somg12) / (1 + comg12) :
602  sbet12a - cbet2 * sbet1 * Math::sq(somg12) / (1 - comg12);
603 
604  real
605  ssig12 = Math::hypot(salp1, calp1),
606  csig12 = sbet1 * sbet2 + cbet1 * cbet2 * comg12;
607 
608  if (shortline && ssig12 < _etol2) {
609  // really short lines
610  salp2 = cbet1 * somg12;
611  calp2 = sbet12 - cbet1 * sbet2 *
612  (comg12 >= 0 ? Math::sq(somg12) / (1 + comg12) : 1 - comg12);
613  Math::norm(salp2, calp2);
614  // Set return value
615  sig12 = atan2(ssig12, csig12);
616  } else if (abs(_n) > real(0.1) || // Skip astroid calc if too eccentric
617  csig12 >= 0 ||
618  ssig12 >= 6 * abs(_n) * Math::pi() * Math::sq(cbet1)) {
619  // Nothing to do, zeroth order spherical approximation is OK
620  } else {
621  // Scale lam12 and bet2 to x, y coordinate system where antipodal point
622  // is at origin and singular point is at y = 0, x = -1.
623  real y, lamscale, betscale;
624  // Volatile declaration needed to fix inverse case
625  // 56.320923501171 0 -56.320923501171 179.664747671772880215
626  // which otherwise fails with g++ 4.4.4 x86 -O3
628  if (_f >= 0) { // In fact f == 0 does not get here
629  // x = dlong, y = dlat
630  {
631  real
632  k2 = Math::sq(sbet1) * _ep2,
633  eps = k2 / (2 * (1 + sqrt(1 + k2)) + k2);
634  lamscale = _f * cbet1 * A3f(eps) * Math::pi();
635  }
636  betscale = lamscale * cbet1;
637 
638  x = (lam12 - Math::pi()) / lamscale;
639  y = sbet12a / betscale;
640  } else { // _f < 0
641  // x = dlat, y = dlong
642  real
643  cbet12a = cbet2 * cbet1 - sbet2 * sbet1,
644  bet12a = atan2(sbet12a, cbet12a);
645  real m12b, m0, dummy;
646  // In the case of lon12 = 180, this repeats a calculation made in
647  // Inverse.
648  Lengths(_n, Math::pi() + bet12a,
649  sbet1, -cbet1, dn1, sbet2, cbet2, dn2,
650  cbet1, cbet2, dummy, m12b, m0, false,
651  dummy, dummy, C1a, C2a);
652  x = -1 + m12b / (cbet1 * cbet2 * m0 * Math::pi());
653  betscale = x < -real(0.01) ? sbet12a / x :
654  -_f * Math::sq(cbet1) * Math::pi();
655  lamscale = betscale / cbet1;
656  y = (lam12 - Math::pi()) / lamscale;
657  }
658 
659  if (y > -tol1_ && x > -1 - xthresh_) {
660  // strip near cut
661  // Need real(x) here to cast away the volatility of x for min/max
662  if (_f >= 0) {
663  salp1 = min(real(1), -real(x)); calp1 = - sqrt(1 - Math::sq(salp1));
664  } else {
665  calp1 = max(real(x > -tol1_ ? 0 : -1), real(x));
666  salp1 = sqrt(1 - Math::sq(calp1));
667  }
668  } else {
669  // Estimate alp1, by solving the astroid problem.
670  //
671  // Could estimate alpha1 = theta + pi/2, directly, i.e.,
672  // calp1 = y/k; salp1 = -x/(1+k); for _f >= 0
673  // calp1 = x/(1+k); salp1 = -y/k; for _f < 0 (need to check)
674  //
675  // However, it's better to estimate omg12 from astroid and use
676  // spherical formula to compute alp1. This reduces the mean number of
677  // Newton iterations for astroid cases from 2.24 (min 0, max 6) to 2.12
678  // (min 0 max 5). The changes in the number of iterations are as
679  // follows:
680  //
681  // change percent
682  // 1 5
683  // 0 78
684  // -1 16
685  // -2 0.6
686  // -3 0.04
687  // -4 0.002
688  //
689  // The histogram of iterations is (m = number of iterations estimating
690  // alp1 directly, n = number of iterations estimating via omg12, total
691  // number of trials = 148605):
692  //
693  // iter m n
694  // 0 148 186
695  // 1 13046 13845
696  // 2 93315 102225
697  // 3 36189 32341
698  // 4 5396 7
699  // 5 455 1
700  // 6 56 0
701  //
702  // Because omg12 is near pi, estimate work with omg12a = pi - omg12
703  real k = Astroid(x, y);
704  real
705  omg12a = lamscale * ( _f >= 0 ? -x * k/(1 + k) : -y * (1 + k)/k );
706  somg12 = sin(omg12a); comg12 = -cos(omg12a);
707  // Update spherical estimate of alp1 using omg12 instead of lam12
708  salp1 = cbet2 * somg12;
709  calp1 = sbet12a - cbet2 * sbet1 * Math::sq(somg12) / (1 - comg12);
710  }
711  }
712  // Sanity check on starting guess. Backwards check allows NaN through.
713  if (!(salp1 <= 0))
714  Math::norm(salp1, calp1);
715  else {
716  salp1 = 1; calp1 = 0;
717  }
718  return sig12;
719  }
720 
721  Math::real Geodesic::Lambda12(real sbet1, real cbet1, real dn1,
722  real sbet2, real cbet2, real dn2,
723  real salp1, real calp1,
724  real& salp2, real& calp2,
725  real& sig12,
726  real& ssig1, real& csig1,
727  real& ssig2, real& csig2,
728  real& eps, real& domg12,
729  bool diffp, real& dlam12,
730  // Scratch areas of the right size
731  real C1a[], real C2a[], real C3a[]) const {
732 
733  if (sbet1 == 0 && calp1 == 0)
734  // Break degeneracy of equatorial line. This case has already been
735  // handled.
736  calp1 = -tiny_;
737 
738  real
739  // sin(alp1) * cos(bet1) = sin(alp0)
740  salp0 = salp1 * cbet1,
741  calp0 = Math::hypot(calp1, salp1 * sbet1); // calp0 > 0
742 
743  real somg1, comg1, somg2, comg2, omg12, lam12;
744  // tan(bet1) = tan(sig1) * cos(alp1)
745  // tan(omg1) = sin(alp0) * tan(sig1) = tan(omg1)=tan(alp1)*sin(bet1)
746  ssig1 = sbet1; somg1 = salp0 * sbet1;
747  csig1 = comg1 = calp1 * cbet1;
748  Math::norm(ssig1, csig1);
749  // Math::norm(somg1, comg1); -- don't need to normalize!
750 
751  // Enforce symmetries in the case abs(bet2) = -bet1. Need to be careful
752  // about this case, since this can yield singularities in the Newton
753  // iteration.
754  // sin(alp2) * cos(bet2) = sin(alp0)
755  salp2 = cbet2 != cbet1 ? salp0 / cbet2 : salp1;
756  // calp2 = sqrt(1 - sq(salp2))
757  // = sqrt(sq(calp0) - sq(sbet2)) / cbet2
758  // and subst for calp0 and rearrange to give (choose positive sqrt
759  // to give alp2 in [0, pi/2]).
760  calp2 = cbet2 != cbet1 || abs(sbet2) != -sbet1 ?
761  sqrt(Math::sq(calp1 * cbet1) +
762  (cbet1 < -sbet1 ?
763  (cbet2 - cbet1) * (cbet1 + cbet2) :
764  (sbet1 - sbet2) * (sbet1 + sbet2))) / cbet2 :
765  abs(calp1);
766  // tan(bet2) = tan(sig2) * cos(alp2)
767  // tan(omg2) = sin(alp0) * tan(sig2).
768  ssig2 = sbet2; somg2 = salp0 * sbet2;
769  csig2 = comg2 = calp2 * cbet2;
770  Math::norm(ssig2, csig2);
771  // Math::norm(somg2, comg2); -- don't need to normalize!
772 
773  // sig12 = sig2 - sig1, limit to [0, pi]
774  sig12 = atan2(max(csig1 * ssig2 - ssig1 * csig2, real(0)),
775  csig1 * csig2 + ssig1 * ssig2);
776 
777  // omg12 = omg2 - omg1, limit to [0, pi]
778  omg12 = atan2(max(comg1 * somg2 - somg1 * comg2, real(0)),
779  comg1 * comg2 + somg1 * somg2);
780  real B312, h0;
781  real k2 = Math::sq(calp0) * _ep2;
782  eps = k2 / (2 * (1 + sqrt(1 + k2)) + k2);
783  C3f(eps, C3a);
784  B312 = (SinCosSeries(true, ssig2, csig2, C3a, nC3_-1) -
785  SinCosSeries(true, ssig1, csig1, C3a, nC3_-1));
786  h0 = -_f * A3f(eps);
787  domg12 = salp0 * h0 * (sig12 + B312);
788  lam12 = omg12 + domg12;
789 
790  if (diffp) {
791  if (calp2 == 0)
792  dlam12 = - 2 * _f1 * dn1 / sbet1;
793  else {
794  real dummy;
795  Lengths(eps, sig12, ssig1, csig1, dn1, ssig2, csig2, dn2,
796  cbet1, cbet2, dummy, dlam12, dummy,
797  false, dummy, dummy, C1a, C2a);
798  dlam12 *= _f1 / (calp2 * cbet2);
799  }
800  }
801 
802  return lam12;
803  }
804 
805  Math::real Geodesic::A3f(real eps) const {
806  // Evaluate sum(_A3x[k] * eps^k, k, 0, nA3x_-1) by Horner's method
807  real v = 0;
808  for (int i = nA3x_; i > 0; )
809  v = eps * v + _A3x[--i];
810  return v;
811  }
812 
813  void Geodesic::C3f(real eps, real c[]) const {
814  // Evaluate C3 coeffs by Horner's method
815  // Elements c[1] thru c[nC3_ - 1] are set
816  for (int j = nC3x_, k = nC3_ - 1; k > 0; ) {
817  real t = 0;
818  for (int i = nC3_ - k; i > 0; --i) {
819  t = eps * t + _C3x[--j];
820  }
821  c[k--] = t;
822  }
823 
824  real mult = 1;
825  for (int k = 1; k < nC3_; ) {
826  mult *= eps;
827  c[k++] *= mult;
828  }
829  }
830 
831  void Geodesic::C4f(real eps, real c[]) const {
832  // Evaluate C4 coeffs by Horner's method
833  // Elements c[0] thru c[nC4_ - 1] are set
834  for (int j = nC4x_, k = nC4_; k > 0; ) {
835  real t = 0;
836  for (int i = nC4_ - k + 1; i > 0; --i)
837  t = eps * t + _C4x[--j];
838  c[--k] = t;
839  }
840 
841  real mult = 1;
842  for (int k = 1; k < nC4_; ) {
843  mult *= eps;
844  c[k++] *= mult;
845  }
846  }
847 
848  // Generated by Maxima on 2010-09-04 10:26:17-04:00
849 
850  // The scale factor A1-1 = mean value of (d/dsigma)I1 - 1
851  Math::real Geodesic::A1m1f(real eps) {
852  real
853  eps2 = Math::sq(eps),
854  t;
855  switch (nA1_/2) {
856  case 0:
857  t = 0;
858  break;
859  case 1:
860  t = eps2/4;
861  break;
862  case 2:
863  t = eps2*(eps2+16)/64;
864  break;
865  case 3:
866  t = eps2*(eps2*(eps2+4)+64)/256;
867  break;
868  case 4:
869  t = eps2*(eps2*(eps2*(25*eps2+64)+256)+4096)/16384;
870  break;
871  default:
872  GEOGRAPHICLIB_STATIC_ASSERT(nA1_ >= 0 && nA1_ <= 8, "Bad value of nA1_");
873  t = 0;
874  }
875  return (t + eps) / (1 - eps);
876  }
877 
878  // The coefficients C1[l] in the Fourier expansion of B1
879  void Geodesic::C1f(real eps, real c[]) {
880  real
881  eps2 = Math::sq(eps),
882  d = eps;
883  switch (nC1_) {
884  case 0:
885  break;
886  case 1:
887  c[1] = -d/2;
888  break;
889  case 2:
890  c[1] = -d/2;
891  d *= eps;
892  c[2] = -d/16;
893  break;
894  case 3:
895  c[1] = d*(3*eps2-8)/16;
896  d *= eps;
897  c[2] = -d/16;
898  d *= eps;
899  c[3] = -d/48;
900  break;
901  case 4:
902  c[1] = d*(3*eps2-8)/16;
903  d *= eps;
904  c[2] = d*(eps2-2)/32;
905  d *= eps;
906  c[3] = -d/48;
907  d *= eps;
908  c[4] = -5*d/512;
909  break;
910  case 5:
911  c[1] = d*((6-eps2)*eps2-16)/32;
912  d *= eps;
913  c[2] = d*(eps2-2)/32;
914  d *= eps;
915  c[3] = d*(9*eps2-16)/768;
916  d *= eps;
917  c[4] = -5*d/512;
918  d *= eps;
919  c[5] = -7*d/1280;
920  break;
921  case 6:
922  c[1] = d*((6-eps2)*eps2-16)/32;
923  d *= eps;
924  c[2] = d*((64-9*eps2)*eps2-128)/2048;
925  d *= eps;
926  c[3] = d*(9*eps2-16)/768;
927  d *= eps;
928  c[4] = d*(3*eps2-5)/512;
929  d *= eps;
930  c[5] = -7*d/1280;
931  d *= eps;
932  c[6] = -7*d/2048;
933  break;
934  case 7:
935  c[1] = d*(eps2*(eps2*(19*eps2-64)+384)-1024)/2048;
936  d *= eps;
937  c[2] = d*((64-9*eps2)*eps2-128)/2048;
938  d *= eps;
939  c[3] = d*((72-9*eps2)*eps2-128)/6144;
940  d *= eps;
941  c[4] = d*(3*eps2-5)/512;
942  d *= eps;
943  c[5] = d*(35*eps2-56)/10240;
944  d *= eps;
945  c[6] = -7*d/2048;
946  d *= eps;
947  c[7] = -33*d/14336;
948  break;
949  case 8:
950  c[1] = d*(eps2*(eps2*(19*eps2-64)+384)-1024)/2048;
951  d *= eps;
952  c[2] = d*(eps2*(eps2*(7*eps2-18)+128)-256)/4096;
953  d *= eps;
954  c[3] = d*((72-9*eps2)*eps2-128)/6144;
955  d *= eps;
956  c[4] = d*((96-11*eps2)*eps2-160)/16384;
957  d *= eps;
958  c[5] = d*(35*eps2-56)/10240;
959  d *= eps;
960  c[6] = d*(9*eps2-14)/4096;
961  d *= eps;
962  c[7] = -33*d/14336;
963  d *= eps;
964  c[8] = -429*d/262144;
965  break;
966  default:
967  GEOGRAPHICLIB_STATIC_ASSERT(nC1_ >= 0 && nC1_ <= 8, "Bad value of nC1_");
968  }
969  }
970 
971  // The coefficients C1p[l] in the Fourier expansion of B1p
972  void Geodesic::C1pf(real eps, real c[]) {
973  real
974  eps2 = Math::sq(eps),
975  d = eps;
976  switch (nC1p_) {
977  case 0:
978  break;
979  case 1:
980  c[1] = d/2;
981  break;
982  case 2:
983  c[1] = d/2;
984  d *= eps;
985  c[2] = 5*d/16;
986  break;
987  case 3:
988  c[1] = d*(16-9*eps2)/32;
989  d *= eps;
990  c[2] = 5*d/16;
991  d *= eps;
992  c[3] = 29*d/96;
993  break;
994  case 4:
995  c[1] = d*(16-9*eps2)/32;
996  d *= eps;
997  c[2] = d*(30-37*eps2)/96;
998  d *= eps;
999  c[3] = 29*d/96;
1000  d *= eps;
1001  c[4] = 539*d/1536;
1002  break;
1003  case 5:
1004  c[1] = d*(eps2*(205*eps2-432)+768)/1536;
1005  d *= eps;
1006  c[2] = d*(30-37*eps2)/96;
1007  d *= eps;
1008  c[3] = d*(116-225*eps2)/384;
1009  d *= eps;
1010  c[4] = 539*d/1536;
1011  d *= eps;
1012  c[5] = 3467*d/7680;
1013  break;
1014  case 6:
1015  c[1] = d*(eps2*(205*eps2-432)+768)/1536;
1016  d *= eps;
1017  c[2] = d*(eps2*(4005*eps2-4736)+3840)/12288;
1018  d *= eps;
1019  c[3] = d*(116-225*eps2)/384;
1020  d *= eps;
1021  c[4] = d*(2695-7173*eps2)/7680;
1022  d *= eps;
1023  c[5] = 3467*d/7680;
1024  d *= eps;
1025  c[6] = 38081*d/61440;
1026  break;
1027  case 7:
1028  c[1] = d*(eps2*((9840-4879*eps2)*eps2-20736)+36864)/73728;
1029  d *= eps;
1030  c[2] = d*(eps2*(4005*eps2-4736)+3840)/12288;
1031  d *= eps;
1032  c[3] = d*(eps2*(8703*eps2-7200)+3712)/12288;
1033  d *= eps;
1034  c[4] = d*(2695-7173*eps2)/7680;
1035  d *= eps;
1036  c[5] = d*(41604-141115*eps2)/92160;
1037  d *= eps;
1038  c[6] = 38081*d/61440;
1039  d *= eps;
1040  c[7] = 459485*d/516096;
1041  break;
1042  case 8:
1043  c[1] = d*(eps2*((9840-4879*eps2)*eps2-20736)+36864)/73728;
1044  d *= eps;
1045  c[2] = d*(eps2*((120150-86171*eps2)*eps2-142080)+115200)/368640;
1046  d *= eps;
1047  c[3] = d*(eps2*(8703*eps2-7200)+3712)/12288;
1048  d *= eps;
1049  c[4] = d*(eps2*(1082857*eps2-688608)+258720)/737280;
1050  d *= eps;
1051  c[5] = d*(41604-141115*eps2)/92160;
1052  d *= eps;
1053  c[6] = d*(533134-2200311*eps2)/860160;
1054  d *= eps;
1055  c[7] = 459485*d/516096;
1056  d *= eps;
1057  c[8] = 109167851*d/82575360;
1058  break;
1059  default:
1060  GEOGRAPHICLIB_STATIC_ASSERT(nC1p_ >= 0 && nC1p_ <= 8,
1061  "Bad value of nC1p_");
1062  }
1063  }
1064 
1065  // The scale factor A2-1 = mean value of (d/dsigma)I2 - 1
1066  Math::real Geodesic::A2m1f(real eps) {
1067  real
1068  eps2 = Math::sq(eps),
1069  t;
1070  switch (nA2_/2) {
1071  case 0:
1072  t = 0;
1073  break;
1074  case 1:
1075  t = eps2/4;
1076  break;
1077  case 2:
1078  t = eps2*(9*eps2+16)/64;
1079  break;
1080  case 3:
1081  t = eps2*(eps2*(25*eps2+36)+64)/256;
1082  break;
1083  case 4:
1084  t = eps2*(eps2*(eps2*(1225*eps2+1600)+2304)+4096)/16384;
1085  break;
1086  default:
1087  GEOGRAPHICLIB_STATIC_ASSERT(nA2_ >= 0 && nA2_ <= 8, "Bad value of nA2_");
1088  t = 0;
1089  }
1090  return t * (1 - eps) - eps;
1091  }
1092 
1093  // The coefficients C2[l] in the Fourier expansion of B2
1094  void Geodesic::C2f(real eps, real c[]) {
1095  real
1096  eps2 = Math::sq(eps),
1097  d = eps;
1098  switch (nC2_) {
1099  case 0:
1100  break;
1101  case 1:
1102  c[1] = d/2;
1103  break;
1104  case 2:
1105  c[1] = d/2;
1106  d *= eps;
1107  c[2] = 3*d/16;
1108  break;
1109  case 3:
1110  c[1] = d*(eps2+8)/16;
1111  d *= eps;
1112  c[2] = 3*d/16;
1113  d *= eps;
1114  c[3] = 5*d/48;
1115  break;
1116  case 4:
1117  c[1] = d*(eps2+8)/16;
1118  d *= eps;
1119  c[2] = d*(eps2+6)/32;
1120  d *= eps;
1121  c[3] = 5*d/48;
1122  d *= eps;
1123  c[4] = 35*d/512;
1124  break;
1125  case 5:
1126  c[1] = d*(eps2*(eps2+2)+16)/32;
1127  d *= eps;
1128  c[2] = d*(eps2+6)/32;
1129  d *= eps;
1130  c[3] = d*(15*eps2+80)/768;
1131  d *= eps;
1132  c[4] = 35*d/512;
1133  d *= eps;
1134  c[5] = 63*d/1280;
1135  break;
1136  case 6:
1137  c[1] = d*(eps2*(eps2+2)+16)/32;
1138  d *= eps;
1139  c[2] = d*(eps2*(35*eps2+64)+384)/2048;
1140  d *= eps;
1141  c[3] = d*(15*eps2+80)/768;
1142  d *= eps;
1143  c[4] = d*(7*eps2+35)/512;
1144  d *= eps;
1145  c[5] = 63*d/1280;
1146  d *= eps;
1147  c[6] = 77*d/2048;
1148  break;
1149  case 7:
1150  c[1] = d*(eps2*(eps2*(41*eps2+64)+128)+1024)/2048;
1151  d *= eps;
1152  c[2] = d*(eps2*(35*eps2+64)+384)/2048;
1153  d *= eps;
1154  c[3] = d*(eps2*(69*eps2+120)+640)/6144;
1155  d *= eps;
1156  c[4] = d*(7*eps2+35)/512;
1157  d *= eps;
1158  c[5] = d*(105*eps2+504)/10240;
1159  d *= eps;
1160  c[6] = 77*d/2048;
1161  d *= eps;
1162  c[7] = 429*d/14336;
1163  break;
1164  case 8:
1165  c[1] = d*(eps2*(eps2*(41*eps2+64)+128)+1024)/2048;
1166  d *= eps;
1167  c[2] = d*(eps2*(eps2*(47*eps2+70)+128)+768)/4096;
1168  d *= eps;
1169  c[3] = d*(eps2*(69*eps2+120)+640)/6144;
1170  d *= eps;
1171  c[4] = d*(eps2*(133*eps2+224)+1120)/16384;
1172  d *= eps;
1173  c[5] = d*(105*eps2+504)/10240;
1174  d *= eps;
1175  c[6] = d*(33*eps2+154)/4096;
1176  d *= eps;
1177  c[7] = 429*d/14336;
1178  d *= eps;
1179  c[8] = 6435*d/262144;
1180  break;
1181  default:
1182  GEOGRAPHICLIB_STATIC_ASSERT(nC2_ >= 0 && nC2_ <= 8, "Bad value of nC2_");
1183  }
1184  }
1185 
1186  // The scale factor A3 = mean value of (d/dsigma)I3
1187  void Geodesic::A3coeff() {
1188  switch (nA3_) {
1189  case 0:
1190  break;
1191  case 1:
1192  _A3x[0] = 1;
1193  break;
1194  case 2:
1195  _A3x[0] = 1;
1196  _A3x[1] = -1/real(2);
1197  break;
1198  case 3:
1199  _A3x[0] = 1;
1200  _A3x[1] = (_n-1)/2;
1201  _A3x[2] = -1/real(4);
1202  break;
1203  case 4:
1204  _A3x[0] = 1;
1205  _A3x[1] = (_n-1)/2;
1206  _A3x[2] = (-_n-2)/8;
1207  _A3x[3] = -1/real(16);
1208  break;
1209  case 5:
1210  _A3x[0] = 1;
1211  _A3x[1] = (_n-1)/2;
1212  _A3x[2] = (_n*(3*_n-1)-2)/8;
1213  _A3x[3] = (-3*_n-1)/16;
1214  _A3x[4] = -3/real(64);
1215  break;
1216  case 6:
1217  _A3x[0] = 1;
1218  _A3x[1] = (_n-1)/2;
1219  _A3x[2] = (_n*(3*_n-1)-2)/8;
1220  _A3x[3] = ((-_n-3)*_n-1)/16;
1221  _A3x[4] = (-2*_n-3)/64;
1222  _A3x[5] = -3/real(128);
1223  break;
1224  case 7:
1225  _A3x[0] = 1;
1226  _A3x[1] = (_n-1)/2;
1227  _A3x[2] = (_n*(3*_n-1)-2)/8;
1228  _A3x[3] = (_n*(_n*(5*_n-1)-3)-1)/16;
1229  _A3x[4] = ((-10*_n-2)*_n-3)/64;
1230  _A3x[5] = (-5*_n-3)/128;
1231  _A3x[6] = -5/real(256);
1232  break;
1233  case 8:
1234  _A3x[0] = 1;
1235  _A3x[1] = (_n-1)/2;
1236  _A3x[2] = (_n*(3*_n-1)-2)/8;
1237  _A3x[3] = (_n*(_n*(5*_n-1)-3)-1)/16;
1238  _A3x[4] = (_n*((-5*_n-20)*_n-4)-6)/128;
1239  _A3x[5] = ((-5*_n-10)*_n-6)/256;
1240  _A3x[6] = (-15*_n-20)/1024;
1241  _A3x[7] = -25/real(2048);
1242  break;
1243  default:
1244  GEOGRAPHICLIB_STATIC_ASSERT(nA3_ >= 0 && nA3_ <= 8, "Bad value of nA3_");
1245  }
1246  }
1247 
1248  // The coefficients C3[l] in the Fourier expansion of B3
1249  void Geodesic::C3coeff() {
1250  switch (nC3_) {
1251  case 0:
1252  break;
1253  case 1:
1254  break;
1255  case 2:
1256  _C3x[0] = 1/real(4);
1257  break;
1258  case 3:
1259  _C3x[0] = (1-_n)/4;
1260  _C3x[1] = 1/real(8);
1261  _C3x[2] = 1/real(16);
1262  break;
1263  case 4:
1264  _C3x[0] = (1-_n)/4;
1265  _C3x[1] = 1/real(8);
1266  _C3x[2] = 3/real(64);
1267  _C3x[3] = (2-3*_n)/32;
1268  _C3x[4] = 3/real(64);
1269  _C3x[5] = 5/real(192);
1270  break;
1271  case 5:
1272  _C3x[0] = (1-_n)/4;
1273  _C3x[1] = (1-_n*_n)/8;
1274  _C3x[2] = (3*_n+3)/64;
1275  _C3x[3] = 5/real(128);
1276  _C3x[4] = ((_n-3)*_n+2)/32;
1277  _C3x[5] = (3-2*_n)/64;
1278  _C3x[6] = 3/real(128);
1279  _C3x[7] = (5-9*_n)/192;
1280  _C3x[8] = 3/real(128);
1281  _C3x[9] = 7/real(512);
1282  break;
1283  case 6:
1284  _C3x[0] = (1-_n)/4;
1285  _C3x[1] = (1-_n*_n)/8;
1286  _C3x[2] = ((3-_n)*_n+3)/64;
1287  _C3x[3] = (2*_n+5)/128;
1288  _C3x[4] = 3/real(128);
1289  _C3x[5] = ((_n-3)*_n+2)/32;
1290  _C3x[6] = ((-3*_n-2)*_n+3)/64;
1291  _C3x[7] = (_n+3)/128;
1292  _C3x[8] = 5/real(256);
1293  _C3x[9] = (_n*(5*_n-9)+5)/192;
1294  _C3x[10] = (9-10*_n)/384;
1295  _C3x[11] = 7/real(512);
1296  _C3x[12] = (7-14*_n)/512;
1297  _C3x[13] = 7/real(512);
1298  _C3x[14] = 21/real(2560);
1299  break;
1300  case 7:
1301  _C3x[0] = (1-_n)/4;
1302  _C3x[1] = (1-_n*_n)/8;
1303  _C3x[2] = (_n*((-5*_n-1)*_n+3)+3)/64;
1304  _C3x[3] = (_n*(2*_n+2)+5)/128;
1305  _C3x[4] = (11*_n+12)/512;
1306  _C3x[5] = 21/real(1024);
1307  _C3x[6] = ((_n-3)*_n+2)/32;
1308  _C3x[7] = (_n*(_n*(2*_n-3)-2)+3)/64;
1309  _C3x[8] = ((2-9*_n)*_n+6)/256;
1310  _C3x[9] = (_n+5)/256;
1311  _C3x[10] = 27/real(2048);
1312  _C3x[11] = (_n*((5-_n)*_n-9)+5)/192;
1313  _C3x[12] = ((-6*_n-10)*_n+9)/384;
1314  _C3x[13] = (21-4*_n)/1536;
1315  _C3x[14] = 3/real(256);
1316  _C3x[15] = (_n*(10*_n-14)+7)/512;
1317  _C3x[16] = (7-10*_n)/512;
1318  _C3x[17] = 9/real(1024);
1319  _C3x[18] = (21-45*_n)/2560;
1320  _C3x[19] = 9/real(1024);
1321  _C3x[20] = 11/real(2048);
1322  break;
1323  case 8:
1324  _C3x[0] = (1-_n)/4;
1325  _C3x[1] = (1-_n*_n)/8;
1326  _C3x[2] = (_n*((-5*_n-1)*_n+3)+3)/64;
1327  _C3x[3] = (_n*((2-2*_n)*_n+2)+5)/128;
1328  _C3x[4] = (_n*(3*_n+11)+12)/512;
1329  _C3x[5] = (10*_n+21)/1024;
1330  _C3x[6] = 243/real(16384);
1331  _C3x[7] = ((_n-3)*_n+2)/32;
1332  _C3x[8] = (_n*(_n*(2*_n-3)-2)+3)/64;
1333  _C3x[9] = (_n*((-6*_n-9)*_n+2)+6)/256;
1334  _C3x[10] = ((1-2*_n)*_n+5)/256;
1335  _C3x[11] = (69*_n+108)/8192;
1336  _C3x[12] = 187/real(16384);
1337  _C3x[13] = (_n*((5-_n)*_n-9)+5)/192;
1338  _C3x[14] = (_n*(_n*(10*_n-6)-10)+9)/384;
1339  _C3x[15] = ((-77*_n-8)*_n+42)/3072;
1340  _C3x[16] = (12-_n)/1024;
1341  _C3x[17] = 139/real(16384);
1342  _C3x[18] = (_n*((20-7*_n)*_n-28)+14)/1024;
1343  _C3x[19] = ((-7*_n-40)*_n+28)/2048;
1344  _C3x[20] = (72-43*_n)/8192;
1345  _C3x[21] = 127/real(16384);
1346  _C3x[22] = (_n*(75*_n-90)+42)/5120;
1347  _C3x[23] = (9-15*_n)/1024;
1348  _C3x[24] = 99/real(16384);
1349  _C3x[25] = (44-99*_n)/8192;
1350  _C3x[26] = 99/real(16384);
1351  _C3x[27] = 429/real(114688);
1352  break;
1353  default:
1354  GEOGRAPHICLIB_STATIC_ASSERT(nC3_ >= 0 && nC3_ <= 8, "Bad value of nC3_");
1355  }
1356  }
1357 
1358  // Generated by Maxima on 2012-10-19 08:02:34-04:00
1359 
1360  // The coefficients C4[l] in the Fourier expansion of I4
1361  void Geodesic::C4coeff() {
1362  switch (nC4_) {
1363  case 0:
1364  break;
1365  case 1:
1366  _C4x[0] = 2/real(3);
1367  break;
1368  case 2:
1369  _C4x[0] = (10-4*_n)/15;
1370  _C4x[1] = -1/real(5);
1371  _C4x[2] = 1/real(45);
1372  break;
1373  case 3:
1374  _C4x[0] = (_n*(8*_n-28)+70)/105;
1375  _C4x[1] = (16*_n-7)/35;
1376  _C4x[2] = -2/real(105);
1377  _C4x[3] = (7-16*_n)/315;
1378  _C4x[4] = -2/real(105);
1379  _C4x[5] = 4/real(525);
1380  break;
1381  case 4:
1382  _C4x[0] = (_n*(_n*(4*_n+24)-84)+210)/315;
1383  _C4x[1] = ((48-32*_n)*_n-21)/105;
1384  _C4x[2] = (-32*_n-6)/315;
1385  _C4x[3] = 11/real(315);
1386  _C4x[4] = (_n*(32*_n-48)+21)/945;
1387  _C4x[5] = (64*_n-18)/945;
1388  _C4x[6] = -1/real(105);
1389  _C4x[7] = (12-32*_n)/1575;
1390  _C4x[8] = -8/real(1575);
1391  _C4x[9] = 8/real(2205);
1392  break;
1393  case 5:
1394  _C4x[0] = (_n*(_n*(_n*(16*_n+44)+264)-924)+2310)/3465;
1395  _C4x[1] = (_n*(_n*(48*_n-352)+528)-231)/1155;
1396  _C4x[2] = (_n*(1088*_n-352)-66)/3465;
1397  _C4x[3] = (121-368*_n)/3465;
1398  _C4x[4] = 4/real(1155);
1399  _C4x[5] = (_n*((352-48*_n)*_n-528)+231)/10395;
1400  _C4x[6] = ((704-896*_n)*_n-198)/10395;
1401  _C4x[7] = (80*_n-99)/10395;
1402  _C4x[8] = 4/real(1155);
1403  _C4x[9] = (_n*(320*_n-352)+132)/17325;
1404  _C4x[10] = (384*_n-88)/17325;
1405  _C4x[11] = -8/real(1925);
1406  _C4x[12] = (88-256*_n)/24255;
1407  _C4x[13] = -16/real(8085);
1408  _C4x[14] = 64/real(31185);
1409  break;
1410  case 6:
1411  _C4x[0] = (_n*(_n*(_n*(_n*(100*_n+208)+572)+3432)-12012)+30030)/45045;
1412  _C4x[1] = (_n*(_n*(_n*(64*_n+624)-4576)+6864)-3003)/15015;
1413  _C4x[2] = (_n*((14144-10656*_n)*_n-4576)-858)/45045;
1414  _C4x[3] = ((-224*_n-4784)*_n+1573)/45045;
1415  _C4x[4] = (1088*_n+156)/45045;
1416  _C4x[5] = 97/real(15015);
1417  _C4x[6] = (_n*(_n*((-64*_n-624)*_n+4576)-6864)+3003)/135135;
1418  _C4x[7] = (_n*(_n*(5952*_n-11648)+9152)-2574)/135135;
1419  _C4x[8] = (_n*(5792*_n+1040)-1287)/135135;
1420  _C4x[9] = (468-2944*_n)/135135;
1421  _C4x[10] = 1/real(9009);
1422  _C4x[11] = (_n*((4160-1440*_n)*_n-4576)+1716)/225225;
1423  _C4x[12] = ((4992-8448*_n)*_n-1144)/225225;
1424  _C4x[13] = (1856*_n-936)/225225;
1425  _C4x[14] = 8/real(10725);
1426  _C4x[15] = (_n*(3584*_n-3328)+1144)/315315;
1427  _C4x[16] = (1024*_n-208)/105105;
1428  _C4x[17] = -136/real(63063);
1429  _C4x[18] = (832-2560*_n)/405405;
1430  _C4x[19] = -128/real(135135);
1431  _C4x[20] = 128/real(99099);
1432  break;
1433  case 7:
1434  _C4x[0] = (_n*(_n*(_n*(_n*(_n*(56*_n+100)+208)+572)+3432)-12012)+30030)/
1435  45045;
1436  _C4x[1] = (_n*(_n*(_n*(_n*(16*_n+64)+624)-4576)+6864)-3003)/15015;
1437  _C4x[2] = (_n*(_n*(_n*(1664*_n-10656)+14144)-4576)-858)/45045;
1438  _C4x[3] = (_n*(_n*(10736*_n-224)-4784)+1573)/45045;
1439  _C4x[4] = ((1088-4480*_n)*_n+156)/45045;
1440  _C4x[5] = (291-464*_n)/45045;
1441  _C4x[6] = 10/real(9009);
1442  _C4x[7] = (_n*(_n*(_n*((-16*_n-64)*_n-624)+4576)-6864)+3003)/135135;
1443  _C4x[8] = (_n*(_n*((5952-768*_n)*_n-11648)+9152)-2574)/135135;
1444  _C4x[9] = (_n*((5792-10704*_n)*_n+1040)-1287)/135135;
1445  _C4x[10] = (_n*(3840*_n-2944)+468)/135135;
1446  _C4x[11] = (112*_n+15)/135135;
1447  _C4x[12] = 10/real(9009);
1448  _C4x[13] = (_n*(_n*(_n*(128*_n-1440)+4160)-4576)+1716)/225225;
1449  _C4x[14] = (_n*(_n*(6784*_n-8448)+4992)-1144)/225225;
1450  _C4x[15] = (_n*(1664*_n+1856)-936)/225225;
1451  _C4x[16] = (168-1664*_n)/225225;
1452  _C4x[17] = -4/real(25025);
1453  _C4x[18] = (_n*((3584-1792*_n)*_n-3328)+1144)/315315;
1454  _C4x[19] = ((1024-2048*_n)*_n-208)/105105;
1455  _C4x[20] = (1792*_n-680)/315315;
1456  _C4x[21] = 64/real(315315);
1457  _C4x[22] = (_n*(3072*_n-2560)+832)/405405;
1458  _C4x[23] = (2048*_n-384)/405405;
1459  _C4x[24] = -512/real(405405);
1460  _C4x[25] = (640-2048*_n)/495495;
1461  _C4x[26] = -256/real(495495);
1462  _C4x[27] = 512/real(585585);
1463  break;
1464  case 8:
1465  _C4x[0] = (_n*(_n*(_n*(_n*(_n*(_n*(588*_n+952)+1700)+3536)+9724)+58344)-
1466  204204)+510510)/765765;
1467  _C4x[1] = (_n*(_n*(_n*(_n*(_n*(96*_n+272)+1088)+10608)-77792)+116688)-
1468  51051)/255255;
1469  _C4x[2] = (_n*(_n*(_n*(_n*(3232*_n+28288)-181152)+240448)-77792)-14586)/
1470  765765;
1471  _C4x[3] = (_n*(_n*((182512-154048*_n)*_n-3808)-81328)+26741)/765765;
1472  _C4x[4] = (_n*(_n*(12480*_n-76160)+18496)+2652)/765765;
1473  _C4x[5] = (_n*(20960*_n-7888)+4947)/765765;
1474  _C4x[6] = (4192*_n+850)/765765;
1475  _C4x[7] = 193/real(85085);
1476  _C4x[8] = (_n*(_n*(_n*(_n*((-96*_n-272)*_n-1088)-10608)+77792)-116688)+
1477  51051)/2297295;
1478  _C4x[9] = (_n*(_n*(_n*((-1344*_n-13056)*_n+101184)-198016)+155584)-43758)/
1479  2297295;
1480  _C4x[10] = (_n*(_n*(_n*(103744*_n-181968)+98464)+17680)-21879)/2297295;
1481  _C4x[11] = (_n*(_n*(52608*_n+65280)-50048)+7956)/2297295;
1482  _C4x[12] = ((1904-39840*_n)*_n+255)/2297295;
1483  _C4x[13] = (510-1472*_n)/459459;
1484  _C4x[14] = 349/real(2297295);
1485  _C4x[15] = (_n*(_n*(_n*(_n*(160*_n+2176)-24480)+70720)-77792)+29172)/
1486  3828825;
1487  _C4x[16] = (_n*(_n*((115328-41472*_n)*_n-143616)+84864)-19448)/3828825;
1488  _C4x[17] = (_n*((28288-126528*_n)*_n+31552)-15912)/3828825;
1489  _C4x[18] = (_n*(64256*_n-28288)+2856)/3828825;
1490  _C4x[19] = (-928*_n-612)/3828825;
1491  _C4x[20] = 464/real(1276275);
1492  _C4x[21] = (_n*(_n*(_n*(7168*_n-30464)+60928)-56576)+19448)/5360355;
1493  _C4x[22] = (_n*(_n*(35840*_n-34816)+17408)-3536)/1786785;
1494  _C4x[23] = ((30464-2560*_n)*_n-11560)/5360355;
1495  _C4x[24] = (1088-16384*_n)/5360355;
1496  _C4x[25] = -16/real(97461);
1497  _C4x[26] = (_n*((52224-32256*_n)*_n-43520)+14144)/6891885;
1498  _C4x[27] = ((34816-77824*_n)*_n-6528)/6891885;
1499  _C4x[28] = (26624*_n-8704)/6891885;
1500  _C4x[29] = 128/real(2297295);
1501  _C4x[30] = (_n*(45056*_n-34816)+10880)/8423415;
1502  _C4x[31] = (24576*_n-4352)/8423415;
1503  _C4x[32] = -6784/real(8423415);
1504  _C4x[33] = (8704-28672*_n)/9954945;
1505  _C4x[34] = -1024/real(3318315);
1506  _C4x[35] = 1024/real(1640925);
1507  break;
1508  default:
1509  GEOGRAPHICLIB_STATIC_ASSERT(nC4_ >= 0 && nC4_ <= 8, "Bad value of nC4_");
1510  }
1511  }
1512 
1513 } // namespace GeographicLib
static T AngNormalize(T x)
Definition: Math.hpp:428
Geodesic(real a, real f)
Definition: Geodesic.cpp:42
Header for GeographicLib::GeodesicLine class.
static T pi()
Definition: Math.hpp:214
GeodesicLine Line(real lat1, real lon1, real azi1, unsigned caps=ALL) const
Definition: Geodesic.cpp:118
GeographicLib::Math::real real
Definition: GeodSolve.cpp:32
static T cbrt(T x)
Definition: Math.hpp:357
static const Geodesic & WGS84()
Definition: Geodesic.cpp:89
Math::real GenInverse(real lat1, real lon1, real lat2, real lon2, unsigned outmask, real &s12, real &azi1, real &azi2, real &m12, real &M12, real &M21, real &S12) const
Definition: Geodesic.cpp:136
static bool isfinite(T x)
Definition: Math.hpp:596
Mathematical functions needed by GeographicLib.
Definition: Math.hpp:102
static void norm(T &x, T &y)
Definition: Math.hpp:392
#define GEOGRAPHICLIB_VOLATILE
Definition: Math.hpp:84
Header for GeographicLib::Geodesic class.
friend class GeodesicLine
Definition: Geodesic.hpp:174
static T hypot(T x, T y)
Definition: Math.hpp:255
Math::real GenDirect(real lat1, real lon1, real azi1, bool arcmode, real s12_a12, unsigned outmask, real &lat2, real &lon2, real &azi2, real &s12, real &m12, real &M12, real &M21, real &S12) const
Definition: Geodesic.cpp:123
static T sq(T x)
Definition: Math.hpp:244
static T atan2d(T y, T x)
Definition: Math.hpp:534
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
static T degree()
Definition: Math.hpp:228
static T AngDiff(T x, T y)
Definition: Math.hpp:458
Exception handling for GeographicLib.
Definition: Constants.hpp:382
Geodesic calculations
Definition: Geodesic.hpp:171
static T AngRound(T x)
Definition: Math.hpp:481
#define GEOGRAPHICLIB_PANIC
Definition: Math.hpp:87