GeographicLib: Geodesic.cpp Source File

GeographicLib 2.5.2

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