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