GeographicLib  1.42
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-2015) <charles@karney.com> and licensed
6  * under the MIT/X11 License. For more information, see
7  * http://geographiclib.sourceforge.net/
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 
36  real lat1, real lon1, real azi1,
37  unsigned caps)
38  : tiny_(g.tiny_)
39  , _lat1(lat1)
40  , _lon1(lon1)
41  // Guard against underflow in salp0
42  , _azi1(Math::AngRound(Math::AngNormalize(azi1)))
43  , _a(g._a)
44  , _f(g._f)
45  , _b(g._b)
46  , _c2(g._c2)
47  , _f1(g._f1)
48  // Always allow latitude and azimuth
49  , _caps(caps | LATITUDE | AZIMUTH)
50  {
51  real alp1 = _azi1 * Math::degree();
52  // Enforce sin(pi) == 0 and cos(pi/2) == 0. Better to face the ensuing
53  // problems directly than to skirt them.
54  _salp1 = _azi1 == -180 ? 0 : sin(alp1);
55  _calp1 = abs(_azi1) == 90 ? 0 : cos(alp1);
56  real cbet1, sbet1, phi;
57  phi = lat1 * Math::degree();
58  // Ensure cbet1 = +epsilon at poles
59  sbet1 = _f1 * sin(phi);
60  cbet1 = abs(lat1) == 90 ? tiny_ : cos(phi);
61  Math::norm(sbet1, 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 = Math::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  _A1m1 = Geodesic::A1m1f(eps);
88  Geodesic::C1f(eps, _C1a);
89  _B11 = Geodesic::SinCosSeries(true, _ssig1, _csig1, _C1a, nC1_);
90  real s = sin(_B11), c = cos(_B11);
91  // tau1 = sig1 + B11
92  _stau1 = _ssig1 * c + _csig1 * s;
93  _ctau1 = _csig1 * c - _ssig1 * s;
94  // Not necessary because C1pa reverts C1a
95  // _B11 = -SinCosSeries(true, _stau1, _ctau1, _C1pa, nC1p_);
96  }
97 
98  if (_caps & CAP_C1p)
99  Geodesic::C1pf(eps, _C1pa);
100 
101  if (_caps & CAP_C2) {
102  _A2m1 = Geodesic::A2m1f(eps);
103  Geodesic::C2f(eps, _C2a);
104  _B21 = Geodesic::SinCosSeries(true, _ssig1, _csig1, _C2a, nC2_);
105  }
106 
107  if (_caps & CAP_C3) {
108  g.C3f(eps, _C3a);
109  _A3c = -_f * _salp0 * g.A3f(eps);
110  _B31 = Geodesic::SinCosSeries(true, _ssig1, _csig1, _C3a, nC3_-1);
111  }
112 
113  if (_caps & CAP_C4) {
114  g.C4f(eps, _C4a);
115  // Multiplier = a^2 * e^2 * cos(alpha0) * sin(alpha0)
116  _A4 = Math::sq(_a) * _calp0 * _salp0 * g._e2;
117  _B41 = Geodesic::SinCosSeries(false, _ssig1, _csig1, _C4a, nC4_);
118  }
119  }
120 
121  Math::real GeodesicLine::GenPosition(bool arcmode, real s12_a12,
122  unsigned outmask,
123  real& lat2, real& lon2, real& azi2,
124  real& s12, real& m12,
125  real& M12, real& M21,
126  real& S12)
127  const {
128  outmask &= _caps & OUT_MASK;
129  if (!( Init() && (arcmode || (_caps & DISTANCE_IN & OUT_MASK)) ))
130  // Uninitialized or impossible distance calculation requested
131  return Math::NaN();
132 
133  // Avoid warning about uninitialized B12.
134  real sig12, ssig12, csig12, B12 = 0, AB1 = 0;
135  if (arcmode) {
136  // Interpret s12_a12 as spherical arc length
137  sig12 = s12_a12 * Math::degree();
138  real s12a = abs(s12_a12);
139  s12a -= 180 * floor(s12a / 180);
140  ssig12 = s12a == 0 ? 0 : sin(sig12);
141  csig12 = s12a == 90 ? 0 : cos(sig12);
142  } else {
143  // Interpret s12_a12 as distance
144  real
145  tau12 = s12_a12 / (_b * (1 + _A1m1)),
146  s = sin(tau12),
147  c = cos(tau12);
148  // tau2 = tau1 + tau12
149  B12 = - Geodesic::SinCosSeries(true,
150  _stau1 * c + _ctau1 * s,
151  _ctau1 * c - _stau1 * s,
152  _C1pa, nC1p_);
153  sig12 = tau12 - (B12 - _B11);
154  ssig12 = sin(sig12); csig12 = cos(sig12);
155  if (abs(_f) > 0.01) {
156  // Reverted distance series is inaccurate for |f| > 1/100, so correct
157  // sig12 with 1 Newton iteration. The following table shows the
158  // approximate maximum error for a = WGS_a() and various f relative to
159  // GeodesicExact.
160  // erri = the error in the inverse solution (nm)
161  // errd = the error in the direct solution (series only) (nm)
162  // errda = the error in the direct solution (series + 1 Newton) (nm)
163  //
164  // f erri errd errda
165  // -1/5 12e6 1.2e9 69e6
166  // -1/10 123e3 12e6 765e3
167  // -1/20 1110 108e3 7155
168  // -1/50 18.63 200.9 27.12
169  // -1/100 18.63 23.78 23.37
170  // -1/150 18.63 21.05 20.26
171  // 1/150 22.35 24.73 25.83
172  // 1/100 22.35 25.03 25.31
173  // 1/50 29.80 231.9 30.44
174  // 1/20 5376 146e3 10e3
175  // 1/10 829e3 22e6 1.5e6
176  // 1/5 157e6 3.8e9 280e6
177  real
178  ssig2 = _ssig1 * csig12 + _csig1 * ssig12,
179  csig2 = _csig1 * csig12 - _ssig1 * ssig12;
180  B12 = Geodesic::SinCosSeries(true, ssig2, csig2, _C1a, nC1_);
181  real serr = (1 + _A1m1) * (sig12 + (B12 - _B11)) - s12_a12 / _b;
182  sig12 = sig12 - serr / sqrt(1 + _k2 * Math::sq(ssig2));
183  ssig12 = sin(sig12); csig12 = cos(sig12);
184  // Update B12 below
185  }
186  }
187 
188  real ssig2, csig2, sbet2, cbet2, salp2, calp2;
189  // sig2 = sig1 + sig12
190  ssig2 = _ssig1 * csig12 + _csig1 * ssig12;
191  csig2 = _csig1 * csig12 - _ssig1 * ssig12;
192  real dn2 = sqrt(1 + _k2 * Math::sq(ssig2));
193  if (outmask & (DISTANCE | REDUCEDLENGTH | GEODESICSCALE)) {
194  if (arcmode || abs(_f) > 0.01)
195  B12 = Geodesic::SinCosSeries(true, ssig2, csig2, _C1a, nC1_);
196  AB1 = (1 + _A1m1) * (B12 - _B11);
197  }
198  // sin(bet2) = cos(alp0) * sin(sig2)
199  sbet2 = _calp0 * ssig2;
200  // Alt: cbet2 = hypot(csig2, salp0 * ssig2);
201  cbet2 = Math::hypot(_salp0, _calp0 * csig2);
202  if (cbet2 == 0)
203  // I.e., salp0 = 0, csig2 = 0. Break the degeneracy in this case
204  cbet2 = csig2 = tiny_;
205  // tan(alp0) = cos(sig2)*tan(alp2)
206  salp2 = _salp0; calp2 = _calp0 * csig2; // No need to normalize
207 
208  if (outmask & DISTANCE)
209  s12 = arcmode ? _b * ((1 + _A1m1) * sig12 + AB1) : s12_a12;
210 
211  if (outmask & LONGITUDE) {
212  // tan(omg2) = sin(alp0) * tan(sig2)
213  real somg2 = _salp0 * ssig2, comg2 = csig2; // No need to normalize
214  // omg12 = omg2 - omg1
215  real omg12 = outmask & LONG_NOWRAP ? sig12
216  - (atan2(ssig2, csig2) - atan2(_ssig1, _csig1))
217  + (atan2(somg2, comg2) - atan2(_somg1, _comg1))
218  : atan2(somg2 * _comg1 - comg2 * _somg1,
219  comg2 * _comg1 + somg2 * _somg1);
220  real lam12 = omg12 + _A3c *
221  ( sig12 + (Geodesic::SinCosSeries(true, ssig2, csig2, _C3a, nC3_-1)
222  - _B31));
223  real lon12 = lam12 / Math::degree();
224  // Use Math::AngNormalize2 because longitude might have wrapped
225  // multiple times.
226  lon2 = outmask & LONG_NOWRAP ? _lon1 + lon12 :
228  Math::AngNormalize2(lon12));
229  }
230 
231  if (outmask & LATITUDE)
232  lat2 = atan2(sbet2, _f1 * cbet2) / Math::degree();
233 
234  if (outmask & AZIMUTH)
235  // minus signs give range [-180, 180). 0- converts -0 to +0.
236  azi2 = Math::atan2d(salp2, calp2);
237 
238  if (outmask & (REDUCEDLENGTH | GEODESICSCALE)) {
239  real
240  B22 = Geodesic::SinCosSeries(true, ssig2, csig2, _C2a, nC2_),
241  AB2 = (1 + _A2m1) * (B22 - _B21),
242  J12 = (_A1m1 - _A2m1) * sig12 + (AB1 - AB2);
243  if (outmask & REDUCEDLENGTH)
244  // Add parens around (_csig1 * ssig2) and (_ssig1 * csig2) to ensure
245  // accurate cancellation in the case of coincident points.
246  m12 = _b * ((dn2 * (_csig1 * ssig2) - _dn1 * (_ssig1 * csig2))
247  - _csig1 * csig2 * J12);
248  if (outmask & GEODESICSCALE) {
249  real t = _k2 * (ssig2 - _ssig1) * (ssig2 + _ssig1) / (_dn1 + dn2);
250  M12 = csig12 + (t * ssig2 - csig2 * J12) * _ssig1 / _dn1;
251  M21 = csig12 - (t * _ssig1 - _csig1 * J12) * ssig2 / dn2;
252  }
253  }
254 
255  if (outmask & AREA) {
256  real
257  B42 = Geodesic::SinCosSeries(false, ssig2, csig2, _C4a, nC4_);
258  real salp12, calp12;
259  if (_calp0 == 0 || _salp0 == 0) {
260  // alp12 = alp2 - alp1, used in atan2 so no need to normalize
261  salp12 = salp2 * _calp1 - calp2 * _salp1;
262  calp12 = calp2 * _calp1 + salp2 * _salp1;
263  // The right thing appears to happen if alp1 = +/-180 and alp2 = 0, viz
264  // salp12 = -0 and alp12 = -180. However this depends on the sign being
265  // attached to 0 correctly. The following ensures the correct behavior.
266  if (salp12 == 0 && calp12 < 0) {
267  salp12 = tiny_ * _calp1;
268  calp12 = -1;
269  }
270  } else {
271  // tan(alp) = tan(alp0) * sec(sig)
272  // tan(alp2-alp1) = (tan(alp2) -tan(alp1)) / (tan(alp2)*tan(alp1)+1)
273  // = calp0 * salp0 * (csig1-csig2) / (salp0^2 + calp0^2 * csig1*csig2)
274  // If csig12 > 0, write
275  // csig1 - csig2 = ssig12 * (csig1 * ssig12 / (1 + csig12) + ssig1)
276  // else
277  // csig1 - csig2 = csig1 * (1 - csig12) + ssig12 * ssig1
278  // No need to normalize
279  salp12 = _calp0 * _salp0 *
280  (csig12 <= 0 ? _csig1 * (1 - csig12) + ssig12 * _ssig1 :
281  ssig12 * (_csig1 * ssig12 / (1 + csig12) + _ssig1));
282  calp12 = Math::sq(_salp0) + Math::sq(_calp0) * _csig1 * csig2;
283  }
284  S12 = _c2 * atan2(salp12, calp12) + _A4 * (B42 - _B41);
285  }
286 
287  return arcmode ? s12_a12 : sig12 / Math::degree();
288  }
289 
290 } // namespace GeographicLib
static T AngNormalize(T x)
Definition: Math.hpp:428
static T NaN()
Definition: Math.hpp:611
Header for GeographicLib::GeodesicLine class.
Mathematical functions needed by GeographicLib.
Definition: Math.hpp:102
static void norm(T &x, T &y)
Definition: Math.hpp:392
static T hypot(T x, T y)
Definition: Math.hpp:255
static T sq(T x)
Definition: Math.hpp:244
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
static T atan2d(T y, T x)
Definition: Math.hpp:534
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
static T degree()
Definition: Math.hpp:228
Geodesic calculations
Definition: Geodesic.hpp:171
static T AngNormalize2(T x)
Definition: Math.hpp:440