GeographicLib  1.35
GeodesicLineExact.cpp
Go to the documentation of this file.
1 /**
2  * \file GeodesicLineExact.cpp
3  * \brief Implementation for GeographicLib::GeodesicLineExact class
4  *
5  * Copyright (c) Charles Karney (2012) <charles@karney.com> and licensed under
6  * 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  , _e2(g._e2)
44  , _E(0, 0)
45  // Always allow latitude and azimuth
46  , _caps(caps | LATITUDE | AZIMUTH)
47  {
48  azi1 = GeodesicExact::AngRound(Math::AngNormalize(azi1));
49  lon1 = Math::AngNormalize(lon1);
50  _lat1 = lat1;
51  _lon1 = lon1;
52  _azi1 = azi1;
53  // alp1 is in [0, pi]
54  real alp1 = azi1 * Math::degree<real>();
55  // Enforce sin(pi) == 0 and cos(pi/2) == 0. Better to face the ensuing
56  // problems directly than to skirt them.
57  _salp1 = azi1 == -180 ? 0 : sin(alp1);
58  _calp1 = abs(azi1) == 90 ? 0 : cos(alp1);
59  real cbet1, sbet1, phi;
60  phi = lat1 * Math::degree<real>();
61  // Ensure cbet1 = +epsilon at poles
62  sbet1 = _f1 * sin(phi);
63  cbet1 = abs(lat1) == 90 ? GeodesicExact::tiny_ : cos(phi);
64  GeodesicExact::SinCosNorm(sbet1, cbet1);
65  _dn1 = (_f >= 0 ? sqrt(1 + g._ep2 * Math::sq(sbet1)) :
66  sqrt(1 - _e2 * Math::sq(cbet1)) / _f1);
67 
68  // Evaluate alp0 from sin(alp1) * cos(bet1) = sin(alp0),
69  _salp0 = _salp1 * cbet1; // alp0 in [0, pi/2 - |bet1|]
70  // Alt: calp0 = hypot(sbet1, calp1 * cbet1). The following
71  // is slightly better (consider the case salp1 = 0).
72  _calp0 = Math::hypot(_calp1, _salp1 * sbet1);
73  // Evaluate sig with tan(bet1) = tan(sig1) * cos(alp1).
74  // sig = 0 is nearest northward crossing of equator.
75  // With bet1 = 0, alp1 = pi/2, we have sig1 = 0 (equatorial line).
76  // With bet1 = pi/2, alp1 = -pi, sig1 = pi/2
77  // With bet1 = -pi/2, alp1 = 0 , sig1 = -pi/2
78  // Evaluate omg1 with tan(omg1) = sin(alp0) * tan(sig1).
79  // With alp0 in (0, pi/2], quadrants for sig and omg coincide.
80  // No atan2(0,0) ambiguity at poles since cbet1 = +epsilon.
81  // With alp0 = 0, omg1 = 0 for alp1 = 0, omg1 = pi for alp1 = pi.
82  _ssig1 = sbet1; _somg1 = _salp0 * sbet1;
83  _csig1 = _comg1 = sbet1 != 0 || _calp1 != 0 ? cbet1 * _calp1 : 1;
84  // Without normalization we have schi1 = somg1.
85  _cchi1 = _f1 * _dn1 * _comg1;
86  GeodesicExact::SinCosNorm(_ssig1, _csig1); // sig1 in (-pi, pi]
87  // GeodesicExact::SinCosNorm(_somg1, _comg1); -- don't need to normalize!
88  // GeodesicExact::SinCosNorm(_schi1, _cchi1); -- don't need to normalize!
89 
90  _k2 = Math::sq(_calp0) * g._ep2;
91  _E.Reset(-_k2, -g._ep2, 1 + _k2, 1 + g._ep2);
92 
93  if (_caps & CAP_E) {
94  _E0 = _E.E() / (Math::pi<real>() / 2);
95  _E1 = _E.deltaE(_ssig1, _csig1, _dn1);
96  real s = sin(_E1), c = cos(_E1);
97  // tau1 = sig1 + B11
98  _stau1 = _ssig1 * c + _csig1 * s;
99  _ctau1 = _csig1 * c - _ssig1 * s;
100  // Not necessary because Einv inverts E
101  // _E1 = -_E.deltaEinv(_stau1, _ctau1);
102  }
103 
104  if (_caps & CAP_D) {
105  _D0 = _E.D() / (Math::pi<real>() / 2);
106  _D1 = _E.deltaD(_ssig1, _csig1, _dn1);
107  }
108 
109  if (_caps & CAP_H) {
110  _H0 = _E.H() / (Math::pi<real>() / 2);
111  _H1 = _E.deltaH(_ssig1, _csig1, _dn1);
112  }
113 
114  if (_caps & CAP_C4) {
115  real eps = _k2 / (2 * (1 + sqrt(1 + _k2)) + _k2);
116  g.C4f(eps, _C4a);
117  // Multiplier = a^2 * e^2 * cos(alpha0) * sin(alpha0)
118  _A4 = Math::sq(_a) * _calp0 * _salp0 * _e2;
119  _B41 = GeodesicExact::CosSeries(_ssig1, _csig1, _C4a, nC4_);
120  }
121  }
122 
123  Math::real GeodesicLineExact::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, E2 = 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 * _E0),
148  s = sin(tau12),
149  c = cos(tau12);
150  // tau2 = tau1 + tau12
151  E2 = - _E.deltaEinv(_stau1 * c + _ctau1 * s, _ctau1 * c - _stau1 * s);
152  sig12 = tau12 - (E2 - _E1);
153  ssig12 = sin(sig12);
154  csig12 = cos(sig12);
155  }
156 
157  real lam12, lon12;
158  real ssig2, csig2, sbet2, cbet2, salp2, calp2;
159  // sig2 = sig1 + sig12
160  ssig2 = _ssig1 * csig12 + _csig1 * ssig12;
161  csig2 = _csig1 * csig12 - _ssig1 * ssig12;
162  real dn2 = _E.Delta(ssig2, csig2);
163  if (outmask & (DISTANCE | REDUCEDLENGTH | GEODESICSCALE)) {
164  if (arcmode) {
165  E2 = _E.deltaE(ssig2, csig2, dn2);
166  }
167  AB1 = _E0 * (E2 - _E1);
168  }
169  // sin(bet2) = cos(alp0) * sin(sig2)
170  sbet2 = _calp0 * ssig2;
171  // Alt: cbet2 = hypot(csig2, salp0 * ssig2);
172  cbet2 = Math::hypot(_salp0, _calp0 * csig2);
173  if (cbet2 == 0)
174  // I.e., salp0 = 0, csig2 = 0. Break the degeneracy in this case
175  cbet2 = csig2 = GeodesicExact::tiny_;
176  // tan(alp0) = cos(sig2)*tan(alp2)
177  salp2 = _salp0; calp2 = _calp0 * csig2; // No need to normalize
178 
179  if (outmask & DISTANCE)
180  s12 = arcmode ? _b * (_E0 * sig12 + AB1) : s12_a12;
181 
182  if (outmask & LONGITUDE) {
183  real somg2 = _salp0 * ssig2, comg2 = csig2; // No need to normalize
184  // Without normalization we have schi2 = somg2.
185  real cchi2 = _f1 * dn2 * comg2;
186  lam12 = atan2(somg2 * _cchi1 - cchi2 * _somg1,
187  cchi2 * _cchi1 + somg2 * _somg1) -
188  _e2/_f1 * _salp0 * _H0 * (sig12 + _E.deltaH(ssig2, csig2, dn2) - _H1 );
189  lon12 = lam12 / Math::degree<real>();
190  // Use Math::AngNormalize2 because longitude might have wrapped multiple
191  // times.
192  lon12 = Math::AngNormalize2(lon12);
193  lon2 = Math::AngNormalize(_lon1 + lon12);
194  }
195 
196  if (outmask & LATITUDE)
197  lat2 = atan2(sbet2, _f1 * cbet2) / Math::degree<real>();
198 
199  if (outmask & AZIMUTH)
200  // minus signs give range [-180, 180). 0- converts -0 to +0.
201  azi2 = 0 - atan2(-salp2, calp2) / Math::degree<real>();
202 
203  if (outmask & (REDUCEDLENGTH | GEODESICSCALE)) {
204  real J12 = _k2 * _D0 * (sig12 + _E.deltaD(ssig2, csig2, dn2) - _D1);
205  if (outmask & REDUCEDLENGTH)
206  // Add parens around (_csig1 * ssig2) and (_ssig1 * csig2) to ensure
207  // accurate cancellation in the case of coincident points.
208  m12 = _b * ((dn2 * (_csig1 * ssig2) - _dn1 * (_ssig1 * csig2))
209  - _csig1 * csig2 * J12);
210  if (outmask & GEODESICSCALE) {
211  real t = _k2 * (ssig2 - _ssig1) * (ssig2 + _ssig1) / (_dn1 + dn2);
212  M12 = csig12 + (t * ssig2 - csig2 * J12) * _ssig1 / _dn1;
213  M21 = csig12 - (t * _ssig1 - _csig1 * J12) * ssig2 / dn2;
214  }
215  }
216 
217  if (outmask & AREA) {
218  real
219  B42 = GeodesicExact::CosSeries(ssig2, csig2, _C4a, nC4_);
220  real salp12, calp12;
221  if (_calp0 == 0 || _salp0 == 0) {
222  // alp12 = alp2 - alp1, used in atan2 so no need to normalized
223  salp12 = salp2 * _calp1 - calp2 * _salp1;
224  calp12 = calp2 * _calp1 + salp2 * _salp1;
225  // The right thing appears to happen if alp1 = +/-180 and alp2 = 0, viz
226  // salp12 = -0 and alp12 = -180. However this depends on the sign being
227  // attached to 0 correctly. The following ensures the correct behavior.
228  if (salp12 == 0 && calp12 < 0) {
229  salp12 = GeodesicExact::tiny_ * _calp1;
230  calp12 = -1;
231  }
232  } else {
233  // tan(alp) = tan(alp0) * sec(sig)
234  // tan(alp2-alp1) = (tan(alp2) -tan(alp1)) / (tan(alp2)*tan(alp1)+1)
235  // = calp0 * salp0 * (csig1-csig2) / (salp0^2 + calp0^2 * csig1*csig2)
236  // If csig12 > 0, write
237  // csig1 - csig2 = ssig12 * (csig1 * ssig12 / (1 + csig12) + ssig1)
238  // else
239  // csig1 - csig2 = csig1 * (1 - csig12) + ssig12 * ssig1
240  // No need to normalize
241  salp12 = _calp0 * _salp0 *
242  (csig12 <= 0 ? _csig1 * (1 - csig12) + ssig12 * _ssig1 :
243  ssig12 * (_csig1 * ssig12 / (1 + csig12) + _ssig1));
244  calp12 = Math::sq(_salp0) + Math::sq(_calp0) * _csig1 * csig2;
245  }
246  S12 = _c2 * atan2(salp12, calp12) + _A4 * (B42 - _B41);
247  }
248 
249  return arcmode ? s12_a12 : sig12 / Math::degree<real>();
250  }
251 
252 } // namespace GeographicLib
static T AngNormalize(T x)
Definition: Math.hpp:388
static T hypot(T x, T y)
Definition: Math.hpp:165
static T sq(T x)
Definition: Math.hpp:153
Header for GeographicLib::GeodesicLineExact class.
Exact geodesic calculations.
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 AngNormalize2(T x)
Definition: Math.hpp:400