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