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