GeographicLib  1.43
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-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  , _e2(g._e2)
49  , _E(0, 0)
50  // Always allow latitude and azimuth and unrolling of longitude
51  , _caps(caps | LATITUDE | AZIMUTH | LONG_UNROLL)
52  {
53  real alp1 = _azi1 * Math::degree();
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();
60  // Ensure cbet1 = +epsilon at poles
61  sbet1 = _f1 * sin(phi);
62  cbet1 = abs(lat1) == 90 ? tiny_ : cos(phi);
63  Math::norm(sbet1, cbet1);
64  _dn1 = (_f >= 0 ? sqrt(1 + g._ep2 * Math::sq(sbet1)) :
65  sqrt(1 - _e2 * Math::sq(cbet1)) / _f1);
66 
67  // Evaluate alp0 from sin(alp1) * cos(bet1) = sin(alp0),
68  _salp0 = _salp1 * cbet1; // alp0 in [0, pi/2 - |bet1|]
69  // Alt: calp0 = hypot(sbet1, calp1 * cbet1). The following
70  // is slightly better (consider the case salp1 = 0).
71  _calp0 = Math::hypot(_calp1, _salp1 * sbet1);
72  // Evaluate sig with tan(bet1) = tan(sig1) * cos(alp1).
73  // sig = 0 is nearest northward crossing of equator.
74  // With bet1 = 0, alp1 = pi/2, we have sig1 = 0 (equatorial line).
75  // With bet1 = pi/2, alp1 = -pi, sig1 = pi/2
76  // With bet1 = -pi/2, alp1 = 0 , sig1 = -pi/2
77  // Evaluate omg1 with tan(omg1) = sin(alp0) * tan(sig1).
78  // With alp0 in (0, pi/2], quadrants for sig and omg coincide.
79  // No atan2(0,0) ambiguity at poles since cbet1 = +epsilon.
80  // With alp0 = 0, omg1 = 0 for alp1 = 0, omg1 = pi for alp1 = pi.
81  _ssig1 = sbet1; _somg1 = _salp0 * sbet1;
82  _csig1 = _comg1 = sbet1 != 0 || _calp1 != 0 ? cbet1 * _calp1 : 1;
83  // Without normalization we have schi1 = somg1.
84  _cchi1 = _f1 * _dn1 * _comg1;
85  Math::norm(_ssig1, _csig1); // sig1 in (-pi, pi]
86  // Math::norm(_somg1, _comg1); -- don't need to normalize!
87  // Math::norm(_schi1, _cchi1); -- don't need to normalize!
88 
89  _k2 = Math::sq(_calp0) * g._ep2;
90  _E.Reset(-_k2, -g._ep2, 1 + _k2, 1 + g._ep2);
91 
92  if (_caps & CAP_E) {
93  _E0 = _E.E() / (Math::pi() / 2);
94  _E1 = _E.deltaE(_ssig1, _csig1, _dn1);
95  real s = sin(_E1), c = cos(_E1);
96  // tau1 = sig1 + B11
97  _stau1 = _ssig1 * c + _csig1 * s;
98  _ctau1 = _csig1 * c - _ssig1 * s;
99  // Not necessary because Einv inverts E
100  // _E1 = -_E.deltaEinv(_stau1, _ctau1);
101  }
102 
103  if (_caps & CAP_D) {
104  _D0 = _E.D() / (Math::pi() / 2);
105  _D1 = _E.deltaD(_ssig1, _csig1, _dn1);
106  }
107 
108  if (_caps & CAP_H) {
109  _H0 = _E.H() / (Math::pi() / 2);
110  _H1 = _E.deltaH(_ssig1, _csig1, _dn1);
111  }
112 
113  if (_caps & CAP_C4) {
114  real eps = _k2 / (2 * (1 + sqrt(1 + _k2)) + _k2);
115  g.C4f(eps, _C4a);
116  // Multiplier = a^2 * e^2 * cos(alpha0) * sin(alpha0)
117  _A4 = Math::sq(_a) * _calp0 * _salp0 * _e2;
118  _B41 = GeodesicExact::CosSeries(_ssig1, _csig1, _C4a, nC4_);
119  }
120  }
121 
122  Math::real GeodesicLineExact::GenPosition(bool arcmode, real s12_a12,
123  unsigned outmask,
124  real& lat2, real& lon2, real& azi2,
125  real& s12, real& m12,
126  real& M12, real& M21,
127  real& S12)
128  const {
129  outmask &= _caps & OUT_MASK;
130  if (!( Init() && (arcmode || (_caps & DISTANCE_IN & OUT_MASK)) ))
131  // Uninitialized or impossible distance calculation requested
132  return Math::NaN();
133 
134  // Avoid warning about uninitialized B12.
135  real sig12, ssig12, csig12, E2 = 0, AB1 = 0;
136  if (arcmode) {
137  // Interpret s12_a12 as spherical arc length
138  sig12 = s12_a12 * Math::degree();
139  real s12a = abs(s12_a12);
140  s12a -= 180 * floor(s12a / 180);
141  ssig12 = s12a == 0 ? 0 : sin(sig12);
142  csig12 = s12a == 90 ? 0 : cos(sig12);
143  } else {
144  // Interpret s12_a12 as distance
145  real
146  tau12 = s12_a12 / (_b * _E0),
147  s = sin(tau12),
148  c = cos(tau12);
149  // tau2 = tau1 + tau12
150  E2 = - _E.deltaEinv(_stau1 * c + _ctau1 * s, _ctau1 * c - _stau1 * s);
151  sig12 = tau12 - (E2 - _E1);
152  ssig12 = sin(sig12);
153  csig12 = cos(sig12);
154  }
155 
156  real ssig2, csig2, sbet2, cbet2, salp2, calp2;
157  // sig2 = sig1 + sig12
158  ssig2 = _ssig1 * csig12 + _csig1 * ssig12;
159  csig2 = _csig1 * csig12 - _ssig1 * ssig12;
160  real dn2 = _E.Delta(ssig2, csig2);
161  if (outmask & (DISTANCE | REDUCEDLENGTH | GEODESICSCALE)) {
162  if (arcmode) {
163  E2 = _E.deltaE(ssig2, csig2, dn2);
164  }
165  AB1 = _E0 * (E2 - _E1);
166  }
167  // sin(bet2) = cos(alp0) * sin(sig2)
168  sbet2 = _calp0 * ssig2;
169  // Alt: cbet2 = hypot(csig2, salp0 * ssig2);
170  cbet2 = Math::hypot(_salp0, _calp0 * csig2);
171  if (cbet2 == 0)
172  // I.e., salp0 = 0, csig2 = 0. Break the degeneracy in this case
173  cbet2 = csig2 = tiny_;
174  // tan(alp0) = cos(sig2)*tan(alp2)
175  salp2 = _salp0; calp2 = _calp0 * csig2; // No need to normalize
176 
177  if (outmask & DISTANCE)
178  s12 = arcmode ? _b * (_E0 * sig12 + AB1) : s12_a12;
179 
180  if (outmask & LONGITUDE) {
181  real somg2 = _salp0 * ssig2, comg2 = csig2; // No need to normalize
182  int E = _salp0 < 0 ? -1 : 1; // east-going?
183  // Without normalization we have schi2 = somg2.
184  real cchi2 = _f1 * dn2 * comg2;
185  real chi12 = outmask & LONG_UNROLL
186  ? E * (sig12
187  - (atan2( ssig2, csig2) - atan2( _ssig1, _csig1))
188  + (atan2(E * somg2, cchi2) - atan2(E * _somg1, _cchi1)))
189  : atan2(somg2 * _cchi1 - cchi2 * _somg1,
190  cchi2 * _cchi1 + somg2 * _somg1);
191  real lam12 = chi12 -
192  _e2/_f1 * _salp0 * _H0 * (sig12 + _E.deltaH(ssig2, csig2, dn2) - _H1 );
193  real lon12 = lam12 / Math::degree();
194  // Use Math::AngNormalize2 because longitude might have wrapped
195  // multiple times.
196  lon2 = outmask & LONG_UNROLL ? _lon1 + lon12 :
198  Math::AngNormalize2(lon12));
199  }
200 
201  if (outmask & LATITUDE)
202  lat2 = atan2(sbet2, _f1 * cbet2) / Math::degree();
203 
204  if (outmask & AZIMUTH)
205  azi2 = Math::atan2d(salp2, calp2);
206 
207  if (outmask & (REDUCEDLENGTH | GEODESICSCALE)) {
208  real J12 = _k2 * _D0 * (sig12 + _E.deltaD(ssig2, csig2, dn2) - _D1);
209  if (outmask & REDUCEDLENGTH)
210  // Add parens around (_csig1 * ssig2) and (_ssig1 * csig2) to ensure
211  // accurate cancellation in the case of coincident points.
212  m12 = _b * ((dn2 * (_csig1 * ssig2) - _dn1 * (_ssig1 * csig2))
213  - _csig1 * csig2 * J12);
214  if (outmask & GEODESICSCALE) {
215  real t = _k2 * (ssig2 - _ssig1) * (ssig2 + _ssig1) / (_dn1 + dn2);
216  M12 = csig12 + (t * ssig2 - csig2 * J12) * _ssig1 / _dn1;
217  M21 = csig12 - (t * _ssig1 - _csig1 * J12) * ssig2 / dn2;
218  }
219  }
220 
221  if (outmask & AREA) {
222  real
223  B42 = GeodesicExact::CosSeries(ssig2, csig2, _C4a, nC4_);
224  real salp12, calp12;
225  if (_calp0 == 0 || _salp0 == 0) {
226  // alp12 = alp2 - alp1, used in atan2 so no need to normalize
227  salp12 = salp2 * _calp1 - calp2 * _salp1;
228  calp12 = calp2 * _calp1 + salp2 * _salp1;
229  // The right thing appears to happen if alp1 = +/-180 and alp2 = 0, viz
230  // salp12 = -0 and alp12 = -180. However this depends on the sign being
231  // attached to 0 correctly. The following ensures the correct behavior.
232  if (salp12 == 0 && calp12 < 0) {
233  salp12 = tiny_ * _calp1;
234  calp12 = -1;
235  }
236  } else {
237  // tan(alp) = tan(alp0) * sec(sig)
238  // tan(alp2-alp1) = (tan(alp2) -tan(alp1)) / (tan(alp2)*tan(alp1)+1)
239  // = calp0 * salp0 * (csig1-csig2) / (salp0^2 + calp0^2 * csig1*csig2)
240  // If csig12 > 0, write
241  // csig1 - csig2 = ssig12 * (csig1 * ssig12 / (1 + csig12) + ssig1)
242  // else
243  // csig1 - csig2 = csig1 * (1 - csig12) + ssig12 * ssig1
244  // No need to normalize
245  salp12 = _calp0 * _salp0 *
246  (csig12 <= 0 ? _csig1 * (1 - csig12) + ssig12 * _ssig1 :
247  ssig12 * (_csig1 * ssig12 / (1 + csig12) + _ssig1));
248  calp12 = Math::sq(_salp0) + Math::sq(_calp0) * _csig1 * csig2;
249  }
250  S12 = _c2 * atan2(salp12, calp12) + _A4 * (B42 - _B41);
251  }
252 
253  return arcmode ? s12_a12 : sig12 / Math::degree();
254  }
255 
256 } // namespace GeographicLib
static T AngNormalize(T x)
Definition: Math.hpp:445
static T NaN()
Definition: Math.hpp:629
static T pi()
Definition: Math.hpp:214
void Reset(real k2=0, real alpha2=0)
Math::real deltaE(real sn, real cn, real dn) const
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
Header for GeographicLib::GeodesicLineExact class.
static T atan2d(T y, T x)
Definition: Math.hpp:551
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
static T degree()
Definition: Math.hpp:228
Math::real deltaH(real sn, real cn, real dn) const
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
Math::real Delta(real sn, real cn) const
Math::real deltaD(real sn, real cn, real dn) const
Math::real deltaEinv(real stau, real ctau) const
static T AngNormalize2(T x)
Definition: Math.hpp:457