NumCpp  2.11.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
ECEFtoLLA.hpp
Go to the documentation of this file.
1 #pragma once
29 
30 #include <cmath>
31 
37 #include "NumCpp/Utils/sqr.hpp"
38 
40 {
49  [[nodiscard]] inline reference_frames::LLA ECEFtoLLA(const reference_frames::ECEF& ecef, double tol = 1e-8) noexcept
50  {
51  constexpr int MAX_ITER = 10;
54 
55  const auto p = std::hypot(ecef.x, ecef.y);
56  const auto lon = std::atan2(ecef.y, ecef.x);
57 
58  double alt = 0.0;
59  double lat = 0.0;
60 
61  if (p < tol)
62  {
63  lat = sign(ecef.z) * constants::pi / 2.;
65  }
66  else
67  {
68  // Iteratively update latitude and altitude.
69  // This is expected to converge in ~4 iterations, but apply a maximum number of iterations incase tol is
70  // too small
71  double err = 1.0;
72  int iter = 0;
73  while (err > tol && iter < MAX_ITER)
74  {
76  std::sqrt(1 - E_SQR * utils::sqr(std::sin(lat)));
77  lat = std::atan((ecef.z / p) / (1 - (N * E_SQR / (N + alt))));
78  double newAlt = (p / std::cos(lat)) - N;
79  err = std::abs(alt - newAlt);
80  alt = newAlt;
81  iter++;
82  }
83  }
84  return { lat, lon, alt };
85  }
86 } // namespace nc::coordinates::transforms
double z
Definition: Cartesian.hpp:49
double y
Definition: Cartesian.hpp:48
double x
Definition: Cartesian.hpp:47
ECEF coordinates.
Definition: ECEF.hpp:40
Geodetic coordinates.
Definition: LLA.hpp:40
constexpr double pi
Pi.
Definition: Core/Constants.hpp:39
constexpr double e
eulers number
Definition: Core/Constants.hpp:37
constexpr double EARTH_POLAR_RADIUS
Definition: Coordinates/ReferenceFrames/Constants.hpp:33
constexpr double EARTH_EQUATORIAL_RADIUS
Definition: Coordinates/ReferenceFrames/Constants.hpp:34
Definition: AERtoECEF.hpp:38
reference_frames::LLA ECEFtoLLA(const reference_frames::ECEF &ecef, double tol=1e-8) noexcept
Converts ECEF coordinates to LLA https://en.wikipedia.org/wiki/Geographic_coordinate_conversion#From_...
Definition: ECEFtoLLA.hpp:49
constexpr dtype sqr(dtype inValue) noexcept
Definition: sqr.hpp:42
auto sin(dtype inValue) noexcept
Definition: sin.hpp:49
double hypot(dtype inValue1, dtype inValue2) noexcept
Definition: hypot.hpp:56
int8 sign(dtype inValue) noexcept
Definition: sign.hpp:52
auto abs(dtype inValue) noexcept
Definition: abs.hpp:49
auto cos(dtype inValue) noexcept
Definition: cos.hpp:49
auto sqrt(dtype inValue) noexcept
Definition: sqrt.hpp:48