GeographicLib 2.3
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-2023) <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
31#include <vector>
32
33#if defined(_MSC_VER)
34// Squelch warnings about potentially uninitialized local variables,
35// constant conditional and enum-float expressions and mixing enums
36# pragma warning (disable: 4701 4127 5055 5054)
37#endif
38
39namespace GeographicLib {
40
41 using namespace std;
42
43 GeodesicExact::GeodesicExact(real a, real f)
44 : maxit2_(maxit1_ + Math::digits() + 10)
45 // Underflow guard. We require
46 // tiny_ * epsilon() > 0
47 // tiny_ + epsilon() == epsilon()
48 , tiny_(sqrt(numeric_limits<real>::min()))
49 , tol0_(numeric_limits<real>::epsilon())
50 // Increase multiplier in defn of tol1_ from 100 to 200 to fix inverse
51 // case 52.784459512564 0 -52.784459512563990912 179.634407464943777557
52 // which otherwise failed for Visual Studio 10 (Release and Debug)
53 , tol1_(200 * tol0_)
54 , tol2_(sqrt(tol0_))
55 , tolb_(tol0_) // Check on bisection interval
56 , xthresh_(1000 * tol2_)
57 , _a(a)
58 , _f(f)
59 , _f1(1 - _f)
60 , _e2(_f * (2 - _f))
61 , _ep2(_e2 / Math::sq(_f1)) // e2 / (1 - e2)
62 , _n(_f / ( 2 - _f))
63 , _b(_a * _f1)
64 // The Geodesic class substitutes atanh(sqrt(e2)) for asinh(sqrt(ep2)) in
65 // the definition of _c2. The latter is more accurate for very oblate
66 // ellipsoids (which the Geodesic class does not attempt to handle).
67 , _c2((Math::sq(_a) + Math::sq(_b) *
68 (_f == 0 ? 1 :
69 (_f > 0 ? asinh(sqrt(_ep2)) : atan(sqrt(-_e2))) /
70 sqrt(fabs(_e2))))/2) // authalic radius squared
71 // The sig12 threshold for "really short". Using the auxiliary sphere
72 // solution with dnm computed at (bet1 + bet2) / 2, the relative error in
73 // the azimuth consistency check is sig12^2 * abs(f) * min(1, 1-f/2) / 2.
74 // (Error measured for 1/100 < b/a < 100 and abs(f) >= 1/1000. For a
75 // given f and sig12, the max error occurs for lines near the pole. If
76 // the old rule for computing dnm = (dn1 + dn2)/2 is used, then the error
77 // increases by a factor of 2.) Setting this equal to epsilon gives
78 // sig12 = etol2. Here 0.1 is a safety factor (error decreased by 100)
79 // and max(0.001, abs(f)) stops etol2 getting too large in the nearly
80 // spherical case.
81 , _etol2(real(0.1) * tol2_ /
82 sqrt( fmax(real(0.001), fabs(_f)) * fmin(real(1), 1 - _f/2) / 2 ))
83 {
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
89 // Required number of terms in DST for full accuracy for all precisions as
90 // a function of n in [-0.99, 0.99]. Values determined by running
91 // develop/AreaEst compiled with GEOGRAPHICLIB_PRECISION = 5. For
92 // precision 4 and 5, GEOGRAPHICLIB_DIGITS was set to, resp., 384 and 768.
93 // The error criterion is relative error less than or equal to epsilon/2 =
94 // 0.5^digits, with digits = 24, 53, 64, 113, 256. The first 4 are the the
95 // "standard" values for float, double, long double, and float128; the last
96 // is the default for GeographicLib + mpfr. Also listed is the value of
97 // alp0 resulting in the most error for the given N.
98 //
99 // float double long double quad mpfr
100 // n N alp0 N alp0 N alp0 N alp0 N alp0
101 // -0.99 1024 0.09 3072 0.05 4096 0.04 8192 43.50 16384 22.01
102 // -0.98 512 0.18 1536 0.10 2048 0.09 4096 0.06 8192 0.04
103 // -0.97 384 0.25 1024 0.16 1536 0.13 3072 0.09 6144 0.06
104 // -0.96 256 0.36 768 0.21 1024 0.18 2048 0.13 4096 0.09
105 // -0.95 192 0.47 768 0.23 768 0.23 1536 0.17 4096 0.10
106 // -0.94 192 0.51 512 0.31 768 0.26 1536 0.18 3072 0.13
107 // -0.93 192 0.55 384 0.39 512 0.34 1024 0.24 3072 0.14
108 // -0.92 128 0.73 384 0.42 512 0.37 1024 0.26 2048 0.18
109 // -0.91 128 0.77 384 0.45 384 0.45 768 0.32 2048 0.19
110 // -0.90 96 0.94 256 0.58 384 0.47 768 0.34 2048 0.21
111 // -0.89 96 0.99 256 0.61 384 0.50 768 0.35 1536 0.25
112 // -0.88 96 1.04 256 0.64 384 0.52 768 0.37 1536 0.26
113 // -0.87 96 1.09 192 0.77 256 0.67 512 0.47 1536 0.27
114 // -0.86 64 1.38 192 0.80 256 0.69 512 0.49 1536 0.28
115 // -0.85 64 1.43 192 0.83 256 0.72 512 0.51 1024 0.36
116 // -0.84 64 1.49 192 0.86 256 0.75 384 0.61 1024 0.37
117 // -0.83 64 1.54 192 0.89 192 0.89 384 0.63 1024 0.39
118 // -0.82 48 1.82 192 0.92 192 0.92 384 0.65 1024 0.40
119 // -0.81 48 1.88 128 1.16 192 0.95 384 0.67 1024 0.41
120 // -0.80 48 1.94 128 1.19 192 0.97 384 0.69 768 0.49
121 // -0.79 48 1.99 128 1.23 192 1.00 384 0.71 768 0.50
122 // -0.78 48 2.04 128 1.26 192 1.03 384 0.73 768 0.51
123 // -0.77 48 2.10 128 1.29 192 1.05 256 0.91 768 0.53
124 // -0.76 48 2.15 128 1.32 128 1.32 256 0.93 768 0.54
125 // -0.75 48 2.20 96 1.56 128 1.35 256 0.96 768 0.55
126 // -0.74 32 2.74 96 1.60 128 1.38 256 0.98 768 0.57
127 // -0.73 32 2.81 96 1.63 128 1.41 256 1.00 768 0.58
128 // -0.72 32 2.87 96 1.67 128 1.44 256 1.02 512 0.72
129 // -0.71 32 2.93 96 1.70 128 1.47 192 1.20 512 0.74
130 // -0.70 32 2.99 96 1.73 96 1.73 192 1.23 512 0.75
131 // -0.69 32 3.05 96 1.77 96 1.77 192 1.25 512 0.77
132 // -0.68 32 3.11 96 1.80 96 1.80 192 1.28 512 0.78
133 // -0.67 24 3.64 96 1.84 96 1.84 192 1.30 512 0.80
134 // -0.66 24 3.71 96 1.87 96 1.87 192 1.32 512 0.81
135 // -0.65 24 3.77 64 2.33 96 1.90 192 1.35 384 0.95
136 // -0.64 24 3.84 64 2.37 96 1.93 192 1.37 384 0.97
137 // -0.63 24 3.90 64 2.41 96 1.97 192 1.39 384 0.98
138 // -0.62 24 3.97 64 2.45 96 2.00 192 1.42 384 1.00
139 // -0.61 24 4.04 64 2.49 96 2.03 192 1.44 384 1.02
140 // -0.60 24 4.10 64 2.53 96 2.06 192 1.46 384 1.03
141 // -0.59 24 4.16 64 2.57 64 2.57 128 1.82 384 1.05
142 // -0.58 24 4.23 64 2.60 64 2.60 128 1.84 384 1.07
143 // -0.57 24 4.29 48 3.05 64 2.64 128 1.87 384 1.08
144 // -0.56 24 4.36 48 3.10 64 2.68 128 1.90 384 1.10
145 // -0.55 16 5.38 48 3.14 64 2.72 128 1.93 384 1.11
146 // -0.54 16 5.46 48 3.19 64 2.76 128 1.96 384 1.13
147 // -0.53 16 5.54 48 3.23 64 2.80 128 1.98 256 1.40
148 // -0.52 16 5.61 48 3.27 64 2.84 128 2.01 256 1.42
149 // -0.51 16 5.69 48 3.32 64 2.88 128 2.04 256 1.44
150 // -0.50 16 5.77 48 3.36 64 2.92 96 2.38 256 1.46
151 // -0.49 16 5.85 48 3.41 48 3.41 96 2.42 256 1.48
152 // -0.48 16 5.92 48 3.45 48 3.45 96 2.45 256 1.50
153 // -0.47 16 6.00 48 3.50 48 3.50 96 2.48 256 1.52
154 // -0.46 16 6.08 48 3.54 48 3.54 96 2.51 256 1.54
155 // -0.45 12 7.06 48 3.59 48 3.59 96 2.54 256 1.56
156 // -0.44 12 7.15 48 3.63 48 3.63 96 2.57 256 1.58
157 // -0.43 12 7.24 32 4.49 48 3.68 96 2.61 256 1.60
158 // -0.42 12 7.33 32 4.55 48 3.72 96 2.64 192 1.87
159 // -0.41 12 7.42 32 4.60 48 3.77 96 2.67 192 1.89
160 // -0.40 12 7.51 32 4.66 48 3.81 96 2.70 192 1.91
161 // -0.39 12 7.60 32 4.71 48 3.86 96 2.73 192 1.94
162 // -0.38 12 7.69 32 4.77 48 3.90 96 2.77 192 1.96
163 // -0.37 12 7.77 32 4.82 48 3.95 96 2.80 192 1.98
164 // -0.36 12 7.86 32 4.88 48 3.99 96 2.83 192 2.00
165 // -0.35 12 7.95 32 4.94 32 4.94 64 3.50 192 2.03
166 // -0.34 12 8.04 32 4.99 32 4.99 64 3.54 192 2.05
167 // -0.33 12 8.13 24 5.81 32 5.05 64 3.58 192 2.07
168 // -0.32 12 8.22 24 5.88 32 5.10 64 3.62 192 2.10
169 // -0.31 12 8.31 24 5.94 32 5.16 64 3.66 192 2.12
170 // -0.30 8 10.16 24 6.01 32 5.22 64 3.70 192 2.14
171 // -0.29 8 10.27 24 6.07 32 5.27 64 3.74 192 2.17
172 // -0.28 8 10.38 24 6.14 32 5.33 64 3.78 128 2.68
173 // -0.27 8 10.49 24 6.20 32 5.39 64 3.82 128 2.71
174 // -0.26 8 10.60 24 6.27 32 5.45 64 3.87 128 2.74
175 // -0.25 8 10.72 24 6.34 32 5.50 48 4.51 128 2.77
176 // -0.24 8 10.83 24 6.40 24 6.40 48 4.55 128 2.80
177 // -0.23 8 10.94 24 6.47 24 6.47 48 4.60 128 2.83
178 // -0.22 8 11.05 24 6.54 24 6.54 48 4.65 128 2.86
179 // -0.21 6 12.72 24 6.60 24 6.60 48 4.70 128 2.89
180 // -0.20 6 12.85 24 6.67 24 6.67 48 4.75 128 2.92
181 // -0.19 6 12.97 16 8.21 24 6.74 48 4.80 128 2.95
182 // -0.18 6 13.10 16 8.29 24 6.81 48 4.85 96 3.44
183 // -0.17 6 13.23 16 8.37 24 6.88 48 4.89 96 3.47
184 // -0.16 6 13.36 16 8.46 24 6.95 48 4.94 96 3.51
185 // -0.15 6 13.49 16 8.54 24 7.02 48 5.00 96 3.54
186 // -0.14 6 13.62 16 8.62 24 7.09 48 5.05 96 3.58
187 // -0.13 6 13.75 16 8.71 24 7.16 48 5.10 96 3.62
188 // -0.12 6 13.88 16 8.80 16 8.80 32 6.28 96 3.65
189 // -0.11 6 14.01 12 10.19 16 8.88 32 6.35 96 3.69
190 // -0.10 4 16.85 12 10.28 16 8.97 32 6.41 96 3.73
191 // -0.09 4 17.01 12 10.38 16 9.06 32 6.47 96 3.76
192 // -0.08 4 17.17 12 10.48 16 9.14 32 6.54 96 3.80
193 // -0.07 4 17.32 12 10.58 16 9.23 32 6.60 64 4.69
194 // -0.06 4 17.48 12 10.69 12 10.69 24 7.67 64 4.74
195 // -0.05 4 17.64 12 10.79 12 10.79 24 7.75 64 4.79
196 // -0.04 4 17.80 12 10.89 12 10.89 24 7.82 64 4.84
197 // -0.03 4 17.96 8 13.26 12 10.99 24 7.90 48 5.63
198 // -0.02 4 18.11 8 13.38 12 11.10 24 7.97 48 5.68
199 // -0.01 4 18.27 6 15.36 8 13.51 16 9.78 48 5.74
200 // 0.00 4 1.00 4 1.00 4 1.00 4 1.00 4 1.00
201 // 0.01 4 18.57 6 15.62 8 13.75 16 9.96 48 5.85
202 // 0.02 4 18.70 8 13.86 12 11.51 24 8.28 48 5.91
203 // 0.03 4 18.83 8 13.97 12 11.61 24 8.36 48 5.97
204 // 0.04 4 18.96 12 11.71 12 11.71 24 8.44 64 5.23
205 // 0.05 4 19.09 12 11.81 12 11.81 24 8.52 64 5.28
206 // 0.06 4 19.22 12 11.92 12 11.92 24 8.60 64 5.33
207 // 0.07 4 19.36 12 12.02 16 10.52 32 7.55 64 5.39
208 // 0.08 4 19.49 12 12.13 16 10.61 32 7.63 64 5.44
209 // 0.09 4 19.62 12 12.23 16 10.71 32 7.70 96 4.50
210 // 0.10 4 19.76 12 12.34 16 10.80 32 7.77 96 4.54
211 // 0.11 4 19.89 12 12.45 16 10.90 32 7.85 96 4.59
212 // 0.12 6 17.01 16 11.00 16 11.00 32 7.92 96 4.63
213 // 0.13 6 17.14 16 11.10 16 11.10 32 8.00 96 4.68
214 // 0.14 6 17.27 16 11.20 24 9.26 48 6.64 96 4.73
215 // 0.15 6 17.40 16 11.30 24 9.35 48 6.70 96 4.77
216 // 0.16 6 17.53 16 11.40 24 9.44 48 6.77 96 4.82
217 // 0.17 6 17.67 16 11.51 24 9.53 48 6.84 96 4.87
218 // 0.18 6 17.80 16 11.61 24 9.62 48 6.90 96 4.92
219 // 0.19 6 17.94 16 11.72 24 9.71 48 6.97 128 4.31
220 // 0.20 6 18.07 16 11.83 24 9.80 48 7.04 128 4.36
221 // 0.21 6 18.21 24 9.90 24 9.90 48 7.11 128 4.40
222 // 0.22 6 18.35 24 9.99 24 9.99 48 7.18 128 4.45
223 // 0.23 6 18.49 24 10.09 24 10.09 48 7.26 128 4.49
224 // 0.24 6 18.63 24 10.19 24 10.19 48 7.33 128 4.54
225 // 0.25 6 18.77 24 10.28 24 10.28 48 7.41 128 4.59
226 // 0.26 6 18.92 24 10.39 24 10.39 48 7.48 128 4.64
227 // 0.27 8 17.00 24 10.49 32 9.17 64 6.58 128 4.69
228 // 0.28 8 17.14 24 10.59 32 9.26 64 6.65 128 4.74
229 // 0.29 8 17.28 24 10.69 32 9.35 64 6.72 192 3.92
230 // 0.30 8 17.43 24 10.80 32 9.45 64 6.79 192 3.96
231 // 0.31 8 17.57 24 10.91 32 9.54 64 6.86 192 4.01
232 // 0.32 8 17.72 24 11.02 32 9.64 64 6.93 192 4.05
233 // 0.33 8 17.87 24 11.13 32 9.74 64 7.01 192 4.09
234 // 0.34 8 18.02 24 11.24 32 9.84 64 7.08 192 4.14
235 // 0.35 8 18.17 24 11.36 32 9.95 64 7.16 192 4.19
236 // 0.36 8 18.32 24 11.47 32 10.05 64 7.24 192 4.23
237 // 0.37 8 18.48 32 10.16 32 10.16 64 7.32 192 4.28
238 // 0.38 8 18.63 32 10.27 32 10.27 96 6.08 192 4.33
239 // 0.39 8 18.79 32 10.38 48 8.58 96 6.15 192 4.38
240 // 0.40 8 18.95 32 10.49 48 8.68 96 6.22 192 4.43
241 // 0.41 8 19.11 32 10.60 48 8.78 96 6.30 192 4.49
242 // 0.42 12 16.45 32 10.72 48 8.88 96 6.37 192 4.54
243 // 0.43 12 16.61 32 10.84 48 8.98 96 6.45 192 4.59
244 // 0.44 12 16.77 32 10.96 48 9.08 96 6.52 256 4.04
245 // 0.45 12 16.93 32 11.09 48 9.19 96 6.60 256 4.09
246 // 0.46 12 17.10 32 11.21 48 9.30 96 6.68 256 4.14
247 // 0.47 12 17.26 32 11.34 48 9.41 96 6.77 256 4.19
248 // 0.48 12 17.44 32 11.47 48 9.52 96 6.85 256 4.24
249 // 0.49 12 17.61 48 9.64 48 9.64 96 6.94 256 4.30
250 // 0.50 12 17.79 48 9.76 48 9.76 96 7.03 256 4.35
251 // 0.51 12 17.97 48 9.88 48 9.88 96 7.12 256 4.41
252 // 0.52 12 18.15 48 10.00 48 10.00 96 7.21 256 4.47
253 // 0.53 12 18.34 48 10.13 48 10.13 128 6.36 256 4.53
254 // 0.54 12 18.53 48 10.26 48 10.26 128 6.45 256 4.59
255 // 0.55 12 18.72 48 10.40 64 9.10 128 6.53 384 3.82
256 // 0.56 12 18.92 48 10.53 64 9.22 128 6.63 384 3.87
257 // 0.57 12 19.12 48 10.68 64 9.35 128 6.72 384 3.93
258 // 0.58 12 19.33 48 10.82 64 9.48 128 6.82 384 3.98
259 // 0.59 12 19.54 48 10.97 64 9.61 128 6.92 384 4.05
260 // 0.60 12 19.75 48 11.13 64 9.75 128 7.02 384 4.11
261 // 0.61 12 19.97 48 11.28 64 9.89 128 7.13 384 4.17
262 // 0.62 12 20.20 48 11.45 64 10.04 128 7.24 384 4.24
263 // 0.63 16 18.39 48 11.62 64 10.19 192 6.05 384 4.31
264 // 0.64 16 18.62 64 10.35 64 10.35 192 6.15 384 4.38
265 // 0.65 16 18.86 64 10.51 96 8.71 192 6.25 384 4.45
266 // 0.66 16 19.10 64 10.68 96 8.85 192 6.36 384 4.53
267 // 0.67 16 19.35 64 10.86 96 9.00 192 6.47 512 4.00
268 // 0.68 16 19.60 64 11.04 96 9.16 192 6.58 512 4.08
269 // 0.69 16 19.87 64 11.23 96 9.32 192 6.70 512 4.15
270 // 0.70 16 20.14 64 11.43 96 9.49 192 6.83 512 4.23
271 // 0.71 16 20.42 64 11.63 96 9.67 192 6.96 512 4.32
272 // 0.72 16 20.71 64 11.85 96 9.85 192 7.10 512 4.40
273 // 0.73 16 21.01 96 10.04 96 10.04 192 7.25 512 4.50
274 // 0.74 16 21.32 96 10.25 96 10.25 256 6.44 768 3.76
275 // 0.75 16 21.65 96 10.46 96 10.46 256 6.58 768 3.84
276 // 0.76 16 21.99 96 10.68 128 9.36 256 6.73 768 3.93
277 // 0.77 24 19.41 96 10.92 128 9.57 256 6.89 768 4.03
278 // 0.78 24 19.76 96 11.17 128 9.79 256 7.06 768 4.13
279 // 0.79 24 20.13 96 11.44 128 10.03 256 7.24 768 4.24
280 // 0.80 24 20.51 96 11.72 128 10.29 384 6.11 768 4.35
281 // 0.81 24 20.92 96 12.02 128 10.56 384 6.28 768 4.48
282 // 0.82 24 21.35 96 12.34 192 8.99 384 6.46 1024 4.00
283 // 0.83 24 21.81 128 11.16 192 9.26 384 6.66 1024 4.13
284 // 0.84 24 22.29 128 11.50 192 9.55 384 6.88 1024 4.26
285 // 0.85 24 22.82 128 11.86 192 9.87 384 7.11 1024 4.41
286 // 0.86 24 23.38 128 12.26 192 10.21 384 7.37 1024 4.58
287 // 0.87 24 24.00 128 12.70 192 10.59 512 6.67 1536 3.90
288 // 0.88 24 24.67 192 11.01 192 11.01 512 6.95 1536 4.06
289 // 0.89 24 25.41 192 11.48 256 10.07 512 7.26 1536 4.25
290 // 0.90 24 26.24 192 12.00 256 10.54 768 6.27 1536 4.47
291 // 0.91 24 27.17 192 12.61 256 11.09 768 6.62 2048 4.10
292 // 0.92 24 28.23 192 13.30 384 9.74 768 7.02 2048 4.35
293 // 0.93 24 29.45 256 12.46 384 10.38 768 7.50 3072 3.82
294 // 0.94 24 30.86 256 13.36 384 11.16 1024 7.05 3072 4.13
295 // 0.95 24 32.53 384 12.14 512 10.67 1024 7.72 3072 4.53
296 // 0.96 24 34.51 384 13.42 512 11.83 1536 7.09 4096 4.40
297 // 0.97 24 36.88 512 13.45 768 11.24 2048 7.11 6144 4.16
298 // 0.98 16 41.78 768 13.48 1024 11.88 3072 7.12 8192 4.42
299 // 0.99 8 44.82 1024 16.00 1536 13.51 6144 7.14 16384 4.43
300 static const int ndiv = 100;
301 // Encode N as small integer: 2,3,4,6,8,12... -> 0,1,2,3,4,5...
302 // using this awk script
303 //
304 // {
305 // n = $1;
306 // if (n % 3 == 0) {
307 // s = 1;
308 // n = n/3;
309 // } else {
310 // s = 0;
311 // n = n/2;
312 // }
313 // p = int( log(n)/log(2)+0.5 );
314 // printf "%d\n", 2*p+s;
315 // }
316 //
317 // A couple of changes have been made: (1) the decrease in N for float and
318 // n > 0.97 has been removed; (2) entrys of n=+/-1 have been included
319 // (incrementing the previous code value by 1).
320#if GEOGRAPHICLIB_PRECISION == 1
321 static const unsigned char narr[2*ndiv+1] = {
322 19,18,16,15,14,13,13,13,12,12,11,11,11,11,10,10,10,10,9,9,9,9,9,9,9,9,8,
323 8,8,8,8,8,8,7,7,7,7,7,7,7,7,7,7,7,7,6,6,6,6,6,6,6,6,6,6,5,5,5,5,5,5,5,5,
324 5,5,5,5,5,5,5,4,4,4,4,4,4,4,4,4,3,3,3,3,3,3,3,3,3,3,3,2,2,2,2,2,2,2,2,2,
325 2,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,
326 4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,
327 6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,8
328 };
329#elif GEOGRAPHICLIB_PRECISION == 2
330 static const unsigned char narr[2*ndiv+1] = {
331 22,21,19,18,17,17,16,15,15,15,14,14,14,13,13,13,13,13,13,12,12,12,12,12,
332 12,11,11,11,11,11,11,11,11,11,11,10,10,10,10,10,10,10,10,9,9,9,9,9,9,9,9,
333 9,9,9,9,9,9,8,8,8,8,8,8,8,8,8,8,7,7,7,7,7,7,7,7,7,7,7,7,7,7,6,6,6,6,6,6,
334 6,6,5,5,5,5,5,5,5,5,4,4,3,2,3,4,4,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,7,7,
335 7,7,7,7,7,7,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,9,9,
336 9,9,9,9,9,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11,11,11,11,12,12,
337 12,12,12,13,13,13,13,13,14,14,15,15,16,17,18,19
338 };
339#elif GEOGRAPHICLIB_PRECISION == 3
340 static const unsigned char narr[2*ndiv+1] = {
341 23,22,20,19,18,17,17,16,16,15,15,15,15,14,14,14,14,13,13,13,13,13,13,13,
342 12,12,12,12,12,12,11,11,11,11,11,11,11,11,11,11,11,10,10,10,10,10,10,10,
343 10,10,10,9,9,9,9,9,9,9,9,9,9,9,9,9,9,8,8,8,8,8,8,8,8,8,8,8,7,7,7,7,7,7,7,
344 7,7,7,7,7,6,6,6,6,6,6,5,5,5,5,5,4,2,4,5,5,5,5,5,6,6,6,6,6,6,6,7,7,7,7,7,
345 7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
346 10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11,11,11,11,11,12,12,12,
347 12,12,12,13,13,13,13,13,13,13,14,14,14,15,15,15,16,16,17,18,19,20
348 };
349#elif GEOGRAPHICLIB_PRECISION == 4
350 static const unsigned char narr[2*ndiv+1] = {
351 25,24,22,21,20,19,19,18,18,17,17,17,17,16,16,16,15,15,15,15,15,15,15,14,
352 14,14,14,14,14,13,13,13,13,13,13,13,13,13,13,13,13,12,12,12,12,12,12,12,
353 12,12,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,10,10,10,10,10,10,10,
354 10,10,10,9,9,9,9,9,9,9,9,9,9,9,9,9,8,8,8,8,8,8,7,7,7,7,7,6,2,6,7,7,7,7,7,
355 8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,9,9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,
356 11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,12,12,12,12,12,12,12,12,12,
357 12,13,13,13,13,13,13,13,13,13,13,13,14,14,14,14,14,14,15,15,15,15,15,15,
358 15,16,16,16,17,17,17,17,18,18,19,20,21,23,24
359 };
360#elif GEOGRAPHICLIB_PRECISION == 5
361 static const unsigned char narr[2*ndiv+1] = {
362 27,26,24,23,22,22,21,21,20,20,20,19,19,19,19,18,18,18,18,18,17,17,17,17,
363 17,17,17,17,16,16,16,16,16,16,16,15,15,15,15,15,15,15,15,15,15,15,15,14,
364 14,14,14,14,14,14,14,14,14,14,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
365 12,12,12,12,12,12,12,12,12,12,11,11,11,11,11,11,11,11,11,11,11,10,10,10,
366 10,9,9,9,2,9,9,9,10,10,10,10,10,11,11,11,11,11,11,11,11,11,11,12,12,12,
367 12,12,12,12,12,12,12,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,14,14,
368 14,14,14,14,14,14,14,14,14,15,15,15,15,15,15,15,15,15,15,15,15,16,16,16,
369 16,16,16,16,17,17,17,17,17,17,17,17,18,18,18,18,18,19,19,19,19,20,20,21,
370 21,21,22,23,24,26,27
371 };
372#else
373#error "Bad value for GEOGRAPHICLIB_PRECISION"
374#endif
375 real n = ndiv * _n; // n in [-ndiv, ndiv]
376 int j = ndiv + int(n < 0 ? floor(n) : ceil(n)); // j in [0, 2*ndiv]
377 int N = int(narr[j]);
378 // Decode 0,1,2,3,4,5... -> 2,3,4,6,8,12...
379 N = (N % 2 == 0 ? 2 : 3) * (1 << (N/2));
380#if GEOGRAPHICLIB_PRECISION == 5
381 if (Math::digits() > 256) {
382 // Scale up N by the number of digits in the precision relative to
383 // the number used for the test = 256.
384 int M = (Math::digits() * N) / 256;
385 while (N < M) N = N % 3 == 0 ? 4*N/3 : 3*N/2;
386 }
387#endif
388 _fft.reset(N);
389 _nC4 = N;
390 }
391
393 static const GeodesicExact wgs84(Constants::WGS84_a(),
395 return wgs84;
396 }
397
398 GeodesicLineExact GeodesicExact::Line(real lat1, real lon1, real azi1,
399 unsigned caps) const {
400 return GeodesicLineExact(*this, lat1, lon1, azi1, caps);
401 }
402
403 Math::real GeodesicExact::GenDirect(real lat1, real lon1, real azi1,
404 bool arcmode, real s12_a12,
405 unsigned outmask,
406 real& lat2, real& lon2, real& azi2,
407 real& s12, real& m12,
408 real& M12, real& M21,
409 real& S12) const {
410 // Automatically supply DISTANCE_IN if necessary
411 if (!arcmode) outmask |= DISTANCE_IN;
412 return GeodesicLineExact(*this, lat1, lon1, azi1, outmask)
413 . // Note the dot!
414 GenPosition(arcmode, s12_a12, outmask,
415 lat2, lon2, azi2, s12, m12, M12, M21, S12);
416 }
417
419 real azi1,
420 bool arcmode, real s12_a12,
421 unsigned caps) const {
422 azi1 = Math::AngNormalize(azi1);
423 real salp1, calp1;
424 // Guard against underflow in salp0. Also -0 is converted to +0.
425 Math::sincosd(Math::AngRound(azi1), salp1, calp1);
426 // Automatically supply DISTANCE_IN if necessary
427 if (!arcmode) caps |= DISTANCE_IN;
428 return GeodesicLineExact(*this, lat1, lon1, azi1, salp1, calp1,
429 caps, arcmode, s12_a12);
430 }
431
433 real azi1, real s12,
434 unsigned caps) const {
435 return GenDirectLine(lat1, lon1, azi1, false, s12, caps);
436 }
437
439 real azi1, real a12,
440 unsigned caps) const {
441 return GenDirectLine(lat1, lon1, azi1, true, a12, caps);
442 }
443
444 Math::real GeodesicExact::GenInverse(real lat1, real lon1,
445 real lat2, real lon2,
446 unsigned outmask, real& s12,
447 real& salp1, real& calp1,
448 real& salp2, real& calp2,
449 real& m12, real& M12, real& M21,
450 real& S12) const {
451 // Compute longitude difference (AngDiff does this carefully). Result is
452 // in [-180, 180] but -180 is only for west-going geodesics. 180 is for
453 // east-going and meridional geodesics.
454 using std::isnan; // Needed for Centos 7, ubuntu 14
455 real lon12s, lon12 = Math::AngDiff(lon1, lon2, lon12s);
456 // Make longitude difference positive.
457 int lonsign = signbit(lon12) ? -1 : 1;
458 lon12 *= lonsign; lon12s *= lonsign;
459 real
460 lam12 = lon12 * Math::degree(),
461 slam12, clam12;
462 // Calculate sincos of lon12 + error (this applies AngRound internally).
463 Math::sincosde(lon12, lon12s, slam12, clam12);
464 // the supplementary longitude difference
465 lon12s = (Math::hd - lon12) - lon12s;
466
467 // If really close to the equator, treat as on equator.
468 lat1 = Math::AngRound(Math::LatFix(lat1));
469 lat2 = Math::AngRound(Math::LatFix(lat2));
470 // Swap points so that point with higher (abs) latitude is point 1
471 // If one latitude is a nan, then it becomes lat1.
472 int swapp = fabs(lat1) < fabs(lat2) || isnan(lat2) ? -1 : 1;
473 if (swapp < 0) {
474 lonsign *= -1;
475 swap(lat1, lat2);
476 }
477 // Make lat1 <= -0
478 int latsign = signbit(lat1) ? 1 : -1;
479 lat1 *= latsign;
480 lat2 *= latsign;
481 // Now we have
482 //
483 // 0 <= lon12 <= 180
484 // -90 <= lat1 <= -0
485 // lat1 <= lat2 <= -lat1
486 //
487 // longsign, swapp, latsign register the transformation to bring the
488 // coordinates to this canonical form. In all cases, 1 means no change was
489 // made. We make these transformations so that there are few cases to
490 // check, e.g., on verifying quadrants in atan2. In addition, this
491 // enforces some symmetries in the results returned.
492
493 real sbet1, cbet1, sbet2, cbet2, s12x, m12x;
494 // Initialize for the meridian. No longitude calculation is done in this
495 // case to let the parameter default to 0.
496 EllipticFunction E(-_ep2);
497
498 Math::sincosd(lat1, sbet1, cbet1); sbet1 *= _f1;
499 // Ensure cbet1 = +epsilon at poles; doing the fix on beta means that sig12
500 // will be <= 2*tiny for two points at the same pole.
501 Math::norm(sbet1, cbet1); cbet1 = fmax(tiny_, cbet1);
502
503 Math::sincosd(lat2, sbet2, cbet2); sbet2 *= _f1;
504 // Ensure cbet2 = +epsilon at poles
505 Math::norm(sbet2, cbet2); cbet2 = fmax(tiny_, cbet2);
506
507 // If cbet1 < -sbet1, then cbet2 - cbet1 is a sensitive measure of the
508 // |bet1| - |bet2|. Alternatively (cbet1 >= -sbet1), abs(sbet2) + sbet1 is
509 // a better measure. This logic is used in assigning calp2 in Lambda12.
510 // Sometimes these quantities vanish and in that case we force bet2 = +/-
511 // bet1 exactly. An example where is is necessary is the inverse problem
512 // 48.522876735459 0 -48.52287673545898293 179.599720456223079643
513 // which failed with Visual Studio 10 (Release and Debug)
514
515 if (cbet1 < -sbet1) {
516 if (cbet2 == cbet1)
517 sbet2 = copysign(sbet1, sbet2);
518 } else {
519 if (fabs(sbet2) == -sbet1)
520 cbet2 = cbet1;
521 }
522
523 real
524 dn1 = (_f >= 0 ? sqrt(1 + _ep2 * Math::sq(sbet1)) :
525 sqrt(1 - _e2 * Math::sq(cbet1)) / _f1),
526 dn2 = (_f >= 0 ? sqrt(1 + _ep2 * Math::sq(sbet2)) :
527 sqrt(1 - _e2 * Math::sq(cbet2)) / _f1);
528
529 real a12, sig12;
530
531 bool meridian = lat1 == -Math::qd || slam12 == 0;
532
533 if (meridian) {
534
535 // Endpoints are on a single full meridian, so the geodesic might lie on
536 // a meridian.
537
538 calp1 = clam12; salp1 = slam12; // Head to the target longitude
539 calp2 = 1; salp2 = 0; // At the target we're heading north
540
541 real
542 // tan(bet) = tan(sig) * cos(alp)
543 ssig1 = sbet1, csig1 = calp1 * cbet1,
544 ssig2 = sbet2, csig2 = calp2 * cbet2;
545
546 // sig12 = sig2 - sig1
547 sig12 = atan2(fmax(real(0), csig1 * ssig2 - ssig1 * csig2),
548 csig1 * csig2 + ssig1 * ssig2);
549 {
550 real dummy;
551 Lengths(E, sig12, ssig1, csig1, dn1, ssig2, csig2, dn2,
552 cbet1, cbet2, outmask | REDUCEDLENGTH,
553 s12x, m12x, dummy, M12, M21);
554 }
555 // Add the check for sig12 since zero length geodesics might yield m12 <
556 // 0. Test case was
557 //
558 // echo 20.001 0 20.001 0 | GeodSolve -i
559 //
560 // In fact, we will have sig12 > pi/2 for meridional geodesic which is
561 // not a shortest path.
562 if (sig12 < 1 || m12x >= 0) {
563 // Need at least 2, to handle 90 0 90 180
564 if (sig12 < 3 * tiny_ ||
565 // Prevent negative s12 or m12 for short lines
566 (sig12 < tol0_ && (s12x < 0 || m12x < 0)))
567 sig12 = m12x = s12x = 0;
568 m12x *= _b;
569 s12x *= _b;
570 a12 = sig12 / Math::degree();
571 } else
572 // m12 < 0, i.e., prolate and too close to anti-podal
573 meridian = false;
574 }
575
576 // somg12 == 2 marks that it needs to be calculated
577 real omg12 = 0, somg12 = 2, comg12 = 0;
578 if (!meridian &&
579 sbet1 == 0 && // and sbet2 == 0
580 (_f <= 0 || lon12s >= _f * Math::hd)) {
581
582 // Geodesic runs along equator
583 calp1 = calp2 = 0; salp1 = salp2 = 1;
584 s12x = _a * lam12;
585 sig12 = omg12 = lam12 / _f1;
586 m12x = _b * sin(sig12);
587 if (outmask & GEODESICSCALE)
588 M12 = M21 = cos(sig12);
589 a12 = lon12 / _f1;
590
591 } else if (!meridian) {
592
593 // Now point1 and point2 belong within a hemisphere bounded by a
594 // meridian and geodesic is neither meridional or equatorial.
595
596 // Figure a starting point for Newton's method
597 real dnm;
598 sig12 = InverseStart(E, sbet1, cbet1, dn1, sbet2, cbet2, dn2,
599 lam12, slam12, clam12,
600 salp1, calp1, salp2, calp2, dnm);
601
602 if (sig12 >= 0) {
603 // Short lines (InverseStart sets salp2, calp2, dnm)
604 s12x = sig12 * _b * dnm;
605 m12x = Math::sq(dnm) * _b * sin(sig12 / dnm);
606 if (outmask & GEODESICSCALE)
607 M12 = M21 = cos(sig12 / dnm);
608 a12 = sig12 / Math::degree();
609 omg12 = lam12 / (_f1 * dnm);
610 } else {
611
612 // Newton's method. This is a straightforward solution of f(alp1) =
613 // lambda12(alp1) - lam12 = 0 with one wrinkle. f(alp) has exactly one
614 // root in the interval (0, pi) and its derivative is positive at the
615 // root. Thus f(alp) is positive for alp > alp1 and negative for alp <
616 // alp1. During the course of the iteration, a range (alp1a, alp1b) is
617 // maintained which brackets the root and with each evaluation of
618 // f(alp) the range is shrunk, if possible. Newton's method is
619 // restarted whenever the derivative of f is negative (because the new
620 // value of alp1 is then further from the solution) or if the new
621 // estimate of alp1 lies outside (0,pi); in this case, the new starting
622 // guess is taken to be (alp1a + alp1b) / 2.
623 //
624 // initial values to suppress warnings (if loop is executed 0 times)
625 real ssig1 = 0, csig1 = 0, ssig2 = 0, csig2 = 0, domg12 = 0;
626 unsigned numit = 0;
627 // Bracketing range
628 real salp1a = tiny_, calp1a = 1, salp1b = tiny_, calp1b = -1;
629 for (bool tripn = false, tripb = false;; ++numit) {
630 // 1/4 meridian = 10e6 m and random input. max err is estimated max
631 // error in nm (checking solution of inverse problem by direct
632 // solution). iter is mean and sd of number of iterations
633 //
634 // max iter
635 // log2(b/a) err mean sd
636 // -7 387 5.33 3.68
637 // -6 345 5.19 3.43
638 // -5 269 5.00 3.05
639 // -4 210 4.76 2.44
640 // -3 115 4.55 1.87
641 // -2 69 4.35 1.38
642 // -1 36 4.05 1.03
643 // 0 15 0.01 0.13
644 // 1 25 5.10 1.53
645 // 2 96 5.61 2.09
646 // 3 318 6.02 2.74
647 // 4 985 6.24 3.22
648 // 5 2352 6.32 3.44
649 // 6 6008 6.30 3.45
650 // 7 19024 6.19 3.30
651 real dv;
652 real v = Lambda12(sbet1, cbet1, dn1, sbet2, cbet2, dn2, salp1, calp1,
653 slam12, clam12,
654 salp2, calp2, sig12, ssig1, csig1, ssig2, csig2,
655 E, domg12, numit < maxit1_, dv);
656 if (tripb ||
657 // Reversed test to allow escape with NaNs
658 !(fabs(v) >= (tripn ? 8 : 1) * tol0_) ||
659 // Enough bisections to get accurate result
660 numit == maxit2_)
661 break;
662 // Update bracketing values
663 if (v > 0 && (numit > maxit1_ || calp1/salp1 > calp1b/salp1b))
664 { salp1b = salp1; calp1b = calp1; }
665 else if (v < 0 && (numit > maxit1_ || calp1/salp1 < calp1a/salp1a))
666 { salp1a = salp1; calp1a = calp1; }
667 if (numit < maxit1_ && dv > 0) {
668 real
669 dalp1 = -v/dv;
670 // |dalp1| < pi test moved earlier because GEOGRAPHICLIB_PRECISION
671 // = 5 can result in dalp1 = 10^(10^8). Then sin(dalp1) takes ages
672 // (because of the need to do accurate range reduction).
673 if (fabs(dalp1) < Math::pi()) {
674 real
675 sdalp1 = sin(dalp1), cdalp1 = cos(dalp1),
676 nsalp1 = salp1 * cdalp1 + calp1 * sdalp1;
677 if (nsalp1 > 0) {
678 calp1 = calp1 * cdalp1 - salp1 * sdalp1;
679 salp1 = nsalp1;
680 Math::norm(salp1, calp1);
681 // In some regimes we don't get quadratic convergence because
682 // slope -> 0. So use convergence conditions based on epsilon
683 // instead of sqrt(epsilon).
684 tripn = fabs(v) <= 16 * tol0_;
685 continue;
686 }
687 }
688 }
689 // Either dv was not positive or updated value was outside legal
690 // range. Use the midpoint of the bracket as the next estimate.
691 // This mechanism is not needed for the WGS84 ellipsoid, but it does
692 // catch problems with more eccentric ellipsoids. Its efficacy is
693 // such for the WGS84 test set with the starting guess set to alp1 =
694 // 90deg:
695 // the WGS84 test set: mean = 5.21, sd = 3.93, max = 24
696 // WGS84 and random input: mean = 4.74, sd = 0.99
697 salp1 = (salp1a + salp1b)/2;
698 calp1 = (calp1a + calp1b)/2;
699 Math::norm(salp1, calp1);
700 tripn = false;
701 tripb = (fabs(salp1a - salp1) + (calp1a - calp1) < tolb_ ||
702 fabs(salp1 - salp1b) + (calp1 - calp1b) < tolb_);
703 }
704 {
705 real dummy;
706 Lengths(E, sig12, ssig1, csig1, dn1, ssig2, csig2, dn2,
707 cbet1, cbet2, outmask, s12x, m12x, dummy, M12, M21);
708 }
709 m12x *= _b;
710 s12x *= _b;
711 a12 = sig12 / Math::degree();
712 if (outmask & AREA) {
713 // omg12 = lam12 - domg12
714 real sdomg12 = sin(domg12), cdomg12 = cos(domg12);
715 somg12 = slam12 * cdomg12 - clam12 * sdomg12;
716 comg12 = clam12 * cdomg12 + slam12 * sdomg12;
717 }
718 }
719 }
720
721 if (outmask & DISTANCE)
722 s12 = real(0) + s12x; // Convert -0 to 0
723
724 if (outmask & REDUCEDLENGTH)
725 m12 = real(0) + m12x; // Convert -0 to 0
726
727 if (outmask & AREA) {
728 real
729 // From Lambda12: sin(alp1) * cos(bet1) = sin(alp0)
730 salp0 = salp1 * cbet1,
731 calp0 = hypot(calp1, salp1 * sbet1); // calp0 > 0
732 real alp12,
733 // Multiplier = a^2 * e^2 * cos(alpha0) * sin(alpha0).
734 A4 = Math::sq(_a) * calp0 * salp0 * _e2;
735 if (A4 != 0) {
736 real
737 k2 = Math::sq(calp0) * _ep2,
738 // From Lambda12: tan(bet) = tan(sig) * cos(alp)
739 ssig1 = sbet1, csig1 = calp1 * cbet1,
740 ssig2 = sbet2, csig2 = calp2 * cbet2;
741 Math::norm(ssig1, csig1);
742 Math::norm(ssig2, csig2);
743 I4Integrand i4(_ep2, k2);
744 vector<real> C4a(_nC4);
745 _fft.transform(i4, C4a.data());
746 S12 = A4 * DST::integral(ssig1, csig1, ssig2, csig2, C4a.data(), _nC4);
747 } else
748 // Avoid problems with indeterminate sig1, sig2 on equator
749 S12 = 0;
750
751 if (!meridian && somg12 == 2) {
752 somg12 = sin(omg12); comg12 = cos(omg12);
753 }
754
755 if (!meridian &&
756 // omg12 < 3/4 * pi
757 comg12 > -real(0.7071) && // Long difference not too big
758 sbet2 - sbet1 < real(1.75)) { // Lat difference not too big
759 // Use tan(Gamma/2) = tan(omg12/2)
760 // * (tan(bet1/2)+tan(bet2/2))/(1+tan(bet1/2)*tan(bet2/2))
761 // with tan(x/2) = sin(x)/(1+cos(x))
762 real domg12 = 1 + comg12, dbet1 = 1 + cbet1, dbet2 = 1 + cbet2;
763 alp12 = 2 * atan2( somg12 * ( sbet1 * dbet2 + sbet2 * dbet1 ),
764 domg12 * ( sbet1 * sbet2 + dbet1 * dbet2 ) );
765 } else {
766 // alp12 = alp2 - alp1, used in atan2 so no need to normalize
767 real
768 salp12 = salp2 * calp1 - calp2 * salp1,
769 calp12 = calp2 * calp1 + salp2 * salp1;
770 // The right thing appears to happen if alp1 = +/-180 and alp2 = 0, viz
771 // salp12 = -0 and alp12 = -180. However this depends on the sign
772 // being attached to 0 correctly. The following ensures the correct
773 // behavior.
774 if (salp12 == 0 && calp12 < 0) {
775 salp12 = tiny_ * calp1;
776 calp12 = -1;
777 }
778 alp12 = atan2(salp12, calp12);
779 }
780 S12 += _c2 * alp12;
781 S12 *= swapp * lonsign * latsign;
782 // Convert -0 to 0
783 S12 += 0;
784 }
785
786 // Convert calp, salp to azimuth accounting for lonsign, swapp, latsign.
787 if (swapp < 0) {
788 swap(salp1, salp2);
789 swap(calp1, calp2);
790 if (outmask & GEODESICSCALE)
791 swap(M12, M21);
792 }
793
794 salp1 *= swapp * lonsign; calp1 *= swapp * latsign;
795 salp2 *= swapp * lonsign; calp2 *= swapp * latsign;
796
797 // Returned value in [0, 180]
798 return a12;
799 }
800
801 Math::real GeodesicExact::GenInverse(real lat1, real lon1,
802 real lat2, real lon2,
803 unsigned outmask,
804 real& s12, real& azi1, real& azi2,
805 real& m12, real& M12, real& M21,
806 real& S12) const {
807 outmask &= OUT_MASK;
808 real salp1, calp1, salp2, calp2,
809 a12 = GenInverse(lat1, lon1, lat2, lon2,
810 outmask, s12, salp1, calp1, salp2, calp2,
811 m12, M12, M21, S12);
812 if (outmask & AZIMUTH) {
813 azi1 = Math::atan2d(salp1, calp1);
814 azi2 = Math::atan2d(salp2, calp2);
815 }
816 return a12;
817 }
818
820 real lat2, real lon2,
821 unsigned caps) const {
822 real t, salp1, calp1, salp2, calp2,
823 a12 = GenInverse(lat1, lon1, lat2, lon2,
824 // No need to specify AZIMUTH here
825 0u, t, salp1, calp1, salp2, calp2,
826 t, t, t, t),
827 azi1 = Math::atan2d(salp1, calp1);
828 // Ensure that a12 can be converted to a distance
829 if (caps & (OUT_MASK & DISTANCE_IN)) caps |= DISTANCE;
830 return GeodesicLineExact(*this, lat1, lon1, azi1, salp1, calp1, caps,
831 true, a12);
832 }
833
834 void GeodesicExact::Lengths(const EllipticFunction& E,
835 real sig12,
836 real ssig1, real csig1, real dn1,
837 real ssig2, real csig2, real dn2,
838 real cbet1, real cbet2, unsigned outmask,
839 real& s12b, real& m12b, real& m0,
840 real& M12, real& M21) const {
841 // Return m12b = (reduced length)/_b; also calculate s12b = distance/_b,
842 // and m0 = coefficient of secular term in expression for reduced length.
843
844 outmask &= OUT_ALL;
845 // outmask & DISTANCE: set s12b
846 // outmask & REDUCEDLENGTH: set m12b & m0
847 // outmask & GEODESICSCALE: set M12 & M21
848
849 // It's OK to have repeated dummy arguments,
850 // e.g., s12b = m0 = M12 = M21 = dummy
851
852 if (outmask & DISTANCE)
853 // Missing a factor of _b
854 s12b = E.E() / (Math::pi() / 2) *
855 (sig12 + (E.deltaE(ssig2, csig2, dn2) - E.deltaE(ssig1, csig1, dn1)));
856 if (outmask & (REDUCEDLENGTH | GEODESICSCALE)) {
857 real
858 m0x = - E.k2() * E.D() / (Math::pi() / 2),
859 J12 = m0x *
860 (sig12 + (E.deltaD(ssig2, csig2, dn2) - E.deltaD(ssig1, csig1, dn1)));
861 if (outmask & REDUCEDLENGTH) {
862 m0 = m0x;
863 // Missing a factor of _b. Add parens around (csig1 * ssig2) and
864 // (ssig1 * csig2) to ensure accurate cancellation in the case of
865 // coincident points.
866 m12b = dn2 * (csig1 * ssig2) - dn1 * (ssig1 * csig2) -
867 csig1 * csig2 * J12;
868 }
869 if (outmask & GEODESICSCALE) {
870 real csig12 = csig1 * csig2 + ssig1 * ssig2;
871 real t = _ep2 * (cbet1 - cbet2) * (cbet1 + cbet2) / (dn1 + dn2);
872 M12 = csig12 + (t * ssig2 - csig2 * J12) * ssig1 / dn1;
873 M21 = csig12 - (t * ssig1 - csig1 * J12) * ssig2 / dn2;
874 }
875 }
876 }
877
878 Math::real GeodesicExact::Astroid(real x, real y) {
879 // Solve k^4+2*k^3-(x^2+y^2-1)*k^2-2*y^2*k-y^2 = 0 for positive root k.
880 // This solution is adapted from Geocentric::Reverse.
881 real k;
882 real
883 p = Math::sq(x),
884 q = Math::sq(y),
885 r = (p + q - 1) / 6;
886 if ( !(q == 0 && r <= 0) ) {
887 real
888 // Avoid possible division by zero when r = 0 by multiplying equations
889 // for s and t by r^3 and r, resp.
890 S = p * q / 4, // S = r^3 * s
891 r2 = Math::sq(r),
892 r3 = r * r2,
893 // The discriminant of the quadratic equation for T3. This is zero on
894 // the evolute curve p^(1/3)+q^(1/3) = 1
895 disc = S * (S + 2 * r3);
896 real u = r;
897 if (disc >= 0) {
898 real T3 = S + r3;
899 // Pick the sign on the sqrt to maximize abs(T3). This minimizes loss
900 // of precision due to cancellation. The result is unchanged because
901 // of the way the T is used in definition of u.
902 T3 += T3 < 0 ? -sqrt(disc) : sqrt(disc); // T3 = (r * t)^3
903 // N.B. cbrt always returns the real root. cbrt(-8) = -2.
904 real T = cbrt(T3); // T = r * t
905 // T can be zero; but then r2 / T -> 0.
906 u += T + (T != 0 ? r2 / T : 0);
907 } else {
908 // T is complex, but the way u is defined the result is real.
909 real ang = atan2(sqrt(-disc), -(S + r3));
910 // There are three possible cube roots. We choose the root which
911 // avoids cancellation. Note that disc < 0 implies that r < 0.
912 u += 2 * r * cos(ang / 3);
913 }
914 real
915 v = sqrt(Math::sq(u) + q), // guaranteed positive
916 // Avoid loss of accuracy when u < 0.
917 uv = u < 0 ? q / (v - u) : u + v, // u+v, guaranteed positive
918 w = (uv - q) / (2 * v); // positive?
919 // Rearrange expression for k to avoid loss of accuracy due to
920 // subtraction. Division by 0 not possible because uv > 0, w >= 0.
921 k = uv / (sqrt(uv + Math::sq(w)) + w); // guaranteed positive
922 } else { // q == 0 && r <= 0
923 // y = 0 with |x| <= 1. Handle this case directly.
924 // for y small, positive root is k = abs(y)/sqrt(1-x^2)
925 k = 0;
926 }
927 return k;
928 }
929
930 Math::real GeodesicExact::InverseStart(EllipticFunction& E,
931 real sbet1, real cbet1, real dn1,
932 real sbet2, real cbet2, real dn2,
933 real lam12, real slam12, real clam12,
934 real& salp1, real& calp1,
935 // Only updated if return val >= 0
936 real& salp2, real& calp2,
937 // Only updated for short lines
938 real& dnm) const {
939 // Return a starting point for Newton's method in salp1 and calp1 (function
940 // value is -1). If Newton's method doesn't need to be used, return also
941 // salp2 and calp2 and function value is sig12.
942 real
943 sig12 = -1, // Return value
944 // bet12 = bet2 - bet1 in [0, pi); bet12a = bet2 + bet1 in (-pi, 0]
945 sbet12 = sbet2 * cbet1 - cbet2 * sbet1,
946 cbet12 = cbet2 * cbet1 + sbet2 * sbet1;
947 real sbet12a = sbet2 * cbet1 + cbet2 * sbet1;
948 bool shortline = cbet12 >= 0 && sbet12 < real(0.5) &&
949 cbet2 * lam12 < real(0.5);
950 real somg12, comg12;
951 if (shortline) {
952 real sbetm2 = Math::sq(sbet1 + sbet2);
953 // sin((bet1+bet2)/2)^2
954 // = (sbet1 + sbet2)^2 / ((sbet1 + sbet2)^2 + (cbet1 + cbet2)^2)
955 sbetm2 /= sbetm2 + Math::sq(cbet1 + cbet2);
956 dnm = sqrt(1 + _ep2 * sbetm2);
957 real omg12 = lam12 / (_f1 * dnm);
958 somg12 = sin(omg12); comg12 = cos(omg12);
959 } else {
960 somg12 = slam12; comg12 = clam12;
961 }
962
963 salp1 = cbet2 * somg12;
964 calp1 = comg12 >= 0 ?
965 sbet12 + cbet2 * sbet1 * Math::sq(somg12) / (1 + comg12) :
966 sbet12a - cbet2 * sbet1 * Math::sq(somg12) / (1 - comg12);
967
968 real
969 ssig12 = hypot(salp1, calp1),
970 csig12 = sbet1 * sbet2 + cbet1 * cbet2 * comg12;
971
972 if (shortline && ssig12 < _etol2) {
973 // really short lines
974 salp2 = cbet1 * somg12;
975 calp2 = sbet12 - cbet1 * sbet2 *
976 (comg12 >= 0 ? Math::sq(somg12) / (1 + comg12) : 1 - comg12);
977 Math::norm(salp2, calp2);
978 // Set return value
979 sig12 = atan2(ssig12, csig12);
980 } else if (fabs(_n) > real(0.1) || // Skip astroid calc if too eccentric
981 csig12 >= 0 ||
982 ssig12 >= 6 * fabs(_n) * Math::pi() * Math::sq(cbet1)) {
983 // Nothing to do, zeroth order spherical approximation is OK
984 } else {
985 // Scale lam12 and bet2 to x, y coordinate system where antipodal point
986 // is at origin and singular point is at y = 0, x = -1.
987 real x, y, lamscale, betscale;
988 real lam12x = atan2(-slam12, -clam12); // lam12 - pi
989 if (_f >= 0) { // In fact f == 0 does not get here
990 // x = dlong, y = dlat
991 {
992 real k2 = Math::sq(sbet1) * _ep2;
993 E.Reset(-k2, -_ep2, 1 + k2, 1 + _ep2);
994 lamscale = _e2/_f1 * cbet1 * 2 * E.H();
995 }
996 betscale = lamscale * cbet1;
997
998 x = lam12x / lamscale;
999 y = sbet12a / betscale;
1000 } else { // _f < 0
1001 // x = dlat, y = dlong
1002 real
1003 cbet12a = cbet2 * cbet1 - sbet2 * sbet1,
1004 bet12a = atan2(sbet12a, cbet12a);
1005 real m12b, m0, dummy;
1006 // In the case of lon12 = 180, this repeats a calculation made in
1007 // Inverse.
1008 Lengths(E, Math::pi() + bet12a,
1009 sbet1, -cbet1, dn1, sbet2, cbet2, dn2,
1010 cbet1, cbet2, REDUCEDLENGTH, dummy, m12b, m0, dummy, dummy);
1011 x = -1 + m12b / (cbet1 * cbet2 * m0 * Math::pi());
1012 betscale = x < -real(0.01) ? sbet12a / x :
1013 -_f * Math::sq(cbet1) * Math::pi();
1014 lamscale = betscale / cbet1;
1015 y = lam12x / lamscale;
1016 }
1017
1018 if (y > -tol1_ && x > -1 - xthresh_) {
1019 // strip near cut
1020 // Need real(x) here to cast away the volatility of x for min/max
1021 if (_f >= 0) {
1022 salp1 = fmin(real(1), -x); calp1 = - sqrt(1 - Math::sq(salp1));
1023 } else {
1024 calp1 = fmax(real(x > -tol1_ ? 0 : -1), x);
1025 salp1 = sqrt(1 - Math::sq(calp1));
1026 }
1027 } else {
1028 // Estimate alp1, by solving the astroid problem.
1029 //
1030 // Could estimate alpha1 = theta + pi/2, directly, i.e.,
1031 // calp1 = y/k; salp1 = -x/(1+k); for _f >= 0
1032 // calp1 = x/(1+k); salp1 = -y/k; for _f < 0 (need to check)
1033 //
1034 // However, it's better to estimate omg12 from astroid and use
1035 // spherical formula to compute alp1. This reduces the mean number of
1036 // Newton iterations for astroid cases from 2.24 (min 0, max 6) to 2.12
1037 // (min 0 max 5). The changes in the number of iterations are as
1038 // follows:
1039 //
1040 // change percent
1041 // 1 5
1042 // 0 78
1043 // -1 16
1044 // -2 0.6
1045 // -3 0.04
1046 // -4 0.002
1047 //
1048 // The histogram of iterations is (m = number of iterations estimating
1049 // alp1 directly, n = number of iterations estimating via omg12, total
1050 // number of trials = 148605):
1051 //
1052 // iter m n
1053 // 0 148 186
1054 // 1 13046 13845
1055 // 2 93315 102225
1056 // 3 36189 32341
1057 // 4 5396 7
1058 // 5 455 1
1059 // 6 56 0
1060 //
1061 // Because omg12 is near pi, estimate work with omg12a = pi - omg12
1062 real k = Astroid(x, y);
1063 real
1064 omg12a = lamscale * ( _f >= 0 ? -x * k/(1 + k) : -y * (1 + k)/k );
1065 somg12 = sin(omg12a); comg12 = -cos(omg12a);
1066 // Update spherical estimate of alp1 using omg12 instead of lam12
1067 salp1 = cbet2 * somg12;
1068 calp1 = sbet12a - cbet2 * sbet1 * Math::sq(somg12) / (1 - comg12);
1069 }
1070 }
1071 // Sanity check on starting guess. Backwards check allows NaN through.
1072 if (!(salp1 <= 0))
1073 Math::norm(salp1, calp1);
1074 else {
1075 salp1 = 1; calp1 = 0;
1076 }
1077 return sig12;
1078 }
1079
1080 Math::real GeodesicExact::Lambda12(real sbet1, real cbet1, real dn1,
1081 real sbet2, real cbet2, real dn2,
1082 real salp1, real calp1,
1083 real slam120, real clam120,
1084 real& salp2, real& calp2,
1085 real& sig12,
1086 real& ssig1, real& csig1,
1087 real& ssig2, real& csig2,
1088 EllipticFunction& E,
1089 real& domg12,
1090 bool diffp, real& dlam12) const
1091 {
1092
1093 if (sbet1 == 0 && calp1 == 0)
1094 // Break degeneracy of equatorial line. This case has already been
1095 // handled.
1096 calp1 = -tiny_;
1097
1098 real
1099 // sin(alp1) * cos(bet1) = sin(alp0)
1100 salp0 = salp1 * cbet1,
1101 calp0 = hypot(calp1, salp1 * sbet1); // calp0 > 0
1102
1103 real somg1, comg1, somg2, comg2, somg12, comg12, cchi1, cchi2, lam12;
1104 // tan(bet1) = tan(sig1) * cos(alp1)
1105 // tan(omg1) = sin(alp0) * tan(sig1) = tan(omg1)=tan(alp1)*sin(bet1)
1106 ssig1 = sbet1; somg1 = salp0 * sbet1;
1107 csig1 = comg1 = calp1 * cbet1;
1108 // Without normalization we have schi1 = somg1.
1109 cchi1 = _f1 * dn1 * comg1;
1110 Math::norm(ssig1, csig1);
1111 // Math::norm(somg1, comg1); -- don't need to normalize!
1112 // Math::norm(schi1, cchi1); -- don't need to normalize!
1113
1114 // Enforce symmetries in the case abs(bet2) = -bet1. Need to be careful
1115 // about this case, since this can yield singularities in the Newton
1116 // iteration.
1117 // sin(alp2) * cos(bet2) = sin(alp0)
1118 salp2 = cbet2 != cbet1 ? salp0 / cbet2 : salp1;
1119 // calp2 = sqrt(1 - sq(salp2))
1120 // = sqrt(sq(calp0) - sq(sbet2)) / cbet2
1121 // and subst for calp0 and rearrange to give (choose positive sqrt
1122 // to give alp2 in [0, pi/2]).
1123 calp2 = cbet2 != cbet1 || fabs(sbet2) != -sbet1 ?
1124 sqrt(Math::sq(calp1 * cbet1) +
1125 (cbet1 < -sbet1 ?
1126 (cbet2 - cbet1) * (cbet1 + cbet2) :
1127 (sbet1 - sbet2) * (sbet1 + sbet2))) / cbet2 :
1128 fabs(calp1);
1129 // tan(bet2) = tan(sig2) * cos(alp2)
1130 // tan(omg2) = sin(alp0) * tan(sig2).
1131 ssig2 = sbet2; somg2 = salp0 * sbet2;
1132 csig2 = comg2 = calp2 * cbet2;
1133 // Without normalization we have schi2 = somg2.
1134 cchi2 = _f1 * dn2 * comg2;
1135 Math::norm(ssig2, csig2);
1136 // Math::norm(somg2, comg2); -- don't need to normalize!
1137 // Math::norm(schi2, cchi2); -- don't need to normalize!
1138
1139 // sig12 = sig2 - sig1, limit to [0, pi]
1140 sig12 = atan2(fmax(real(0), csig1 * ssig2 - ssig1 * csig2),
1141 csig1 * csig2 + ssig1 * ssig2);
1142
1143 // omg12 = omg2 - omg1, limit to [0, pi]
1144 somg12 = fmax(real(0), comg1 * somg2 - somg1 * comg2);
1145 comg12 = comg1 * comg2 + somg1 * somg2;
1146 real k2 = Math::sq(calp0) * _ep2;
1147 E.Reset(-k2, -_ep2, 1 + k2, 1 + _ep2);
1148 // chi12 = chi2 - chi1, limit to [0, pi]
1149 real
1150 schi12 = fmax(real(0), cchi1 * somg2 - somg1 * cchi2),
1151 cchi12 = cchi1 * cchi2 + somg1 * somg2;
1152 // eta = chi12 - lam120
1153 real eta = atan2(schi12 * clam120 - cchi12 * slam120,
1154 cchi12 * clam120 + schi12 * slam120);
1155 real deta12 = -_e2/_f1 * salp0 * E.H() / (Math::pi() / 2) *
1156 (sig12 + (E.deltaH(ssig2, csig2, dn2) - E.deltaH(ssig1, csig1, dn1)));
1157 lam12 = eta + deta12;
1158 // domg12 = deta12 + chi12 - omg12
1159 domg12 = deta12 + atan2(schi12 * comg12 - cchi12 * somg12,
1160 cchi12 * comg12 + schi12 * somg12);
1161 if (diffp) {
1162 if (calp2 == 0)
1163 dlam12 = - 2 * _f1 * dn1 / sbet1;
1164 else {
1165 real dummy;
1166 Lengths(E, sig12, ssig1, csig1, dn1, ssig2, csig2, dn2,
1167 cbet1, cbet2, REDUCEDLENGTH,
1168 dummy, dlam12, dummy, dummy, dummy);
1169 dlam12 *= _f1 / (calp2 * cbet2);
1170 }
1171 }
1172
1173 return lam12;
1174 }
1175
1176 Math::real GeodesicExact::I4Integrand::asinhsqrt(real x) {
1177 // return asinh(sqrt(x))/sqrt(x)
1178 return x == 0 ? 1 :
1179 (x > 0 ? asinh(sqrt(x))/sqrt(x) :
1180 asin(sqrt(-x))/sqrt(-x)); // NaNs end up here
1181 }
1182 Math::real GeodesicExact::I4Integrand::t(real x) {
1183 // This differs by from t as defined following Eq 61 in Karney (2013) by
1184 // the final subtraction of 1. This changes nothing since Eq 61 uses the
1185 // difference of two evaluations of t and improves the accuracy(?).
1186 // Group terms to minimize roundoff
1187 // with x = ep2, this is the same as
1188 // e2/(1-e2) + (atanh(e)/e - 1)
1189 return x + (sqrt(1 + x) * asinhsqrt(x) - 1);
1190 }
1191 Math::real GeodesicExact::I4Integrand::td(real x) {
1192 // d t(x) / dx
1193 return x == 0 ? 4/real(3) :
1194 // Group terms to minimize roundoff
1195 1 + (1 - asinhsqrt(x) / sqrt(1+x)) / (2*x);
1196 }
1197 // Math::real GeodesicExact::I4Integrand::Dt(real x, real y) {
1198 // // ( t(x) - t(y) ) / (x - y)
1199 // if (x == y) return td(x);
1200 // if (x * y <= 0) return ( t(x) - t(y) ) / (x - y);
1201 // real
1202 // sx = sqrt(fabs(x)), sx1 = sqrt(1 + x),
1203 // sy = sqrt(fabs(y)), sy1 = sqrt(1 + y),
1204 // z = (x - y) / (sx * sy1 + sy * sx1),
1205 // d1 = 2 * sx * sy,
1206 // d2 = 2 * (x * sy * sy1 + y * sx * sx1);
1207 // return x > 0 ?
1208 // ( 1 + (asinh(z)/z) / d1 - (asinh(sx) + asinh(sy)) / d2 ) :
1209 // // NaNs fall through to here
1210 // ( 1 - (asin (z)/z) / d1 - (asin (sx) + asin (sy)) / d2 );
1211 // }
1212 Math::real GeodesicExact::I4Integrand::DtX(real y) const {
1213 // idiot version:
1214 // return ( tX - t(y) ) / (X - y);
1215 if (X == y) return tdX;
1216 if (X * y <= 0) return ( tX - t(y) ) / (X - y);
1217 real
1218 sy = sqrt(fabs(y)), sy1 = sqrt(1 + y),
1219 z = (X - y) / (sX * sy1 + sy * sX1),
1220 d1 = 2 * sX * sy,
1221 d2 = 2 * (X * sy * sy1 + y * sXX1);
1222 return X > 0 ?
1223 ( 1 + (asinh(z)/z) / d1 - (asinhsX + asinh(sy)) / d2 ) :
1224 // NaNs fall through to here
1225 ( 1 - (asin (z)/z) / d1 - (asinhsX + asin (sy)) / d2 );
1226 }
1227 GeodesicExact::I4Integrand::I4Integrand(real ep2, real k2)
1228 : X( ep2 )
1229 , tX( t(X) )
1230 , tdX( td(X) )
1231 , _k2( k2 )
1232 {
1233 sX = sqrt(fabs(X)); // ep
1234 sX1 = sqrt(1 + X); // 1/(1-f)
1235 sXX1 = sX * sX1;
1236 asinhsX = X > 0 ? asinh(sX) : asin(sX); // atanh(e)
1237 }
1238 Math::real GeodesicExact::I4Integrand::operator()(real sig) const {
1239 real ssig = sin(sig);
1240 return - DtX(_k2 * Math::sq(ssig)) * ssig/2;
1241 }
1242
1243} // namespace GeographicLib
GeographicLib::Math::real real
Definition: GeodSolve.cpp:29
Header for GeographicLib::GeodesicExact class.
Header for GeographicLib::GeodesicLineExact class.
void reset(int N)
Definition: DST.cpp:24
void transform(std::function< real(real)> f, real F[]) const
Definition: DST.cpp:77
static real integral(real sinx, real cosx, const real F[], int N)
Definition: DST.cpp:110
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:77
static T degree()
Definition: Math.hpp:193
static T LatFix(T x)
Definition: Math.hpp:293
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:215
static T AngRound(T x)
Definition: Math.cpp:97
static T sq(T x)
Definition: Math.hpp:205
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:183
static T AngDiff(T x, T y, T &e)
Definition: Math.cpp:82
@ hd
degrees per half turn
Definition: Math.hpp:141
@ qd
degrees per quarter turn
Definition: Math.hpp:138
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)