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