src/GeodesicLineExact.cpp
Go to the documentation of this file.
1 
30 
31 namespace GeographicLib {
32 
33  using namespace std;
34 
36  real lat1, real lon1,
37  real azi1, real salp1, real calp1,
38  unsigned caps) {
39  tiny_ = g.tiny_;
40  _lat1 = Math::LatFix(lat1);
41  _lon1 = lon1;
42  _azi1 = azi1;
43  _salp1 = salp1;
44  _calp1 = calp1;
45  _a = g._a;
46  _f = g._f;
47  _b = g._b;
48  _c2 = g._c2;
49  _f1 = g._f1;
50  _e2 = g._e2;
51  // Always allow latitude and azimuth and unrolling of longitude
52  _caps = caps | LATITUDE | AZIMUTH | LONG_UNROLL;
53 
54  real cbet1, sbet1;
55  Math::sincosd(Math::AngRound(_lat1), sbet1, cbet1); sbet1 *= _f1;
56  // Ensure cbet1 = +epsilon at poles
57  Math::norm(sbet1, cbet1); cbet1 = max(tiny_, cbet1);
58  _dn1 = (_f >= 0 ? sqrt(1 + g._ep2 * Math::sq(sbet1)) :
59  sqrt(1 - _e2 * Math::sq(cbet1)) / _f1);
60 
61  // Evaluate alp0 from sin(alp1) * cos(bet1) = sin(alp0),
62  _salp0 = _salp1 * cbet1; // alp0 in [0, pi/2 - |bet1|]
63  // Alt: calp0 = hypot(sbet1, calp1 * cbet1). The following
64  // is slightly better (consider the case salp1 = 0).
65  _calp0 = Math::hypot(_calp1, _salp1 * sbet1);
66  // Evaluate sig with tan(bet1) = tan(sig1) * cos(alp1).
67  // sig = 0 is nearest northward crossing of equator.
68  // With bet1 = 0, alp1 = pi/2, we have sig1 = 0 (equatorial line).
69  // With bet1 = pi/2, alp1 = -pi, sig1 = pi/2
70  // With bet1 = -pi/2, alp1 = 0 , sig1 = -pi/2
71  // Evaluate omg1 with tan(omg1) = sin(alp0) * tan(sig1).
72  // With alp0 in (0, pi/2], quadrants for sig and omg coincide.
73  // No atan2(0,0) ambiguity at poles since cbet1 = +epsilon.
74  // With alp0 = 0, omg1 = 0 for alp1 = 0, omg1 = pi for alp1 = pi.
75  _ssig1 = sbet1; _somg1 = _salp0 * sbet1;
76  _csig1 = _comg1 = sbet1 != 0 || _calp1 != 0 ? cbet1 * _calp1 : 1;
77  // Without normalization we have schi1 = somg1.
78  _cchi1 = _f1 * _dn1 * _comg1;
79  Math::norm(_ssig1, _csig1); // sig1 in (-pi, pi]
80  // Math::norm(_somg1, _comg1); -- don't need to normalize!
81  // Math::norm(_schi1, _cchi1); -- don't need to normalize!
82 
83  _k2 = Math::sq(_calp0) * g._ep2;
84  _E.Reset(-_k2, -g._ep2, 1 + _k2, 1 + g._ep2);
85 
86  if (_caps & CAP_E) {
87  _E0 = _E.E() / (Math::pi() / 2);
88  _E1 = _E.deltaE(_ssig1, _csig1, _dn1);
89  real s = sin(_E1), c = cos(_E1);
90  // tau1 = sig1 + B11
91  _stau1 = _ssig1 * c + _csig1 * s;
92  _ctau1 = _csig1 * c - _ssig1 * s;
93  // Not necessary because Einv inverts E
94  // _E1 = -_E.deltaEinv(_stau1, _ctau1);
95  }
96 
97  if (_caps & CAP_D) {
98  _D0 = _E.D() / (Math::pi() / 2);
99  _D1 = _E.deltaD(_ssig1, _csig1, _dn1);
100  }
101 
102  if (_caps & CAP_H) {
103  _H0 = _E.H() / (Math::pi() / 2);
104  _H1 = _E.deltaH(_ssig1, _csig1, _dn1);
105  }
106 
107  if (_caps & CAP_C4) {
108  real eps = _k2 / (2 * (1 + sqrt(1 + _k2)) + _k2);
109  g.C4f(eps, _C4a);
110  // Multiplier = a^2 * e^2 * cos(alpha0) * sin(alpha0)
111  _A4 = Math::sq(_a) * _calp0 * _salp0 * _e2;
112  _B41 = GeodesicExact::CosSeries(_ssig1, _csig1, _C4a, nC4_);
113  }
114 
115  _a13 = _s13 = Math::NaN();
116  }
117 
119  real lat1, real lon1, real azi1,
120  unsigned caps) {
121  azi1 = Math::AngNormalize(azi1);
122  real salp1, calp1;
123  // Guard against underflow in salp0. Also -0 is converted to +0.
124  Math::sincosd(Math::AngRound(azi1), salp1, calp1);
125  LineInit(g, lat1, lon1, azi1, salp1, calp1, caps);
126  }
127 
129  real lat1, real lon1,
130  real azi1, real salp1, real calp1,
131  unsigned caps,
132  bool arcmode, real s13_a13) {
133  LineInit(g, lat1, lon1, azi1, salp1, calp1, caps);
134  GenSetDistance(arcmode, s13_a13);
135  }
136 
138  unsigned outmask,
139  real& lat2, real& lon2, real& azi2,
140  real& s12, real& m12,
141  real& M12, real& M21,
142  real& S12) const {
143  outmask &= _caps & OUT_MASK;
144  if (!( Init() && (arcmode || (_caps & (OUT_MASK & DISTANCE_IN))) ))
145  // Uninitialized or impossible distance calculation requested
146  return Math::NaN();
147 
148  // Avoid warning about uninitialized B12.
149  real sig12, ssig12, csig12, E2 = 0, AB1 = 0;
150  if (arcmode) {
151  // Interpret s12_a12 as spherical arc length
152  sig12 = s12_a12 * Math::degree();
153  real s12a = abs(s12_a12);
154  s12a -= 180 * floor(s12a / 180);
155  ssig12 = s12a == 0 ? 0 : sin(sig12);
156  csig12 = s12a == 90 ? 0 : cos(sig12);
157  } else {
158  // Interpret s12_a12 as distance
159  real
160  tau12 = s12_a12 / (_b * _E0),
161  s = sin(tau12),
162  c = cos(tau12);
163  // tau2 = tau1 + tau12
164  E2 = - _E.deltaEinv(_stau1 * c + _ctau1 * s, _ctau1 * c - _stau1 * s);
165  sig12 = tau12 - (E2 - _E1);
166  ssig12 = sin(sig12);
167  csig12 = cos(sig12);
168  }
169 
170  real ssig2, csig2, sbet2, cbet2, salp2, calp2;
171  // sig2 = sig1 + sig12
172  ssig2 = _ssig1 * csig12 + _csig1 * ssig12;
173  csig2 = _csig1 * csig12 - _ssig1 * ssig12;
174  real dn2 = _E.Delta(ssig2, csig2);
175  if (outmask & (DISTANCE | REDUCEDLENGTH | GEODESICSCALE)) {
176  if (arcmode) {
177  E2 = _E.deltaE(ssig2, csig2, dn2);
178  }
179  AB1 = _E0 * (E2 - _E1);
180  }
181  // sin(bet2) = cos(alp0) * sin(sig2)
182  sbet2 = _calp0 * ssig2;
183  // Alt: cbet2 = hypot(csig2, salp0 * ssig2);
184  cbet2 = Math::hypot(_salp0, _calp0 * csig2);
185  if (cbet2 == 0)
186  // I.e., salp0 = 0, csig2 = 0. Break the degeneracy in this case
187  cbet2 = csig2 = tiny_;
188  // tan(alp0) = cos(sig2)*tan(alp2)
189  salp2 = _salp0; calp2 = _calp0 * csig2; // No need to normalize
190 
191  if (outmask & DISTANCE)
192  s12 = arcmode ? _b * (_E0 * sig12 + AB1) : s12_a12;
193 
194  if (outmask & LONGITUDE) {
195  real somg2 = _salp0 * ssig2, comg2 = csig2, // No need to normalize
196  E = Math::copysign(real(1), _salp0); // east-going?
197  // Without normalization we have schi2 = somg2.
198  real cchi2 = _f1 * dn2 * comg2;
199  real chi12 = outmask & LONG_UNROLL
200  ? E * (sig12
201  - (atan2( ssig2, csig2) - atan2( _ssig1, _csig1))
202  + (atan2(E * somg2, cchi2) - atan2(E * _somg1, _cchi1)))
203  : atan2(somg2 * _cchi1 - cchi2 * _somg1,
204  cchi2 * _cchi1 + somg2 * _somg1);
205  real lam12 = chi12 -
206  _e2/_f1 * _salp0 * _H0 *
207  (sig12 + (_E.deltaH(ssig2, csig2, dn2) - _H1));
208  real lon12 = lam12 / Math::degree();
209  lon2 = outmask & LONG_UNROLL ? _lon1 + lon12 :
211  Math::AngNormalize(lon12));
212  }
213 
214  if (outmask & LATITUDE)
215  lat2 = Math::atan2d(sbet2, _f1 * cbet2);
216 
217  if (outmask & AZIMUTH)
218  azi2 = Math::atan2d(salp2, calp2);
219 
220  if (outmask & (REDUCEDLENGTH | GEODESICSCALE)) {
221  real J12 = _k2 * _D0 * (sig12 + (_E.deltaD(ssig2, csig2, dn2) - _D1));
222  if (outmask & REDUCEDLENGTH)
223  // Add parens around (_csig1 * ssig2) and (_ssig1 * csig2) to ensure
224  // accurate cancellation in the case of coincident points.
225  m12 = _b * ((dn2 * (_csig1 * ssig2) - _dn1 * (_ssig1 * csig2))
226  - _csig1 * csig2 * J12);
227  if (outmask & GEODESICSCALE) {
228  real t = _k2 * (ssig2 - _ssig1) * (ssig2 + _ssig1) / (_dn1 + dn2);
229  M12 = csig12 + (t * ssig2 - csig2 * J12) * _ssig1 / _dn1;
230  M21 = csig12 - (t * _ssig1 - _csig1 * J12) * ssig2 / dn2;
231  }
232  }
233 
234  if (outmask & AREA) {
235  real
236  B42 = GeodesicExact::CosSeries(ssig2, csig2, _C4a, nC4_);
237  real salp12, calp12;
238  if (_calp0 == 0 || _salp0 == 0) {
239  // alp12 = alp2 - alp1, used in atan2 so no need to normalize
240  salp12 = salp2 * _calp1 - calp2 * _salp1;
241  calp12 = calp2 * _calp1 + salp2 * _salp1;
242  // We used to include here some patch up code that purported to deal
243  // with nearly meridional geodesics properly. However, this turned out
244  // to be wrong once _salp1 = -0 was allowed (via
245  // GeodesicExact::InverseLine). In fact, the calculation of {s,c}alp12
246  // was already correct (following the IEEE rules for handling signed
247  // zeros). So the patch up code was unnecessary (as well as
248  // dangerous).
249  } else {
250  // tan(alp) = tan(alp0) * sec(sig)
251  // tan(alp2-alp1) = (tan(alp2) -tan(alp1)) / (tan(alp2)*tan(alp1)+1)
252  // = calp0 * salp0 * (csig1-csig2) / (salp0^2 + calp0^2 * csig1*csig2)
253  // If csig12 > 0, write
254  // csig1 - csig2 = ssig12 * (csig1 * ssig12 / (1 + csig12) + ssig1)
255  // else
256  // csig1 - csig2 = csig1 * (1 - csig12) + ssig12 * ssig1
257  // No need to normalize
258  salp12 = _calp0 * _salp0 *
259  (csig12 <= 0 ? _csig1 * (1 - csig12) + ssig12 * _ssig1 :
260  ssig12 * (_csig1 * ssig12 / (1 + csig12) + _ssig1));
261  calp12 = Math::sq(_salp0) + Math::sq(_calp0) * _csig1 * csig2;
262  }
263  S12 = _c2 * atan2(salp12, calp12) + _A4 * (B42 - _B41);
264  }
265 
266  return arcmode ? s12_a12 : sig12 / Math::degree();
267  }
268 
270  _s13 = s13;
271  real t;
272  // This will set _a13 to NaN if the GeodesicLineExact doesn't have the
273  // DISTANCE_IN capability.
274  _a13 = GenPosition(false, _s13, 0u, t, t, t, t, t, t, t, t);
275  }
276 
278  _a13 = a13;
279  // In case the GeodesicLineExact doesn't have the DISTANCE capability.
280  _s13 = Math::NaN();
281  real t;
282  GenPosition(true, _a13, DISTANCE, t, t, t, _s13, t, t, t, t);
283  }
284 
285  void GeodesicLineExact::GenSetDistance(bool arcmode, real s13_a13) {
286  arcmode ? SetArc(s13_a13) : SetDistance(s13_a13);
287  }
288 
289 } // namespace GeographicLib
static T AngNormalize(T x)
Definition: Math.hpp:440
static T NaN()
Definition: Math.hpp:830
#define max(a, b)
Definition: datatypes.h:20
static T pi()
Definition: Math.hpp:202
Key E(std::uint64_t j)
float real
Definition: datatypes.h:10
static real CosSeries(real sinx, real cosx, const real c[], int n)
void LineInit(const GeodesicExact &g, real lat1, real lon1, real azi1, real salp1, real calp1, unsigned caps)
static T LatFix(T x)
Definition: Math.hpp:467
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
EIGEN_DEVICE_FUNC const SqrtReturnType sqrt() const
Definition: Half.h:150
static void sincosd(T x, T &sinx, T &cosx)
Definition: Math.hpp:558
static void norm(T &x, T &y)
Definition: Math.hpp:384
void g(const string &key, int i)
Definition: testBTree.cpp:43
void GenSetDistance(bool arcmode, real s13_a13)
EIGEN_DEVICE_FUNC const CosReturnType cos() const
static T hypot(T x, T y)
Definition: Math.hpp:243
static T sq(T x)
Definition: Math.hpp:232
Header for GeographicLib::GeodesicLineExact class.
static T atan2d(T y, T x)
Definition: Math.hpp:691
Namespace for GeographicLib.
RealScalar s
static T degree()
Definition: Math.hpp:216
Jet< T, N > atan2(const Jet< T, N > &g, const Jet< T, N > &f)
Definition: jet.h:556
Exact geodesic calculations.
static T copysign(T x, T y)
Definition: Math.hpp:751
EIGEN_DEVICE_FUNC const FloorReturnType floor() const
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
EIGEN_DEVICE_FUNC const SinReturnType sin() const
void C4f(real k2, real c[]) const
#define abs(x)
Definition: datatypes.h:17
static T AngRound(T x)
Definition: Math.hpp:535
Point2 t(10, 10)


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:42:08