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