GeographicLib: GeodesicLineExact.cpp Source File

GeographicLib 2.6

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