GeographicLib 2.5.2

Loading...
Searching...
No Matches
Gnomonic.cpp
Go to the documentation of this file.
1/**
2 * \file Gnomonic.cpp
3 * \brief Implementation for GeographicLib::Gnomonic class
4 *
5 * Copyright (c) Charles Karney (2010-2022) <karney@alum.mit.edu> and licensed
6 * under the MIT/X11 License. For more information, see
7 * https://geographiclib.sourceforge.io/
8 **********************************************************************/
9
10#include <GeographicLib/Gnomonic.hpp>
11
12#if defined(_MSC_VER)
13// Squelch warnings about potentially uninitialized local variables
14# pragma warning (disable: 4701)
15#endif
16
17 namespace GeographicLib {
18
19 using namespace std;
20
21 Gnomonic::Gnomonic(const Geodesic& earth)
22 : eps0_(numeric_limits<real>::epsilon())
23 , eps_(real(0.01) * sqrt(eps0_))
24 , _earth(earth)
25 , _a(_earth.EquatorialRadius())
26 , _f(_earth.Flattening())
27 {}
28
29 void Gnomonic::Forward(real lat0, real lon0, real lat, real lon,
30 real& x, real& y, real& azi, real& rk) const {
31 real azi0, m, M, t;
32 _earth.GenInverse(lat0, lon0, lat, lon,
33 Geodesic::AZIMUTH | Geodesic::REDUCEDLENGTH |
34 Geodesic::GEODESICSCALE,
35 t, azi0, azi, m, M, t, t);
36 rk = M;
37 if (M <= 0)
38 x = y = Math::NaN();
39 else {
40 real rho = m/M;
41 Math::sincosd(azi0, x, y);
42 x *= rho; y *= rho;
43 }
44 }
45
46 void Gnomonic::Reverse(real lat0, real lon0, real x, real y,
47 real& lat, real& lon, real& azi, real& rk) const {
48 real
49 azi0 = Math::atan2d(x, y),
50 rho = hypot(x, y),
51 s = _a * atan(rho/_a);
52 bool little = rho <= _a;
53 if (!little)
54 rho = 1/rho;
55 GeodesicLine line(_earth.Line(lat0, lon0, azi0,
56 Geodesic::LATITUDE | Geodesic::LONGITUDE |
57 Geodesic::AZIMUTH | Geodesic::DISTANCE_IN |
58 Geodesic::REDUCEDLENGTH |
59 Geodesic::GEODESICSCALE));
60 int count = numit_, trip = 0;
61 real lat1, lon1, azi1, M;
62 while (count-- ||
63 GEOGRAPHICLIB_PANIC("Convergence failure in Gnomonic")) {
64 real m, t;
65 line.Position(s, lat1, lon1, azi1, m, M, t);
66 if (trip)
67 break;
68 // If little, solve rho(s) = rho with drho(s)/ds = 1/M^2
69 // else solve 1/rho(s) = 1/rho with d(1/rho(s))/ds = -1/m^2
70 real ds = little ? (m - rho * M) * M : (rho * m - M) * m;
71 s -= ds;
72 // Reversed test to allow escape with NaNs
73 if (!(fabs(ds) >= eps_ * _a))
74 ++trip;
75 }
76 if (trip) {
77 lat = lat1; lon = lon1; azi = azi1; rk = M;
78 } else
79 lat = lon = azi = rk = Math::NaN();
80 return;
81 }
82
83} // namespace GeographicLib
Gnomonic.hpp
Header for GeographicLib::Gnomonic class.
GEOGRAPHICLIB_PANIC
#define GEOGRAPHICLIB_PANIC(msg)
Definition Math.hpp:67
GeographicLib::GeodesicLine
A geodesic line.
Definition GeodesicLine.hpp:74
GeographicLib::GeodesicLine::Position
Math::real Position(real s12, real &lat2, real &lon2, real &azi2, real &m12, real &M12, real &M21, real &S12) const
Definition GeodesicLine.hpp:297
GeographicLib::Geodesic
Geodesic calculations
Definition Geodesic.hpp:175
GeographicLib::Geodesic::Line
GeodesicLine Line(real lat1, real lon1, real azi1, unsigned caps=ALL) const
Definition Geodesic.cpp:123
GeographicLib::Geodesic::REDUCEDLENGTH
@ REDUCEDLENGTH
Definition Geodesic.hpp:311
GeographicLib::Geodesic::LATITUDE
@ LATITUDE
Definition Geodesic.hpp:277
GeographicLib::Geodesic::LONGITUDE
@ LONGITUDE
Definition Geodesic.hpp:282
GeographicLib::Geodesic::AZIMUTH
@ AZIMUTH
Definition Geodesic.hpp:289
GeographicLib::Geodesic::DISTANCE_IN
@ DISTANCE_IN
Definition Geodesic.hpp:306
GeographicLib::Geodesic::GEODESICSCALE
@ GEODESICSCALE
Definition Geodesic.hpp:316
GeographicLib::Gnomonic::Gnomonic
Gnomonic(const Geodesic &earth=Geodesic::WGS84())
Definition Gnomonic.cpp:21
GeographicLib::Gnomonic::Forward
void Forward(real lat0, real lon0, real lat, real lon, real &x, real &y, real &azi, real &rk) const
Definition Gnomonic.cpp:29
GeographicLib::Gnomonic::Reverse
void Reverse(real lat0, real lon0, real x, real y, real &lat, real &lon, real &azi, real &rk) const
Definition Gnomonic.cpp:46
GeographicLib::Math::sincosd
static void sincosd(T x, T &sinx, T &cosx)
Definition Math.cpp:104
GeographicLib::Math::atan2d
static T atan2d(T y, T x)
Definition Math.cpp:202
GeographicLib::Math::NaN
static T NaN()
Definition Math.cpp:283
GeographicLib
Namespace for GeographicLib.
Definition Accumulator.cpp:12
std
Definition NearestNeighbor.hpp:806