GeographicLib 2.3

GeodesicLine.cpp
Go to the documentation of this file.
1/**
2 * \file GeodesicLine.cpp
3 * \brief Implementation for GeographicLib::GeodesicLine class
4 *
5 * Copyright (c) Charles Karney (2009-2023) <karney@alum.mit.edu> and licensed
6 * under the MIT/X11 License. For more information, see
7 * https://geographiclib.sourceforge.io/
8 *
9 * This is a reformulation of the geodesic problem. The notation is as
10 * follows:
11 * - at a general point (no suffix or 1 or 2 as suffix)
12 * - phi = latitude
13 * - beta = latitude on auxiliary sphere
14 * - omega = longitude on auxiliary sphere
15 * - lambda = longitude
16 * - alpha = azimuth of great circle
17 * - sigma = arc length along great circle
18 * - s = distance
19 * - tau = scaled distance (= sigma at multiples of pi/2)
20 * - at northwards equator crossing
21 * - beta = phi = 0
22 * - omega = lambda = 0
23 * - alpha = alpha0
24 * - sigma = s = 0
25 * - a 12 suffix means a difference, e.g., s12 = s2 - s1.
26 * - s and c prefixes mean sin and cos
27 **********************************************************************/
28
29#include <GeographicLib/GeodesicLine.hpp>
30
31#if defined(_MSC_VER)
32// Squelch warnings about mixing enums
33# pragma warning (disable: 5054)
34#endif
35
36 namespace GeographicLib {
37
38 using namespace std;
39
40 void GeodesicLine::LineInit(const Geodesic& g,
41 real lat1, real lon1,
42 real azi1, real salp1, real calp1,
43 unsigned caps) {
44 tiny_ = g.tiny_;
45 _lat1 = Math::LatFix(lat1);
46 _lon1 = lon1;
47 _azi1 = azi1;
48 _salp1 = salp1;
49 _calp1 = calp1;
50 _a = g._a;
51 _f = g._f;
52 _b = g._b;
53 _c2 = g._c2;
54 _f1 = g._f1;
55 // Always allow latitude and azimuth and unrolling of longitude
56 _caps = caps | LATITUDE | AZIMUTH | LONG_UNROLL;
57
58 real cbet1, sbet1;
59 Math::sincosd(Math::AngRound(_lat1), sbet1, cbet1); sbet1 *= _f1;
60 // Ensure cbet1 = +epsilon at poles
61 Math::norm(sbet1, cbet1); cbet1 = fmax(tiny_, cbet1);
62 _dn1 = sqrt(1 + g._ep2 * Math::sq(sbet1));
63
64 // Evaluate alp0 from sin(alp1) * cos(bet1) = sin(alp0),
65 _salp0 = _salp1 * cbet1; // alp0 in [0, pi/2 - |bet1|]
66 // Alt: calp0 = hypot(sbet1, calp1 * cbet1). The following
67 // is slightly better (consider the case salp1 = 0).
68 _calp0 = hypot(_calp1, _salp1 * sbet1);
69 // Evaluate sig with tan(bet1) = tan(sig1) * cos(alp1).
70 // sig = 0 is nearest northward crossing of equator.
71 // With bet1 = 0, alp1 = pi/2, we have sig1 = 0 (equatorial line).
72 // With bet1 = pi/2, alp1 = -pi, sig1 = pi/2
73 // With bet1 = -pi/2, alp1 = 0 , sig1 = -pi/2
74 // Evaluate omg1 with tan(omg1) = sin(alp0) * tan(sig1).
75 // With alp0 in (0, pi/2], quadrants for sig and omg coincide.
76 // No atan2(0,0) ambiguity at poles since cbet1 = +epsilon.
77 // With alp0 = 0, omg1 = 0 for alp1 = 0, omg1 = pi for alp1 = pi.
78 _ssig1 = sbet1; _somg1 = _salp0 * sbet1;
79 _csig1 = _comg1 = sbet1 != 0 || _calp1 != 0 ? cbet1 * _calp1 : 1;
80 Math::norm(_ssig1, _csig1); // sig1 in (-pi, pi]
81 // Math::norm(_somg1, _comg1); -- don't need to normalize!
82
83 _a13 = _s13 = Math::NaN();
84 _exact = g._exact;
85 if (_exact) {
86 _lineexact.LineInit(g._geodexact, lat1, lon1, azi1, salp1, calp1, caps);
87 return;
88 }
89
90 _k2 = Math::sq(_calp0) * g._ep2;
91 real eps = _k2 / (2 * (1 + sqrt(1 + _k2)) + _k2);
92
93 if (_caps & CAP_C1) {
94 _aA1m1 = Geodesic::A1m1f(eps);
95 Geodesic::C1f(eps, _cC1a);
96 _bB11 = Geodesic::SinCosSeries(true, _ssig1, _csig1, _cC1a, nC1_);
97 real s = sin(_bB11), c = cos(_bB11);
98 // tau1 = sig1 + B11
99 _stau1 = _ssig1 * c + _csig1 * s;
100 _ctau1 = _csig1 * c - _ssig1 * s;
101 // Not necessary because C1pa reverts C1a
102 // _bB11 = -SinCosSeries(true, _stau1, _ctau1, _cC1pa, nC1p_);
103 }
104
105 if (_caps & CAP_C1p)
106 Geodesic::C1pf(eps, _cC1pa);
107
108 if (_caps & CAP_C2) {
109 _aA2m1 = Geodesic::A2m1f(eps);
110 Geodesic::C2f(eps, _cC2a);
111 _bB21 = Geodesic::SinCosSeries(true, _ssig1, _csig1, _cC2a, nC2_);
112 }
113
114 if (_caps & CAP_C3) {
115 g.C3f(eps, _cC3a);
116 _aA3c = -_f * _salp0 * g.A3f(eps);
117 _bB31 = Geodesic::SinCosSeries(true, _ssig1, _csig1, _cC3a, nC3_-1);
118 }
119
120 if (_caps & CAP_C4) {
121 g.C4f(eps, _cC4a);
122 // Multiplier = a^2 * e^2 * cos(alpha0) * sin(alpha0)
123 _aA4 = Math::sq(_a) * _calp0 * _salp0 * g._e2;
124 _bB41 = Geodesic::SinCosSeries(false, _ssig1, _csig1, _cC4a, nC4_);
125 }
126
127 }
128
129 GeodesicLine::GeodesicLine(const Geodesic& g,
130 real lat1, real lon1, real azi1,
131 unsigned caps) {
132 azi1 = Math::AngNormalize(azi1);
133 real salp1, calp1;
134 // Guard against underflow in salp0. Also -0 is converted to +0.
135 Math::sincosd(Math::AngRound(azi1), salp1, calp1);
136 LineInit(g, lat1, lon1, azi1, salp1, calp1, caps);
137 }
138
139 GeodesicLine::GeodesicLine(const Geodesic& g,
140 real lat1, real lon1,
141 real azi1, real salp1, real calp1,
142 unsigned caps, bool arcmode, real s13_a13) {
143 LineInit(g, lat1, lon1, azi1, salp1, calp1, caps);
144 GenSetDistance(arcmode, s13_a13);
145 }
146
147 Math::real GeodesicLine::GenPosition(bool arcmode, real s12_a12,
148 unsigned outmask,
149 real& lat2, real& lon2, real& azi2,
150 real& s12, real& m12,
151 real& M12, real& M21,
152 real& S12) const {
153 if (_exact)
154 return _lineexact.GenPosition(arcmode, s12_a12, outmask,
155 lat2, lon2, azi2,
156 s12, m12, M12, M21, S12);
157 outmask &= _caps & OUT_MASK;
158 if (!( Init() && (arcmode || (_caps & (OUT_MASK & DISTANCE_IN))) ))
159 // Uninitialized or impossible distance calculation requested
160 return Math::NaN();
161
162 // Avoid warning about uninitialized B12.
163 real sig12, ssig12, csig12, B12 = 0, AB1 = 0;
164 if (arcmode) {
165 // Interpret s12_a12 as spherical arc length
166 sig12 = s12_a12 * Math::degree();
167 Math::sincosd(s12_a12, ssig12, csig12);
168 } else {
169 // Interpret s12_a12 as distance
170 real
171 tau12 = s12_a12 / (_b * (1 + _aA1m1)),
172 s = sin(tau12),
173 c = cos(tau12);
174 // tau2 = tau1 + tau12
175 B12 = - Geodesic::SinCosSeries(true,
176 _stau1 * c + _ctau1 * s,
177 _ctau1 * c - _stau1 * s,
178 _cC1pa, nC1p_);
179 sig12 = tau12 - (B12 - _bB11);
180 ssig12 = sin(sig12); csig12 = cos(sig12);
181 if (fabs(_f) > 0.01) {
182 // Reverted distance series is inaccurate for |f| > 1/100, so correct
183 // sig12 with 1 Newton iteration. The following table shows the
184 // approximate maximum error for a = WGS_a() and various f relative to
185 // GeodesicExact.
186 // erri = the error in the inverse solution (nm)
187 // errd = the error in the direct solution (series only) (nm)
188 // errda = the error in the direct solution
189 // (series + 1 Newton) (nm)
190 //
191 // f erri errd errda
192 // -1/5 12e6 1.2e9 69e6
193 // -1/10 123e3 12e6 765e3
194 // -1/20 1110 108e3 7155
195 // -1/50 18.63 200.9 27.12
196 // -1/100 18.63 23.78 23.37
197 // -1/150 18.63 21.05 20.26
198 // 1/150 22.35 24.73 25.83
199 // 1/100 22.35 25.03 25.31
200 // 1/50 29.80 231.9 30.44
201 // 1/20 5376 146e3 10e3
202 // 1/10 829e3 22e6 1.5e6
203 // 1/5 157e6 3.8e9 280e6
204 real
205 ssig2 = _ssig1 * csig12 + _csig1 * ssig12,
206 csig2 = _csig1 * csig12 - _ssig1 * ssig12;
207 B12 = Geodesic::SinCosSeries(true, ssig2, csig2, _cC1a, nC1_);
208 real serr = (1 + _aA1m1) * (sig12 + (B12 - _bB11)) - s12_a12 / _b;
209 sig12 = sig12 - serr / sqrt(1 + _k2 * Math::sq(ssig2));
210 ssig12 = sin(sig12); csig12 = cos(sig12);
211 // Update B12 below
212 }
213 }
214
215 real ssig2, csig2, sbet2, cbet2, salp2, calp2;
216 // sig2 = sig1 + sig12
217 ssig2 = _ssig1 * csig12 + _csig1 * ssig12;
218 csig2 = _csig1 * csig12 - _ssig1 * ssig12;
219 real dn2 = sqrt(1 + _k2 * Math::sq(ssig2));
220 if (outmask & (DISTANCE | REDUCEDLENGTH | GEODESICSCALE)) {
221 if (arcmode || fabs(_f) > 0.01)
222 B12 = Geodesic::SinCosSeries(true, ssig2, csig2, _cC1a, nC1_);
223 AB1 = (1 + _aA1m1) * (B12 - _bB11);
224 }
225 // sin(bet2) = cos(alp0) * sin(sig2)
226 sbet2 = _calp0 * ssig2;
227 // Alt: cbet2 = hypot(csig2, salp0 * ssig2);
228 cbet2 = hypot(_salp0, _calp0 * csig2);
229 if (cbet2 == 0)
230 // I.e., salp0 = 0, csig2 = 0. Break the degeneracy in this case
231 cbet2 = csig2 = tiny_;
232 // tan(alp0) = cos(sig2)*tan(alp2)
233 salp2 = _salp0; calp2 = _calp0 * csig2; // No need to normalize
234
235 if (outmask & DISTANCE)
236 s12 = arcmode ? _b * ((1 + _aA1m1) * sig12 + AB1) : s12_a12;
237
238 if (outmask & LONGITUDE) {
239 // tan(omg2) = sin(alp0) * tan(sig2)
240 real somg2 = _salp0 * ssig2, comg2 = csig2, // No need to normalize
241 E = copysign(real(1), _salp0); // east-going?
242 // omg12 = omg2 - omg1
243 real omg12 = outmask & LONG_UNROLL
244 ? E * (sig12
245 - (atan2( ssig2, csig2) - atan2( _ssig1, _csig1))
246 + (atan2(E * somg2, comg2) - atan2(E * _somg1, _comg1)))
247 : atan2(somg2 * _comg1 - comg2 * _somg1,
248 comg2 * _comg1 + somg2 * _somg1);
249 real lam12 = omg12 + _aA3c *
250 ( sig12 + (Geodesic::SinCosSeries(true, ssig2, csig2, _cC3a, nC3_-1)
251 - _bB31));
252 real lon12 = lam12 / Math::degree();
253 lon2 = outmask & LONG_UNROLL ? _lon1 + lon12 :
254 Math::AngNormalize(Math::AngNormalize(_lon1) +
255 Math::AngNormalize(lon12));
256 }
257
258 if (outmask & LATITUDE)
259 lat2 = Math::atan2d(sbet2, _f1 * cbet2);
260
261 if (outmask & AZIMUTH)
262 azi2 = Math::atan2d(salp2, calp2);
263
264 if (outmask & (REDUCEDLENGTH | GEODESICSCALE)) {
265 real
266 B22 = Geodesic::SinCosSeries(true, ssig2, csig2, _cC2a, nC2_),
267 AB2 = (1 + _aA2m1) * (B22 - _bB21),
268 J12 = (_aA1m1 - _aA2m1) * sig12 + (AB1 - AB2);
269 if (outmask & REDUCEDLENGTH)
270 // Add parens around (_csig1 * ssig2) and (_ssig1 * csig2) to ensure
271 // accurate cancellation in the case of coincident points.
272 m12 = _b * ((dn2 * (_csig1 * ssig2) - _dn1 * (_ssig1 * csig2))
273 - _csig1 * csig2 * J12);
274 if (outmask & GEODESICSCALE) {
275 real t = _k2 * (ssig2 - _ssig1) * (ssig2 + _ssig1) / (_dn1 + dn2);
276 M12 = csig12 + (t * ssig2 - csig2 * J12) * _ssig1 / _dn1;
277 M21 = csig12 - (t * _ssig1 - _csig1 * J12) * ssig2 / dn2;
278 }
279 }
280
281 if (outmask & AREA) {
282 real
283 B42 = Geodesic::SinCosSeries(false, ssig2, csig2, _cC4a, nC4_);
284 real salp12, calp12;
285 if (_calp0 == 0 || _salp0 == 0) {
286 // alp12 = alp2 - alp1, used in atan2 so no need to normalize
287 salp12 = salp2 * _calp1 - calp2 * _salp1;
288 calp12 = calp2 * _calp1 + salp2 * _salp1;
289 // We used to include here some patch up code that purported to deal
290 // with nearly meridional geodesics properly. However, this turned out
291 // to be wrong once _salp1 = -0 was allowed (via
292 // Geodesic::InverseLine). In fact, the calculation of {s,c}alp12
293 // was already correct (following the IEEE rules for handling signed
294 // zeros). So the patch up code was unnecessary (as well as
295 // dangerous).
296 } else {
297 // tan(alp) = tan(alp0) * sec(sig)
298 // tan(alp2-alp1) = (tan(alp2) -tan(alp1)) / (tan(alp2)*tan(alp1)+1)
299 // = calp0 * salp0 * (csig1-csig2) / (salp0^2 + calp0^2 * csig1*csig2)
300 // If csig12 > 0, write
301 // csig1 - csig2 = ssig12 * (csig1 * ssig12 / (1 + csig12) + ssig1)
302 // else
303 // csig1 - csig2 = csig1 * (1 - csig12) + ssig12 * ssig1
304 // No need to normalize
305 salp12 = _calp0 * _salp0 *
306 (csig12 <= 0 ? _csig1 * (1 - csig12) + ssig12 * _ssig1 :
307 ssig12 * (_csig1 * ssig12 / (1 + csig12) + _ssig1));
308 calp12 = Math::sq(_salp0) + Math::sq(_calp0) * _csig1 * csig2;
309 }
310 S12 = _c2 * atan2(salp12, calp12) + _aA4 * (B42 - _bB41);
311 }
312
313 return arcmode ? s12_a12 : sig12 / Math::degree();
314 }
315
316 void GeodesicLine::SetDistance(real s13) {
317 _s13 = s13;
318 real t;
319 // This will set _a13 to NaN if the GeodesicLine doesn't have the
320 // DISTANCE_IN capability.
321 _a13 = GenPosition(false, _s13, 0u, t, t, t, t, t, t, t, t);
322 }
323
324 void GeodesicLine::SetArc(real a13) {
325 _a13 = a13;
326 // In case the GeodesicLine doesn't have the DISTANCE capability.
327 _s13 = Math::NaN();
328 real t;
329 GenPosition(true, _a13, DISTANCE, t, t, t, _s13, t, t, t, t);
330 }
331
332 void GeodesicLine::GenSetDistance(bool arcmode, real s13_a13) {
333 arcmode ? SetArc(s13_a13) : SetDistance(s13_a13);
334 }
335
336} // namespace GeographicLib
real
GeographicLib::Math::real real
Definition: GeodSolve.cpp:29
GeodesicLine.hpp
Header for GeographicLib::GeodesicLine class.
GeographicLib::GeodesicLineExact::GenPosition
Math::real GenPosition(bool arcmode, real s12_a12, unsigned outmask, real &lat2, real &lon2, real &azi2, real &s12, real &m12, real &M12, real &M21, real &S12) const
Definition: GeodesicLineExact.cpp:148
GeographicLib::GeodesicLine::SetArc
void SetArc(real a13)
Definition: GeodesicLine.cpp:324
GeographicLib::GeodesicLine::Init
bool Init() const
Definition: GeodesicLine.hpp:606
GeographicLib::GeodesicLine::GenSetDistance
void GenSetDistance(bool arcmode, real s13_a13)
Definition: GeodesicLine.cpp:332
GeographicLib::GeodesicLine::GenPosition
Math::real GenPosition(bool arcmode, real s12_a12, unsigned outmask, real &lat2, real &lon2, real &azi2, real &s12, real &m12, real &M12, real &M21, real &S12) const
Definition: GeodesicLine.cpp:147
GeographicLib::GeodesicLine::SetDistance
void SetDistance(real s13)
Definition: GeodesicLine.cpp:316
GeographicLib::GeodesicLine::LATITUDE
@ LATITUDE
Definition: GeodesicLine.hpp:138
GeographicLib::GeodesicLine::AZIMUTH
@ AZIMUTH
Definition: GeodesicLine.hpp:150
GeographicLib::GeodesicLine::DISTANCE
@ DISTANCE
Definition: GeodesicLine.hpp:155
GeographicLib::GeodesicLine::AREA
@ AREA
Definition: GeodesicLine.hpp:182
GeographicLib::GeodesicLine::GEODESICSCALE
@ GEODESICSCALE
Definition: GeodesicLine.hpp:177
GeographicLib::GeodesicLine::DISTANCE_IN
@ DISTANCE_IN
Definition: GeodesicLine.hpp:167
GeographicLib::GeodesicLine::REDUCEDLENGTH
@ REDUCEDLENGTH
Definition: GeodesicLine.hpp:172
GeographicLib::GeodesicLine::LONGITUDE
@ LONGITUDE
Definition: GeodesicLine.hpp:143
GeographicLib::GeodesicLine::LONG_UNROLL
@ LONG_UNROLL
Definition: GeodesicLine.hpp:187
GeographicLib::GeodesicLine::GeodesicLine
GeodesicLine()
Definition: GeodesicLine.hpp:252
GeographicLib::Geodesic
Geodesic calculations
Definition: Geodesic.hpp:175
GeographicLib::Math::degree
static T degree()
Definition: Math.hpp:193
GeographicLib::Math::LatFix
static T LatFix(T x)
Definition: Math.hpp:293
GeographicLib::Math::sincosd
static void sincosd(T x, T &sinx, T &cosx)
Definition: Math.cpp:106
GeographicLib::Math::atan2d
static T atan2d(T y, T x)
Definition: Math.cpp:183
GeographicLib::Math::norm
static void norm(T &x, T &y)
Definition: Math.hpp:215
GeographicLib::Math::AngRound
static T AngRound(T x)
Definition: Math.cpp:97
GeographicLib::Math::sq
static T sq(T x)
Definition: Math.hpp:205
GeographicLib::Math::AngNormalize
static T AngNormalize(T x)
Definition: Math.cpp:71
GeographicLib::Math::NaN
static T NaN()
Definition: Math.cpp:250
GeographicLib::Math::real
double real
Definition: Math.hpp:100
GeographicLib
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
std
Definition: NearestNeighbor.hpp:812