GeographicLib  1.45
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(Math::LatFix(lat1))
40  , _lon1(lon1)
41  , _azi1(Math::AngNormalize(azi1))
42  , _a(g._a)
43  , _f(g._f)
44  , _b(g._b)
45  , _c2(g._c2)
46  , _f1(g._f1)
47  // Always allow latitude and azimuth and unrolling of longitude
48  , _caps(caps | LATITUDE | AZIMUTH | LONG_UNROLL)
49  {
50  // Guard against underflow in salp0. Also -0 is converted to +0.
51  Math::sincosd(Math::AngRound(_azi1), _salp1, _calp1);
52  real cbet1, sbet1;
53  Math::sincosd(Math::AngRound(_lat1), sbet1, cbet1); sbet1 *= _f1;
54  // Ensure cbet1 = +epsilon at poles
55  Math::norm(sbet1, cbet1); cbet1 = max(tiny_, cbet1);
56  _dn1 = sqrt(1 + g._ep2 * Math::sq(sbet1));
57 
58  // Evaluate alp0 from sin(alp1) * cos(bet1) = sin(alp0),
59  _salp0 = _salp1 * cbet1; // alp0 in [0, pi/2 - |bet1|]
60  // Alt: calp0 = hypot(sbet1, calp1 * cbet1). The following
61  // is slightly better (consider the case salp1 = 0).
62  _calp0 = Math::hypot(_calp1, _salp1 * sbet1);
63  // Evaluate sig with tan(bet1) = tan(sig1) * cos(alp1).
64  // sig = 0 is nearest northward crossing of equator.
65  // With bet1 = 0, alp1 = pi/2, we have sig1 = 0 (equatorial line).
66  // With bet1 = pi/2, alp1 = -pi, sig1 = pi/2
67  // With bet1 = -pi/2, alp1 = 0 , sig1 = -pi/2
68  // Evaluate omg1 with tan(omg1) = sin(alp0) * tan(sig1).
69  // With alp0 in (0, pi/2], quadrants for sig and omg coincide.
70  // No atan2(0,0) ambiguity at poles since cbet1 = +epsilon.
71  // With alp0 = 0, omg1 = 0 for alp1 = 0, omg1 = pi for alp1 = pi.
72  _ssig1 = sbet1; _somg1 = _salp0 * sbet1;
73  _csig1 = _comg1 = sbet1 != 0 || _calp1 != 0 ? cbet1 * _calp1 : 1;
74  Math::norm(_ssig1, _csig1); // sig1 in (-pi, pi]
75  // Math::norm(_somg1, _comg1); -- don't need to normalize!
76 
77  _k2 = Math::sq(_calp0) * g._ep2;
78  real eps = _k2 / (2 * (1 + sqrt(1 + _k2)) + _k2);
79 
80  if (_caps & CAP_C1) {
81  _A1m1 = Geodesic::A1m1f(eps);
82  Geodesic::C1f(eps, _C1a);
83  _B11 = Geodesic::SinCosSeries(true, _ssig1, _csig1, _C1a, nC1_);
84  real s = sin(_B11), c = cos(_B11);
85  // tau1 = sig1 + B11
86  _stau1 = _ssig1 * c + _csig1 * s;
87  _ctau1 = _csig1 * c - _ssig1 * s;
88  // Not necessary because C1pa reverts C1a
89  // _B11 = -SinCosSeries(true, _stau1, _ctau1, _C1pa, nC1p_);
90  }
91 
92  if (_caps & CAP_C1p)
93  Geodesic::C1pf(eps, _C1pa);
94 
95  if (_caps & CAP_C2) {
96  _A2m1 = Geodesic::A2m1f(eps);
97  Geodesic::C2f(eps, _C2a);
98  _B21 = Geodesic::SinCosSeries(true, _ssig1, _csig1, _C2a, nC2_);
99  }
100 
101  if (_caps & CAP_C3) {
102  g.C3f(eps, _C3a);
103  _A3c = -_f * _salp0 * g.A3f(eps);
104  _B31 = Geodesic::SinCosSeries(true, _ssig1, _csig1, _C3a, nC3_-1);
105  }
106 
107  if (_caps & CAP_C4) {
108  g.C4f(eps, _C4a);
109  // Multiplier = a^2 * e^2 * cos(alpha0) * sin(alpha0)
110  _A4 = Math::sq(_a) * _calp0 * _salp0 * g._e2;
111  _B41 = Geodesic::SinCosSeries(false, _ssig1, _csig1, _C4a, nC4_);
112  }
113  }
114 
115  Math::real GeodesicLine::GenPosition(bool arcmode, real s12_a12,
116  unsigned outmask,
117  real& lat2, real& lon2, real& azi2,
118  real& s12, real& m12,
119  real& M12, real& M21,
120  real& S12)
121  const {
122  outmask &= _caps & OUT_MASK;
123  if (!( Init() && (arcmode || (_caps & DISTANCE_IN & OUT_MASK)) ))
124  // Uninitialized or impossible distance calculation requested
125  return Math::NaN();
126 
127  // Avoid warning about uninitialized B12.
128  real sig12, ssig12, csig12, B12 = 0, AB1 = 0;
129  if (arcmode) {
130  // Interpret s12_a12 as spherical arc length
131  sig12 = s12_a12 * Math::degree();
132  Math::sincosd(s12_a12, ssig12, csig12);
133  } else {
134  // Interpret s12_a12 as distance
135  real
136  tau12 = s12_a12 / (_b * (1 + _A1m1)),
137  s = sin(tau12),
138  c = cos(tau12);
139  // tau2 = tau1 + tau12
140  B12 = - Geodesic::SinCosSeries(true,
141  _stau1 * c + _ctau1 * s,
142  _ctau1 * c - _stau1 * s,
143  _C1pa, nC1p_);
144  sig12 = tau12 - (B12 - _B11);
145  ssig12 = sin(sig12); csig12 = cos(sig12);
146  if (abs(_f) > 0.01) {
147  // Reverted distance series is inaccurate for |f| > 1/100, so correct
148  // sig12 with 1 Newton iteration. The following table shows the
149  // approximate maximum error for a = WGS_a() and various f relative to
150  // GeodesicExact.
151  // erri = the error in the inverse solution (nm)
152  // errd = the error in the direct solution (series only) (nm)
153  // errda = the error in the direct solution (series + 1 Newton) (nm)
154  //
155  // f erri errd errda
156  // -1/5 12e6 1.2e9 69e6
157  // -1/10 123e3 12e6 765e3
158  // -1/20 1110 108e3 7155
159  // -1/50 18.63 200.9 27.12
160  // -1/100 18.63 23.78 23.37
161  // -1/150 18.63 21.05 20.26
162  // 1/150 22.35 24.73 25.83
163  // 1/100 22.35 25.03 25.31
164  // 1/50 29.80 231.9 30.44
165  // 1/20 5376 146e3 10e3
166  // 1/10 829e3 22e6 1.5e6
167  // 1/5 157e6 3.8e9 280e6
168  real
169  ssig2 = _ssig1 * csig12 + _csig1 * ssig12,
170  csig2 = _csig1 * csig12 - _ssig1 * ssig12;
171  B12 = Geodesic::SinCosSeries(true, ssig2, csig2, _C1a, nC1_);
172  real serr = (1 + _A1m1) * (sig12 + (B12 - _B11)) - s12_a12 / _b;
173  sig12 = sig12 - serr / sqrt(1 + _k2 * Math::sq(ssig2));
174  ssig12 = sin(sig12); csig12 = cos(sig12);
175  // Update B12 below
176  }
177  }
178 
179  real ssig2, csig2, sbet2, cbet2, salp2, calp2;
180  // sig2 = sig1 + sig12
181  ssig2 = _ssig1 * csig12 + _csig1 * ssig12;
182  csig2 = _csig1 * csig12 - _ssig1 * ssig12;
183  real dn2 = sqrt(1 + _k2 * Math::sq(ssig2));
184  if (outmask & (DISTANCE | REDUCEDLENGTH | GEODESICSCALE)) {
185  if (arcmode || abs(_f) > 0.01)
186  B12 = Geodesic::SinCosSeries(true, ssig2, csig2, _C1a, nC1_);
187  AB1 = (1 + _A1m1) * (B12 - _B11);
188  }
189  // sin(bet2) = cos(alp0) * sin(sig2)
190  sbet2 = _calp0 * ssig2;
191  // Alt: cbet2 = hypot(csig2, salp0 * ssig2);
192  cbet2 = Math::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 * ((1 + _A1m1) * sig12 + AB1) : s12_a12;
201 
202  if (outmask & LONGITUDE) {
203  // tan(omg2) = sin(alp0) * tan(sig2)
204  real somg2 = _salp0 * ssig2, comg2 = csig2; // No need to normalize
205  int E = _salp0 < 0 ? -1 : 1; // east-going?
206  // omg12 = omg2 - omg1
207  real omg12 = outmask & LONG_UNROLL
208  ? E * (sig12
209  - (atan2( ssig2, csig2) - atan2( _ssig1, _csig1))
210  + (atan2(E * somg2, comg2) - atan2(E * _somg1, _comg1)))
211  : atan2(somg2 * _comg1 - comg2 * _somg1,
212  comg2 * _comg1 + somg2 * _somg1);
213  real lam12 = omg12 + _A3c *
214  ( sig12 + (Geodesic::SinCosSeries(true, ssig2, csig2, _C3a, nC3_-1)
215  - _B31));
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
230  B22 = Geodesic::SinCosSeries(true, ssig2, csig2, _C2a, nC2_),
231  AB2 = (1 + _A2m1) * (B22 - _B21),
232  J12 = (_A1m1 - _A2m1) * sig12 + (AB1 - AB2);
233  if (outmask & REDUCEDLENGTH)
234  // Add parens around (_csig1 * ssig2) and (_ssig1 * csig2) to ensure
235  // accurate cancellation in the case of coincident points.
236  m12 = _b * ((dn2 * (_csig1 * ssig2) - _dn1 * (_ssig1 * csig2))
237  - _csig1 * csig2 * J12);
238  if (outmask & GEODESICSCALE) {
239  real t = _k2 * (ssig2 - _ssig1) * (ssig2 + _ssig1) / (_dn1 + dn2);
240  M12 = csig12 + (t * ssig2 - csig2 * J12) * _ssig1 / _dn1;
241  M21 = csig12 - (t * _ssig1 - _csig1 * J12) * ssig2 / dn2;
242  }
243  }
244 
245  if (outmask & AREA) {
246  real
247  B42 = Geodesic::SinCosSeries(false, ssig2, csig2, _C4a, nC4_);
248  real salp12, calp12;
249  if (_calp0 == 0 || _salp0 == 0) {
250  // alp12 = alp2 - alp1, used in atan2 so no need to normalize
251  salp12 = salp2 * _calp1 - calp2 * _salp1;
252  calp12 = calp2 * _calp1 + salp2 * _salp1;
253  // The right thing appears to happen if alp1 = +/-180 and alp2 = 0, viz
254  // salp12 = -0 and alp12 = -180. However this depends on the sign being
255  // attached to 0 correctly. The following ensures the correct behavior.
256  if (salp12 == 0 && calp12 < 0) {
257  salp12 = tiny_ * _calp1;
258  calp12 = -1;
259  }
260  } else {
261  // tan(alp) = tan(alp0) * sec(sig)
262  // tan(alp2-alp1) = (tan(alp2) -tan(alp1)) / (tan(alp2)*tan(alp1)+1)
263  // = calp0 * salp0 * (csig1-csig2) / (salp0^2 + calp0^2 * csig1*csig2)
264  // If csig12 > 0, write
265  // csig1 - csig2 = ssig12 * (csig1 * ssig12 / (1 + csig12) + ssig1)
266  // else
267  // csig1 - csig2 = csig1 * (1 - csig12) + ssig12 * ssig1
268  // No need to normalize
269  salp12 = _calp0 * _salp0 *
270  (csig12 <= 0 ? _csig1 * (1 - csig12) + ssig12 * _ssig1 :
271  ssig12 * (_csig1 * ssig12 / (1 + csig12) + _ssig1));
272  calp12 = Math::sq(_salp0) + Math::sq(_calp0) * _csig1 * csig2;
273  }
274  S12 = _c2 * atan2(salp12, calp12) + _A4 * (B42 - _B41);
275  }
276 
277  return arcmode ? s12_a12 : sig12 / Math::degree();
278  }
279 
280 } // namespace GeographicLib
static T AngNormalize(T x)
Definition: Math.hpp:451
static T NaN()
Definition: Math.hpp:783
Header for GeographicLib::GeodesicLine class.
Mathematical functions needed by GeographicLib.
Definition: Math.hpp:102
static void sincosd(T x, T &sinx, T &cosx)
Definition: Math.hpp:559
static void norm(T &x, T &y)
Definition: Math.hpp:398
static T hypot(T x, T y)
Definition: Math.hpp:257
static T sq(T x)
Definition: Math.hpp:246
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:676
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
static T degree()
Definition: Math.hpp:230
Geodesic calculations
Definition: Geodesic.hpp:171
static T AngRound(T x)
Definition: Math.hpp:530