GeographicLib  2.1
GeodesicExact.cpp
Go to the documentation of this file.
1 /**
2  * \file GeodesicExact.cpp
3  * \brief Implementation for GeographicLib::GeodesicExact class
4  *
5  * Copyright (c) Charles Karney (2012-2022) <charles@karney.com> and licensed
6  * under the MIT/X11 License. For more information, see
7  * https://geographiclib.sourceforge.io/
8  *
9  * This is a reformulation of the geodesic problem. The notation is as
10  * follows:
11  * - at a general point (no suffix or 1 or 2 as suffix)
12  * - phi = latitude
13  * - beta = latitude on auxiliary sphere
14  * - omega = longitude on auxiliary sphere
15  * - lambda = longitude
16  * - alpha = azimuth of great circle
17  * - sigma = arc length along great circle
18  * - s = distance
19  * - tau = scaled distance (= sigma at multiples of pi/2)
20  * - at northwards equator crossing
21  * - beta = phi = 0
22  * - omega = lambda = 0
23  * - alpha = alpha0
24  * - sigma = s = 0
25  * - a 12 suffix means a difference, e.g., s12 = s2 - s1.
26  * - s and c prefixes mean sin and cos
27  **********************************************************************/
28 
31 
32 #if defined(_MSC_VER)
33 // Squelch warnings about potentially uninitialized local variables,
34 // constant conditional and enum-float expressions and mixing enums
35 # pragma warning (disable: 4701 4127 5055 5054)
36 #endif
37 
38 namespace GeographicLib {
39 
40  using namespace std;
41 
43  : maxit2_(maxit1_ + Math::digits() + 10)
44  // Underflow guard. We require
45  // tiny_ * epsilon() > 0
46  // tiny_ + epsilon() == epsilon()
47  , tiny_(sqrt(numeric_limits<real>::min()))
48  , tol0_(numeric_limits<real>::epsilon())
49  // Increase multiplier in defn of tol1_ from 100 to 200 to fix inverse
50  // case 52.784459512564 0 -52.784459512563990912 179.634407464943777557
51  // which otherwise failed for Visual Studio 10 (Release and Debug)
52  , tol1_(200 * tol0_)
53  , tol2_(sqrt(tol0_))
54  , tolb_(tol0_ * tol2_) // Check on bisection interval
55  , xthresh_(1000 * tol2_)
56  , _a(a)
57  , _f(f)
58  , _f1(1 - _f)
59  , _e2(_f * (2 - _f))
60  , _ep2(_e2 / Math::sq(_f1)) // e2 / (1 - e2)
61  , _n(_f / ( 2 - _f))
62  , _b(_a * _f1)
63  // The Geodesic class substitutes atanh(sqrt(e2)) for asinh(sqrt(ep2)) in
64  // the definition of _c2. The latter is more accurate for very oblate
65  // ellipsoids (which the Geodesic class does not attempt to handle). Of
66  // course, the area calculation in GeodesicExact is still based on a
67  // series and so only holds for moderately oblate (or prolate)
68  // ellipsoids.
69  , _c2((Math::sq(_a) + Math::sq(_b) *
70  (_f == 0 ? 1 :
71  (_f > 0 ? asinh(sqrt(_ep2)) : atan(sqrt(-_e2))) /
72  sqrt(fabs(_e2))))/2) // authalic radius squared
73  // The sig12 threshold for "really short". Using the auxiliary sphere
74  // solution with dnm computed at (bet1 + bet2) / 2, the relative error in
75  // the azimuth consistency check is sig12^2 * abs(f) * min(1, 1-f/2) / 2.
76  // (Error measured for 1/100 < b/a < 100 and abs(f) >= 1/1000. For a
77  // given f and sig12, the max error occurs for lines near the pole. If
78  // the old rule for computing dnm = (dn1 + dn2)/2 is used, then the error
79  // increases by a factor of 2.) Setting this equal to epsilon gives
80  // sig12 = etol2. Here 0.1 is a safety factor (error decreased by 100)
81  // and max(0.001, abs(f)) stops etol2 getting too large in the nearly
82  // spherical case.
83  , _etol2(real(0.1) * tol2_ /
84  sqrt( fmax(real(0.001), fabs(_f)) * fmin(real(1), 1 - _f/2) / 2 ))
85  {
86  if (!(isfinite(_a) && _a > 0))
87  throw GeographicErr("Equatorial radius is not positive");
88  if (!(isfinite(_b) && _b > 0))
89  throw GeographicErr("Polar semi-axis is not positive");
90 
91  // Required number of terms in DST for full accuracy for all precisions as
92  // a function of n in [-0.99, 0.99]. For each precision the P- and P+
93  // columns list the values for negative and positive n. Values determined
94  // by running develop/AreaEst compiled with GEOGRAPHICLIB_PRECISION = 5.
95  // For precision 4 and 5, GEOGRAPHICLIB_DIGITS was set to, resp., 384 and
96  // 768. The error criterion is relative error less than or equal to
97  // epsilon/2 = 0.5^digits, with digits = 24, 53, 64, 113, 256. The first 4
98  // are the the "standard" values for float, double, long double, and
99  // float128; the last is the default for GeographicLib + mpfr.
100  //
101  // float double long d quad mpfr
102  // n f- f+ d- d+ l- l+ q- q+ m- m+
103  // 0.01 4 4 6 6 8 8 16 16 48 48
104  // 0.02 4 4 8 8 12 12 24 24 48 48
105  // 0.03 4 4 8 8 12 12 24 24 48 48
106  // 0.04 4 4 12 12 12 12 24 24 64 64
107  // 0.05 4 4 12 12 12 12 24 24 64 64
108  // 0.06 4 4 12 12 12 12 24 24 64 64
109  // 0.07 4 4 12 12 16 16 32 32 64 64
110  // 0.08 4 4 12 12 16 16 32 32 96 64
111  // 0.09 4 4 12 12 16 16 32 32 96 96
112  // 0.1 4 4 12 12 16 16 32 32 96 96
113  // 0.11 6 4 12 12 16 16 32 32 96 96
114  // 0.12 6 6 16 16 16 16 32 32 96 96
115  // 0.13 6 6 16 16 24 16 48 32 96 96
116  // 0.14 6 6 16 16 24 24 48 48 96 96
117  // 0.15 6 6 16 16 24 24 48 48 96 96
118  // 0.16 6 6 16 16 24 24 48 48 96 96
119  // 0.17 6 6 16 16 24 24 48 48 96 96
120  // 0.18 6 6 16 16 24 24 48 48 96 96
121  // 0.19 6 6 16 16 24 24 48 48 128 128
122  // 0.2 6 6 24 16 24 24 48 48 128 128
123  // 0.21 6 6 24 24 24 24 48 48 128 128
124  // 0.22 8 6 24 24 24 24 48 48 128 128
125  // 0.23 8 6 24 24 24 24 48 48 128 128
126  // 0.24 8 6 24 24 24 24 48 48 128 128
127  // 0.25 8 6 24 24 32 24 48 48 128 128
128  // 0.26 8 6 24 24 32 24 64 48 128 128
129  // 0.27 8 8 24 24 32 32 64 64 128 128
130  // 0.28 8 8 24 24 32 32 64 64 128 128
131  // 0.29 8 8 24 24 32 32 64 64 192 192
132  // 0.3 8 8 24 24 32 32 64 64 192 192
133  // 0.31 12 8 24 24 32 32 64 64 192 192
134  // 0.32 12 8 24 24 32 32 64 64 192 192
135  // 0.33 12 8 24 24 32 32 64 64 192 192
136  // 0.34 12 8 32 24 32 32 64 64 192 192
137  // 0.35 12 8 32 24 32 32 64 64 192 192
138  // 0.36 12 8 32 24 48 32 96 64 192 192
139  // 0.37 12 8 32 32 48 32 96 64 192 192
140  // 0.38 12 8 32 32 48 32 96 96 192 192
141  // 0.39 12 8 32 32 48 48 96 96 192 192
142  // 0.4 12 8 32 32 48 48 96 96 192 192
143  // 0.41 12 8 32 32 48 48 96 96 192 192
144  // 0.42 12 12 32 32 48 48 96 96 192 192
145  // 0.43 12 12 32 32 48 48 96 96 256 192
146  // 0.44 12 12 48 32 48 48 96 96 256 256
147  // 0.45 12 12 48 32 48 48 96 96 256 256
148  // 0.46 16 12 48 32 48 48 96 96 256 256
149  // 0.47 16 12 48 32 48 48 96 96 256 256
150  // 0.48 16 12 48 32 48 48 96 96 256 256
151  // 0.49 16 12 48 48 48 48 96 96 256 256
152  // 0.5 16 12 48 48 64 48 96 96 256 256
153  // 0.51 16 12 48 48 64 48 128 96 256 256
154  // 0.52 16 12 48 48 64 48 128 96 256 256
155  // 0.53 16 12 48 48 64 48 128 128 256 256
156  // 0.54 16 12 48 48 64 48 128 128 384 256
157  // 0.55 16 12 48 48 64 64 128 128 384 384
158  // 0.56 24 12 48 48 64 64 128 128 384 384
159  // 0.57 24 12 48 48 64 64 128 128 384 384
160  // 0.58 24 12 64 48 64 64 128 128 384 384
161  // 0.59 24 12 64 48 64 64 128 128 384 384
162  // 0.6 24 12 64 48 96 64 192 128 384 384
163  // 0.61 24 12 64 48 96 64 192 128 384 384
164  // 0.62 24 12 64 48 96 64 192 128 384 384
165  // 0.63 24 16 64 48 96 64 192 192 384 384
166  // 0.64 24 16 64 64 96 64 192 192 384 384
167  // 0.65 24 16 64 64 96 96 192 192 384 384
168  // 0.66 24 16 96 64 96 96 192 192 512 384
169  // 0.67 24 16 96 64 96 96 192 192 512 512
170  // 0.68 32 16 96 64 96 96 192 192 512 512
171  // 0.69 32 16 96 64 96 96 192 192 512 512
172  // 0.7 32 16 96 64 96 96 192 192 512 512
173  // 0.71 32 16 96 64 128 96 192 192 512 512
174  // 0.72 32 16 96 64 128 96 256 192 512 512
175  // 0.73 32 16 96 96 128 96 256 192 768 512
176  // 0.74 32 16 96 96 128 96 256 256 768 768
177  // 0.75 48 16 96 96 128 96 256 256 768 768
178  // 0.76 48 16 128 96 128 128 256 256 768 768
179  // 0.77 48 24 128 96 192 128 256 256 768 768
180  // 0.78 48 24 128 96 192 128 384 256 768 768
181  // 0.79 48 24 128 96 192 128 384 256 768 768
182  // 0.8 48 24 128 96 192 128 384 384 768 768
183  // 0.81 48 24 128 96 192 128 384 384 1024 768
184  // 0.82 48 24 192 96 192 192 384 384 1024 1024
185  // 0.83 64 24 192 128 192 192 384 384 1024 1024
186  // 0.84 64 24 192 128 256 192 384 384 1024 1024
187  // 0.85 64 24 192 128 256 192 512 384 1024 1024
188  // 0.86 64 24 192 128 256 192 512 384 1536 1024
189  // 0.87 96 24 192 128 256 192 512 512 1536 1536
190  // 0.88 96 24 256 192 384 192 768 512 1536 1536
191  // 0.89 96 24 256 192 384 256 768 512 1536 1536
192  // 0.9 96 24 256 192 384 256 768 768 2048 1536
193  // 0.91 128 24 384 192 384 256 768 768 2048 2048
194  // 0.92 128 24 384 192 512 384 1024 768 2048 2048
195  // 0.93 192 24 384 256 512 384 1024 768 3072 3072
196  // 0.94 192 24 512 256 768 384 1536 1024 3072 3072
197  // 0.95 192 24 768 384 768 512 1536 1024 4096 3072
198  // 0.96 256 24 768 384 1024 512 2048 1536 4096 4096
199  // 0.97 384 24 1024 512 1536 768 3072 2048 6144 6144
200  // 0.98 512 16 1536 768 2048 1024 4096 3072 8192 8192
201  // 0.99 1024 8 3072 1024 4096 1536 8192 6144 16384 16384
202  real n = 100 * _n;
203  int N;
204 #if GEOGRAPHICLIB_PRECISION == 1
205  if (n >= -10 && n <= 11) N = 4;
206  else if (n >= -21 && n <= 26) N = 6;
207  else if (n >= -30 && n <= 41) N = 8;
208  else if (n >= -45 && n <= 62) N = 12;
209  else if (n >= -55 && n <= 76) N = 16;
210  // ignore the *reduction* in N for n > 97
211  else if (n >= -67 && n <= 99) N = 24;
212  else if (n >= -74 && n <= 99) N = 32;
213  else if (n >= -82 && n <= 99) N = 48;
214  else if (n >= -86 && n <= 99) N = 64;
215  else if (n >= -90 && n <= 99) N = 96;
216  else if (n >= -92 && n <= 99) N = 128;
217  else if (n >= -95 && n <= 99) N = 192;
218  else if (n >= -96 && n <= 99) N = 256;
219  else if (n >= -97 && n <= 99) N = 384;
220  else if (n >= -98 && n <= 99) N = 512;
221  else N = 1024;
222 #elif GEOGRAPHICLIB_PRECISION == 2
223  if (n >= - 1 && n <= 1) N = 6;
224  else if (n >= - 3 && n <= 3) N = 8;
225  else if (n >= -11 && n <= 11) N = 12;
226  else if (n >= -19 && n <= 20) N = 16;
227  else if (n >= -33 && n <= 36) N = 24;
228  else if (n >= -43 && n <= 48) N = 32;
229  else if (n >= -57 && n <= 63) N = 48;
230  else if (n >= -65 && n <= 72) N = 64;
231  else if (n >= -75 && n <= 82) N = 96;
232  else if (n >= -81 && n <= 87) N = 128;
233  else if (n >= -87 && n <= 92) N = 192;
234  else if (n >= -90 && n <= 94) N = 256;
235  else if (n >= -93 && n <= 96) N = 384;
236  else if (n >= -94 && n <= 97) N = 512;
237  else if (n >= -96 && n <= 98) N = 768;
238  else if (n >= -97 && n <= 99) N = 1024;
239  else if (n >= -98 && n <= 99) N = 1536;
240  else N = 3072;
241 #elif GEOGRAPHICLIB_PRECISION == 3
242  if (n >= - 1 && n <= 1) N = 8;
243  else if (n >= - 6 && n <= 6) N = 12;
244  else if (n >= -12 && n <= 13) N = 16;
245  else if (n >= -23 && n <= 26) N = 24;
246  else if (n >= -35 && n <= 38) N = 32;
247  else if (n >= -49 && n <= 54) N = 48;
248  else if (n >= -59 && n <= 64) N = 64;
249  else if (n >= -70 && n <= 75) N = 96;
250  else if (n >= -76 && n <= 81) N = 128;
251  else if (n >= -83 && n <= 88) N = 192;
252  else if (n >= -87 && n <= 91) N = 256;
253  else if (n >= -91 && n <= 94) N = 384;
254  else if (n >= -93 && n <= 96) N = 512;
255  else if (n >= -95 && n <= 97) N = 768;
256  else if (n >= -96 && n <= 98) N = 1024;
257  else if (n >= -97 && n <= 99) N = 1536;
258  else if (n >= -98 && n <= 99) N = 2048;
259  else N = 4096;
260 #elif GEOGRAPHICLIB_PRECISION == 4
261  if (n >= - 1 && n <= 1) N = 16;
262  else if (n >= - 6 && n <= 6) N = 24;
263  else if (n >= -12 && n <= 13) N = 32;
264  else if (n >= -25 && n <= 26) N = 48;
265  else if (n >= -35 && n <= 37) N = 64;
266  else if (n >= -50 && n <= 52) N = 96;
267  else if (n >= -59 && n <= 62) N = 128;
268  else if (n >= -71 && n <= 73) N = 192;
269  else if (n >= -77 && n <= 79) N = 256;
270  else if (n >= -84 && n <= 86) N = 384;
271  else if (n >= -87 && n <= 89) N = 512;
272  else if (n >= -91 && n <= 93) N = 768;
273  else if (n >= -93 && n <= 95) N = 1024;
274  else if (n >= -95 && n <= 96) N = 1536;
275  else if (n >= -96 && n <= 97) N = 2048;
276  else if (n >= -97 && n <= 98) N = 3072;
277  else if (n >= -98 && n <= 98) N = 4096;
278  else if (n >= -98 && n <= 99) N = 6144;
279  else N = 8192;
280 #elif GEOGRAPHICLIB_PRECISION == 5
281  if (n >= - 3 && n <= 3) N = 48;
282  else if (n >= - 7 && n <= 8) N = 64;
283  else if (n >= -18 && n <= 18) N = 96;
284  else if (n >= -28 && n <= 28) N = 128;
285  else if (n >= -42 && n <= 43) N = 192;
286  else if (n >= -53 && n <= 54) N = 256;
287  else if (n >= -65 && n <= 66) N = 384;
288  else if (n >= -72 && n <= 73) N = 512;
289  else if (n >= -80 && n <= 81) N = 768;
290  else if (n >= -85 && n <= 86) N = 1024;
291  else if (n >= -89 && n <= 90) N = 1536;
292  else if (n >= -92 && n <= 92) N = 2048;
293  else if (n >= -94 && n <= 95) N = 3072;
294  else if (n >= -96 && n <= 96) N = 4096;
295  else if (n >= -97 && n <= 97) N = 6144;
296  else if (n >= -98 && n <= 98) N = 8192;
297  else N = 16384;
298  if (Math::digits() > 256) {
299  // Scale up N by the number of digits in the precision relative to
300  // the number used for the test = 256.
301  int M = (Math::digits() * N) / 256;
302  while (N < M) N = N % 3 == 0 ? 4*N/3 : 3*N/2;
303  }
304 #else
305 #error "Bad value for GEOGRAPHICLIB_PRECISION"
306 #endif
307  _fft.reset(N);
308  _nC4 = N;
309  }
310 
312  static const GeodesicExact wgs84(Constants::WGS84_a(),
314  return wgs84;
315  }
316 
317  GeodesicLineExact GeodesicExact::Line(real lat1, real lon1, real azi1,
318  unsigned caps) const {
319  return GeodesicLineExact(*this, lat1, lon1, azi1, caps);
320  }
321 
322  Math::real GeodesicExact::GenDirect(real lat1, real lon1, real azi1,
323  bool arcmode, real s12_a12,
324  unsigned outmask,
325  real& lat2, real& lon2, real& azi2,
326  real& s12, real& m12,
327  real& M12, real& M21,
328  real& S12) const {
329  // Automatically supply DISTANCE_IN if necessary
330  if (!arcmode) outmask |= DISTANCE_IN;
331  return GeodesicLineExact(*this, lat1, lon1, azi1, outmask)
332  . // Note the dot!
333  GenPosition(arcmode, s12_a12, outmask,
334  lat2, lon2, azi2, s12, m12, M12, M21, S12);
335  }
336 
338  real azi1,
339  bool arcmode, real s12_a12,
340  unsigned caps) const {
341  azi1 = Math::AngNormalize(azi1);
342  real salp1, calp1;
343  // Guard against underflow in salp0. Also -0 is converted to +0.
344  Math::sincosd(Math::AngRound(azi1), salp1, calp1);
345  // Automatically supply DISTANCE_IN if necessary
346  if (!arcmode) caps |= DISTANCE_IN;
347  return GeodesicLineExact(*this, lat1, lon1, azi1, salp1, calp1,
348  caps, arcmode, s12_a12);
349  }
350 
352  real azi1, real s12,
353  unsigned caps) const {
354  return GenDirectLine(lat1, lon1, azi1, false, s12, caps);
355  }
356 
358  real azi1, real a12,
359  unsigned caps) const {
360  return GenDirectLine(lat1, lon1, azi1, true, a12, caps);
361  }
362 
363  Math::real GeodesicExact::GenInverse(real lat1, real lon1,
364  real lat2, real lon2,
365  unsigned outmask, real& s12,
366  real& salp1, real& calp1,
367  real& salp2, real& calp2,
368  real& m12, real& M12, real& M21,
369  real& S12) const {
370  // Compute longitude difference (AngDiff does this carefully). Result is
371  // in [-180, 180] but -180 is only for west-going geodesics. 180 is for
372  // east-going and meridional geodesics.
373  using std::isnan; // Needed for Centos 7, ubuntu 14
374  real lon12s, lon12 = Math::AngDiff(lon1, lon2, lon12s);
375  // Make longitude difference positive.
376  int lonsign = signbit(lon12) ? -1 : 1;
377  lon12 *= lonsign; lon12s *= lonsign;
378  real
379  lam12 = lon12 * Math::degree(),
380  slam12, clam12;
381  // Calculate sincos of lon12 + error (this applies AngRound internally).
382  Math::sincosde(lon12, lon12s, slam12, clam12);
383  // the supplementary longitude difference
384  lon12s = (Math::hd - lon12) - lon12s;
385 
386  // If really close to the equator, treat as on equator.
387  lat1 = Math::AngRound(Math::LatFix(lat1));
388  lat2 = Math::AngRound(Math::LatFix(lat2));
389  // Swap points so that point with higher (abs) latitude is point 1
390  // If one latitude is a nan, then it becomes lat1.
391  int swapp = fabs(lat1) < fabs(lat2) || isnan(lat2) ? -1 : 1;
392  if (swapp < 0) {
393  lonsign *= -1;
394  swap(lat1, lat2);
395  }
396  // Make lat1 <= -0
397  int latsign = signbit(lat1) ? 1 : -1;
398  lat1 *= latsign;
399  lat2 *= latsign;
400  // Now we have
401  //
402  // 0 <= lon12 <= 180
403  // -90 <= lat1 <= -0
404  // lat1 <= lat2 <= -lat1
405  //
406  // longsign, swapp, latsign register the transformation to bring the
407  // coordinates to this canonical form. In all cases, 1 means no change was
408  // made. We make these transformations so that there are few cases to
409  // check, e.g., on verifying quadrants in atan2. In addition, this
410  // enforces some symmetries in the results returned.
411 
412  real sbet1, cbet1, sbet2, cbet2, s12x, m12x;
413  // Initialize for the meridian. No longitude calculation is done in this
414  // case to let the parameter default to 0.
415  EllipticFunction E(-_ep2);
416 
417  Math::sincosd(lat1, sbet1, cbet1); sbet1 *= _f1;
418  // Ensure cbet1 = +epsilon at poles; doing the fix on beta means that sig12
419  // will be <= 2*tiny for two points at the same pole.
420  Math::norm(sbet1, cbet1); cbet1 = fmax(tiny_, cbet1);
421 
422  Math::sincosd(lat2, sbet2, cbet2); sbet2 *= _f1;
423  // Ensure cbet2 = +epsilon at poles
424  Math::norm(sbet2, cbet2); cbet2 = fmax(tiny_, cbet2);
425 
426  // If cbet1 < -sbet1, then cbet2 - cbet1 is a sensitive measure of the
427  // |bet1| - |bet2|. Alternatively (cbet1 >= -sbet1), abs(sbet2) + sbet1 is
428  // a better measure. This logic is used in assigning calp2 in Lambda12.
429  // Sometimes these quantities vanish and in that case we force bet2 = +/-
430  // bet1 exactly. An example where is is necessary is the inverse problem
431  // 48.522876735459 0 -48.52287673545898293 179.599720456223079643
432  // which failed with Visual Studio 10 (Release and Debug)
433 
434  if (cbet1 < -sbet1) {
435  if (cbet2 == cbet1)
436  sbet2 = copysign(sbet1, sbet2);
437  } else {
438  if (fabs(sbet2) == -sbet1)
439  cbet2 = cbet1;
440  }
441 
442  real
443  dn1 = (_f >= 0 ? sqrt(1 + _ep2 * Math::sq(sbet1)) :
444  sqrt(1 - _e2 * Math::sq(cbet1)) / _f1),
445  dn2 = (_f >= 0 ? sqrt(1 + _ep2 * Math::sq(sbet2)) :
446  sqrt(1 - _e2 * Math::sq(cbet2)) / _f1);
447 
448  real a12, sig12;
449 
450  bool meridian = lat1 == -Math::qd || slam12 == 0;
451 
452  if (meridian) {
453 
454  // Endpoints are on a single full meridian, so the geodesic might lie on
455  // a meridian.
456 
457  calp1 = clam12; salp1 = slam12; // Head to the target longitude
458  calp2 = 1; salp2 = 0; // At the target we're heading north
459 
460  real
461  // tan(bet) = tan(sig) * cos(alp)
462  ssig1 = sbet1, csig1 = calp1 * cbet1,
463  ssig2 = sbet2, csig2 = calp2 * cbet2;
464 
465  // sig12 = sig2 - sig1
466  sig12 = atan2(fmax(real(0), csig1 * ssig2 - ssig1 * csig2),
467  csig1 * csig2 + ssig1 * ssig2);
468  {
469  real dummy;
470  Lengths(E, sig12, ssig1, csig1, dn1, ssig2, csig2, dn2,
471  cbet1, cbet2, outmask | REDUCEDLENGTH,
472  s12x, m12x, dummy, M12, M21);
473  }
474  // Add the check for sig12 since zero length geodesics might yield m12 <
475  // 0. Test case was
476  //
477  // echo 20.001 0 20.001 0 | GeodSolve -i
478  //
479  // In fact, we will have sig12 > pi/2 for meridional geodesic which is
480  // not a shortest path.
481  if (sig12 < 1 || m12x >= 0) {
482  // Need at least 2, to handle 90 0 90 180
483  if (sig12 < 3 * tiny_ ||
484  // Prevent negative s12 or m12 for short lines
485  (sig12 < tol0_ && (s12x < 0 || m12x < 0)))
486  sig12 = m12x = s12x = 0;
487  m12x *= _b;
488  s12x *= _b;
489  a12 = sig12 / Math::degree();
490  } else
491  // m12 < 0, i.e., prolate and too close to anti-podal
492  meridian = false;
493  }
494 
495  // somg12 == 2 marks that it needs to be calculated
496  real omg12 = 0, somg12 = 2, comg12 = 0;
497  if (!meridian &&
498  sbet1 == 0 && // and sbet2 == 0
499  (_f <= 0 || lon12s >= _f * Math::hd)) {
500 
501  // Geodesic runs along equator
502  calp1 = calp2 = 0; salp1 = salp2 = 1;
503  s12x = _a * lam12;
504  sig12 = omg12 = lam12 / _f1;
505  m12x = _b * sin(sig12);
506  if (outmask & GEODESICSCALE)
507  M12 = M21 = cos(sig12);
508  a12 = lon12 / _f1;
509 
510  } else if (!meridian) {
511 
512  // Now point1 and point2 belong within a hemisphere bounded by a
513  // meridian and geodesic is neither meridional or equatorial.
514 
515  // Figure a starting point for Newton's method
516  real dnm;
517  sig12 = InverseStart(E, sbet1, cbet1, dn1, sbet2, cbet2, dn2,
518  lam12, slam12, clam12,
519  salp1, calp1, salp2, calp2, dnm);
520 
521  if (sig12 >= 0) {
522  // Short lines (InverseStart sets salp2, calp2, dnm)
523  s12x = sig12 * _b * dnm;
524  m12x = Math::sq(dnm) * _b * sin(sig12 / dnm);
525  if (outmask & GEODESICSCALE)
526  M12 = M21 = cos(sig12 / dnm);
527  a12 = sig12 / Math::degree();
528  omg12 = lam12 / (_f1 * dnm);
529  } else {
530 
531  // Newton's method. This is a straightforward solution of f(alp1) =
532  // lambda12(alp1) - lam12 = 0 with one wrinkle. f(alp) has exactly one
533  // root in the interval (0, pi) and its derivative is positive at the
534  // root. Thus f(alp) is positive for alp > alp1 and negative for alp <
535  // alp1. During the course of the iteration, a range (alp1a, alp1b) is
536  // maintained which brackets the root and with each evaluation of
537  // f(alp) the range is shrunk, if possible. Newton's method is
538  // restarted whenever the derivative of f is negative (because the new
539  // value of alp1 is then further from the solution) or if the new
540  // estimate of alp1 lies outside (0,pi); in this case, the new starting
541  // guess is taken to be (alp1a + alp1b) / 2.
542  //
543  // initial values to suppress warnings (if loop is executed 0 times)
544  real ssig1 = 0, csig1 = 0, ssig2 = 0, csig2 = 0, domg12 = 0;
545  unsigned numit = 0;
546  // Bracketing range
547  real salp1a = tiny_, calp1a = 1, salp1b = tiny_, calp1b = -1;
548  for (bool tripn = false, tripb = false;
549  numit < maxit2_ || GEOGRAPHICLIB_PANIC;
550  ++numit) {
551  // 1/4 meridian = 10e6 m and random input. max err is estimated max
552  // error in nm (checking solution of inverse problem by direct
553  // solution). iter is mean and sd of number of iterations
554  //
555  // max iter
556  // log2(b/a) err mean sd
557  // -7 387 5.33 3.68
558  // -6 345 5.19 3.43
559  // -5 269 5.00 3.05
560  // -4 210 4.76 2.44
561  // -3 115 4.55 1.87
562  // -2 69 4.35 1.38
563  // -1 36 4.05 1.03
564  // 0 15 0.01 0.13
565  // 1 25 5.10 1.53
566  // 2 96 5.61 2.09
567  // 3 318 6.02 2.74
568  // 4 985 6.24 3.22
569  // 5 2352 6.32 3.44
570  // 6 6008 6.30 3.45
571  // 7 19024 6.19 3.30
572  real dv;
573  real v = Lambda12(sbet1, cbet1, dn1, sbet2, cbet2, dn2, salp1, calp1,
574  slam12, clam12,
575  salp2, calp2, sig12, ssig1, csig1, ssig2, csig2,
576  E, domg12, numit < maxit1_, dv);
577  // Reversed test to allow escape with NaNs
578  if (tripb || !(fabs(v) >= (tripn ? 8 : 1) * tol0_)) break;
579  // Update bracketing values
580  if (v > 0 && (numit > maxit1_ || calp1/salp1 > calp1b/salp1b))
581  { salp1b = salp1; calp1b = calp1; }
582  else if (v < 0 && (numit > maxit1_ || calp1/salp1 < calp1a/salp1a))
583  { salp1a = salp1; calp1a = calp1; }
584  if (numit < maxit1_ && dv > 0) {
585  real
586  dalp1 = -v/dv;
587  // |dalp1| < pi test moved earlier because GEOGRAPHICLIB_PRECISION
588  // = 5 can result in dalp1 = 10^(10^8). Then sin(dalp1) takes ages
589  // (because of the need to do accurate range reduction).
590  if (fabs(dalp1) < Math::pi()) {
591  real
592  sdalp1 = sin(dalp1), cdalp1 = cos(dalp1),
593  nsalp1 = salp1 * cdalp1 + calp1 * sdalp1;
594  if (nsalp1 > 0) {
595  calp1 = calp1 * cdalp1 - salp1 * sdalp1;
596  salp1 = nsalp1;
597  Math::norm(salp1, calp1);
598  // In some regimes we don't get quadratic convergence because
599  // slope -> 0. So use convergence conditions based on epsilon
600  // instead of sqrt(epsilon).
601  tripn = fabs(v) <= 16 * tol0_;
602  continue;
603  }
604  }
605  }
606  // Either dv was not positive or updated value was outside legal
607  // range. Use the midpoint of the bracket as the next estimate.
608  // This mechanism is not needed for the WGS84 ellipsoid, but it does
609  // catch problems with more eccentric ellipsoids. Its efficacy is
610  // such for the WGS84 test set with the starting guess set to alp1 =
611  // 90deg:
612  // the WGS84 test set: mean = 5.21, sd = 3.93, max = 24
613  // WGS84 and random input: mean = 4.74, sd = 0.99
614  salp1 = (salp1a + salp1b)/2;
615  calp1 = (calp1a + calp1b)/2;
616  Math::norm(salp1, calp1);
617  tripn = false;
618  tripb = (fabs(salp1a - salp1) + (calp1a - calp1) < tolb_ ||
619  fabs(salp1 - salp1b) + (calp1 - calp1b) < tolb_);
620  }
621  {
622  real dummy;
623  Lengths(E, sig12, ssig1, csig1, dn1, ssig2, csig2, dn2,
624  cbet1, cbet2, outmask, s12x, m12x, dummy, M12, M21);
625  }
626  m12x *= _b;
627  s12x *= _b;
628  a12 = sig12 / Math::degree();
629  if (outmask & AREA) {
630  // omg12 = lam12 - domg12
631  real sdomg12 = sin(domg12), cdomg12 = cos(domg12);
632  somg12 = slam12 * cdomg12 - clam12 * sdomg12;
633  comg12 = clam12 * cdomg12 + slam12 * sdomg12;
634  }
635  }
636  }
637 
638  if (outmask & DISTANCE)
639  s12 = real(0) + s12x; // Convert -0 to 0
640 
641  if (outmask & REDUCEDLENGTH)
642  m12 = real(0) + m12x; // Convert -0 to 0
643 
644  if (outmask & AREA) {
645  real
646  // From Lambda12: sin(alp1) * cos(bet1) = sin(alp0)
647  salp0 = salp1 * cbet1,
648  calp0 = hypot(calp1, salp1 * sbet1); // calp0 > 0
649  real alp12,
650  // Multiplier = a^2 * e^2 * cos(alpha0) * sin(alpha0).
651  A4 = Math::sq(_a) * calp0 * salp0 * _e2;
652  if (A4 != 0) {
653  real
654  k2 = Math::sq(calp0) * _ep2,
655  // From Lambda12: tan(bet) = tan(sig) * cos(alp)
656  ssig1 = sbet1, csig1 = calp1 * cbet1,
657  ssig2 = sbet2, csig2 = calp2 * cbet2;
658  Math::norm(ssig1, csig1);
659  Math::norm(ssig2, csig2);
660  I4Integrand i4(_ep2, k2);
661  vector<real> C4a(_nC4);
662  _fft.transform(i4, C4a.data());
663  real
664  B41 = DST::integral(ssig1, csig1, C4a.data(), _nC4),
665  B42 = DST::integral(ssig2, csig2, C4a.data(), _nC4);
666  S12 = A4 * (B42 - B41);
667  } else
668  // Avoid problems with indeterminate sig1, sig2 on equator
669  S12 = 0;
670 
671  if (!meridian && somg12 == 2) {
672  somg12 = sin(omg12); comg12 = cos(omg12);
673  }
674 
675  if (!meridian &&
676  // omg12 < 3/4 * pi
677  comg12 > -real(0.7071) && // Long difference not too big
678  sbet2 - sbet1 < real(1.75)) { // Lat difference not too big
679  // Use tan(Gamma/2) = tan(omg12/2)
680  // * (tan(bet1/2)+tan(bet2/2))/(1+tan(bet1/2)*tan(bet2/2))
681  // with tan(x/2) = sin(x)/(1+cos(x))
682  real domg12 = 1 + comg12, dbet1 = 1 + cbet1, dbet2 = 1 + cbet2;
683  alp12 = 2 * atan2( somg12 * ( sbet1 * dbet2 + sbet2 * dbet1 ),
684  domg12 * ( sbet1 * sbet2 + dbet1 * dbet2 ) );
685  } else {
686  // alp12 = alp2 - alp1, used in atan2 so no need to normalize
687  real
688  salp12 = salp2 * calp1 - calp2 * salp1,
689  calp12 = calp2 * calp1 + salp2 * salp1;
690  // The right thing appears to happen if alp1 = +/-180 and alp2 = 0, viz
691  // salp12 = -0 and alp12 = -180. However this depends on the sign
692  // being attached to 0 correctly. The following ensures the correct
693  // behavior.
694  if (salp12 == 0 && calp12 < 0) {
695  salp12 = tiny_ * calp1;
696  calp12 = -1;
697  }
698  alp12 = atan2(salp12, calp12);
699  }
700  S12 += _c2 * alp12;
701  S12 *= swapp * lonsign * latsign;
702  // Convert -0 to 0
703  S12 += 0;
704  }
705 
706  // Convert calp, salp to azimuth accounting for lonsign, swapp, latsign.
707  if (swapp < 0) {
708  swap(salp1, salp2);
709  swap(calp1, calp2);
710  if (outmask & GEODESICSCALE)
711  swap(M12, M21);
712  }
713 
714  salp1 *= swapp * lonsign; calp1 *= swapp * latsign;
715  salp2 *= swapp * lonsign; calp2 *= swapp * latsign;
716 
717  // Returned value in [0, 180]
718  return a12;
719  }
720 
721  Math::real GeodesicExact::GenInverse(real lat1, real lon1,
722  real lat2, real lon2,
723  unsigned outmask,
724  real& s12, real& azi1, real& azi2,
725  real& m12, real& M12, real& M21,
726  real& S12) const {
727  outmask &= OUT_MASK;
728  real salp1, calp1, salp2, calp2,
729  a12 = GenInverse(lat1, lon1, lat2, lon2,
730  outmask, s12, salp1, calp1, salp2, calp2,
731  m12, M12, M21, S12);
732  if (outmask & AZIMUTH) {
733  azi1 = Math::atan2d(salp1, calp1);
734  azi2 = Math::atan2d(salp2, calp2);
735  }
736  return a12;
737  }
738 
740  real lat2, real lon2,
741  unsigned caps) const {
742  real t, salp1, calp1, salp2, calp2,
743  a12 = GenInverse(lat1, lon1, lat2, lon2,
744  // No need to specify AZIMUTH here
745  0u, t, salp1, calp1, salp2, calp2,
746  t, t, t, t),
747  azi1 = Math::atan2d(salp1, calp1);
748  // Ensure that a12 can be converted to a distance
749  if (caps & (OUT_MASK & DISTANCE_IN)) caps |= DISTANCE;
750  return GeodesicLineExact(*this, lat1, lon1, azi1, salp1, calp1, caps,
751  true, a12);
752  }
753 
754  void GeodesicExact::Lengths(const EllipticFunction& E,
755  real sig12,
756  real ssig1, real csig1, real dn1,
757  real ssig2, real csig2, real dn2,
758  real cbet1, real cbet2, unsigned outmask,
759  real& s12b, real& m12b, real& m0,
760  real& M12, real& M21) const {
761  // Return m12b = (reduced length)/_b; also calculate s12b = distance/_b,
762  // and m0 = coefficient of secular term in expression for reduced length.
763 
764  outmask &= OUT_ALL;
765  // outmask & DISTANCE: set s12b
766  // outmask & REDUCEDLENGTH: set m12b & m0
767  // outmask & GEODESICSCALE: set M12 & M21
768 
769  // It's OK to have repeated dummy arguments,
770  // e.g., s12b = m0 = M12 = M21 = dummy
771 
772  if (outmask & DISTANCE)
773  // Missing a factor of _b
774  s12b = E.E() / (Math::pi() / 2) *
775  (sig12 + (E.deltaE(ssig2, csig2, dn2) - E.deltaE(ssig1, csig1, dn1)));
776  if (outmask & (REDUCEDLENGTH | GEODESICSCALE)) {
777  real
778  m0x = - E.k2() * E.D() / (Math::pi() / 2),
779  J12 = m0x *
780  (sig12 + (E.deltaD(ssig2, csig2, dn2) - E.deltaD(ssig1, csig1, dn1)));
781  if (outmask & REDUCEDLENGTH) {
782  m0 = m0x;
783  // Missing a factor of _b. Add parens around (csig1 * ssig2) and
784  // (ssig1 * csig2) to ensure accurate cancellation in the case of
785  // coincident points.
786  m12b = dn2 * (csig1 * ssig2) - dn1 * (ssig1 * csig2) -
787  csig1 * csig2 * J12;
788  }
789  if (outmask & GEODESICSCALE) {
790  real csig12 = csig1 * csig2 + ssig1 * ssig2;
791  real t = _ep2 * (cbet1 - cbet2) * (cbet1 + cbet2) / (dn1 + dn2);
792  M12 = csig12 + (t * ssig2 - csig2 * J12) * ssig1 / dn1;
793  M21 = csig12 - (t * ssig1 - csig1 * J12) * ssig2 / dn2;
794  }
795  }
796  }
797 
798  Math::real GeodesicExact::Astroid(real x, real y) {
799  // Solve k^4+2*k^3-(x^2+y^2-1)*k^2-2*y^2*k-y^2 = 0 for positive root k.
800  // This solution is adapted from Geocentric::Reverse.
801  real k;
802  real
803  p = Math::sq(x),
804  q = Math::sq(y),
805  r = (p + q - 1) / 6;
806  if ( !(q == 0 && r <= 0) ) {
807  real
808  // Avoid possible division by zero when r = 0 by multiplying equations
809  // for s and t by r^3 and r, resp.
810  S = p * q / 4, // S = r^3 * s
811  r2 = Math::sq(r),
812  r3 = r * r2,
813  // The discriminant of the quadratic equation for T3. This is zero on
814  // the evolute curve p^(1/3)+q^(1/3) = 1
815  disc = S * (S + 2 * r3);
816  real u = r;
817  if (disc >= 0) {
818  real T3 = S + r3;
819  // Pick the sign on the sqrt to maximize abs(T3). This minimizes loss
820  // of precision due to cancellation. The result is unchanged because
821  // of the way the T is used in definition of u.
822  T3 += T3 < 0 ? -sqrt(disc) : sqrt(disc); // T3 = (r * t)^3
823  // N.B. cbrt always returns the real root. cbrt(-8) = -2.
824  real T = cbrt(T3); // T = r * t
825  // T can be zero; but then r2 / T -> 0.
826  u += T + (T != 0 ? r2 / T : 0);
827  } else {
828  // T is complex, but the way u is defined the result is real.
829  real ang = atan2(sqrt(-disc), -(S + r3));
830  // There are three possible cube roots. We choose the root which
831  // avoids cancellation. Note that disc < 0 implies that r < 0.
832  u += 2 * r * cos(ang / 3);
833  }
834  real
835  v = sqrt(Math::sq(u) + q), // guaranteed positive
836  // Avoid loss of accuracy when u < 0.
837  uv = u < 0 ? q / (v - u) : u + v, // u+v, guaranteed positive
838  w = (uv - q) / (2 * v); // positive?
839  // Rearrange expression for k to avoid loss of accuracy due to
840  // subtraction. Division by 0 not possible because uv > 0, w >= 0.
841  k = uv / (sqrt(uv + Math::sq(w)) + w); // guaranteed positive
842  } else { // q == 0 && r <= 0
843  // y = 0 with |x| <= 1. Handle this case directly.
844  // for y small, positive root is k = abs(y)/sqrt(1-x^2)
845  k = 0;
846  }
847  return k;
848  }
849 
850  Math::real GeodesicExact::InverseStart(EllipticFunction& E,
851  real sbet1, real cbet1, real dn1,
852  real sbet2, real cbet2, real dn2,
853  real lam12, real slam12, real clam12,
854  real& salp1, real& calp1,
855  // Only updated if return val >= 0
856  real& salp2, real& calp2,
857  // Only updated for short lines
858  real& dnm) const {
859  // Return a starting point for Newton's method in salp1 and calp1 (function
860  // value is -1). If Newton's method doesn't need to be used, return also
861  // salp2 and calp2 and function value is sig12.
862  real
863  sig12 = -1, // Return value
864  // bet12 = bet2 - bet1 in [0, pi); bet12a = bet2 + bet1 in (-pi, 0]
865  sbet12 = sbet2 * cbet1 - cbet2 * sbet1,
866  cbet12 = cbet2 * cbet1 + sbet2 * sbet1;
867  real sbet12a = sbet2 * cbet1 + cbet2 * sbet1;
868  bool shortline = cbet12 >= 0 && sbet12 < real(0.5) &&
869  cbet2 * lam12 < real(0.5);
870  real somg12, comg12;
871  if (shortline) {
872  real sbetm2 = Math::sq(sbet1 + sbet2);
873  // sin((bet1+bet2)/2)^2
874  // = (sbet1 + sbet2)^2 / ((sbet1 + sbet2)^2 + (cbet1 + cbet2)^2)
875  sbetm2 /= sbetm2 + Math::sq(cbet1 + cbet2);
876  dnm = sqrt(1 + _ep2 * sbetm2);
877  real omg12 = lam12 / (_f1 * dnm);
878  somg12 = sin(omg12); comg12 = cos(omg12);
879  } else {
880  somg12 = slam12; comg12 = clam12;
881  }
882 
883  salp1 = cbet2 * somg12;
884  calp1 = comg12 >= 0 ?
885  sbet12 + cbet2 * sbet1 * Math::sq(somg12) / (1 + comg12) :
886  sbet12a - cbet2 * sbet1 * Math::sq(somg12) / (1 - comg12);
887 
888  real
889  ssig12 = hypot(salp1, calp1),
890  csig12 = sbet1 * sbet2 + cbet1 * cbet2 * comg12;
891 
892  if (shortline && ssig12 < _etol2) {
893  // really short lines
894  salp2 = cbet1 * somg12;
895  calp2 = sbet12 - cbet1 * sbet2 *
896  (comg12 >= 0 ? Math::sq(somg12) / (1 + comg12) : 1 - comg12);
897  Math::norm(salp2, calp2);
898  // Set return value
899  sig12 = atan2(ssig12, csig12);
900  } else if (fabs(_n) > real(0.1) || // Skip astroid calc if too eccentric
901  csig12 >= 0 ||
902  ssig12 >= 6 * fabs(_n) * Math::pi() * Math::sq(cbet1)) {
903  // Nothing to do, zeroth order spherical approximation is OK
904  } else {
905  // Scale lam12 and bet2 to x, y coordinate system where antipodal point
906  // is at origin and singular point is at y = 0, x = -1.
907  real x, y, lamscale, betscale;
908  real lam12x = atan2(-slam12, -clam12); // lam12 - pi
909  if (_f >= 0) { // In fact f == 0 does not get here
910  // x = dlong, y = dlat
911  {
912  real k2 = Math::sq(sbet1) * _ep2;
913  E.Reset(-k2, -_ep2, 1 + k2, 1 + _ep2);
914  lamscale = _e2/_f1 * cbet1 * 2 * E.H();
915  }
916  betscale = lamscale * cbet1;
917 
918  x = lam12x / lamscale;
919  y = sbet12a / betscale;
920  } else { // _f < 0
921  // x = dlat, y = dlong
922  real
923  cbet12a = cbet2 * cbet1 - sbet2 * sbet1,
924  bet12a = atan2(sbet12a, cbet12a);
925  real m12b, m0, dummy;
926  // In the case of lon12 = 180, this repeats a calculation made in
927  // Inverse.
928  Lengths(E, Math::pi() + bet12a,
929  sbet1, -cbet1, dn1, sbet2, cbet2, dn2,
930  cbet1, cbet2, REDUCEDLENGTH, dummy, m12b, m0, dummy, dummy);
931  x = -1 + m12b / (cbet1 * cbet2 * m0 * Math::pi());
932  betscale = x < -real(0.01) ? sbet12a / x :
933  -_f * Math::sq(cbet1) * Math::pi();
934  lamscale = betscale / cbet1;
935  y = lam12x / lamscale;
936  }
937 
938  if (y > -tol1_ && x > -1 - xthresh_) {
939  // strip near cut
940  // Need real(x) here to cast away the volatility of x for min/max
941  if (_f >= 0) {
942  salp1 = fmin(real(1), -x); calp1 = - sqrt(1 - Math::sq(salp1));
943  } else {
944  calp1 = fmax(real(x > -tol1_ ? 0 : -1), x);
945  salp1 = sqrt(1 - Math::sq(calp1));
946  }
947  } else {
948  // Estimate alp1, by solving the astroid problem.
949  //
950  // Could estimate alpha1 = theta + pi/2, directly, i.e.,
951  // calp1 = y/k; salp1 = -x/(1+k); for _f >= 0
952  // calp1 = x/(1+k); salp1 = -y/k; for _f < 0 (need to check)
953  //
954  // However, it's better to estimate omg12 from astroid and use
955  // spherical formula to compute alp1. This reduces the mean number of
956  // Newton iterations for astroid cases from 2.24 (min 0, max 6) to 2.12
957  // (min 0 max 5). The changes in the number of iterations are as
958  // follows:
959  //
960  // change percent
961  // 1 5
962  // 0 78
963  // -1 16
964  // -2 0.6
965  // -3 0.04
966  // -4 0.002
967  //
968  // The histogram of iterations is (m = number of iterations estimating
969  // alp1 directly, n = number of iterations estimating via omg12, total
970  // number of trials = 148605):
971  //
972  // iter m n
973  // 0 148 186
974  // 1 13046 13845
975  // 2 93315 102225
976  // 3 36189 32341
977  // 4 5396 7
978  // 5 455 1
979  // 6 56 0
980  //
981  // Because omg12 is near pi, estimate work with omg12a = pi - omg12
982  real k = Astroid(x, y);
983  real
984  omg12a = lamscale * ( _f >= 0 ? -x * k/(1 + k) : -y * (1 + k)/k );
985  somg12 = sin(omg12a); comg12 = -cos(omg12a);
986  // Update spherical estimate of alp1 using omg12 instead of lam12
987  salp1 = cbet2 * somg12;
988  calp1 = sbet12a - cbet2 * sbet1 * Math::sq(somg12) / (1 - comg12);
989  }
990  }
991  // Sanity check on starting guess. Backwards check allows NaN through.
992  if (!(salp1 <= 0))
993  Math::norm(salp1, calp1);
994  else {
995  salp1 = 1; calp1 = 0;
996  }
997  return sig12;
998  }
999 
1000  Math::real GeodesicExact::Lambda12(real sbet1, real cbet1, real dn1,
1001  real sbet2, real cbet2, real dn2,
1002  real salp1, real calp1,
1003  real slam120, real clam120,
1004  real& salp2, real& calp2,
1005  real& sig12,
1006  real& ssig1, real& csig1,
1007  real& ssig2, real& csig2,
1008  EllipticFunction& E,
1009  real& domg12,
1010  bool diffp, real& dlam12) const
1011  {
1012 
1013  if (sbet1 == 0 && calp1 == 0)
1014  // Break degeneracy of equatorial line. This case has already been
1015  // handled.
1016  calp1 = -tiny_;
1017 
1018  real
1019  // sin(alp1) * cos(bet1) = sin(alp0)
1020  salp0 = salp1 * cbet1,
1021  calp0 = hypot(calp1, salp1 * sbet1); // calp0 > 0
1022 
1023  real somg1, comg1, somg2, comg2, somg12, comg12, cchi1, cchi2, lam12;
1024  // tan(bet1) = tan(sig1) * cos(alp1)
1025  // tan(omg1) = sin(alp0) * tan(sig1) = tan(omg1)=tan(alp1)*sin(bet1)
1026  ssig1 = sbet1; somg1 = salp0 * sbet1;
1027  csig1 = comg1 = calp1 * cbet1;
1028  // Without normalization we have schi1 = somg1.
1029  cchi1 = _f1 * dn1 * comg1;
1030  Math::norm(ssig1, csig1);
1031  // Math::norm(somg1, comg1); -- don't need to normalize!
1032  // Math::norm(schi1, cchi1); -- don't need to normalize!
1033 
1034  // Enforce symmetries in the case abs(bet2) = -bet1. Need to be careful
1035  // about this case, since this can yield singularities in the Newton
1036  // iteration.
1037  // sin(alp2) * cos(bet2) = sin(alp0)
1038  salp2 = cbet2 != cbet1 ? salp0 / cbet2 : salp1;
1039  // calp2 = sqrt(1 - sq(salp2))
1040  // = sqrt(sq(calp0) - sq(sbet2)) / cbet2
1041  // and subst for calp0 and rearrange to give (choose positive sqrt
1042  // to give alp2 in [0, pi/2]).
1043  calp2 = cbet2 != cbet1 || fabs(sbet2) != -sbet1 ?
1044  sqrt(Math::sq(calp1 * cbet1) +
1045  (cbet1 < -sbet1 ?
1046  (cbet2 - cbet1) * (cbet1 + cbet2) :
1047  (sbet1 - sbet2) * (sbet1 + sbet2))) / cbet2 :
1048  fabs(calp1);
1049  // tan(bet2) = tan(sig2) * cos(alp2)
1050  // tan(omg2) = sin(alp0) * tan(sig2).
1051  ssig2 = sbet2; somg2 = salp0 * sbet2;
1052  csig2 = comg2 = calp2 * cbet2;
1053  // Without normalization we have schi2 = somg2.
1054  cchi2 = _f1 * dn2 * comg2;
1055  Math::norm(ssig2, csig2);
1056  // Math::norm(somg2, comg2); -- don't need to normalize!
1057  // Math::norm(schi2, cchi2); -- don't need to normalize!
1058 
1059  // sig12 = sig2 - sig1, limit to [0, pi]
1060  sig12 = atan2(fmax(real(0), csig1 * ssig2 - ssig1 * csig2),
1061  csig1 * csig2 + ssig1 * ssig2);
1062 
1063  // omg12 = omg2 - omg1, limit to [0, pi]
1064  somg12 = fmax(real(0), comg1 * somg2 - somg1 * comg2);
1065  comg12 = comg1 * comg2 + somg1 * somg2;
1066  real k2 = Math::sq(calp0) * _ep2;
1067  E.Reset(-k2, -_ep2, 1 + k2, 1 + _ep2);
1068  // chi12 = chi2 - chi1, limit to [0, pi]
1069  real
1070  schi12 = fmax(real(0), cchi1 * somg2 - somg1 * cchi2),
1071  cchi12 = cchi1 * cchi2 + somg1 * somg2;
1072  // eta = chi12 - lam120
1073  real eta = atan2(schi12 * clam120 - cchi12 * slam120,
1074  cchi12 * clam120 + schi12 * slam120);
1075  real deta12 = -_e2/_f1 * salp0 * E.H() / (Math::pi() / 2) *
1076  (sig12 + (E.deltaH(ssig2, csig2, dn2) - E.deltaH(ssig1, csig1, dn1)));
1077  lam12 = eta + deta12;
1078  // domg12 = deta12 + chi12 - omg12
1079  domg12 = deta12 + atan2(schi12 * comg12 - cchi12 * somg12,
1080  cchi12 * comg12 + schi12 * somg12);
1081  if (diffp) {
1082  if (calp2 == 0)
1083  dlam12 = - 2 * _f1 * dn1 / sbet1;
1084  else {
1085  real dummy;
1086  Lengths(E, sig12, ssig1, csig1, dn1, ssig2, csig2, dn2,
1087  cbet1, cbet2, REDUCEDLENGTH,
1088  dummy, dlam12, dummy, dummy, dummy);
1089  dlam12 *= _f1 / (calp2 * cbet2);
1090  }
1091  }
1092 
1093  return lam12;
1094  }
1095 
1096  Math::real GeodesicExact::I4Integrand::asinhsqrt(real x) {
1097  // return asinh(sqrt(x))/sqrt(x)
1098  using std::sqrt; using std::asinh; using std::asin;
1099  return x == 0 ? 1 :
1100  (x > 0 ? asinh(sqrt(x))/sqrt(x) :
1101  asin(sqrt(-x))/sqrt(-x)); // NaNs end up here
1102  }
1103  Math::real GeodesicExact::I4Integrand::t(real x) {
1104  // This differs by from t as defined following Eq 61 in Karney (2013) by
1105  // the final subtraction of 1. This changes nothing since Eq 61 uses the
1106  // difference of two evaluations of t and improves the accuracy(?).
1107  using std::sqrt;
1108  // Group terms to minimize roundoff
1109  // with x = ep2, this is the same as
1110  // e2/(1-e2) + (atanh(e)/e - 1)
1111  return x + (sqrt(1 + x) * asinhsqrt(x) - 1);
1112  }
1113  Math::real GeodesicExact::I4Integrand::td(real x) {
1114  // d t(x) / dx
1115  using std::sqrt;
1116  return x == 0 ? 4/real(3) :
1117  // Group terms to minimize roundoff
1118  1 + (1 - asinhsqrt(x) / sqrt(1+x)) / (2*x);
1119  }
1120  // Math::real GeodesicExact::I4Integrand::Dt(real x, real y) {
1121  // // ( t(x) - t(y) ) / (x - y)
1122  // using std::sqrt; using std::fabs; using std::asinh; using std::asin;
1123  // if (x == y) return td(x);
1124  // if (x * y <= 0) return ( t(x) - t(y) ) / (x - y);
1125  // real
1126  // sx = sqrt(fabs(x)), sx1 = sqrt(1 + x),
1127  // sy = sqrt(fabs(y)), sy1 = sqrt(1 + y),
1128  // z = (x - y) / (sx * sy1 + sy * sx1),
1129  // d1 = 2 * sx * sy,
1130  // d2 = 2 * (x * sy * sy1 + y * sx * sx1);
1131  // return x > 0 ?
1132  // ( 1 + (asinh(z)/z) / d1 - (asinh(sx) + asinh(sy)) / d2 ) :
1133  // // NaNs fall through to here
1134  // ( 1 - (asin (z)/z) / d1 - (asin (sx) + asin (sy)) / d2 );
1135  // }
1136  Math::real GeodesicExact::I4Integrand::DtX(real y) const {
1137  // idiot version:
1138  // return ( tX - t(y) ) / (X - y);
1139  using std::sqrt; using std::fabs; using std::asinh; using std::asin;
1140  if (X == y) return tdX;
1141  if (X * y <= 0) return ( tX - t(y) ) / (X - y);
1142  real
1143  sy = sqrt(fabs(y)), sy1 = sqrt(1 + y),
1144  z = (X - y) / (sX * sy1 + sy * sX1),
1145  d1 = 2 * sX * sy,
1146  d2 = 2 * (X * sy * sy1 + y * sXX1);
1147  return X > 0 ?
1148  ( 1 + (asinh(z)/z) / d1 - (asinhsX + asinh(sy)) / d2 ) :
1149  // NaNs fall through to here
1150  ( 1 - (asin (z)/z) / d1 - (asinhsX + asin (sy)) / d2 );
1151  }
1152  GeodesicExact::I4Integrand::I4Integrand(real ep2, real k2)
1153  : X( ep2 )
1154  , tX( t(X) )
1155  , tdX( td(X) )
1156  , _k2( k2 )
1157  {
1158  using std::fabs; using std::sqrt; using std::asinh; using std::asin;
1159  sX = sqrt(fabs(X)); // ep
1160  sX1 = sqrt(1 + X); // 1/(1-f)
1161  sXX1 = sX * sX1;
1162  asinhsX = X > 0 ? asinh(sX) : asin(sX); // atanh(e)
1163  }
1164  Math::real GeodesicExact::I4Integrand::operator()(real sig) const {
1165  using std::sin;
1166  real ssig = sin(sig);
1167  return - DtX(_k2 * Math::sq(ssig)) * ssig/2;
1168  }
1169 
1170 } // namespace GeographicLib
GeographicLib::Math::real real
Definition: GeodSolve.cpp:31
Header for GeographicLib::GeodesicExact class.
Header for GeographicLib::GeodesicLineExact class.
#define GEOGRAPHICLIB_PANIC
Definition: Math.hpp:61
void reset(int N)
Definition: DST.cpp:24
void transform(std::function< real(real)> f, real F[]) const
Definition: DST.cpp:75
static real integral(real sinx, real cosx, const real F[], int N)
Definition: DST.cpp:108
Elliptic integrals and functions.
Math::real deltaE(real sn, real cn, real dn) const
Math::real deltaD(real sn, real cn, real dn) const
Exact geodesic calculations.
GeodesicLineExact InverseLine(real lat1, real lon1, real lat2, real lon2, unsigned caps=ALL) const
GeodesicLineExact GenDirectLine(real lat1, real lon1, real azi1, bool arcmode, real s12_a12, unsigned caps=ALL) const
GeodesicLineExact DirectLine(real lat1, real lon1, real azi1, real s12, unsigned caps=ALL) const
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
GeodesicLineExact Line(real lat1, real lon1, real azi1, unsigned caps=ALL) const
static const GeodesicExact & WGS84()
GeodesicLineExact ArcDirectLine(real lat1, real lon1, real azi1, real a12, unsigned caps=ALL) const
Exception handling for GeographicLib.
Definition: Constants.hpp:316
Mathematical functions needed by GeographicLib.
Definition: Math.hpp:76
static T degree()
Definition: Math.hpp:200
static T LatFix(T x)
Definition: Math.hpp:300
static void sincosd(T x, T &sinx, T &cosx)
Definition: Math.cpp:106
static T atan2d(T y, T x)
Definition: Math.cpp:183
static void norm(T &x, T &y)
Definition: Math.hpp:222
static T AngRound(T x)
Definition: Math.cpp:97
static T sq(T x)
Definition: Math.hpp:212
static T AngNormalize(T x)
Definition: Math.cpp:71
static int digits()
Definition: Math.cpp:26
static void sincosde(T x, T t, T &sinx, T &cosx)
Definition: Math.cpp:126
static T pi()
Definition: Math.hpp:190
static T AngDiff(T x, T y, T &e)
Definition: Math.cpp:82
@ hd
degrees per half turn
Definition: Math.hpp:144
@ qd
degrees per quarter turn
Definition: Math.hpp:141
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
void swap(GeographicLib::NearestNeighbor< dist_t, pos_t, distfun_t > &a, GeographicLib::NearestNeighbor< dist_t, pos_t, distfun_t > &b)