GeographicLib 2.1.2
GeodesicLineExact.cpp
Go to the documentation of this file.
1/**
2 * \file GeodesicLineExact.cpp
3 * \brief Implementation for GeographicLib::GeodesicLineExact 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
30
31#if defined(_MSC_VER)
32// Squelch warnings about mixing enums
33# pragma warning (disable: 5054)
34#endif
35
36namespace GeographicLib {
37
38 using namespace std;
39
40 void GeodesicLineExact::LineInit(const GeodesicExact& 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 _e2 = g._e2;
56 _nC4 = g._nC4;
57 // Always allow latitude and azimuth and unrolling of longitude
58 _caps = caps | LATITUDE | AZIMUTH | LONG_UNROLL;
59
60 real cbet1, sbet1;
61 Math::sincosd(Math::AngRound(_lat1), sbet1, cbet1); sbet1 *= _f1;
62 // Ensure cbet1 = +epsilon at poles
63 Math::norm(sbet1, cbet1); cbet1 = fmax(tiny_, cbet1);
64 _dn1 = (_f >= 0 ? sqrt(1 + g._ep2 * Math::sq(sbet1)) :
65 sqrt(1 - _e2 * Math::sq(cbet1)) / _f1);
66
67 // Evaluate alp0 from sin(alp1) * cos(bet1) = sin(alp0),
68 _salp0 = _salp1 * cbet1; // alp0 in [0, pi/2 - |bet1|]
69 // Alt: calp0 = hypot(sbet1, calp1 * cbet1). The following
70 // is slightly better (consider the case salp1 = 0).
71 _calp0 = hypot(_calp1, _salp1 * sbet1);
72 // Evaluate sig with tan(bet1) = tan(sig1) * cos(alp1).
73 // sig = 0 is nearest northward crossing of equator.
74 // With bet1 = 0, alp1 = pi/2, we have sig1 = 0 (equatorial line).
75 // With bet1 = pi/2, alp1 = -pi, sig1 = pi/2
76 // With bet1 = -pi/2, alp1 = 0 , sig1 = -pi/2
77 // Evaluate omg1 with tan(omg1) = sin(alp0) * tan(sig1).
78 // With alp0 in (0, pi/2], quadrants for sig and omg coincide.
79 // No atan2(0,0) ambiguity at poles since cbet1 = +epsilon.
80 // With alp0 = 0, omg1 = 0 for alp1 = 0, omg1 = pi for alp1 = pi.
81 _ssig1 = sbet1; _somg1 = _salp0 * sbet1;
82 _csig1 = _comg1 = sbet1 != 0 || _calp1 != 0 ? cbet1 * _calp1 : 1;
83 // Without normalization we have schi1 = somg1.
84 _cchi1 = _f1 * _dn1 * _comg1;
85 Math::norm(_ssig1, _csig1); // sig1 in (-pi, pi]
86 // Math::norm(_somg1, _comg1); -- don't need to normalize!
87 // Math::norm(_schi1, _cchi1); -- don't need to normalize!
88
89 _k2 = Math::sq(_calp0) * g._ep2;
90 _eE.Reset(-_k2, -g._ep2, 1 + _k2, 1 + g._ep2);
91
92 if (_caps & CAP_E) {
93 _eE0 = _eE.E() / (Math::pi() / 2);
94 _eE1 = _eE.deltaE(_ssig1, _csig1, _dn1);
95 real s = sin(_eE1), c = cos(_eE1);
96 // tau1 = sig1 + B11
97 _stau1 = _ssig1 * c + _csig1 * s;
98 _ctau1 = _csig1 * c - _ssig1 * s;
99 // Not necessary because Einv inverts E
100 // _eE1 = -_eE.deltaEinv(_stau1, _ctau1);
101 }
102
103 if (_caps & CAP_D) {
104 _dD0 = _eE.D() / (Math::pi() / 2);
105 _dD1 = _eE.deltaD(_ssig1, _csig1, _dn1);
106 }
107
108 if (_caps & CAP_H) {
109 _hH0 = _eE.H() / (Math::pi() / 2);
110 _hH1 = _eE.deltaH(_ssig1, _csig1, _dn1);
111 }
112
113 if (_caps & CAP_C4) {
114 // Multiplier = a^2 * e^2 * cos(alpha0) * sin(alpha0)
115 _aA4 = Math::sq(_a) * _calp0 * _salp0 * _e2;
116 if (_aA4 == 0)
117 _bB41 = 0;
118 else {
119 GeodesicExact::I4Integrand i4(g._ep2, _k2);
120 _cC4a.resize(_nC4);
121 g._fft.transform(i4, _cC4a.data());
122 _bB41 = DST::integral(_ssig1, _csig1, _cC4a.data(), _nC4);
123 }
124 }
125
126 _a13 = _s13 = Math::NaN();
127 }
128
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
140 real lat1, real lon1,
141 real azi1, real salp1, real calp1,
142 unsigned caps,
143 bool arcmode, real s13_a13) {
144 LineInit(g, lat1, lon1, azi1, salp1, calp1, caps);
145 GenSetDistance(arcmode, s13_a13);
146 }
147
148 Math::real GeodesicLineExact::GenPosition(bool arcmode, real s12_a12,
149 unsigned outmask,
150 real& lat2, real& lon2, real& azi2,
151 real& s12, real& m12,
152 real& M12, real& M21,
153 real& S12) const {
154 outmask &= _caps & OUT_MASK;
155 if (!( Init() && (arcmode || (_caps & (OUT_MASK & DISTANCE_IN))) ))
156 // Uninitialized or impossible distance calculation requested
157 return Math::NaN();
158
159 // Avoid warning about uninitialized B12.
160 real sig12, ssig12, csig12, E2 = 0, AB1 = 0;
161 if (arcmode) {
162 // Interpret s12_a12 as spherical arc length
163 sig12 = s12_a12 * Math::degree();
164 Math::sincosd(s12_a12, ssig12, csig12);
165 } else {
166 // Interpret s12_a12 as distance
167 real
168 tau12 = s12_a12 / (_b * _eE0),
169 s = sin(tau12),
170 c = cos(tau12);
171 // tau2 = tau1 + tau12
172 E2 = - _eE.deltaEinv(_stau1 * c + _ctau1 * s, _ctau1 * c - _stau1 * s);
173 sig12 = tau12 - (E2 - _eE1);
174 ssig12 = sin(sig12);
175 csig12 = cos(sig12);
176 }
177
178 real ssig2, csig2, sbet2, cbet2, salp2, calp2;
179 // sig2 = sig1 + sig12
180 ssig2 = _ssig1 * csig12 + _csig1 * ssig12;
181 csig2 = _csig1 * csig12 - _ssig1 * ssig12;
182 real dn2 = _eE.Delta(ssig2, csig2);
183 if (outmask & (DISTANCE | REDUCEDLENGTH | GEODESICSCALE)) {
184 if (arcmode) {
185 E2 = _eE.deltaE(ssig2, csig2, dn2);
186 }
187 AB1 = _eE0 * (E2 - _eE1);
188 }
189 // sin(bet2) = cos(alp0) * sin(sig2)
190 sbet2 = _calp0 * ssig2;
191 // Alt: cbet2 = hypot(csig2, salp0 * ssig2);
192 cbet2 = hypot(_salp0, _calp0 * csig2);
193 if (cbet2 == 0)
194 // I.e., salp0 = 0, csig2 = 0. Break the degeneracy in this case
195 cbet2 = csig2 = tiny_;
196 // tan(alp0) = cos(sig2)*tan(alp2)
197 salp2 = _salp0; calp2 = _calp0 * csig2; // No need to normalize
198
199 if (outmask & DISTANCE)
200 s12 = arcmode ? _b * (_eE0 * sig12 + AB1) : s12_a12;
201
202 if (outmask & LONGITUDE) {
203 real somg2 = _salp0 * ssig2, comg2 = csig2, // No need to normalize
204 E = copysign(real(1), _salp0); // east-going?
205 // Without normalization we have schi2 = somg2.
206 real cchi2 = _f1 * dn2 * comg2;
207 real chi12 = outmask & LONG_UNROLL
208 ? E * (sig12
209 - (atan2( ssig2, csig2) - atan2( _ssig1, _csig1))
210 + (atan2(E * somg2, cchi2) - atan2(E * _somg1, _cchi1)))
211 : atan2(somg2 * _cchi1 - cchi2 * _somg1,
212 cchi2 * _cchi1 + somg2 * _somg1);
213 real lam12 = chi12 -
214 _e2/_f1 * _salp0 * _hH0 *
215 (sig12 + (_eE.deltaH(ssig2, csig2, dn2) - _hH1));
216 real lon12 = lam12 / Math::degree();
217 lon2 = outmask & LONG_UNROLL ? _lon1 + lon12 :
219 Math::AngNormalize(lon12));
220 }
221
222 if (outmask & LATITUDE)
223 lat2 = Math::atan2d(sbet2, _f1 * cbet2);
224
225 if (outmask & AZIMUTH)
226 azi2 = Math::atan2d(salp2, calp2);
227
228 if (outmask & (REDUCEDLENGTH | GEODESICSCALE)) {
229 real J12 = _k2 * _dD0 * (sig12 + (_eE.deltaD(ssig2, csig2, dn2) - _dD1));
230 if (outmask & REDUCEDLENGTH)
231 // Add parens around (_csig1 * ssig2) and (_ssig1 * csig2) to ensure
232 // accurate cancellation in the case of coincident points.
233 m12 = _b * ((dn2 * (_csig1 * ssig2) - _dn1 * (_ssig1 * csig2))
234 - _csig1 * csig2 * J12);
235 if (outmask & GEODESICSCALE) {
236 real t = _k2 * (ssig2 - _ssig1) * (ssig2 + _ssig1) / (_dn1 + dn2);
237 M12 = csig12 + (t * ssig2 - csig2 * J12) * _ssig1 / _dn1;
238 M21 = csig12 - (t * _ssig1 - _csig1 * J12) * ssig2 / dn2;
239 }
240 }
241
242 if (outmask & AREA) {
243 real B42 = _aA4 == 0 ? 0 :
244 DST::integral(ssig2, csig2, _cC4a.data(), _nC4);
245 real salp12, calp12;
246 if (_calp0 == 0 || _salp0 == 0) {
247 // alp12 = alp2 - alp1, used in atan2 so no need to normalize
248 salp12 = salp2 * _calp1 - calp2 * _salp1;
249 calp12 = calp2 * _calp1 + salp2 * _salp1;
250 // We used to include here some patch up code that purported to deal
251 // with nearly meridional geodesics properly. However, this turned out
252 // to be wrong once _salp1 = -0 was allowed (via
253 // GeodesicExact::InverseLine). In fact, the calculation of {s,c}alp12
254 // was already correct (following the IEEE rules for handling signed
255 // zeros). So the patch up code was unnecessary (as well as
256 // dangerous).
257 } else {
258 // tan(alp) = tan(alp0) * sec(sig)
259 // tan(alp2-alp1) = (tan(alp2) -tan(alp1)) / (tan(alp2)*tan(alp1)+1)
260 // = calp0 * salp0 * (csig1-csig2) / (salp0^2 + calp0^2 * csig1*csig2)
261 // If csig12 > 0, write
262 // csig1 - csig2 = ssig12 * (csig1 * ssig12 / (1 + csig12) + ssig1)
263 // else
264 // csig1 - csig2 = csig1 * (1 - csig12) + ssig12 * ssig1
265 // No need to normalize
266 salp12 = _calp0 * _salp0 *
267 (csig12 <= 0 ? _csig1 * (1 - csig12) + ssig12 * _ssig1 :
268 ssig12 * (_csig1 * ssig12 / (1 + csig12) + _ssig1));
269 calp12 = Math::sq(_salp0) + Math::sq(_calp0) * _csig1 * csig2;
270 }
271 S12 = _c2 * atan2(salp12, calp12) + _aA4 * (B42 - _bB41);
272 }
273
274 return arcmode ? s12_a12 : sig12 / Math::degree();
275 }
276
278 _s13 = s13;
279 real t;
280 // This will set _a13 to NaN if the GeodesicLineExact doesn't have the
281 // DISTANCE_IN capability.
282 _a13 = GenPosition(false, _s13, 0u, t, t, t, t, t, t, t, t);
283 }
284
286 _a13 = a13;
287 // In case the GeodesicLineExact doesn't have the DISTANCE capability.
288 _s13 = Math::NaN();
289 real t;
290 GenPosition(true, _a13, DISTANCE, t, t, t, _s13, t, t, t, t);
291 }
292
293 void GeodesicLineExact::GenSetDistance(bool arcmode, real s13_a13) {
294 arcmode ? SetArc(s13_a13) : SetDistance(s13_a13);
295 }
296
297} // namespace GeographicLib
GeographicLib::Math::real real
Definition: GeodSolve.cpp:31
Header for GeographicLib::GeodesicLineExact class.
static real integral(real sinx, real cosx, const real F[], int N)
Definition: DST.cpp:110
Math::real deltaE(real sn, real cn, real dn) const
void Reset(real k2=0, real alpha2=0)
Math::real Delta(real sn, real cn) const
Math::real deltaD(real sn, real cn, real dn) const
Math::real deltaH(real sn, real cn, real dn) const
Math::real deltaEinv(real stau, real ctau) const
Exact geodesic calculations.
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
void GenSetDistance(bool arcmode, real s13_a13)
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 T pi()
Definition: Math.hpp:190
static T NaN()
Definition: Math.cpp:250
Namespace for GeographicLib.
Definition: Accumulator.cpp:12