GeographicLib  2.1.1
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-2022) <charles@karney.com> and licensed
6  * under the MIT/X11 License. For more information, see
7  * https://geographiclib.sourceforge.io/
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 // constant conditional and enum-float expressions and mixing enums
35 # pragma warning (disable: 4701 4127 5055 5054)
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  , tolb_(tol0_) // Check on bisection interval
55  , xthresh_(1000 * tol2_)
56  , _a(a)
57  , _f(f)
58  , _f1(1 - _f)
59  , _e2(_f * (2 - _f))
60  , _ep2(_e2 / Math::sq(_f1)) // e2 / (1 - e2)
61  , _n(_f / ( 2 - _f))
62  , _b(_a * _f1)
63  , _c2((Math::sq(_a) + Math::sq(_b) *
64  (_e2 == 0 ? 1 :
65  Math::eatanhe(real(1), (_f < 0 ? -1 : 1) * sqrt(fabs(_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(real(0.1) * tol2_ /
78  sqrt( fmax(real(0.001), fabs(_f)) * fmin(real(1), 1 - _f/2) / 2 ))
79  {
80  if (!(isfinite(_a) && _a > 0))
81  throw GeographicErr("Equatorial radius is not positive");
82  if (!(isfinite(_b) && _b > 0))
83  throw GeographicErr("Polar semi-axis 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,
119  unsigned caps) 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  // Automatically supply DISTANCE_IN if necessary
129  if (!arcmode) outmask |= DISTANCE_IN;
130  return GeodesicLine(*this, lat1, lon1, azi1, outmask)
131  . // Note the dot!
132  GenPosition(arcmode, s12_a12, outmask,
133  lat2, lon2, azi2, s12, m12, M12, M21, S12);
134  }
135 
136  GeodesicLine Geodesic::GenDirectLine(real lat1, real lon1, real azi1,
137  bool arcmode, real s12_a12,
138  unsigned caps) const {
139  azi1 = Math::AngNormalize(azi1);
140  real salp1, calp1;
141  // Guard against underflow in salp0. Also -0 is converted to +0.
142  Math::sincosd(Math::AngRound(azi1), salp1, calp1);
143  // Automatically supply DISTANCE_IN if necessary
144  if (!arcmode) caps |= DISTANCE_IN;
145  return GeodesicLine(*this, lat1, lon1, azi1, salp1, calp1,
146  caps, arcmode, s12_a12);
147  }
148 
149  GeodesicLine Geodesic::DirectLine(real lat1, real lon1, real azi1, real s12,
150  unsigned caps) const {
151  return GenDirectLine(lat1, lon1, azi1, false, s12, caps);
152  }
153 
154  GeodesicLine Geodesic::ArcDirectLine(real lat1, real lon1, real azi1,
155  real a12, unsigned caps) const {
156  return GenDirectLine(lat1, lon1, azi1, true, a12, caps);
157  }
158 
159  Math::real Geodesic::GenInverse(real lat1, real lon1, real lat2, real lon2,
160  unsigned outmask, real& s12,
161  real& salp1, real& calp1,
162  real& salp2, real& calp2,
163  real& m12, real& M12, real& M21,
164  real& S12) const {
165  // Compute longitude difference (AngDiff does this carefully).
166  using std::isnan; // Needed for Centos 7, ubuntu 14
167  real lon12s, lon12 = Math::AngDiff(lon1, lon2, lon12s);
168  // Make longitude difference positive.
169  int lonsign = signbit(lon12) ? -1 : 1;
170  lon12 *= lonsign; lon12s *= lonsign;
171  real
172  lam12 = lon12 * Math::degree(),
173  slam12, clam12;
174  // Calculate sincos of lon12 + error (this applies AngRound internally).
175  Math::sincosde(lon12, lon12s, slam12, clam12);
176  // the supplementary longitude difference
177  lon12s = (Math::hd - lon12) - lon12s;
178 
179  // If really close to the equator, treat as on equator.
180  lat1 = Math::AngRound(Math::LatFix(lat1));
181  lat2 = Math::AngRound(Math::LatFix(lat2));
182  // Swap points so that point with higher (abs) latitude is point 1.
183  // If one latitude is a nan, then it becomes lat1.
184  int swapp = fabs(lat1) < fabs(lat2) || isnan(lat2) ? -1 : 1;
185  if (swapp < 0) {
186  lonsign *= -1;
187  swap(lat1, lat2);
188  }
189  // Make lat1 <= -0
190  int latsign = signbit(lat1) ? 1 : -1;
191  lat1 *= latsign;
192  lat2 *= latsign;
193  // Now we have
194  //
195  // 0 <= lon12 <= 180
196  // -90 <= lat1 <= -0
197  // lat1 <= lat2 <= -lat1
198  //
199  // longsign, swapp, latsign register the transformation to bring the
200  // coordinates to this canonical form. In all cases, 1 means no change was
201  // made. We make these transformations so that there are few cases to
202  // check, e.g., on verifying quadrants in atan2. In addition, this
203  // enforces some symmetries in the results returned.
204 
205  real sbet1, cbet1, sbet2, cbet2, s12x, m12x;
206 
207  Math::sincosd(lat1, sbet1, cbet1); sbet1 *= _f1;
208  // Ensure cbet1 = +epsilon at poles; doing the fix on beta means that sig12
209  // will be <= 2*tiny for two points at the same pole.
210  Math::norm(sbet1, cbet1); cbet1 = fmax(tiny_, cbet1);
211 
212  Math::sincosd(lat2, sbet2, cbet2); sbet2 *= _f1;
213  // Ensure cbet2 = +epsilon at poles
214  Math::norm(sbet2, cbet2); cbet2 = fmax(tiny_, cbet2);
215 
216  // If cbet1 < -sbet1, then cbet2 - cbet1 is a sensitive measure of the
217  // |bet1| - |bet2|. Alternatively (cbet1 >= -sbet1), abs(sbet2) + sbet1 is
218  // a better measure. This logic is used in assigning calp2 in Lambda12.
219  // Sometimes these quantities vanish and in that case we force bet2 = +/-
220  // bet1 exactly. An example where is is necessary is the inverse problem
221  // 48.522876735459 0 -48.52287673545898293 179.599720456223079643
222  // which failed with Visual Studio 10 (Release and Debug)
223 
224  if (cbet1 < -sbet1) {
225  if (cbet2 == cbet1)
226  sbet2 = copysign(sbet1, sbet2);
227  } else {
228  if (fabs(sbet2) == -sbet1)
229  cbet2 = cbet1;
230  }
231 
232  real
233  dn1 = sqrt(1 + _ep2 * Math::sq(sbet1)),
234  dn2 = sqrt(1 + _ep2 * Math::sq(sbet2));
235 
236  real a12, sig12;
237  // index zero element of this array is unused
238  real Ca[nC_];
239 
240  bool meridian = lat1 == -Math::qd || slam12 == 0;
241 
242  if (meridian) {
243 
244  // Endpoints are on a single full meridian, so the geodesic might lie on
245  // a meridian.
246 
247  calp1 = clam12; salp1 = slam12; // Head to the target longitude
248  calp2 = 1; salp2 = 0; // At the target we're heading north
249 
250  real
251  // tan(bet) = tan(sig) * cos(alp)
252  ssig1 = sbet1, csig1 = calp1 * cbet1,
253  ssig2 = sbet2, csig2 = calp2 * cbet2;
254 
255  // sig12 = sig2 - sig1
256  sig12 = atan2(fmax(real(0), csig1 * ssig2 - ssig1 * csig2) + real(0),
257  csig1 * csig2 + ssig1 * ssig2);
258  {
259  real dummy;
260  Lengths(_n, sig12, ssig1, csig1, dn1, ssig2, csig2, dn2, cbet1, cbet2,
261  outmask | DISTANCE | REDUCEDLENGTH,
262  s12x, m12x, dummy, M12, M21, Ca);
263  }
264  // Add the check for sig12 since zero length geodesics might yield m12 <
265  // 0. Test case was
266  //
267  // echo 20.001 0 20.001 0 | GeodSolve -i
268  //
269  // In fact, we will have sig12 > pi/2 for meridional geodesic which is
270  // not a shortest path.
271  // TODO: investigate m12 < 0 result for aarch/ppc (with -f -p 20)
272  // 20.001000000000001 0.000000000000000 180.000000000000000
273  // 20.001000000000001 0.000000000000000 180.000000000000000
274  // 0.0000000002 0.000000000000001 -0.0000000001
275  // 0.99999999999999989 0.99999999999999989 0.000
276  if (sig12 < 1 || m12x >= 0) {
277  // Need at least 2, to handle 90 0 90 180
278  if (sig12 < 3 * tiny_ ||
279  // Prevent negative s12 or m12 for short lines
280  (sig12 < tol0_ && (s12x < 0 || m12x < 0)))
281  sig12 = m12x = s12x = 0;
282  m12x *= _b;
283  s12x *= _b;
284  a12 = sig12 / Math::degree();
285  } else
286  // m12 < 0, i.e., prolate and too close to anti-podal
287  meridian = false;
288  }
289 
290  // somg12 == 2 marks that it needs to be calculated
291  real omg12 = 0, somg12 = 2, comg12 = 0;
292  if (!meridian &&
293  sbet1 == 0 && // and sbet2 == 0
294  (_f <= 0 || lon12s >= _f * Math::hd)) {
295 
296  // Geodesic runs along equator
297  calp1 = calp2 = 0; salp1 = salp2 = 1;
298  s12x = _a * lam12;
299  sig12 = omg12 = lam12 / _f1;
300  m12x = _b * sin(sig12);
301  if (outmask & GEODESICSCALE)
302  M12 = M21 = cos(sig12);
303  a12 = lon12 / _f1;
304 
305  } else if (!meridian) {
306 
307  // Now point1 and point2 belong within a hemisphere bounded by a
308  // meridian and geodesic is neither meridional or equatorial.
309 
310  // Figure a starting point for Newton's method
311  real dnm;
312  sig12 = InverseStart(sbet1, cbet1, dn1, sbet2, cbet2, dn2,
313  lam12, slam12, clam12,
314  salp1, calp1, salp2, calp2, dnm,
315  Ca);
316 
317  if (sig12 >= 0) {
318  // Short lines (InverseStart sets salp2, calp2, dnm)
319  s12x = sig12 * _b * dnm;
320  m12x = Math::sq(dnm) * _b * sin(sig12 / dnm);
321  if (outmask & GEODESICSCALE)
322  M12 = M21 = cos(sig12 / dnm);
323  a12 = sig12 / Math::degree();
324  omg12 = lam12 / (_f1 * dnm);
325  } else {
326 
327  // Newton's method. This is a straightforward solution of f(alp1) =
328  // lambda12(alp1) - lam12 = 0 with one wrinkle. f(alp) has exactly one
329  // root in the interval (0, pi) and its derivative is positive at the
330  // root. Thus f(alp) is positive for alp > alp1 and negative for alp <
331  // alp1. During the course of the iteration, a range (alp1a, alp1b) is
332  // maintained which brackets the root and with each evaluation of
333  // f(alp) the range is shrunk, if possible. Newton's method is
334  // restarted whenever the derivative of f is negative (because the new
335  // value of alp1 is then further from the solution) or if the new
336  // estimate of alp1 lies outside (0,pi); in this case, the new starting
337  // guess is taken to be (alp1a + alp1b) / 2.
338  //
339  // initial values to suppress warnings (if loop is executed 0 times)
340  real ssig1 = 0, csig1 = 0, ssig2 = 0, csig2 = 0, eps = 0, domg12 = 0;
341  unsigned numit = 0;
342  // Bracketing range
343  real salp1a = tiny_, calp1a = 1, salp1b = tiny_, calp1b = -1;
344  for (bool tripn = false, tripb = false;; ++numit) {
345  // the WGS84 test set: mean = 1.47, sd = 1.25, max = 16
346  // WGS84 and random input: mean = 2.85, sd = 0.60
347  real dv;
348  real v = Lambda12(sbet1, cbet1, dn1, sbet2, cbet2, dn2, salp1, calp1,
349  slam12, clam12,
350  salp2, calp2, sig12, ssig1, csig1, ssig2, csig2,
351  eps, domg12, numit < maxit1_, dv, Ca);
352  if (tripb ||
353  // Reversed test to allow escape with NaNs
354  !(fabs(v) >= (tripn ? 8 : 1) * tol0_) ||
355  // Enough bisections to get accurate result
356  numit == maxit2_)
357  break;
358  // Update bracketing values
359  if (v > 0 && (numit > maxit1_ || calp1/salp1 > calp1b/salp1b))
360  { salp1b = salp1; calp1b = calp1; }
361  else if (v < 0 && (numit > maxit1_ || calp1/salp1 < calp1a/salp1a))
362  { salp1a = salp1; calp1a = calp1; }
363  if (numit < maxit1_ && dv > 0) {
364  real
365  dalp1 = -v/dv;
366  // |dalp1| < pi test moved earlier because GEOGRAPHICLIB_PRECISION
367  // = 5 can result in dalp1 = 10^(10^8). Then sin(dalp1) takes ages
368  // (because of the need to do accurate range reduction).
369  if (fabs(dalp1) < Math::pi()) {
370  real
371  sdalp1 = sin(dalp1), cdalp1 = cos(dalp1),
372  nsalp1 = salp1 * cdalp1 + calp1 * sdalp1;
373  if (nsalp1 > 0) {
374  calp1 = calp1 * cdalp1 - salp1 * sdalp1;
375  salp1 = nsalp1;
376  Math::norm(salp1, calp1);
377  // In some regimes we don't get quadratic convergence because
378  // slope -> 0. So use convergence conditions based on epsilon
379  // instead of sqrt(epsilon).
380  tripn = fabs(v) <= 16 * tol0_;
381  continue;
382  }
383  }
384  }
385  // Either dv was not positive or updated value was outside legal
386  // range. Use the midpoint of the bracket as the next estimate.
387  // This mechanism is not needed for the WGS84 ellipsoid, but it does
388  // catch problems with more eccentric ellipsoids. Its efficacy is
389  // such for the WGS84 test set with the starting guess set to alp1 =
390  // 90deg:
391  // the WGS84 test set: mean = 5.21, sd = 3.93, max = 24
392  // WGS84 and random input: mean = 4.74, sd = 0.99
393  salp1 = (salp1a + salp1b)/2;
394  calp1 = (calp1a + calp1b)/2;
395  Math::norm(salp1, calp1);
396  tripn = false;
397  tripb = (fabs(salp1a - salp1) + (calp1a - calp1) < tolb_ ||
398  fabs(salp1 - salp1b) + (calp1 - calp1b) < tolb_);
399  }
400  {
401  real dummy;
402  // Ensure that the reduced length and geodesic scale are computed in
403  // a "canonical" way, with the I2 integral.
404  unsigned lengthmask = outmask |
405  (outmask & (REDUCEDLENGTH | GEODESICSCALE) ? DISTANCE : NONE);
406  Lengths(eps, sig12, ssig1, csig1, dn1, ssig2, csig2, dn2,
407  cbet1, cbet2, lengthmask, s12x, m12x, dummy, M12, M21, Ca);
408  }
409  m12x *= _b;
410  s12x *= _b;
411  a12 = sig12 / Math::degree();
412  if (outmask & AREA) {
413  // omg12 = lam12 - domg12
414  real sdomg12 = sin(domg12), cdomg12 = cos(domg12);
415  somg12 = slam12 * cdomg12 - clam12 * sdomg12;
416  comg12 = clam12 * cdomg12 + slam12 * sdomg12;
417  }
418  }
419  }
420 
421  if (outmask & DISTANCE)
422  s12 = real(0) + s12x; // Convert -0 to 0
423 
424  if (outmask & REDUCEDLENGTH)
425  m12 = real(0) + m12x; // Convert -0 to 0
426 
427  if (outmask & AREA) {
428  real
429  // From Lambda12: sin(alp1) * cos(bet1) = sin(alp0)
430  salp0 = salp1 * cbet1,
431  calp0 = hypot(calp1, salp1 * sbet1); // calp0 > 0
432  real alp12;
433  if (calp0 != 0 && salp0 != 0) {
434  real
435  // From Lambda12: tan(bet) = tan(sig) * cos(alp)
436  ssig1 = sbet1, csig1 = calp1 * cbet1,
437  ssig2 = sbet2, csig2 = calp2 * cbet2,
438  k2 = Math::sq(calp0) * _ep2,
439  eps = k2 / (2 * (1 + sqrt(1 + k2)) + k2),
440  // Multiplier = a^2 * e^2 * cos(alpha0) * sin(alpha0).
441  A4 = Math::sq(_a) * calp0 * salp0 * _e2;
442  Math::norm(ssig1, csig1);
443  Math::norm(ssig2, csig2);
444  C4f(eps, Ca);
445  real
446  B41 = SinCosSeries(false, ssig1, csig1, Ca, nC4_),
447  B42 = SinCosSeries(false, ssig2, csig2, Ca, nC4_);
448  S12 = A4 * (B42 - B41);
449  } else
450  // Avoid problems with indeterminate sig1, sig2 on equator
451  S12 = 0;
452  if (!meridian && somg12 == 2) {
453  somg12 = sin(omg12); comg12 = cos(omg12);
454  }
455 
456  if (!meridian &&
457  // omg12 < 3/4 * pi
458  comg12 > -real(0.7071) && // Long difference not too big
459  sbet2 - sbet1 < real(1.75)) { // Lat difference not too big
460  // Use tan(Gamma/2) = tan(omg12/2)
461  // * (tan(bet1/2)+tan(bet2/2))/(1+tan(bet1/2)*tan(bet2/2))
462  // with tan(x/2) = sin(x)/(1+cos(x))
463  real domg12 = 1 + comg12, dbet1 = 1 + cbet1, dbet2 = 1 + cbet2;
464  alp12 = 2 * atan2( somg12 * ( sbet1 * dbet2 + sbet2 * dbet1 ),
465  domg12 * ( sbet1 * sbet2 + dbet1 * dbet2 ) );
466  } else {
467  // alp12 = alp2 - alp1, used in atan2 so no need to normalize
468  real
469  salp12 = salp2 * calp1 - calp2 * salp1,
470  calp12 = calp2 * calp1 + salp2 * salp1;
471  // The right thing appears to happen if alp1 = +/-180 and alp2 = 0, viz
472  // salp12 = -0 and alp12 = -180. However this depends on the sign
473  // being attached to 0 correctly. The following ensures the correct
474  // behavior.
475  if (salp12 == 0 && calp12 < 0) {
476  salp12 = tiny_ * calp1;
477  calp12 = -1;
478  }
479  alp12 = atan2(salp12, calp12);
480  }
481  S12 += _c2 * alp12;
482  S12 *= swapp * lonsign * latsign;
483  // Convert -0 to 0
484  S12 += 0;
485  }
486 
487  // Convert calp, salp to azimuth accounting for lonsign, swapp, latsign.
488  if (swapp < 0) {
489  swap(salp1, salp2);
490  swap(calp1, calp2);
491  if (outmask & GEODESICSCALE)
492  swap(M12, M21);
493  }
494 
495  salp1 *= swapp * lonsign; calp1 *= swapp * latsign;
496  salp2 *= swapp * lonsign; calp2 *= swapp * latsign;
497  // Returned value in [0, 180]
498  return a12;
499  }
500 
501  Math::real Geodesic::GenInverse(real lat1, real lon1, real lat2, real lon2,
502  unsigned outmask,
503  real& s12, real& azi1, real& azi2,
504  real& m12, real& M12, real& M21,
505  real& S12) const {
506  outmask &= OUT_MASK;
507  real salp1, calp1, salp2, calp2,
508  a12 = GenInverse(lat1, lon1, lat2, lon2,
509  outmask, s12, salp1, calp1, salp2, calp2,
510  m12, M12, M21, S12);
511  if (outmask & AZIMUTH) {
512  azi1 = Math::atan2d(salp1, calp1);
513  azi2 = Math::atan2d(salp2, calp2);
514  }
515  return a12;
516  }
517 
518  GeodesicLine Geodesic::InverseLine(real lat1, real lon1,
519  real lat2, real lon2,
520  unsigned caps) const {
521  real t, salp1, calp1, salp2, calp2,
522  a12 = GenInverse(lat1, lon1, lat2, lon2,
523  // No need to specify AZIMUTH here
524  0u, t, salp1, calp1, salp2, calp2,
525  t, t, t, t),
526  azi1 = Math::atan2d(salp1, calp1);
527  // Ensure that a12 can be converted to a distance
528  if (caps & (OUT_MASK & DISTANCE_IN)) caps |= DISTANCE;
529  return
530  GeodesicLine(*this, lat1, lon1, azi1, salp1, calp1, caps, true, a12);
531  }
532 
533  void Geodesic::Lengths(real eps, real sig12,
534  real ssig1, real csig1, real dn1,
535  real ssig2, real csig2, real dn2,
536  real cbet1, real cbet2, unsigned outmask,
537  real& s12b, real& m12b, real& m0,
538  real& M12, real& M21,
539  // Scratch area of the right size
540  real Ca[]) const {
541  // Return m12b = (reduced length)/_b; also calculate s12b = distance/_b,
542  // and m0 = coefficient of secular term in expression for reduced length.
543 
544  outmask &= OUT_MASK;
545  // outmask & DISTANCE: set s12b
546  // outmask & REDUCEDLENGTH: set m12b & m0
547  // outmask & GEODESICSCALE: set M12 & M21
548 
549  real m0x = 0, J12 = 0, A1 = 0, A2 = 0;
550  real Cb[nC2_ + 1];
551  if (outmask & (DISTANCE | REDUCEDLENGTH | GEODESICSCALE)) {
552  A1 = A1m1f(eps);
553  C1f(eps, Ca);
554  if (outmask & (REDUCEDLENGTH | GEODESICSCALE)) {
555  A2 = A2m1f(eps);
556  C2f(eps, Cb);
557  m0x = A1 - A2;
558  A2 = 1 + A2;
559  }
560  A1 = 1 + A1;
561  }
562  if (outmask & DISTANCE) {
563  real B1 = SinCosSeries(true, ssig2, csig2, Ca, nC1_) -
564  SinCosSeries(true, ssig1, csig1, Ca, nC1_);
565  // Missing a factor of _b
566  s12b = A1 * (sig12 + B1);
567  if (outmask & (REDUCEDLENGTH | GEODESICSCALE)) {
568  real B2 = SinCosSeries(true, ssig2, csig2, Cb, nC2_) -
569  SinCosSeries(true, ssig1, csig1, Cb, nC2_);
570  J12 = m0x * sig12 + (A1 * B1 - A2 * B2);
571  }
572  } else if (outmask & (REDUCEDLENGTH | GEODESICSCALE)) {
573  // Assume here that nC1_ >= nC2_
574  for (int l = 1; l <= nC2_; ++l)
575  Cb[l] = A1 * Ca[l] - A2 * Cb[l];
576  J12 = m0x * sig12 + (SinCosSeries(true, ssig2, csig2, Cb, nC2_) -
577  SinCosSeries(true, ssig1, csig1, Cb, nC2_));
578  }
579  if (outmask & REDUCEDLENGTH) {
580  m0 = m0x;
581  // Missing a factor of _b.
582  // Add parens around (csig1 * ssig2) and (ssig1 * csig2) to ensure
583  // accurate cancellation in the case of coincident points.
584  m12b = dn2 * (csig1 * ssig2) - dn1 * (ssig1 * csig2) -
585  csig1 * csig2 * J12;
586  }
587  if (outmask & GEODESICSCALE) {
588  real csig12 = csig1 * csig2 + ssig1 * ssig2;
589  real t = _ep2 * (cbet1 - cbet2) * (cbet1 + cbet2) / (dn1 + dn2);
590  M12 = csig12 + (t * ssig2 - csig2 * J12) * ssig1 / dn1;
591  M21 = csig12 - (t * ssig1 - csig1 * J12) * ssig2 / dn2;
592  }
593  }
594 
595  Math::real Geodesic::Astroid(real x, real y) {
596  // Solve k^4+2*k^3-(x^2+y^2-1)*k^2-2*y^2*k-y^2 = 0 for positive root k.
597  // This solution is adapted from Geocentric::Reverse.
598  real k;
599  real
600  p = Math::sq(x),
601  q = Math::sq(y),
602  r = (p + q - 1) / 6;
603  if ( !(q == 0 && r <= 0) ) {
604  real
605  // Avoid possible division by zero when r = 0 by multiplying equations
606  // for s and t by r^3 and r, resp.
607  S = p * q / 4, // S = r^3 * s
608  r2 = Math::sq(r),
609  r3 = r * r2,
610  // The discriminant of the quadratic equation for T3. This is zero on
611  // the evolute curve p^(1/3)+q^(1/3) = 1
612  disc = S * (S + 2 * r3);
613  real u = r;
614  if (disc >= 0) {
615  real T3 = S + r3;
616  // Pick the sign on the sqrt to maximize abs(T3). This minimizes loss
617  // of precision due to cancellation. The result is unchanged because
618  // of the way the T is used in definition of u.
619  T3 += T3 < 0 ? -sqrt(disc) : sqrt(disc); // T3 = (r * t)^3
620  // N.B. cbrt always returns the real root. cbrt(-8) = -2.
621  real T = cbrt(T3); // T = r * t
622  // T can be zero; but then r2 / T -> 0.
623  u += T + (T != 0 ? r2 / T : 0);
624  } else {
625  // T is complex, but the way u is defined the result is real.
626  real ang = atan2(sqrt(-disc), -(S + r3));
627  // There are three possible cube roots. We choose the root which
628  // avoids cancellation. Note that disc < 0 implies that r < 0.
629  u += 2 * r * cos(ang / 3);
630  }
631  real
632  v = sqrt(Math::sq(u) + q), // guaranteed positive
633  // Avoid loss of accuracy when u < 0.
634  uv = u < 0 ? q / (v - u) : u + v, // u+v, guaranteed positive
635  w = (uv - q) / (2 * v); // positive?
636  // Rearrange expression for k to avoid loss of accuracy due to
637  // subtraction. Division by 0 not possible because uv > 0, w >= 0.
638  k = uv / (sqrt(uv + Math::sq(w)) + w); // guaranteed positive
639  } else { // q == 0 && r <= 0
640  // y = 0 with |x| <= 1. Handle this case directly.
641  // for y small, positive root is k = abs(y)/sqrt(1-x^2)
642  k = 0;
643  }
644  return k;
645  }
646 
647  Math::real Geodesic::InverseStart(real sbet1, real cbet1, real dn1,
648  real sbet2, real cbet2, real dn2,
649  real lam12, real slam12, real clam12,
650  real& salp1, real& calp1,
651  // Only updated if return val >= 0
652  real& salp2, real& calp2,
653  // Only updated for short lines
654  real& dnm,
655  // Scratch area of the right size
656  real Ca[]) const {
657  // Return a starting point for Newton's method in salp1 and calp1 (function
658  // value is -1). If Newton's method doesn't need to be used, return also
659  // salp2 and calp2 and function value is sig12.
660  real
661  sig12 = -1, // Return value
662  // bet12 = bet2 - bet1 in [0, pi); bet12a = bet2 + bet1 in (-pi, 0]
663  sbet12 = sbet2 * cbet1 - cbet2 * sbet1,
664  cbet12 = cbet2 * cbet1 + sbet2 * sbet1;
665  real sbet12a = sbet2 * cbet1 + cbet2 * sbet1;
666  bool shortline = cbet12 >= 0 && sbet12 < real(0.5) &&
667  cbet2 * lam12 < real(0.5);
668  real somg12, comg12;
669  if (shortline) {
670  real sbetm2 = Math::sq(sbet1 + sbet2);
671  // sin((bet1+bet2)/2)^2
672  // = (sbet1 + sbet2)^2 / ((sbet1 + sbet2)^2 + (cbet1 + cbet2)^2)
673  sbetm2 /= sbetm2 + Math::sq(cbet1 + cbet2);
674  dnm = sqrt(1 + _ep2 * sbetm2);
675  real omg12 = lam12 / (_f1 * dnm);
676  somg12 = sin(omg12); comg12 = cos(omg12);
677  } else {
678  somg12 = slam12; comg12 = clam12;
679  }
680 
681  salp1 = cbet2 * somg12;
682  calp1 = comg12 >= 0 ?
683  sbet12 + cbet2 * sbet1 * Math::sq(somg12) / (1 + comg12) :
684  sbet12a - cbet2 * sbet1 * Math::sq(somg12) / (1 - comg12);
685 
686  real
687  ssig12 = hypot(salp1, calp1),
688  csig12 = sbet1 * sbet2 + cbet1 * cbet2 * comg12;
689 
690  if (shortline && ssig12 < _etol2) {
691  // really short lines
692  salp2 = cbet1 * somg12;
693  calp2 = sbet12 - cbet1 * sbet2 *
694  (comg12 >= 0 ? Math::sq(somg12) / (1 + comg12) : 1 - comg12);
695  Math::norm(salp2, calp2);
696  // Set return value
697  sig12 = atan2(ssig12, csig12);
698  } else if (fabs(_n) > real(0.1) || // Skip astroid calc if too eccentric
699  csig12 >= 0 ||
700  ssig12 >= 6 * fabs(_n) * Math::pi() * Math::sq(cbet1)) {
701  // Nothing to do, zeroth order spherical approximation is OK
702  } else {
703  // Scale lam12 and bet2 to x, y coordinate system where antipodal point
704  // is at origin and singular point is at y = 0, x = -1.
705  real x, y, lamscale, betscale;
706  real lam12x = atan2(-slam12, -clam12); // lam12 - pi
707  if (_f >= 0) { // In fact f == 0 does not get here
708  // x = dlong, y = dlat
709  {
710  real
711  k2 = Math::sq(sbet1) * _ep2,
712  eps = k2 / (2 * (1 + sqrt(1 + k2)) + k2);
713  lamscale = _f * cbet1 * A3f(eps) * Math::pi();
714  }
715  betscale = lamscale * cbet1;
716 
717  x = lam12x / lamscale;
718  y = sbet12a / betscale;
719  } else { // _f < 0
720  // x = dlat, y = dlong
721  real
722  cbet12a = cbet2 * cbet1 - sbet2 * sbet1,
723  bet12a = atan2(sbet12a, cbet12a);
724  real m12b, m0, dummy;
725  // In the case of lon12 = 180, this repeats a calculation made in
726  // Inverse.
727  Lengths(_n, Math::pi() + bet12a,
728  sbet1, -cbet1, dn1, sbet2, cbet2, dn2,
729  cbet1, cbet2,
730  REDUCEDLENGTH, dummy, m12b, m0, dummy, dummy, Ca);
731  x = -1 + m12b / (cbet1 * cbet2 * m0 * Math::pi());
732  betscale = x < -real(0.01) ? sbet12a / x :
733  -_f * Math::sq(cbet1) * Math::pi();
734  lamscale = betscale / cbet1;
735  y = lam12x / lamscale;
736  }
737 
738  if (y > -tol1_ && x > -1 - xthresh_) {
739  // strip near cut
740  // Need real(x) here to cast away the volatility of x for min/max
741  if (_f >= 0) {
742  salp1 = fmin(real(1), -x); calp1 = - sqrt(1 - Math::sq(salp1));
743  } else {
744  calp1 = fmax(real(x > -tol1_ ? 0 : -1), x);
745  salp1 = sqrt(1 - Math::sq(calp1));
746  }
747  } else {
748  // Estimate alp1, by solving the astroid problem.
749  //
750  // Could estimate alpha1 = theta + pi/2, directly, i.e.,
751  // calp1 = y/k; salp1 = -x/(1+k); for _f >= 0
752  // calp1 = x/(1+k); salp1 = -y/k; for _f < 0 (need to check)
753  //
754  // However, it's better to estimate omg12 from astroid and use
755  // spherical formula to compute alp1. This reduces the mean number of
756  // Newton iterations for astroid cases from 2.24 (min 0, max 6) to 2.12
757  // (min 0 max 5). The changes in the number of iterations are as
758  // follows:
759  //
760  // change percent
761  // 1 5
762  // 0 78
763  // -1 16
764  // -2 0.6
765  // -3 0.04
766  // -4 0.002
767  //
768  // The histogram of iterations is (m = number of iterations estimating
769  // alp1 directly, n = number of iterations estimating via omg12, total
770  // number of trials = 148605):
771  //
772  // iter m n
773  // 0 148 186
774  // 1 13046 13845
775  // 2 93315 102225
776  // 3 36189 32341
777  // 4 5396 7
778  // 5 455 1
779  // 6 56 0
780  //
781  // Because omg12 is near pi, estimate work with omg12a = pi - omg12
782  real k = Astroid(x, y);
783  real
784  omg12a = lamscale * ( _f >= 0 ? -x * k/(1 + k) : -y * (1 + k)/k );
785  somg12 = sin(omg12a); comg12 = -cos(omg12a);
786  // Update spherical estimate of alp1 using omg12 instead of lam12
787  salp1 = cbet2 * somg12;
788  calp1 = sbet12a - cbet2 * sbet1 * Math::sq(somg12) / (1 - comg12);
789  }
790  }
791  // Sanity check on starting guess. Backwards check allows NaN through.
792  if (!(salp1 <= 0))
793  Math::norm(salp1, calp1);
794  else {
795  salp1 = 1; calp1 = 0;
796  }
797  return sig12;
798  }
799 
800  Math::real Geodesic::Lambda12(real sbet1, real cbet1, real dn1,
801  real sbet2, real cbet2, real dn2,
802  real salp1, real calp1,
803  real slam120, real clam120,
804  real& salp2, real& calp2,
805  real& sig12,
806  real& ssig1, real& csig1,
807  real& ssig2, real& csig2,
808  real& eps, real& domg12,
809  bool diffp, real& dlam12,
810  // Scratch area of the right size
811  real Ca[]) const {
812 
813  if (sbet1 == 0 && calp1 == 0)
814  // Break degeneracy of equatorial line. This case has already been
815  // handled.
816  calp1 = -tiny_;
817 
818  real
819  // sin(alp1) * cos(bet1) = sin(alp0)
820  salp0 = salp1 * cbet1,
821  calp0 = hypot(calp1, salp1 * sbet1); // calp0 > 0
822 
823  real somg1, comg1, somg2, comg2, somg12, comg12, lam12;
824  // tan(bet1) = tan(sig1) * cos(alp1)
825  // tan(omg1) = sin(alp0) * tan(sig1) = tan(omg1)=tan(alp1)*sin(bet1)
826  ssig1 = sbet1; somg1 = salp0 * sbet1;
827  csig1 = comg1 = calp1 * cbet1;
828  Math::norm(ssig1, csig1);
829  // Math::norm(somg1, comg1); -- don't need to normalize!
830 
831  // Enforce symmetries in the case abs(bet2) = -bet1. Need to be careful
832  // about this case, since this can yield singularities in the Newton
833  // iteration.
834  // sin(alp2) * cos(bet2) = sin(alp0)
835  salp2 = cbet2 != cbet1 ? salp0 / cbet2 : salp1;
836  // calp2 = sqrt(1 - sq(salp2))
837  // = sqrt(sq(calp0) - sq(sbet2)) / cbet2
838  // and subst for calp0 and rearrange to give (choose positive sqrt
839  // to give alp2 in [0, pi/2]).
840  calp2 = cbet2 != cbet1 || fabs(sbet2) != -sbet1 ?
841  sqrt(Math::sq(calp1 * cbet1) +
842  (cbet1 < -sbet1 ?
843  (cbet2 - cbet1) * (cbet1 + cbet2) :
844  (sbet1 - sbet2) * (sbet1 + sbet2))) / cbet2 :
845  fabs(calp1);
846  // tan(bet2) = tan(sig2) * cos(alp2)
847  // tan(omg2) = sin(alp0) * tan(sig2).
848  ssig2 = sbet2; somg2 = salp0 * sbet2;
849  csig2 = comg2 = calp2 * cbet2;
850  Math::norm(ssig2, csig2);
851  // Math::norm(somg2, comg2); -- don't need to normalize!
852 
853  // sig12 = sig2 - sig1, limit to [0, pi]
854  sig12 = atan2(fmax(real(0), csig1 * ssig2 - ssig1 * csig2) + real(0),
855  csig1 * csig2 + ssig1 * ssig2);
856 
857  // omg12 = omg2 - omg1, limit to [0, pi]
858  somg12 = fmax(real(0), comg1 * somg2 - somg1 * comg2) + real(0);
859  comg12 = comg1 * comg2 + somg1 * somg2;
860  // eta = omg12 - lam120
861  real eta = atan2(somg12 * clam120 - comg12 * slam120,
862  comg12 * clam120 + somg12 * slam120);
863  real B312;
864  real k2 = Math::sq(calp0) * _ep2;
865  eps = k2 / (2 * (1 + sqrt(1 + k2)) + k2);
866  C3f(eps, Ca);
867  B312 = (SinCosSeries(true, ssig2, csig2, Ca, nC3_-1) -
868  SinCosSeries(true, ssig1, csig1, Ca, nC3_-1));
869  domg12 = -_f * A3f(eps) * salp0 * (sig12 + B312);
870  lam12 = eta + domg12;
871 
872  if (diffp) {
873  if (calp2 == 0)
874  dlam12 = - 2 * _f1 * dn1 / sbet1;
875  else {
876  real dummy;
877  Lengths(eps, sig12, ssig1, csig1, dn1, ssig2, csig2, dn2,
878  cbet1, cbet2, REDUCEDLENGTH,
879  dummy, dlam12, dummy, dummy, dummy, Ca);
880  dlam12 *= _f1 / (calp2 * cbet2);
881  }
882  }
883 
884  return lam12;
885  }
886 
887  Math::real Geodesic::A3f(real eps) const {
888  // Evaluate A3
889  return Math::polyval(nA3_ - 1, _aA3x, eps);
890  }
891 
892  void Geodesic::C3f(real eps, real c[]) const {
893  // Evaluate C3 coeffs
894  // Elements c[1] thru c[nC3_ - 1] are set
895  real mult = 1;
896  int o = 0;
897  for (int l = 1; l < nC3_; ++l) { // l is index of C3[l]
898  int m = nC3_ - l - 1; // order of polynomial in eps
899  mult *= eps;
900  c[l] = mult * Math::polyval(m, _cC3x + o, eps);
901  o += m + 1;
902  }
903  // Post condition: o == nC3x_
904  }
905 
906  void Geodesic::C4f(real eps, real c[]) const {
907  // Evaluate C4 coeffs
908  // Elements c[0] thru c[nC4_ - 1] are set
909  real mult = 1;
910  int o = 0;
911  for (int l = 0; l < nC4_; ++l) { // l is index of C4[l]
912  int m = nC4_ - l - 1; // order of polynomial in eps
913  c[l] = mult * Math::polyval(m, _cC4x + o, eps);
914  o += m + 1;
915  mult *= eps;
916  }
917  // Post condition: o == nC4x_
918  }
919 
920  // The static const coefficient arrays in the following functions are
921  // generated by Maxima and give the coefficients of the Taylor expansions for
922  // the geodesics. The convention on the order of these coefficients is as
923  // follows:
924  //
925  // ascending order in the trigonometric expansion,
926  // then powers of eps in descending order,
927  // finally powers of n in descending order.
928  //
929  // (For some expansions, only a subset of levels occur.) For each polynomial
930  // of order n at the lowest level, the (n+1) coefficients of the polynomial
931  // are followed by a divisor which is applied to the whole polynomial. In
932  // this way, the coefficients are expressible with no round off error. The
933  // sizes of the coefficient arrays are:
934  //
935  // A1m1f, A2m1f = floor(N/2) + 2
936  // C1f, C1pf, C2f, A3coeff = (N^2 + 7*N - 2*floor(N/2)) / 4
937  // C3coeff = (N - 1) * (N^2 + 7*N - 2*floor(N/2)) / 8
938  // C4coeff = N * (N + 1) * (N + 5) / 6
939  //
940  // where N = GEOGRAPHICLIB_GEODESIC_ORDER
941  // = nA1 = nA2 = nC1 = nC1p = nA3 = nC4
942 
943  // The scale factor A1-1 = mean value of (d/dsigma)I1 - 1
944  Math::real Geodesic::A1m1f(real eps) {
945  // Generated by Maxima on 2015-05-05 18:08:12-04:00
946 #if GEOGRAPHICLIB_GEODESIC_ORDER/2 == 1
947  static const real coeff[] = {
948  // (1-eps)*A1-1, polynomial in eps2 of order 1
949  1, 0, 4,
950  };
951 #elif GEOGRAPHICLIB_GEODESIC_ORDER/2 == 2
952  static const real coeff[] = {
953  // (1-eps)*A1-1, polynomial in eps2 of order 2
954  1, 16, 0, 64,
955  };
956 #elif GEOGRAPHICLIB_GEODESIC_ORDER/2 == 3
957  static const real coeff[] = {
958  // (1-eps)*A1-1, polynomial in eps2 of order 3
959  1, 4, 64, 0, 256,
960  };
961 #elif GEOGRAPHICLIB_GEODESIC_ORDER/2 == 4
962  static const real coeff[] = {
963  // (1-eps)*A1-1, polynomial in eps2 of order 4
964  25, 64, 256, 4096, 0, 16384,
965  };
966 #else
967 #error "Bad value for GEOGRAPHICLIB_GEODESIC_ORDER"
968 #endif
969  static_assert(sizeof(coeff) / sizeof(real) == nA1_/2 + 2,
970  "Coefficient array size mismatch in A1m1f");
971  int m = nA1_/2;
972  real t = Math::polyval(m, coeff, Math::sq(eps)) / coeff[m + 1];
973  return (t + eps) / (1 - eps);
974  }
975 
976  // The coefficients C1[l] in the Fourier expansion of B1
977  void Geodesic::C1f(real eps, real c[]) {
978  // Generated by Maxima on 2015-05-05 18:08:12-04:00
979 #if GEOGRAPHICLIB_GEODESIC_ORDER == 3
980  static const real coeff[] = {
981  // C1[1]/eps^1, polynomial in eps2 of order 1
982  3, -8, 16,
983  // C1[2]/eps^2, polynomial in eps2 of order 0
984  -1, 16,
985  // C1[3]/eps^3, polynomial in eps2 of order 0
986  -1, 48,
987  };
988 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 4
989  static const real coeff[] = {
990  // C1[1]/eps^1, polynomial in eps2 of order 1
991  3, -8, 16,
992  // C1[2]/eps^2, polynomial in eps2 of order 1
993  1, -2, 32,
994  // C1[3]/eps^3, polynomial in eps2 of order 0
995  -1, 48,
996  // C1[4]/eps^4, polynomial in eps2 of order 0
997  -5, 512,
998  };
999 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 5
1000  static const real coeff[] = {
1001  // C1[1]/eps^1, polynomial in eps2 of order 2
1002  -1, 6, -16, 32,
1003  // C1[2]/eps^2, polynomial in eps2 of order 1
1004  1, -2, 32,
1005  // C1[3]/eps^3, polynomial in eps2 of order 1
1006  9, -16, 768,
1007  // C1[4]/eps^4, polynomial in eps2 of order 0
1008  -5, 512,
1009  // C1[5]/eps^5, polynomial in eps2 of order 0
1010  -7, 1280,
1011  };
1012 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 6
1013  static const real coeff[] = {
1014  // C1[1]/eps^1, polynomial in eps2 of order 2
1015  -1, 6, -16, 32,
1016  // C1[2]/eps^2, polynomial in eps2 of order 2
1017  -9, 64, -128, 2048,
1018  // C1[3]/eps^3, polynomial in eps2 of order 1
1019  9, -16, 768,
1020  // C1[4]/eps^4, polynomial in eps2 of order 1
1021  3, -5, 512,
1022  // C1[5]/eps^5, polynomial in eps2 of order 0
1023  -7, 1280,
1024  // C1[6]/eps^6, polynomial in eps2 of order 0
1025  -7, 2048,
1026  };
1027 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 7
1028  static const real coeff[] = {
1029  // C1[1]/eps^1, polynomial in eps2 of order 3
1030  19, -64, 384, -1024, 2048,
1031  // C1[2]/eps^2, polynomial in eps2 of order 2
1032  -9, 64, -128, 2048,
1033  // C1[3]/eps^3, polynomial in eps2 of order 2
1034  -9, 72, -128, 6144,
1035  // C1[4]/eps^4, polynomial in eps2 of order 1
1036  3, -5, 512,
1037  // C1[5]/eps^5, polynomial in eps2 of order 1
1038  35, -56, 10240,
1039  // C1[6]/eps^6, polynomial in eps2 of order 0
1040  -7, 2048,
1041  // C1[7]/eps^7, polynomial in eps2 of order 0
1042  -33, 14336,
1043  };
1044 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 8
1045  static const real coeff[] = {
1046  // C1[1]/eps^1, polynomial in eps2 of order 3
1047  19, -64, 384, -1024, 2048,
1048  // C1[2]/eps^2, polynomial in eps2 of order 3
1049  7, -18, 128, -256, 4096,
1050  // C1[3]/eps^3, polynomial in eps2 of order 2
1051  -9, 72, -128, 6144,
1052  // C1[4]/eps^4, polynomial in eps2 of order 2
1053  -11, 96, -160, 16384,
1054  // C1[5]/eps^5, polynomial in eps2 of order 1
1055  35, -56, 10240,
1056  // C1[6]/eps^6, polynomial in eps2 of order 1
1057  9, -14, 4096,
1058  // C1[7]/eps^7, polynomial in eps2 of order 0
1059  -33, 14336,
1060  // C1[8]/eps^8, polynomial in eps2 of order 0
1061  -429, 262144,
1062  };
1063 #else
1064 #error "Bad value for GEOGRAPHICLIB_GEODESIC_ORDER"
1065 #endif
1066  static_assert(sizeof(coeff) / sizeof(real) ==
1067  (nC1_*nC1_ + 7*nC1_ - 2*(nC1_/2)) / 4,
1068  "Coefficient array size mismatch in C1f");
1069  real
1070  eps2 = Math::sq(eps),
1071  d = eps;
1072  int o = 0;
1073  for (int l = 1; l <= nC1_; ++l) { // l is index of C1p[l]
1074  int m = (nC1_ - l) / 2; // order of polynomial in eps^2
1075  c[l] = d * Math::polyval(m, coeff + o, eps2) / coeff[o + m + 1];
1076  o += m + 2;
1077  d *= eps;
1078  }
1079  // Post condition: o == sizeof(coeff) / sizeof(real)
1080  }
1081 
1082  // The coefficients C1p[l] in the Fourier expansion of B1p
1083  void Geodesic::C1pf(real eps, real c[]) {
1084  // Generated by Maxima on 2015-05-05 18:08:12-04:00
1085 #if GEOGRAPHICLIB_GEODESIC_ORDER == 3
1086  static const real coeff[] = {
1087  // C1p[1]/eps^1, polynomial in eps2 of order 1
1088  -9, 16, 32,
1089  // C1p[2]/eps^2, polynomial in eps2 of order 0
1090  5, 16,
1091  // C1p[3]/eps^3, polynomial in eps2 of order 0
1092  29, 96,
1093  };
1094 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 4
1095  static const real coeff[] = {
1096  // C1p[1]/eps^1, polynomial in eps2 of order 1
1097  -9, 16, 32,
1098  // C1p[2]/eps^2, polynomial in eps2 of order 1
1099  -37, 30, 96,
1100  // C1p[3]/eps^3, polynomial in eps2 of order 0
1101  29, 96,
1102  // C1p[4]/eps^4, polynomial in eps2 of order 0
1103  539, 1536,
1104  };
1105 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 5
1106  static const real coeff[] = {
1107  // C1p[1]/eps^1, polynomial in eps2 of order 2
1108  205, -432, 768, 1536,
1109  // C1p[2]/eps^2, polynomial in eps2 of order 1
1110  -37, 30, 96,
1111  // C1p[3]/eps^3, polynomial in eps2 of order 1
1112  -225, 116, 384,
1113  // C1p[4]/eps^4, polynomial in eps2 of order 0
1114  539, 1536,
1115  // C1p[5]/eps^5, polynomial in eps2 of order 0
1116  3467, 7680,
1117  };
1118 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 6
1119  static const real coeff[] = {
1120  // C1p[1]/eps^1, polynomial in eps2 of order 2
1121  205, -432, 768, 1536,
1122  // C1p[2]/eps^2, polynomial in eps2 of order 2
1123  4005, -4736, 3840, 12288,
1124  // C1p[3]/eps^3, polynomial in eps2 of order 1
1125  -225, 116, 384,
1126  // C1p[4]/eps^4, polynomial in eps2 of order 1
1127  -7173, 2695, 7680,
1128  // C1p[5]/eps^5, polynomial in eps2 of order 0
1129  3467, 7680,
1130  // C1p[6]/eps^6, polynomial in eps2 of order 0
1131  38081, 61440,
1132  };
1133 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 7
1134  static const real coeff[] = {
1135  // C1p[1]/eps^1, polynomial in eps2 of order 3
1136  -4879, 9840, -20736, 36864, 73728,
1137  // C1p[2]/eps^2, polynomial in eps2 of order 2
1138  4005, -4736, 3840, 12288,
1139  // C1p[3]/eps^3, polynomial in eps2 of order 2
1140  8703, -7200, 3712, 12288,
1141  // C1p[4]/eps^4, polynomial in eps2 of order 1
1142  -7173, 2695, 7680,
1143  // C1p[5]/eps^5, polynomial in eps2 of order 1
1144  -141115, 41604, 92160,
1145  // C1p[6]/eps^6, polynomial in eps2 of order 0
1146  38081, 61440,
1147  // C1p[7]/eps^7, polynomial in eps2 of order 0
1148  459485, 516096,
1149  };
1150 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 8
1151  static const real coeff[] = {
1152  // C1p[1]/eps^1, polynomial in eps2 of order 3
1153  -4879, 9840, -20736, 36864, 73728,
1154  // C1p[2]/eps^2, polynomial in eps2 of order 3
1155  -86171, 120150, -142080, 115200, 368640,
1156  // C1p[3]/eps^3, polynomial in eps2 of order 2
1157  8703, -7200, 3712, 12288,
1158  // C1p[4]/eps^4, polynomial in eps2 of order 2
1159  1082857, -688608, 258720, 737280,
1160  // C1p[5]/eps^5, polynomial in eps2 of order 1
1161  -141115, 41604, 92160,
1162  // C1p[6]/eps^6, polynomial in eps2 of order 1
1163  -2200311, 533134, 860160,
1164  // C1p[7]/eps^7, polynomial in eps2 of order 0
1165  459485, 516096,
1166  // C1p[8]/eps^8, polynomial in eps2 of order 0
1167  109167851, 82575360,
1168  };
1169 #else
1170 #error "Bad value for GEOGRAPHICLIB_GEODESIC_ORDER"
1171 #endif
1172  static_assert(sizeof(coeff) / sizeof(real) ==
1173  (nC1p_*nC1p_ + 7*nC1p_ - 2*(nC1p_/2)) / 4,
1174  "Coefficient array size mismatch in C1pf");
1175  real
1176  eps2 = Math::sq(eps),
1177  d = eps;
1178  int o = 0;
1179  for (int l = 1; l <= nC1p_; ++l) { // l is index of C1p[l]
1180  int m = (nC1p_ - l) / 2; // order of polynomial in eps^2
1181  c[l] = d * Math::polyval(m, coeff + o, eps2) / coeff[o + m + 1];
1182  o += m + 2;
1183  d *= eps;
1184  }
1185  // Post condition: o == sizeof(coeff) / sizeof(real)
1186  }
1187 
1188  // The scale factor A2-1 = mean value of (d/dsigma)I2 - 1
1189  Math::real Geodesic::A2m1f(real eps) {
1190  // Generated by Maxima on 2015-05-29 08:09:47-04:00
1191 #if GEOGRAPHICLIB_GEODESIC_ORDER/2 == 1
1192  static const real coeff[] = {
1193  // (eps+1)*A2-1, polynomial in eps2 of order 1
1194  -3, 0, 4,
1195  }; // count = 3
1196 #elif GEOGRAPHICLIB_GEODESIC_ORDER/2 == 2
1197  static const real coeff[] = {
1198  // (eps+1)*A2-1, polynomial in eps2 of order 2
1199  -7, -48, 0, 64,
1200  }; // count = 4
1201 #elif GEOGRAPHICLIB_GEODESIC_ORDER/2 == 3
1202  static const real coeff[] = {
1203  // (eps+1)*A2-1, polynomial in eps2 of order 3
1204  -11, -28, -192, 0, 256,
1205  }; // count = 5
1206 #elif GEOGRAPHICLIB_GEODESIC_ORDER/2 == 4
1207  static const real coeff[] = {
1208  // (eps+1)*A2-1, polynomial in eps2 of order 4
1209  -375, -704, -1792, -12288, 0, 16384,
1210  }; // count = 6
1211 #else
1212 #error "Bad value for GEOGRAPHICLIB_GEODESIC_ORDER"
1213 #endif
1214  static_assert(sizeof(coeff) / sizeof(real) == nA2_/2 + 2,
1215  "Coefficient array size mismatch in A2m1f");
1216  int m = nA2_/2;
1217  real t = Math::polyval(m, coeff, Math::sq(eps)) / coeff[m + 1];
1218  return (t - eps) / (1 + eps);
1219  }
1220 
1221  // The coefficients C2[l] in the Fourier expansion of B2
1222  void Geodesic::C2f(real eps, real c[]) {
1223  // Generated by Maxima on 2015-05-05 18:08:12-04:00
1224 #if GEOGRAPHICLIB_GEODESIC_ORDER == 3
1225  static const real coeff[] = {
1226  // C2[1]/eps^1, polynomial in eps2 of order 1
1227  1, 8, 16,
1228  // C2[2]/eps^2, polynomial in eps2 of order 0
1229  3, 16,
1230  // C2[3]/eps^3, polynomial in eps2 of order 0
1231  5, 48,
1232  };
1233 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 4
1234  static const real coeff[] = {
1235  // C2[1]/eps^1, polynomial in eps2 of order 1
1236  1, 8, 16,
1237  // C2[2]/eps^2, polynomial in eps2 of order 1
1238  1, 6, 32,
1239  // C2[3]/eps^3, polynomial in eps2 of order 0
1240  5, 48,
1241  // C2[4]/eps^4, polynomial in eps2 of order 0
1242  35, 512,
1243  };
1244 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 5
1245  static const real coeff[] = {
1246  // C2[1]/eps^1, polynomial in eps2 of order 2
1247  1, 2, 16, 32,
1248  // C2[2]/eps^2, polynomial in eps2 of order 1
1249  1, 6, 32,
1250  // C2[3]/eps^3, polynomial in eps2 of order 1
1251  15, 80, 768,
1252  // C2[4]/eps^4, polynomial in eps2 of order 0
1253  35, 512,
1254  // C2[5]/eps^5, polynomial in eps2 of order 0
1255  63, 1280,
1256  };
1257 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 6
1258  static const real coeff[] = {
1259  // C2[1]/eps^1, polynomial in eps2 of order 2
1260  1, 2, 16, 32,
1261  // C2[2]/eps^2, polynomial in eps2 of order 2
1262  35, 64, 384, 2048,
1263  // C2[3]/eps^3, polynomial in eps2 of order 1
1264  15, 80, 768,
1265  // C2[4]/eps^4, polynomial in eps2 of order 1
1266  7, 35, 512,
1267  // C2[5]/eps^5, polynomial in eps2 of order 0
1268  63, 1280,
1269  // C2[6]/eps^6, polynomial in eps2 of order 0
1270  77, 2048,
1271  };
1272 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 7
1273  static const real coeff[] = {
1274  // C2[1]/eps^1, polynomial in eps2 of order 3
1275  41, 64, 128, 1024, 2048,
1276  // C2[2]/eps^2, polynomial in eps2 of order 2
1277  35, 64, 384, 2048,
1278  // C2[3]/eps^3, polynomial in eps2 of order 2
1279  69, 120, 640, 6144,
1280  // C2[4]/eps^4, polynomial in eps2 of order 1
1281  7, 35, 512,
1282  // C2[5]/eps^5, polynomial in eps2 of order 1
1283  105, 504, 10240,
1284  // C2[6]/eps^6, polynomial in eps2 of order 0
1285  77, 2048,
1286  // C2[7]/eps^7, polynomial in eps2 of order 0
1287  429, 14336,
1288  };
1289 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 8
1290  static const real coeff[] = {
1291  // C2[1]/eps^1, polynomial in eps2 of order 3
1292  41, 64, 128, 1024, 2048,
1293  // C2[2]/eps^2, polynomial in eps2 of order 3
1294  47, 70, 128, 768, 4096,
1295  // C2[3]/eps^3, polynomial in eps2 of order 2
1296  69, 120, 640, 6144,
1297  // C2[4]/eps^4, polynomial in eps2 of order 2
1298  133, 224, 1120, 16384,
1299  // C2[5]/eps^5, polynomial in eps2 of order 1
1300  105, 504, 10240,
1301  // C2[6]/eps^6, polynomial in eps2 of order 1
1302  33, 154, 4096,
1303  // C2[7]/eps^7, polynomial in eps2 of order 0
1304  429, 14336,
1305  // C2[8]/eps^8, polynomial in eps2 of order 0
1306  6435, 262144,
1307  };
1308 #else
1309 #error "Bad value for GEOGRAPHICLIB_GEODESIC_ORDER"
1310 #endif
1311  static_assert(sizeof(coeff) / sizeof(real) ==
1312  (nC2_*nC2_ + 7*nC2_ - 2*(nC2_/2)) / 4,
1313  "Coefficient array size mismatch in C2f");
1314  real
1315  eps2 = Math::sq(eps),
1316  d = eps;
1317  int o = 0;
1318  for (int l = 1; l <= nC2_; ++l) { // l is index of C2[l]
1319  int m = (nC2_ - l) / 2; // order of polynomial in eps^2
1320  c[l] = d * Math::polyval(m, coeff + o, eps2) / coeff[o + m + 1];
1321  o += m + 2;
1322  d *= eps;
1323  }
1324  // Post condition: o == sizeof(coeff) / sizeof(real)
1325  }
1326 
1327  // The scale factor A3 = mean value of (d/dsigma)I3
1328  void Geodesic::A3coeff() {
1329  // Generated by Maxima on 2015-05-05 18:08:13-04:00
1330 #if GEOGRAPHICLIB_GEODESIC_ORDER == 3
1331  static const real coeff[] = {
1332  // A3, coeff of eps^2, polynomial in n of order 0
1333  -1, 4,
1334  // A3, coeff of eps^1, polynomial in n of order 1
1335  1, -1, 2,
1336  // A3, coeff of eps^0, polynomial in n of order 0
1337  1, 1,
1338  };
1339 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 4
1340  static const real coeff[] = {
1341  // A3, coeff of eps^3, polynomial in n of order 0
1342  -1, 16,
1343  // A3, coeff of eps^2, polynomial in n of order 1
1344  -1, -2, 8,
1345  // A3, coeff of eps^1, polynomial in n of order 1
1346  1, -1, 2,
1347  // A3, coeff of eps^0, polynomial in n of order 0
1348  1, 1,
1349  };
1350 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 5
1351  static const real coeff[] = {
1352  // A3, coeff of eps^4, polynomial in n of order 0
1353  -3, 64,
1354  // A3, coeff of eps^3, polynomial in n of order 1
1355  -3, -1, 16,
1356  // A3, coeff of eps^2, polynomial in n of order 2
1357  3, -1, -2, 8,
1358  // A3, coeff of eps^1, polynomial in n of order 1
1359  1, -1, 2,
1360  // A3, coeff of eps^0, polynomial in n of order 0
1361  1, 1,
1362  };
1363 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 6
1364  static const real coeff[] = {
1365  // A3, coeff of eps^5, polynomial in n of order 0
1366  -3, 128,
1367  // A3, coeff of eps^4, polynomial in n of order 1
1368  -2, -3, 64,
1369  // A3, coeff of eps^3, polynomial in n of order 2
1370  -1, -3, -1, 16,
1371  // A3, coeff of eps^2, polynomial in n of order 2
1372  3, -1, -2, 8,
1373  // A3, coeff of eps^1, polynomial in n of order 1
1374  1, -1, 2,
1375  // A3, coeff of eps^0, polynomial in n of order 0
1376  1, 1,
1377  };
1378 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 7
1379  static const real coeff[] = {
1380  // A3, coeff of eps^6, polynomial in n of order 0
1381  -5, 256,
1382  // A3, coeff of eps^5, polynomial in n of order 1
1383  -5, -3, 128,
1384  // A3, coeff of eps^4, polynomial in n of order 2
1385  -10, -2, -3, 64,
1386  // A3, coeff of eps^3, polynomial in n of order 3
1387  5, -1, -3, -1, 16,
1388  // A3, coeff of eps^2, polynomial in n of order 2
1389  3, -1, -2, 8,
1390  // A3, coeff of eps^1, polynomial in n of order 1
1391  1, -1, 2,
1392  // A3, coeff of eps^0, polynomial in n of order 0
1393  1, 1,
1394  };
1395 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 8
1396  static const real coeff[] = {
1397  // A3, coeff of eps^7, polynomial in n of order 0
1398  -25, 2048,
1399  // A3, coeff of eps^6, polynomial in n of order 1
1400  -15, -20, 1024,
1401  // A3, coeff of eps^5, polynomial in n of order 2
1402  -5, -10, -6, 256,
1403  // A3, coeff of eps^4, polynomial in n of order 3
1404  -5, -20, -4, -6, 128,
1405  // A3, coeff of eps^3, polynomial in n of order 3
1406  5, -1, -3, -1, 16,
1407  // A3, coeff of eps^2, polynomial in n of order 2
1408  3, -1, -2, 8,
1409  // A3, coeff of eps^1, polynomial in n of order 1
1410  1, -1, 2,
1411  // A3, coeff of eps^0, polynomial in n of order 0
1412  1, 1,
1413  };
1414 #else
1415 #error "Bad value for GEOGRAPHICLIB_GEODESIC_ORDER"
1416 #endif
1417  static_assert(sizeof(coeff) / sizeof(real) ==
1418  (nA3_*nA3_ + 7*nA3_ - 2*(nA3_/2)) / 4,
1419  "Coefficient array size mismatch in A3f");
1420  int o = 0, k = 0;
1421  for (int j = nA3_ - 1; j >= 0; --j) { // coeff of eps^j
1422  int m = min(nA3_ - j - 1, j); // order of polynomial in n
1423  _aA3x[k++] = Math::polyval(m, coeff + o, _n) / coeff[o + m + 1];
1424  o += m + 2;
1425  }
1426  // Post condition: o == sizeof(coeff) / sizeof(real) && k == nA3x_
1427  }
1428 
1429  // The coefficients C3[l] in the Fourier expansion of B3
1430  void Geodesic::C3coeff() {
1431  // Generated by Maxima on 2015-05-05 18:08:13-04:00
1432 #if GEOGRAPHICLIB_GEODESIC_ORDER == 3
1433  static const real coeff[] = {
1434  // C3[1], coeff of eps^2, polynomial in n of order 0
1435  1, 8,
1436  // C3[1], coeff of eps^1, polynomial in n of order 1
1437  -1, 1, 4,
1438  // C3[2], coeff of eps^2, polynomial in n of order 0
1439  1, 16,
1440  };
1441 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 4
1442  static const real coeff[] = {
1443  // C3[1], coeff of eps^3, polynomial in n of order 0
1444  3, 64,
1445  // C3[1], coeff of eps^2, polynomial in n of order 1
1446  // This is a case where a leading 0 term has been inserted to maintain the
1447  // pattern in the orders of the polynomials.
1448  0, 1, 8,
1449  // C3[1], coeff of eps^1, polynomial in n of order 1
1450  -1, 1, 4,
1451  // C3[2], coeff of eps^3, polynomial in n of order 0
1452  3, 64,
1453  // C3[2], coeff of eps^2, polynomial in n of order 1
1454  -3, 2, 32,
1455  // C3[3], coeff of eps^3, polynomial in n of order 0
1456  5, 192,
1457  };
1458 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 5
1459  static const real coeff[] = {
1460  // C3[1], coeff of eps^4, polynomial in n of order 0
1461  5, 128,
1462  // C3[1], coeff of eps^3, polynomial in n of order 1
1463  3, 3, 64,
1464  // C3[1], coeff of eps^2, polynomial in n of order 2
1465  -1, 0, 1, 8,
1466  // C3[1], coeff of eps^1, polynomial in n of order 1
1467  -1, 1, 4,
1468  // C3[2], coeff of eps^4, polynomial in n of order 0
1469  3, 128,
1470  // C3[2], coeff of eps^3, polynomial in n of order 1
1471  -2, 3, 64,
1472  // C3[2], coeff of eps^2, polynomial in n of order 2
1473  1, -3, 2, 32,
1474  // C3[3], coeff of eps^4, polynomial in n of order 0
1475  3, 128,
1476  // C3[3], coeff of eps^3, polynomial in n of order 1
1477  -9, 5, 192,
1478  // C3[4], coeff of eps^4, polynomial in n of order 0
1479  7, 512,
1480  };
1481 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 6
1482  static const real coeff[] = {
1483  // C3[1], coeff of eps^5, polynomial in n of order 0
1484  3, 128,
1485  // C3[1], coeff of eps^4, polynomial in n of order 1
1486  2, 5, 128,
1487  // C3[1], coeff of eps^3, polynomial in n of order 2
1488  -1, 3, 3, 64,
1489  // C3[1], coeff of eps^2, polynomial in n of order 2
1490  -1, 0, 1, 8,
1491  // C3[1], coeff of eps^1, polynomial in n of order 1
1492  -1, 1, 4,
1493  // C3[2], coeff of eps^5, polynomial in n of order 0
1494  5, 256,
1495  // C3[2], coeff of eps^4, polynomial in n of order 1
1496  1, 3, 128,
1497  // C3[2], coeff of eps^3, polynomial in n of order 2
1498  -3, -2, 3, 64,
1499  // C3[2], coeff of eps^2, polynomial in n of order 2
1500  1, -3, 2, 32,
1501  // C3[3], coeff of eps^5, polynomial in n of order 0
1502  7, 512,
1503  // C3[3], coeff of eps^4, polynomial in n of order 1
1504  -10, 9, 384,
1505  // C3[3], coeff of eps^3, polynomial in n of order 2
1506  5, -9, 5, 192,
1507  // C3[4], coeff of eps^5, polynomial in n of order 0
1508  7, 512,
1509  // C3[4], coeff of eps^4, polynomial in n of order 1
1510  -14, 7, 512,
1511  // C3[5], coeff of eps^5, polynomial in n of order 0
1512  21, 2560,
1513  };
1514 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 7
1515  static const real coeff[] = {
1516  // C3[1], coeff of eps^6, polynomial in n of order 0
1517  21, 1024,
1518  // C3[1], coeff of eps^5, polynomial in n of order 1
1519  11, 12, 512,
1520  // C3[1], coeff of eps^4, polynomial in n of order 2
1521  2, 2, 5, 128,
1522  // C3[1], coeff of eps^3, polynomial in n of order 3
1523  -5, -1, 3, 3, 64,
1524  // C3[1], coeff of eps^2, polynomial in n of order 2
1525  -1, 0, 1, 8,
1526  // C3[1], coeff of eps^1, polynomial in n of order 1
1527  -1, 1, 4,
1528  // C3[2], coeff of eps^6, polynomial in n of order 0
1529  27, 2048,
1530  // C3[2], coeff of eps^5, polynomial in n of order 1
1531  1, 5, 256,
1532  // C3[2], coeff of eps^4, polynomial in n of order 2
1533  -9, 2, 6, 256,
1534  // C3[2], coeff of eps^3, polynomial in n of order 3
1535  2, -3, -2, 3, 64,
1536  // C3[2], coeff of eps^2, polynomial in n of order 2
1537  1, -3, 2, 32,
1538  // C3[3], coeff of eps^6, polynomial in n of order 0
1539  3, 256,
1540  // C3[3], coeff of eps^5, polynomial in n of order 1
1541  -4, 21, 1536,
1542  // C3[3], coeff of eps^4, polynomial in n of order 2
1543  -6, -10, 9, 384,
1544  // C3[3], coeff of eps^3, polynomial in n of order 3
1545  -1, 5, -9, 5, 192,
1546  // C3[4], coeff of eps^6, polynomial in n of order 0
1547  9, 1024,
1548  // C3[4], coeff of eps^5, polynomial in n of order 1
1549  -10, 7, 512,
1550  // C3[4], coeff of eps^4, polynomial in n of order 2
1551  10, -14, 7, 512,
1552  // C3[5], coeff of eps^6, polynomial in n of order 0
1553  9, 1024,
1554  // C3[5], coeff of eps^5, polynomial in n of order 1
1555  -45, 21, 2560,
1556  // C3[6], coeff of eps^6, polynomial in n of order 0
1557  11, 2048,
1558  };
1559 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 8
1560  static const real coeff[] = {
1561  // C3[1], coeff of eps^7, polynomial in n of order 0
1562  243, 16384,
1563  // C3[1], coeff of eps^6, polynomial in n of order 1
1564  10, 21, 1024,
1565  // C3[1], coeff of eps^5, polynomial in n of order 2
1566  3, 11, 12, 512,
1567  // C3[1], coeff of eps^4, polynomial in n of order 3
1568  -2, 2, 2, 5, 128,
1569  // C3[1], coeff of eps^3, polynomial in n of order 3
1570  -5, -1, 3, 3, 64,
1571  // C3[1], coeff of eps^2, polynomial in n of order 2
1572  -1, 0, 1, 8,
1573  // C3[1], coeff of eps^1, polynomial in n of order 1
1574  -1, 1, 4,
1575  // C3[2], coeff of eps^7, polynomial in n of order 0
1576  187, 16384,
1577  // C3[2], coeff of eps^6, polynomial in n of order 1
1578  69, 108, 8192,
1579  // C3[2], coeff of eps^5, polynomial in n of order 2
1580  -2, 1, 5, 256,
1581  // C3[2], coeff of eps^4, polynomial in n of order 3
1582  -6, -9, 2, 6, 256,
1583  // C3[2], coeff of eps^3, polynomial in n of order 3
1584  2, -3, -2, 3, 64,
1585  // C3[2], coeff of eps^2, polynomial in n of order 2
1586  1, -3, 2, 32,
1587  // C3[3], coeff of eps^7, polynomial in n of order 0
1588  139, 16384,
1589  // C3[3], coeff of eps^6, polynomial in n of order 1
1590  -1, 12, 1024,
1591  // C3[3], coeff of eps^5, polynomial in n of order 2
1592  -77, -8, 42, 3072,
1593  // C3[3], coeff of eps^4, polynomial in n of order 3
1594  10, -6, -10, 9, 384,
1595  // C3[3], coeff of eps^3, polynomial in n of order 3
1596  -1, 5, -9, 5, 192,
1597  // C3[4], coeff of eps^7, polynomial in n of order 0
1598  127, 16384,
1599  // C3[4], coeff of eps^6, polynomial in n of order 1
1600  -43, 72, 8192,
1601  // C3[4], coeff of eps^5, polynomial in n of order 2
1602  -7, -40, 28, 2048,
1603  // C3[4], coeff of eps^4, polynomial in n of order 3
1604  -7, 20, -28, 14, 1024,
1605  // C3[5], coeff of eps^7, polynomial in n of order 0
1606  99, 16384,
1607  // C3[5], coeff of eps^6, polynomial in n of order 1
1608  -15, 9, 1024,
1609  // C3[5], coeff of eps^5, polynomial in n of order 2
1610  75, -90, 42, 5120,
1611  // C3[6], coeff of eps^7, polynomial in n of order 0
1612  99, 16384,
1613  // C3[6], coeff of eps^6, polynomial in n of order 1
1614  -99, 44, 8192,
1615  // C3[7], coeff of eps^7, polynomial in n of order 0
1616  429, 114688,
1617  };
1618 #else
1619 #error "Bad value for GEOGRAPHICLIB_GEODESIC_ORDER"
1620 #endif
1621  static_assert(sizeof(coeff) / sizeof(real) ==
1622  ((nC3_-1)*(nC3_*nC3_ + 7*nC3_ - 2*(nC3_/2)))/8,
1623  "Coefficient array size mismatch in C3coeff");
1624  int o = 0, k = 0;
1625  for (int l = 1; l < nC3_; ++l) { // l is index of C3[l]
1626  for (int j = nC3_ - 1; j >= l; --j) { // coeff of eps^j
1627  int m = min(nC3_ - j - 1, j); // order of polynomial in n
1628  _cC3x[k++] = Math::polyval(m, coeff + o, _n) / coeff[o + m + 1];
1629  o += m + 2;
1630  }
1631  }
1632  // Post condition: o == sizeof(coeff) / sizeof(real) && k == nC3x_
1633  }
1634 
1635  void Geodesic::C4coeff() {
1636  // Generated by Maxima on 2015-05-05 18:08:13-04:00
1637 #if GEOGRAPHICLIB_GEODESIC_ORDER == 3
1638  static const real coeff[] = {
1639  // C4[0], coeff of eps^2, polynomial in n of order 0
1640  -2, 105,
1641  // C4[0], coeff of eps^1, polynomial in n of order 1
1642  16, -7, 35,
1643  // C4[0], coeff of eps^0, polynomial in n of order 2
1644  8, -28, 70, 105,
1645  // C4[1], coeff of eps^2, polynomial in n of order 0
1646  -2, 105,
1647  // C4[1], coeff of eps^1, polynomial in n of order 1
1648  -16, 7, 315,
1649  // C4[2], coeff of eps^2, polynomial in n of order 0
1650  4, 525,
1651  };
1652 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 4
1653  static const real coeff[] = {
1654  // C4[0], coeff of eps^3, polynomial in n of order 0
1655  11, 315,
1656  // C4[0], coeff of eps^2, polynomial in n of order 1
1657  -32, -6, 315,
1658  // C4[0], coeff of eps^1, polynomial in n of order 2
1659  -32, 48, -21, 105,
1660  // C4[0], coeff of eps^0, polynomial in n of order 3
1661  4, 24, -84, 210, 315,
1662  // C4[1], coeff of eps^3, polynomial in n of order 0
1663  -1, 105,
1664  // C4[1], coeff of eps^2, polynomial in n of order 1
1665  64, -18, 945,
1666  // C4[1], coeff of eps^1, polynomial in n of order 2
1667  32, -48, 21, 945,
1668  // C4[2], coeff of eps^3, polynomial in n of order 0
1669  -8, 1575,
1670  // C4[2], coeff of eps^2, polynomial in n of order 1
1671  -32, 12, 1575,
1672  // C4[3], coeff of eps^3, polynomial in n of order 0
1673  8, 2205,
1674  };
1675 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 5
1676  static const real coeff[] = {
1677  // C4[0], coeff of eps^4, polynomial in n of order 0
1678  4, 1155,
1679  // C4[0], coeff of eps^3, polynomial in n of order 1
1680  -368, 121, 3465,
1681  // C4[0], coeff of eps^2, polynomial in n of order 2
1682  1088, -352, -66, 3465,
1683  // C4[0], coeff of eps^1, polynomial in n of order 3
1684  48, -352, 528, -231, 1155,
1685  // C4[0], coeff of eps^0, polynomial in n of order 4
1686  16, 44, 264, -924, 2310, 3465,
1687  // C4[1], coeff of eps^4, polynomial in n of order 0
1688  4, 1155,
1689  // C4[1], coeff of eps^3, polynomial in n of order 1
1690  80, -99, 10395,
1691  // C4[1], coeff of eps^2, polynomial in n of order 2
1692  -896, 704, -198, 10395,
1693  // C4[1], coeff of eps^1, polynomial in n of order 3
1694  -48, 352, -528, 231, 10395,
1695  // C4[2], coeff of eps^4, polynomial in n of order 0
1696  -8, 1925,
1697  // C4[2], coeff of eps^3, polynomial in n of order 1
1698  384, -88, 17325,
1699  // C4[2], coeff of eps^2, polynomial in n of order 2
1700  320, -352, 132, 17325,
1701  // C4[3], coeff of eps^4, polynomial in n of order 0
1702  -16, 8085,
1703  // C4[3], coeff of eps^3, polynomial in n of order 1
1704  -256, 88, 24255,
1705  // C4[4], coeff of eps^4, polynomial in n of order 0
1706  64, 31185,
1707  };
1708 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 6
1709  static const real coeff[] = {
1710  // C4[0], coeff of eps^5, polynomial in n of order 0
1711  97, 15015,
1712  // C4[0], coeff of eps^4, polynomial in n of order 1
1713  1088, 156, 45045,
1714  // C4[0], coeff of eps^3, polynomial in n of order 2
1715  -224, -4784, 1573, 45045,
1716  // C4[0], coeff of eps^2, polynomial in n of order 3
1717  -10656, 14144, -4576, -858, 45045,
1718  // C4[0], coeff of eps^1, polynomial in n of order 4
1719  64, 624, -4576, 6864, -3003, 15015,
1720  // C4[0], coeff of eps^0, polynomial in n of order 5
1721  100, 208, 572, 3432, -12012, 30030, 45045,
1722  // C4[1], coeff of eps^5, polynomial in n of order 0
1723  1, 9009,
1724  // C4[1], coeff of eps^4, polynomial in n of order 1
1725  -2944, 468, 135135,
1726  // C4[1], coeff of eps^3, polynomial in n of order 2
1727  5792, 1040, -1287, 135135,
1728  // C4[1], coeff of eps^2, polynomial in n of order 3
1729  5952, -11648, 9152, -2574, 135135,
1730  // C4[1], coeff of eps^1, polynomial in n of order 4
1731  -64, -624, 4576, -6864, 3003, 135135,
1732  // C4[2], coeff of eps^5, polynomial in n of order 0
1733  8, 10725,
1734  // C4[2], coeff of eps^4, polynomial in n of order 1
1735  1856, -936, 225225,
1736  // C4[2], coeff of eps^3, polynomial in n of order 2
1737  -8448, 4992, -1144, 225225,
1738  // C4[2], coeff of eps^2, polynomial in n of order 3
1739  -1440, 4160, -4576, 1716, 225225,
1740  // C4[3], coeff of eps^5, polynomial in n of order 0
1741  -136, 63063,
1742  // C4[3], coeff of eps^4, polynomial in n of order 1
1743  1024, -208, 105105,
1744  // C4[3], coeff of eps^3, polynomial in n of order 2
1745  3584, -3328, 1144, 315315,
1746  // C4[4], coeff of eps^5, polynomial in n of order 0
1747  -128, 135135,
1748  // C4[4], coeff of eps^4, polynomial in n of order 1
1749  -2560, 832, 405405,
1750  // C4[5], coeff of eps^5, polynomial in n of order 0
1751  128, 99099,
1752  };
1753 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 7
1754  static const real coeff[] = {
1755  // C4[0], coeff of eps^6, polynomial in n of order 0
1756  10, 9009,
1757  // C4[0], coeff of eps^5, polynomial in n of order 1
1758  -464, 291, 45045,
1759  // C4[0], coeff of eps^4, polynomial in n of order 2
1760  -4480, 1088, 156, 45045,
1761  // C4[0], coeff of eps^3, polynomial in n of order 3
1762  10736, -224, -4784, 1573, 45045,
1763  // C4[0], coeff of eps^2, polynomial in n of order 4
1764  1664, -10656, 14144, -4576, -858, 45045,
1765  // C4[0], coeff of eps^1, polynomial in n of order 5
1766  16, 64, 624, -4576, 6864, -3003, 15015,
1767  // C4[0], coeff of eps^0, polynomial in n of order 6
1768  56, 100, 208, 572, 3432, -12012, 30030, 45045,
1769  // C4[1], coeff of eps^6, polynomial in n of order 0
1770  10, 9009,
1771  // C4[1], coeff of eps^5, polynomial in n of order 1
1772  112, 15, 135135,
1773  // C4[1], coeff of eps^4, polynomial in n of order 2
1774  3840, -2944, 468, 135135,
1775  // C4[1], coeff of eps^3, polynomial in n of order 3
1776  -10704, 5792, 1040, -1287, 135135,
1777  // C4[1], coeff of eps^2, polynomial in n of order 4
1778  -768, 5952, -11648, 9152, -2574, 135135,
1779  // C4[1], coeff of eps^1, polynomial in n of order 5
1780  -16, -64, -624, 4576, -6864, 3003, 135135,
1781  // C4[2], coeff of eps^6, polynomial in n of order 0
1782  -4, 25025,
1783  // C4[2], coeff of eps^5, polynomial in n of order 1
1784  -1664, 168, 225225,
1785  // C4[2], coeff of eps^4, polynomial in n of order 2
1786  1664, 1856, -936, 225225,
1787  // C4[2], coeff of eps^3, polynomial in n of order 3
1788  6784, -8448, 4992, -1144, 225225,
1789  // C4[2], coeff of eps^2, polynomial in n of order 4
1790  128, -1440, 4160, -4576, 1716, 225225,
1791  // C4[3], coeff of eps^6, polynomial in n of order 0
1792  64, 315315,
1793  // C4[3], coeff of eps^5, polynomial in n of order 1
1794  1792, -680, 315315,
1795  // C4[3], coeff of eps^4, polynomial in n of order 2
1796  -2048, 1024, -208, 105105,
1797  // C4[3], coeff of eps^3, polynomial in n of order 3
1798  -1792, 3584, -3328, 1144, 315315,
1799  // C4[4], coeff of eps^6, polynomial in n of order 0
1800  -512, 405405,
1801  // C4[4], coeff of eps^5, polynomial in n of order 1
1802  2048, -384, 405405,
1803  // C4[4], coeff of eps^4, polynomial in n of order 2
1804  3072, -2560, 832, 405405,
1805  // C4[5], coeff of eps^6, polynomial in n of order 0
1806  -256, 495495,
1807  // C4[5], coeff of eps^5, polynomial in n of order 1
1808  -2048, 640, 495495,
1809  // C4[6], coeff of eps^6, polynomial in n of order 0
1810  512, 585585,
1811  };
1812 #elif GEOGRAPHICLIB_GEODESIC_ORDER == 8
1813  static const real coeff[] = {
1814  // C4[0], coeff of eps^7, polynomial in n of order 0
1815  193, 85085,
1816  // C4[0], coeff of eps^6, polynomial in n of order 1
1817  4192, 850, 765765,
1818  // C4[0], coeff of eps^5, polynomial in n of order 2
1819  20960, -7888, 4947, 765765,
1820  // C4[0], coeff of eps^4, polynomial in n of order 3
1821  12480, -76160, 18496, 2652, 765765,
1822  // C4[0], coeff of eps^3, polynomial in n of order 4
1823  -154048, 182512, -3808, -81328, 26741, 765765,
1824  // C4[0], coeff of eps^2, polynomial in n of order 5
1825  3232, 28288, -181152, 240448, -77792, -14586, 765765,
1826  // C4[0], coeff of eps^1, polynomial in n of order 6
1827  96, 272, 1088, 10608, -77792, 116688, -51051, 255255,
1828  // C4[0], coeff of eps^0, polynomial in n of order 7
1829  588, 952, 1700, 3536, 9724, 58344, -204204, 510510, 765765,
1830  // C4[1], coeff of eps^7, polynomial in n of order 0
1831  349, 2297295,
1832  // C4[1], coeff of eps^6, polynomial in n of order 1
1833  -1472, 510, 459459,
1834  // C4[1], coeff of eps^5, polynomial in n of order 2
1835  -39840, 1904, 255, 2297295,
1836  // C4[1], coeff of eps^4, polynomial in n of order 3
1837  52608, 65280, -50048, 7956, 2297295,
1838  // C4[1], coeff of eps^3, polynomial in n of order 4
1839  103744, -181968, 98464, 17680, -21879, 2297295,
1840  // C4[1], coeff of eps^2, polynomial in n of order 5
1841  -1344, -13056, 101184, -198016, 155584, -43758, 2297295,
1842  // C4[1], coeff of eps^1, polynomial in n of order 6
1843  -96, -272, -1088, -10608, 77792, -116688, 51051, 2297295,
1844  // C4[2], coeff of eps^7, polynomial in n of order 0
1845  464, 1276275,
1846  // C4[2], coeff of eps^6, polynomial in n of order 1
1847  -928, -612, 3828825,
1848  // C4[2], coeff of eps^5, polynomial in n of order 2
1849  64256, -28288, 2856, 3828825,
1850  // C4[2], coeff of eps^4, polynomial in n of order 3
1851  -126528, 28288, 31552, -15912, 3828825,
1852  // C4[2], coeff of eps^3, polynomial in n of order 4
1853  -41472, 115328, -143616, 84864, -19448, 3828825,
1854  // C4[2], coeff of eps^2, polynomial in n of order 5
1855  160, 2176, -24480, 70720, -77792, 29172, 3828825,
1856  // C4[3], coeff of eps^7, polynomial in n of order 0
1857  -16, 97461,
1858  // C4[3], coeff of eps^6, polynomial in n of order 1
1859  -16384, 1088, 5360355,
1860  // C4[3], coeff of eps^5, polynomial in n of order 2
1861  -2560, 30464, -11560, 5360355,
1862  // C4[3], coeff of eps^4, polynomial in n of order 3
1863  35840, -34816, 17408, -3536, 1786785,
1864  // C4[3], coeff of eps^3, polynomial in n of order 4
1865  7168, -30464, 60928, -56576, 19448, 5360355,
1866  // C4[4], coeff of eps^7, polynomial in n of order 0
1867  128, 2297295,
1868  // C4[4], coeff of eps^6, polynomial in n of order 1
1869  26624, -8704, 6891885,
1870  // C4[4], coeff of eps^5, polynomial in n of order 2
1871  -77824, 34816, -6528, 6891885,
1872  // C4[4], coeff of eps^4, polynomial in n of order 3
1873  -32256, 52224, -43520, 14144, 6891885,
1874  // C4[5], coeff of eps^7, polynomial in n of order 0
1875  -6784, 8423415,
1876  // C4[5], coeff of eps^6, polynomial in n of order 1
1877  24576, -4352, 8423415,
1878  // C4[5], coeff of eps^5, polynomial in n of order 2
1879  45056, -34816, 10880, 8423415,
1880  // C4[6], coeff of eps^7, polynomial in n of order 0
1881  -1024, 3318315,
1882  // C4[6], coeff of eps^6, polynomial in n of order 1
1883  -28672, 8704, 9954945,
1884  // C4[7], coeff of eps^7, polynomial in n of order 0
1885  1024, 1640925,
1886  };
1887 #else
1888 #error "Bad value for GEOGRAPHICLIB_GEODESIC_ORDER"
1889 #endif
1890  static_assert(sizeof(coeff) / sizeof(real) ==
1891  (nC4_ * (nC4_ + 1) * (nC4_ + 5)) / 6,
1892  "Coefficient array size mismatch in C4coeff");
1893  int o = 0, k = 0;
1894  for (int l = 0; l < nC4_; ++l) { // l is index of C4[l]
1895  for (int j = nC4_ - 1; j >= l; --j) { // coeff of eps^j
1896  int m = nC4_ - j - 1; // order of polynomial in n
1897  _cC4x[k++] = Math::polyval(m, coeff + o, _n) / coeff[o + m + 1];
1898  o += m + 2;
1899  }
1900  }
1901  // Post condition: o == sizeof(coeff) / sizeof(real) && k == nC4x_
1902  }
1903 
1904 } // namespace GeographicLib
GeographicLib::Math::real real
Definition: GeodSolve.cpp:31
Header for GeographicLib::GeodesicLine class.
Header for GeographicLib::Geodesic class.
Geodesic calculations
Definition: Geodesic.hpp:172
GeodesicLine InverseLine(real lat1, real lon1, real lat2, real lon2, unsigned caps=ALL) const
Definition: Geodesic.cpp:518
Geodesic(real a, real f)
Definition: Geodesic.cpp:42
static const Geodesic & WGS84()
Definition: Geodesic.cpp:89
GeodesicLine ArcDirectLine(real lat1, real lon1, real azi1, real a12, unsigned caps=ALL) const
Definition: Geodesic.cpp:154
GeodesicLine Line(real lat1, real lon1, real azi1, unsigned caps=ALL) const
Definition: Geodesic.cpp:118
GeodesicLine GenDirectLine(real lat1, real lon1, real azi1, bool arcmode, real s12_a12, unsigned caps=ALL) const
Definition: Geodesic.cpp:136
friend class GeodesicLine
Definition: Geodesic.hpp:175
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
GeodesicLine DirectLine(real lat1, real lon1, real azi1, real s12, unsigned caps=ALL) const
Definition: Geodesic.cpp:149
Exception handling for GeographicLib.
Definition: Constants.hpp:316
Mathematical functions needed by GeographicLib.
Definition: Math.hpp:76
static T degree()
Definition: Math.hpp:200
static T LatFix(T x)
Definition: Math.hpp:300
static void sincosd(T x, T &sinx, T &cosx)
Definition: Math.cpp:106
static T atan2d(T y, T x)
Definition: Math.cpp:183
static void norm(T &x, T &y)
Definition: Math.hpp:222
static T AngRound(T x)
Definition: Math.cpp:97
static T sq(T x)
Definition: Math.hpp:212
static T AngNormalize(T x)
Definition: Math.cpp:71
static void sincosde(T x, T t, T &sinx, T &cosx)
Definition: Math.cpp:126
static T pi()
Definition: Math.hpp:190
static T polyval(int N, const T p[], T x)
Definition: Math.hpp:271
static T AngDiff(T x, T y, T &e)
Definition: Math.cpp:82
@ hd
degrees per half turn
Definition: Math.hpp:144
@ qd
degrees per quarter turn
Definition: Math.hpp:141
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
void swap(GeographicLib::NearestNeighbor< dist_t, pos_t, distfun_t > &a, GeographicLib::NearestNeighbor< dist_t, pos_t, distfun_t > &b)