GeographicLib 2.1.2
GeodesicExact.cpp
Go to the documentation of this file.
1/**
2 * \file GeodesicExact.cpp
3 * \brief Implementation for GeographicLib::GeodesicExact class
4 *
5 * Copyright (c) Charles Karney (2012-2022) <charles@karney.com> and licensed
6 * under the MIT/X11 License. For more information, see
7 * https://geographiclib.sourceforge.io/
8 *
9 * This is a reformulation of the geodesic problem. The notation is as
10 * follows:
11 * - at a general point (no suffix or 1 or 2 as suffix)
12 * - phi = latitude
13 * - beta = latitude on auxiliary sphere
14 * - omega = longitude on auxiliary sphere
15 * - lambda = longitude
16 * - alpha = azimuth of great circle
17 * - sigma = arc length along great circle
18 * - s = distance
19 * - tau = scaled distance (= sigma at multiples of pi/2)
20 * - at northwards equator crossing
21 * - beta = phi = 0
22 * - omega = lambda = 0
23 * - alpha = alpha0
24 * - sigma = s = 0
25 * - a 12 suffix means a difference, e.g., s12 = s2 - s1.
26 * - s and c prefixes mean sin and cos
27 **********************************************************************/
28
31
32#if defined(_MSC_VER)
33// Squelch warnings about potentially uninitialized local variables,
34// constant conditional and enum-float expressions and mixing enums
35# pragma warning (disable: 4701 4127 5055 5054)
36#endif
37
38namespace GeographicLib {
39
40 using namespace std;
41
43 : maxit2_(maxit1_ + Math::digits() + 10)
44 // Underflow guard. We require
45 // tiny_ * epsilon() > 0
46 // tiny_ + epsilon() == epsilon()
47 , tiny_(sqrt(numeric_limits<real>::min()))
48 , tol0_(numeric_limits<real>::epsilon())
49 // Increase multiplier in defn of tol1_ from 100 to 200 to fix inverse
50 // case 52.784459512564 0 -52.784459512563990912 179.634407464943777557
51 // which otherwise failed for Visual Studio 10 (Release and Debug)
52 , tol1_(200 * tol0_)
53 , tol2_(sqrt(tol0_))
54 , tolb_(tol0_) // 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
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;
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 //
559 // In fact, we will have sig12 > pi/2 for meridional geodesic which is
560 // not a shortest path.
561 if (sig12 < 1 || m12x >= 0) {
562 // Need at least 2, to handle 90 0 90 180
563 if (sig12 < 3 * tiny_ ||
564 // Prevent negative s12 or m12 for short lines
565 (sig12 < tol0_ && (s12x < 0 || m12x < 0)))
566 sig12 = m12x = s12x = 0;
567 m12x *= _b;
568 s12x *= _b;
569 a12 = sig12 / Math::degree();
570 } else
571 // m12 < 0, i.e., prolate and too close to anti-podal
572 meridian = false;
573 }
574
575 // somg12 == 2 marks that it needs to be calculated
576 real omg12 = 0, somg12 = 2, comg12 = 0;
577 if (!meridian &&
578 sbet1 == 0 && // and sbet2 == 0
579 (_f <= 0 || lon12s >= _f * Math::hd)) {
580
581 // Geodesic runs along equator
582 calp1 = calp2 = 0; salp1 = salp2 = 1;
583 s12x = _a * lam12;
584 sig12 = omg12 = lam12 / _f1;
585 m12x = _b * sin(sig12);
586 if (outmask & GEODESICSCALE)
587 M12 = M21 = cos(sig12);
588 a12 = lon12 / _f1;
589
590 } else if (!meridian) {
591
592 // Now point1 and point2 belong within a hemisphere bounded by a
593 // meridian and geodesic is neither meridional or equatorial.
594
595 // Figure a starting point for Newton's method
596 real dnm;
597 sig12 = InverseStart(E, sbet1, cbet1, dn1, sbet2, cbet2, dn2,
598 lam12, slam12, clam12,
599 salp1, calp1, salp2, calp2, dnm);
600
601 if (sig12 >= 0) {
602 // Short lines (InverseStart sets salp2, calp2, dnm)
603 s12x = sig12 * _b * dnm;
604 m12x = Math::sq(dnm) * _b * sin(sig12 / dnm);
605 if (outmask & GEODESICSCALE)
606 M12 = M21 = cos(sig12 / dnm);
607 a12 = sig12 / Math::degree();
608 omg12 = lam12 / (_f1 * dnm);
609 } else {
610
611 // Newton's method. This is a straightforward solution of f(alp1) =
612 // lambda12(alp1) - lam12 = 0 with one wrinkle. f(alp) has exactly one
613 // root in the interval (0, pi) and its derivative is positive at the
614 // root. Thus f(alp) is positive for alp > alp1 and negative for alp <
615 // alp1. During the course of the iteration, a range (alp1a, alp1b) is
616 // maintained which brackets the root and with each evaluation of
617 // f(alp) the range is shrunk, if possible. Newton's method is
618 // restarted whenever the derivative of f is negative (because the new
619 // value of alp1 is then further from the solution) or if the new
620 // estimate of alp1 lies outside (0,pi); in this case, the new starting
621 // guess is taken to be (alp1a + alp1b) / 2.
622 //
623 // initial values to suppress warnings (if loop is executed 0 times)
624 real ssig1 = 0, csig1 = 0, ssig2 = 0, csig2 = 0, domg12 = 0;
625 unsigned numit = 0;
626 // Bracketing range
627 real salp1a = tiny_, calp1a = 1, salp1b = tiny_, calp1b = -1;
628 for (bool tripn = false, tripb = false;; ++numit) {
629 // 1/4 meridian = 10e6 m and random input. max err is estimated max
630 // error in nm (checking solution of inverse problem by direct
631 // solution). iter is mean and sd of number of iterations
632 //
633 // max iter
634 // log2(b/a) err mean sd
635 // -7 387 5.33 3.68
636 // -6 345 5.19 3.43
637 // -5 269 5.00 3.05
638 // -4 210 4.76 2.44
639 // -3 115 4.55 1.87
640 // -2 69 4.35 1.38
641 // -1 36 4.05 1.03
642 // 0 15 0.01 0.13
643 // 1 25 5.10 1.53
644 // 2 96 5.61 2.09
645 // 3 318 6.02 2.74
646 // 4 985 6.24 3.22
647 // 5 2352 6.32 3.44
648 // 6 6008 6.30 3.45
649 // 7 19024 6.19 3.30
650 real dv;
651 real v = Lambda12(sbet1, cbet1, dn1, sbet2, cbet2, dn2, salp1, calp1,
652 slam12, clam12,
653 salp2, calp2, sig12, ssig1, csig1, ssig2, csig2,
654 E, domg12, numit < maxit1_, dv);
655 if (tripb ||
656 // Reversed test to allow escape with NaNs
657 !(fabs(v) >= (tripn ? 8 : 1) * tol0_) ||
658 // Enough bisections to get accurate result
659 numit == maxit2_)
660 break;
661 // Update bracketing values
662 if (v > 0 && (numit > maxit1_ || calp1/salp1 > calp1b/salp1b))
663 { salp1b = salp1; calp1b = calp1; }
664 else if (v < 0 && (numit > maxit1_ || calp1/salp1 < calp1a/salp1a))
665 { salp1a = salp1; calp1a = calp1; }
666 if (numit < maxit1_ && dv > 0) {
667 real
668 dalp1 = -v/dv;
669 // |dalp1| < pi test moved earlier because GEOGRAPHICLIB_PRECISION
670 // = 5 can result in dalp1 = 10^(10^8). Then sin(dalp1) takes ages
671 // (because of the need to do accurate range reduction).
672 if (fabs(dalp1) < Math::pi()) {
673 real
674 sdalp1 = sin(dalp1), cdalp1 = cos(dalp1),
675 nsalp1 = salp1 * cdalp1 + calp1 * sdalp1;
676 if (nsalp1 > 0) {
677 calp1 = calp1 * cdalp1 - salp1 * sdalp1;
678 salp1 = nsalp1;
679 Math::norm(salp1, calp1);
680 // In some regimes we don't get quadratic convergence because
681 // slope -> 0. So use convergence conditions based on epsilon
682 // instead of sqrt(epsilon).
683 tripn = fabs(v) <= 16 * tol0_;
684 continue;
685 }
686 }
687 }
688 // Either dv was not positive or updated value was outside legal
689 // range. Use the midpoint of the bracket as the next estimate.
690 // This mechanism is not needed for the WGS84 ellipsoid, but it does
691 // catch problems with more eccentric ellipsoids. Its efficacy is
692 // such for the WGS84 test set with the starting guess set to alp1 =
693 // 90deg:
694 // the WGS84 test set: mean = 5.21, sd = 3.93, max = 24
695 // WGS84 and random input: mean = 4.74, sd = 0.99
696 salp1 = (salp1a + salp1b)/2;
697 calp1 = (calp1a + calp1b)/2;
698 Math::norm(salp1, calp1);
699 tripn = false;
700 tripb = (fabs(salp1a - salp1) + (calp1a - calp1) < tolb_ ||
701 fabs(salp1 - salp1b) + (calp1 - calp1b) < tolb_);
702 }
703 {
704 real dummy;
705 Lengths(E, sig12, ssig1, csig1, dn1, ssig2, csig2, dn2,
706 cbet1, cbet2, outmask, s12x, m12x, dummy, M12, M21);
707 }
708 m12x *= _b;
709 s12x *= _b;
710 a12 = sig12 / Math::degree();
711 if (outmask & AREA) {
712 // omg12 = lam12 - domg12
713 real sdomg12 = sin(domg12), cdomg12 = cos(domg12);
714 somg12 = slam12 * cdomg12 - clam12 * sdomg12;
715 comg12 = clam12 * cdomg12 + slam12 * sdomg12;
716 }
717 }
718 }
719
720 if (outmask & DISTANCE)
721 s12 = real(0) + s12x; // Convert -0 to 0
722
723 if (outmask & REDUCEDLENGTH)
724 m12 = real(0) + m12x; // Convert -0 to 0
725
726 if (outmask & AREA) {
727 real
728 // From Lambda12: sin(alp1) * cos(bet1) = sin(alp0)
729 salp0 = salp1 * cbet1,
730 calp0 = hypot(calp1, salp1 * sbet1); // calp0 > 0
731 real alp12,
732 // Multiplier = a^2 * e^2 * cos(alpha0) * sin(alpha0).
733 A4 = Math::sq(_a) * calp0 * salp0 * _e2;
734 if (A4 != 0) {
735 real
736 k2 = Math::sq(calp0) * _ep2,
737 // From Lambda12: tan(bet) = tan(sig) * cos(alp)
738 ssig1 = sbet1, csig1 = calp1 * cbet1,
739 ssig2 = sbet2, csig2 = calp2 * cbet2;
740 Math::norm(ssig1, csig1);
741 Math::norm(ssig2, csig2);
742 I4Integrand i4(_ep2, k2);
743 vector<real> C4a(_nC4);
744 _fft.transform(i4, C4a.data());
745 S12 = A4 * DST::integral(ssig1, csig1, ssig2, csig2, C4a.data(), _nC4);
746 } else
747 // Avoid problems with indeterminate sig1, sig2 on equator
748 S12 = 0;
749
750 if (!meridian && somg12 == 2) {
751 somg12 = sin(omg12); comg12 = cos(omg12);
752 }
753
754 if (!meridian &&
755 // omg12 < 3/4 * pi
756 comg12 > -real(0.7071) && // Long difference not too big
757 sbet2 - sbet1 < real(1.75)) { // Lat difference not too big
758 // Use tan(Gamma/2) = tan(omg12/2)
759 // * (tan(bet1/2)+tan(bet2/2))/(1+tan(bet1/2)*tan(bet2/2))
760 // with tan(x/2) = sin(x)/(1+cos(x))
761 real domg12 = 1 + comg12, dbet1 = 1 + cbet1, dbet2 = 1 + cbet2;
762 alp12 = 2 * atan2( somg12 * ( sbet1 * dbet2 + sbet2 * dbet1 ),
763 domg12 * ( sbet1 * sbet2 + dbet1 * dbet2 ) );
764 } else {
765 // alp12 = alp2 - alp1, used in atan2 so no need to normalize
766 real
767 salp12 = salp2 * calp1 - calp2 * salp1,
768 calp12 = calp2 * calp1 + salp2 * salp1;
769 // The right thing appears to happen if alp1 = +/-180 and alp2 = 0, viz
770 // salp12 = -0 and alp12 = -180. However this depends on the sign
771 // being attached to 0 correctly. The following ensures the correct
772 // behavior.
773 if (salp12 == 0 && calp12 < 0) {
774 salp12 = tiny_ * calp1;
775 calp12 = -1;
776 }
777 alp12 = atan2(salp12, calp12);
778 }
779 S12 += _c2 * alp12;
780 S12 *= swapp * lonsign * latsign;
781 // Convert -0 to 0
782 S12 += 0;
783 }
784
785 // Convert calp, salp to azimuth accounting for lonsign, swapp, latsign.
786 if (swapp < 0) {
787 swap(salp1, salp2);
788 swap(calp1, calp2);
789 if (outmask & GEODESICSCALE)
790 swap(M12, M21);
791 }
792
793 salp1 *= swapp * lonsign; calp1 *= swapp * latsign;
794 salp2 *= swapp * lonsign; calp2 *= swapp * latsign;
795
796 // Returned value in [0, 180]
797 return a12;
798 }
799
800 Math::real GeodesicExact::GenInverse(real lat1, real lon1,
801 real lat2, real lon2,
802 unsigned outmask,
803 real& s12, real& azi1, real& azi2,
804 real& m12, real& M12, real& M21,
805 real& S12) const {
806 outmask &= OUT_MASK;
807 real salp1, calp1, salp2, calp2,
808 a12 = GenInverse(lat1, lon1, lat2, lon2,
809 outmask, s12, salp1, calp1, salp2, calp2,
810 m12, M12, M21, S12);
811 if (outmask & AZIMUTH) {
812 azi1 = Math::atan2d(salp1, calp1);
813 azi2 = Math::atan2d(salp2, calp2);
814 }
815 return a12;
816 }
817
819 real lat2, real lon2,
820 unsigned caps) const {
821 real t, salp1, calp1, salp2, calp2,
822 a12 = GenInverse(lat1, lon1, lat2, lon2,
823 // No need to specify AZIMUTH here
824 0u, t, salp1, calp1, salp2, calp2,
825 t, t, t, t),
826 azi1 = Math::atan2d(salp1, calp1);
827 // Ensure that a12 can be converted to a distance
828 if (caps & (OUT_MASK & DISTANCE_IN)) caps |= DISTANCE;
829 return GeodesicLineExact(*this, lat1, lon1, azi1, salp1, calp1, caps,
830 true, a12);
831 }
832
833 void GeodesicExact::Lengths(const EllipticFunction& E,
834 real sig12,
835 real ssig1, real csig1, real dn1,
836 real ssig2, real csig2, real dn2,
837 real cbet1, real cbet2, unsigned outmask,
838 real& s12b, real& m12b, real& m0,
839 real& M12, real& M21) const {
840 // Return m12b = (reduced length)/_b; also calculate s12b = distance/_b,
841 // and m0 = coefficient of secular term in expression for reduced length.
842
843 outmask &= OUT_ALL;
844 // outmask & DISTANCE: set s12b
845 // outmask & REDUCEDLENGTH: set m12b & m0
846 // outmask & GEODESICSCALE: set M12 & M21
847
848 // It's OK to have repeated dummy arguments,
849 // e.g., s12b = m0 = M12 = M21 = dummy
850
851 if (outmask & DISTANCE)
852 // Missing a factor of _b
853 s12b = E.E() / (Math::pi() / 2) *
854 (sig12 + (E.deltaE(ssig2, csig2, dn2) - E.deltaE(ssig1, csig1, dn1)));
855 if (outmask & (REDUCEDLENGTH | GEODESICSCALE)) {
856 real
857 m0x = - E.k2() * E.D() / (Math::pi() / 2),
858 J12 = m0x *
859 (sig12 + (E.deltaD(ssig2, csig2, dn2) - E.deltaD(ssig1, csig1, dn1)));
860 if (outmask & REDUCEDLENGTH) {
861 m0 = m0x;
862 // Missing a factor of _b. Add parens around (csig1 * ssig2) and
863 // (ssig1 * csig2) to ensure accurate cancellation in the case of
864 // coincident points.
865 m12b = dn2 * (csig1 * ssig2) - dn1 * (ssig1 * csig2) -
866 csig1 * csig2 * J12;
867 }
868 if (outmask & GEODESICSCALE) {
869 real csig12 = csig1 * csig2 + ssig1 * ssig2;
870 real t = _ep2 * (cbet1 - cbet2) * (cbet1 + cbet2) / (dn1 + dn2);
871 M12 = csig12 + (t * ssig2 - csig2 * J12) * ssig1 / dn1;
872 M21 = csig12 - (t * ssig1 - csig1 * J12) * ssig2 / dn2;
873 }
874 }
875 }
876
877 Math::real GeodesicExact::Astroid(real x, real y) {
878 // Solve k^4+2*k^3-(x^2+y^2-1)*k^2-2*y^2*k-y^2 = 0 for positive root k.
879 // This solution is adapted from Geocentric::Reverse.
880 real k;
881 real
882 p = Math::sq(x),
883 q = Math::sq(y),
884 r = (p + q - 1) / 6;
885 if ( !(q == 0 && r <= 0) ) {
886 real
887 // Avoid possible division by zero when r = 0 by multiplying equations
888 // for s and t by r^3 and r, resp.
889 S = p * q / 4, // S = r^3 * s
890 r2 = Math::sq(r),
891 r3 = r * r2,
892 // The discriminant of the quadratic equation for T3. This is zero on
893 // the evolute curve p^(1/3)+q^(1/3) = 1
894 disc = S * (S + 2 * r3);
895 real u = r;
896 if (disc >= 0) {
897 real T3 = S + r3;
898 // Pick the sign on the sqrt to maximize abs(T3). This minimizes loss
899 // of precision due to cancellation. The result is unchanged because
900 // of the way the T is used in definition of u.
901 T3 += T3 < 0 ? -sqrt(disc) : sqrt(disc); // T3 = (r * t)^3
902 // N.B. cbrt always returns the real root. cbrt(-8) = -2.
903 real T = cbrt(T3); // T = r * t
904 // T can be zero; but then r2 / T -> 0.
905 u += T + (T != 0 ? r2 / T : 0);
906 } else {
907 // T is complex, but the way u is defined the result is real.
908 real ang = atan2(sqrt(-disc), -(S + r3));
909 // There are three possible cube roots. We choose the root which
910 // avoids cancellation. Note that disc < 0 implies that r < 0.
911 u += 2 * r * cos(ang / 3);
912 }
913 real
914 v = sqrt(Math::sq(u) + q), // guaranteed positive
915 // Avoid loss of accuracy when u < 0.
916 uv = u < 0 ? q / (v - u) : u + v, // u+v, guaranteed positive
917 w = (uv - q) / (2 * v); // positive?
918 // Rearrange expression for k to avoid loss of accuracy due to
919 // subtraction. Division by 0 not possible because uv > 0, w >= 0.
920 k = uv / (sqrt(uv + Math::sq(w)) + w); // guaranteed positive
921 } else { // q == 0 && r <= 0
922 // y = 0 with |x| <= 1. Handle this case directly.
923 // for y small, positive root is k = abs(y)/sqrt(1-x^2)
924 k = 0;
925 }
926 return k;
927 }
928
929 Math::real GeodesicExact::InverseStart(EllipticFunction& E,
930 real sbet1, real cbet1, real dn1,
931 real sbet2, real cbet2, real dn2,
932 real lam12, real slam12, real clam12,
933 real& salp1, real& calp1,
934 // Only updated if return val >= 0
935 real& salp2, real& calp2,
936 // Only updated for short lines
937 real& dnm) const {
938 // Return a starting point for Newton's method in salp1 and calp1 (function
939 // value is -1). If Newton's method doesn't need to be used, return also
940 // salp2 and calp2 and function value is sig12.
941 real
942 sig12 = -1, // Return value
943 // bet12 = bet2 - bet1 in [0, pi); bet12a = bet2 + bet1 in (-pi, 0]
944 sbet12 = sbet2 * cbet1 - cbet2 * sbet1,
945 cbet12 = cbet2 * cbet1 + sbet2 * sbet1;
946 real sbet12a = sbet2 * cbet1 + cbet2 * sbet1;
947 bool shortline = cbet12 >= 0 && sbet12 < real(0.5) &&
948 cbet2 * lam12 < real(0.5);
949 real somg12, comg12;
950 if (shortline) {
951 real sbetm2 = Math::sq(sbet1 + sbet2);
952 // sin((bet1+bet2)/2)^2
953 // = (sbet1 + sbet2)^2 / ((sbet1 + sbet2)^2 + (cbet1 + cbet2)^2)
954 sbetm2 /= sbetm2 + Math::sq(cbet1 + cbet2);
955 dnm = sqrt(1 + _ep2 * sbetm2);
956 real omg12 = lam12 / (_f1 * dnm);
957 somg12 = sin(omg12); comg12 = cos(omg12);
958 } else {
959 somg12 = slam12; comg12 = clam12;
960 }
961
962 salp1 = cbet2 * somg12;
963 calp1 = comg12 >= 0 ?
964 sbet12 + cbet2 * sbet1 * Math::sq(somg12) / (1 + comg12) :
965 sbet12a - cbet2 * sbet1 * Math::sq(somg12) / (1 - comg12);
966
967 real
968 ssig12 = hypot(salp1, calp1),
969 csig12 = sbet1 * sbet2 + cbet1 * cbet2 * comg12;
970
971 if (shortline && ssig12 < _etol2) {
972 // really short lines
973 salp2 = cbet1 * somg12;
974 calp2 = sbet12 - cbet1 * sbet2 *
975 (comg12 >= 0 ? Math::sq(somg12) / (1 + comg12) : 1 - comg12);
976 Math::norm(salp2, calp2);
977 // Set return value
978 sig12 = atan2(ssig12, csig12);
979 } else if (fabs(_n) > real(0.1) || // Skip astroid calc if too eccentric
980 csig12 >= 0 ||
981 ssig12 >= 6 * fabs(_n) * Math::pi() * Math::sq(cbet1)) {
982 // Nothing to do, zeroth order spherical approximation is OK
983 } else {
984 // Scale lam12 and bet2 to x, y coordinate system where antipodal point
985 // is at origin and singular point is at y = 0, x = -1.
986 real x, y, lamscale, betscale;
987 real lam12x = atan2(-slam12, -clam12); // lam12 - pi
988 if (_f >= 0) { // In fact f == 0 does not get here
989 // x = dlong, y = dlat
990 {
991 real k2 = Math::sq(sbet1) * _ep2;
992 E.Reset(-k2, -_ep2, 1 + k2, 1 + _ep2);
993 lamscale = _e2/_f1 * cbet1 * 2 * E.H();
994 }
995 betscale = lamscale * cbet1;
996
997 x = lam12x / lamscale;
998 y = sbet12a / betscale;
999 } else { // _f < 0
1000 // x = dlat, y = dlong
1001 real
1002 cbet12a = cbet2 * cbet1 - sbet2 * sbet1,
1003 bet12a = atan2(sbet12a, cbet12a);
1004 real m12b, m0, dummy;
1005 // In the case of lon12 = 180, this repeats a calculation made in
1006 // Inverse.
1007 Lengths(E, Math::pi() + bet12a,
1008 sbet1, -cbet1, dn1, sbet2, cbet2, dn2,
1009 cbet1, cbet2, REDUCEDLENGTH, dummy, m12b, m0, dummy, dummy);
1010 x = -1 + m12b / (cbet1 * cbet2 * m0 * Math::pi());
1011 betscale = x < -real(0.01) ? sbet12a / x :
1012 -_f * Math::sq(cbet1) * Math::pi();
1013 lamscale = betscale / cbet1;
1014 y = lam12x / lamscale;
1015 }
1016
1017 if (y > -tol1_ && x > -1 - xthresh_) {
1018 // strip near cut
1019 // Need real(x) here to cast away the volatility of x for min/max
1020 if (_f >= 0) {
1021 salp1 = fmin(real(1), -x); calp1 = - sqrt(1 - Math::sq(salp1));
1022 } else {
1023 calp1 = fmax(real(x > -tol1_ ? 0 : -1), x);
1024 salp1 = sqrt(1 - Math::sq(calp1));
1025 }
1026 } else {
1027 // Estimate alp1, by solving the astroid problem.
1028 //
1029 // Could estimate alpha1 = theta + pi/2, directly, i.e.,
1030 // calp1 = y/k; salp1 = -x/(1+k); for _f >= 0
1031 // calp1 = x/(1+k); salp1 = -y/k; for _f < 0 (need to check)
1032 //
1033 // However, it's better to estimate omg12 from astroid and use
1034 // spherical formula to compute alp1. This reduces the mean number of
1035 // Newton iterations for astroid cases from 2.24 (min 0, max 6) to 2.12
1036 // (min 0 max 5). The changes in the number of iterations are as
1037 // follows:
1038 //
1039 // change percent
1040 // 1 5
1041 // 0 78
1042 // -1 16
1043 // -2 0.6
1044 // -3 0.04
1045 // -4 0.002
1046 //
1047 // The histogram of iterations is (m = number of iterations estimating
1048 // alp1 directly, n = number of iterations estimating via omg12, total
1049 // number of trials = 148605):
1050 //
1051 // iter m n
1052 // 0 148 186
1053 // 1 13046 13845
1054 // 2 93315 102225
1055 // 3 36189 32341
1056 // 4 5396 7
1057 // 5 455 1
1058 // 6 56 0
1059 //
1060 // Because omg12 is near pi, estimate work with omg12a = pi - omg12
1061 real k = Astroid(x, y);
1062 real
1063 omg12a = lamscale * ( _f >= 0 ? -x * k/(1 + k) : -y * (1 + k)/k );
1064 somg12 = sin(omg12a); comg12 = -cos(omg12a);
1065 // Update spherical estimate of alp1 using omg12 instead of lam12
1066 salp1 = cbet2 * somg12;
1067 calp1 = sbet12a - cbet2 * sbet1 * Math::sq(somg12) / (1 - comg12);
1068 }
1069 }
1070 // Sanity check on starting guess. Backwards check allows NaN through.
1071 if (!(salp1 <= 0))
1072 Math::norm(salp1, calp1);
1073 else {
1074 salp1 = 1; calp1 = 0;
1075 }
1076 return sig12;
1077 }
1078
1079 Math::real GeodesicExact::Lambda12(real sbet1, real cbet1, real dn1,
1080 real sbet2, real cbet2, real dn2,
1081 real salp1, real calp1,
1082 real slam120, real clam120,
1083 real& salp2, real& calp2,
1084 real& sig12,
1085 real& ssig1, real& csig1,
1086 real& ssig2, real& csig2,
1087 EllipticFunction& E,
1088 real& domg12,
1089 bool diffp, real& dlam12) const
1090 {
1091
1092 if (sbet1 == 0 && calp1 == 0)
1093 // Break degeneracy of equatorial line. This case has already been
1094 // handled.
1095 calp1 = -tiny_;
1096
1097 real
1098 // sin(alp1) * cos(bet1) = sin(alp0)
1099 salp0 = salp1 * cbet1,
1100 calp0 = hypot(calp1, salp1 * sbet1); // calp0 > 0
1101
1102 real somg1, comg1, somg2, comg2, somg12, comg12, cchi1, cchi2, lam12;
1103 // tan(bet1) = tan(sig1) * cos(alp1)
1104 // tan(omg1) = sin(alp0) * tan(sig1) = tan(omg1)=tan(alp1)*sin(bet1)
1105 ssig1 = sbet1; somg1 = salp0 * sbet1;
1106 csig1 = comg1 = calp1 * cbet1;
1107 // Without normalization we have schi1 = somg1.
1108 cchi1 = _f1 * dn1 * comg1;
1109 Math::norm(ssig1, csig1);
1110 // Math::norm(somg1, comg1); -- don't need to normalize!
1111 // Math::norm(schi1, cchi1); -- don't need to normalize!
1112
1113 // Enforce symmetries in the case abs(bet2) = -bet1. Need to be careful
1114 // about this case, since this can yield singularities in the Newton
1115 // iteration.
1116 // sin(alp2) * cos(bet2) = sin(alp0)
1117 salp2 = cbet2 != cbet1 ? salp0 / cbet2 : salp1;
1118 // calp2 = sqrt(1 - sq(salp2))
1119 // = sqrt(sq(calp0) - sq(sbet2)) / cbet2
1120 // and subst for calp0 and rearrange to give (choose positive sqrt
1121 // to give alp2 in [0, pi/2]).
1122 calp2 = cbet2 != cbet1 || fabs(sbet2) != -sbet1 ?
1123 sqrt(Math::sq(calp1 * cbet1) +
1124 (cbet1 < -sbet1 ?
1125 (cbet2 - cbet1) * (cbet1 + cbet2) :
1126 (sbet1 - sbet2) * (sbet1 + sbet2))) / cbet2 :
1127 fabs(calp1);
1128 // tan(bet2) = tan(sig2) * cos(alp2)
1129 // tan(omg2) = sin(alp0) * tan(sig2).
1130 ssig2 = sbet2; somg2 = salp0 * sbet2;
1131 csig2 = comg2 = calp2 * cbet2;
1132 // Without normalization we have schi2 = somg2.
1133 cchi2 = _f1 * dn2 * comg2;
1134 Math::norm(ssig2, csig2);
1135 // Math::norm(somg2, comg2); -- don't need to normalize!
1136 // Math::norm(schi2, cchi2); -- don't need to normalize!
1137
1138 // sig12 = sig2 - sig1, limit to [0, pi]
1139 sig12 = atan2(fmax(real(0), csig1 * ssig2 - ssig1 * csig2),
1140 csig1 * csig2 + ssig1 * ssig2);
1141
1142 // omg12 = omg2 - omg1, limit to [0, pi]
1143 somg12 = fmax(real(0), comg1 * somg2 - somg1 * comg2);
1144 comg12 = comg1 * comg2 + somg1 * somg2;
1145 real k2 = Math::sq(calp0) * _ep2;
1146 E.Reset(-k2, -_ep2, 1 + k2, 1 + _ep2);
1147 // chi12 = chi2 - chi1, limit to [0, pi]
1148 real
1149 schi12 = fmax(real(0), cchi1 * somg2 - somg1 * cchi2),
1150 cchi12 = cchi1 * cchi2 + somg1 * somg2;
1151 // eta = chi12 - lam120
1152 real eta = atan2(schi12 * clam120 - cchi12 * slam120,
1153 cchi12 * clam120 + schi12 * slam120);
1154 real deta12 = -_e2/_f1 * salp0 * E.H() / (Math::pi() / 2) *
1155 (sig12 + (E.deltaH(ssig2, csig2, dn2) - E.deltaH(ssig1, csig1, dn1)));
1156 lam12 = eta + deta12;
1157 // domg12 = deta12 + chi12 - omg12
1158 domg12 = deta12 + atan2(schi12 * comg12 - cchi12 * somg12,
1159 cchi12 * comg12 + schi12 * somg12);
1160 if (diffp) {
1161 if (calp2 == 0)
1162 dlam12 = - 2 * _f1 * dn1 / sbet1;
1163 else {
1164 real dummy;
1165 Lengths(E, sig12, ssig1, csig1, dn1, ssig2, csig2, dn2,
1166 cbet1, cbet2, REDUCEDLENGTH,
1167 dummy, dlam12, dummy, dummy, dummy);
1168 dlam12 *= _f1 / (calp2 * cbet2);
1169 }
1170 }
1171
1172 return lam12;
1173 }
1174
1175 Math::real GeodesicExact::I4Integrand::asinhsqrt(real x) {
1176 // return asinh(sqrt(x))/sqrt(x)
1177 using std::sqrt; using std::asinh; using std::asin;
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 using std::sqrt;
1187 // Group terms to minimize roundoff
1188 // with x = ep2, this is the same as
1189 // e2/(1-e2) + (atanh(e)/e - 1)
1190 return x + (sqrt(1 + x) * asinhsqrt(x) - 1);
1191 }
1192 Math::real GeodesicExact::I4Integrand::td(real x) {
1193 // d t(x) / dx
1194 using std::sqrt;
1195 return x == 0 ? 4/real(3) :
1196 // Group terms to minimize roundoff
1197 1 + (1 - asinhsqrt(x) / sqrt(1+x)) / (2*x);
1198 }
1199 // Math::real GeodesicExact::I4Integrand::Dt(real x, real y) {
1200 // // ( t(x) - t(y) ) / (x - y)
1201 // using std::sqrt; using std::fabs; using std::asinh; using std::asin;
1202 // if (x == y) return td(x);
1203 // if (x * y <= 0) return ( t(x) - t(y) ) / (x - y);
1204 // real
1205 // sx = sqrt(fabs(x)), sx1 = sqrt(1 + x),
1206 // sy = sqrt(fabs(y)), sy1 = sqrt(1 + y),
1207 // z = (x - y) / (sx * sy1 + sy * sx1),
1208 // d1 = 2 * sx * sy,
1209 // d2 = 2 * (x * sy * sy1 + y * sx * sx1);
1210 // return x > 0 ?
1211 // ( 1 + (asinh(z)/z) / d1 - (asinh(sx) + asinh(sy)) / d2 ) :
1212 // // NaNs fall through to here
1213 // ( 1 - (asin (z)/z) / d1 - (asin (sx) + asin (sy)) / d2 );
1214 // }
1215 Math::real GeodesicExact::I4Integrand::DtX(real y) const {
1216 // idiot version:
1217 // return ( tX - t(y) ) / (X - y);
1218 using std::sqrt; using std::fabs; using std::asinh; using std::asin;
1219 if (X == y) return tdX;
1220 if (X * y <= 0) return ( tX - t(y) ) / (X - y);
1221 real
1222 sy = sqrt(fabs(y)), sy1 = sqrt(1 + y),
1223 z = (X - y) / (sX * sy1 + sy * sX1),
1224 d1 = 2 * sX * sy,
1225 d2 = 2 * (X * sy * sy1 + y * sXX1);
1226 return X > 0 ?
1227 ( 1 + (asinh(z)/z) / d1 - (asinhsX + asinh(sy)) / d2 ) :
1228 // NaNs fall through to here
1229 ( 1 - (asin (z)/z) / d1 - (asinhsX + asin (sy)) / d2 );
1230 }
1231 GeodesicExact::I4Integrand::I4Integrand(real ep2, real k2)
1232 : X( ep2 )
1233 , tX( t(X) )
1234 , tdX( td(X) )
1235 , _k2( k2 )
1236 {
1237 using std::fabs; using std::sqrt; using std::asinh; using std::asin;
1238 sX = sqrt(fabs(X)); // ep
1239 sX1 = sqrt(1 + X); // 1/(1-f)
1240 sXX1 = sX * sX1;
1241 asinhsX = X > 0 ? asinh(sX) : asin(sX); // atanh(e)
1242 }
1243 Math::real GeodesicExact::I4Integrand::operator()(real sig) const {
1244 using std::sin;
1245 real ssig = sin(sig);
1246 return - DtX(_k2 * Math::sq(ssig)) * ssig/2;
1247 }
1248
1249} // namespace GeographicLib
GeographicLib::Math::real real
Definition: GeodSolve.cpp:31
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:76
static T degree()
Definition: Math.hpp:200
static T LatFix(T x)
Definition: Math.hpp:300
static void sincosd(T x, T &sinx, T &cosx)
Definition: Math.cpp:106
static T atan2d(T y, T x)
Definition: Math.cpp:183
static void norm(T &x, T &y)
Definition: Math.hpp:222
static T AngRound(T x)
Definition: Math.cpp:97
static T sq(T x)
Definition: Math.hpp:212
static T AngNormalize(T x)
Definition: Math.cpp:71
static int digits()
Definition: Math.cpp:26
static void sincosde(T x, T t, T &sinx, T &cosx)
Definition: Math.cpp:126
static T pi()
Definition: Math.hpp:190
static T AngDiff(T x, T y, T &e)
Definition: Math.cpp:82
@ hd
degrees per half turn
Definition: Math.hpp:144
@ qd
degrees per quarter turn
Definition: Math.hpp:141
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
void swap(GeographicLib::NearestNeighbor< dist_t, pos_t, distfun_t > &a, GeographicLib::NearestNeighbor< dist_t, pos_t, distfun_t > &b)