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