8#include <janus/math/Arithmetic.hpp>
9#include <janus/math/Logic.hpp>
10#include <janus/math/Trig.hpp>
27template <
typename Scalar>
struct LLA {
33 LLA(Scalar lon_, Scalar lat_, Scalar alt_)
55 Spherical(Scalar lon_, Scalar lat_gc_, Scalar radius_)
77template <
typename Scalar>
80 const Scalar x = r(0);
81 const Scalar y = r(1);
82 const Scalar z = r(2);
85 const double e2 = m.e2;
86 const double e4 = e2 * e2;
91 const Scalar p = (x * x + y * y) / (a * a);
92 const Scalar q = (1.0 - e2) * z * z / (a * a);
95 const Scalar r_val = (p + q - e4) / 6.0;
99 const Scalar r_cubed = r_val * r_val * r_val;
100 const Scalar s = e4 * p * q / (4.0 * r_cubed);
104 const Scalar s_term = s * (2.0 + s);
105 const Scalar t = janus::pow(1.0 + s + janus::sqrt(s_term), 1.0 / 3.0);
108 const Scalar u = r_val * (1.0 + t + 1.0 / t);
111 const Scalar v = janus::sqrt(u * u + e4 * q);
114 const Scalar w = e2 * (u + v - q) / (2.0 * v);
117 const Scalar k = janus::sqrt(u + v + w * w) - w;
120 const Scalar xy_dist = janus::sqrt(x * x + y * y);
121 const Scalar D = k * xy_dist / (k + e2);
125 const Scalar lat = 2.0 * janus::atan2(z, D + janus::sqrt(D * D + z * z));
129 const Scalar alt = (k + e2 - 1.0) / k * janus::sqrt(D * D + z * z);
133 constexpr double eps = 1e-15;
134 const Scalar is_pole = xy_dist < eps;
135 const Scalar lon = janus::where(is_pole, Scalar(0.0), janus::atan2(y, x));
151template <
typename Scalar>
154 const Scalar sin_lat = janus::sin(lla.
lat);
155 const Scalar cos_lat = janus::cos(lla.
lat);
156 const Scalar sin_lon = janus::sin(lla.
lon);
157 const Scalar cos_lon = janus::cos(lla.
lon);
159 const double a = m.a;
160 const double e2 = m.e2;
164 const Scalar N = a / janus::sqrt(1.0 - e2 * sin_lat * sin_lat);
171 r(0) = (N + lla.
alt) * cos_lat * cos_lon;
172 r(1) = (N + lla.
alt) * cos_lat * sin_lon;
173 r(2) = (N * (1.0 - e2) + lla.
alt) * sin_lat;
189template <
typename Scalar>
191 const Scalar x = r(0);
192 const Scalar y = r(1);
193 const Scalar z = r(2);
196 const Scalar radius = janus::sqrt(x * x + y * y + z * z);
199 const Scalar lat_gc = janus::asin(z / radius);
202 const Scalar xy_dist = janus::sqrt(x * x + y * y);
203 constexpr double eps = 1e-15;
204 const Scalar is_pole = xy_dist < eps;
205 const Scalar lon = janus::where(is_pole, Scalar(0.0), janus::atan2(y, x));
220template <
typename Scalar>
222 const Scalar sin_lat = janus::sin(geo.
lat_gc);
223 const Scalar cos_lat = janus::cos(geo.
lat_gc);
224 const Scalar sin_lon = janus::sin(geo.
lon);
225 const Scalar cos_lon = janus::cos(geo.
lon);
228 r(0) = geo.
radius * cos_lat * cos_lon;
229 r(1) = geo.
radius * cos_lat * sin_lon;
230 r(2) = geo.
radius * sin_lat;
247template <
typename Scalar>
251 return janus::atan((1.0 - m.e2) * janus::tan(lat_gd));
259template <
typename Scalar>
263 return janus::atan(janus::tan(lat_gc) / (1.0 - m.e2));
274template <
typename Scalar>
277 const Scalar sin_lat = janus::sin(lat);
278 return m.a / janus::sqrt(1.0 - m.e2 * sin_lat * sin_lat);
289template <
typename Scalar>
292 const Scalar sin_lat = janus::sin(lat);
293 const Scalar denom = 1.0 - m.e2 * sin_lat * sin_lat;
294 return m.a * (1.0 - m.e2) / janus::pow(denom, 1.5);
Definition Aerodynamics.hpp:11
Scalar geocentric_to_geodetic_lat(Scalar lat_gc, const EarthModel &m=EarthModel::WGS84())
Definition Geodetic.hpp:260
Vec3< Scalar > lla_to_ecef(const LLA< Scalar > &lla, const EarthModel &m=EarthModel::WGS84())
Definition Geodetic.hpp:152
Vec3< Scalar > spherical_to_ecef(const Spherical< Scalar > &geo)
Definition Geodetic.hpp:221
Scalar radius_of_curvature_M(Scalar lat, const EarthModel &m=EarthModel::WGS84())
Definition Geodetic.hpp:290
Spherical< Scalar > ecef_to_spherical(const Vec3< Scalar > &r)
Definition Geodetic.hpp:190
Scalar geodetic_to_geocentric_lat(Scalar lat_gd, const EarthModel &m=EarthModel::WGS84())
Definition Geodetic.hpp:248
LLA< Scalar > ecef_to_lla(const Vec3< Scalar > &r, const EarthModel &m=EarthModel::WGS84())
Definition Geodetic.hpp:78
Scalar radius_of_curvature_N(Scalar lat, const EarthModel &m=EarthModel::WGS84())
Definition Geodetic.hpp:275
Definition EarthModel.hpp:27
static constexpr EarthModel WGS84()
WGS84 reference ellipsoid (most common for GPS/navigation).
Definition EarthModel.hpp:45
Definition Geodetic.hpp:27
Scalar lon
Longitude [rad], positive East, range [-π, π].
Definition Geodetic.hpp:28
LLA()
Definition Geodetic.hpp:32
Scalar alt
Altitude above ellipsoid [m].
Definition Geodetic.hpp:30
Scalar lat
Geodetic latitude [rad], range [-π/2, π/2].
Definition Geodetic.hpp:29
LLA(Scalar lon_, Scalar lat_, Scalar alt_)
Definition Geodetic.hpp:33
Definition Geodetic.hpp:49
Spherical(Scalar lon_, Scalar lat_gc_, Scalar radius_)
Definition Geodetic.hpp:55
Spherical()
Definition Geodetic.hpp:54
Scalar lon
Longitude [rad], positive East, range [-π, π].
Definition Geodetic.hpp:50
Scalar radius
Distance from Earth center [m].
Definition Geodetic.hpp:52
Scalar lat_gc
Geocentric latitude [rad], range [-π/2, π/2].
Definition Geodetic.hpp:51