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