siderust-cpp 0.8.0
Header-only C++ wrapper for siderust
Loading...
Searching...
No Matches
pos_conversions.hpp
Go to the documentation of this file.
1#pragma once
2
9#include "../constants.hpp"
10#include "cartesian.hpp"
11#include "spherical.hpp"
12
13namespace siderust {
14
15// cartesian::Position::to_spherical implementation
16template <typename C, typename F, typename U>
18 const double x = comp_x.value();
19 const double y = comp_y.value();
20 const double z = comp_z.value();
21 const double r = std::sqrt(x * x + y * y + z * z);
22 double lon = std::atan2(y, x) * 180.0 / constants::pi;
23 if (lon < 0.0)
24 lon += 360.0;
25 const double lat = std::atan2(z, std::sqrt(x * x + y * y)) * 180.0 / constants::pi;
26 return spherical::Position<C, F, U>(qtty::Degree(lon), qtty::Degree(lat), U(r));
27}
28
29// spherical::Position::to_cartesian implementation
30template <typename C, typename F, typename U>
32 double lon_deg = 0.0;
33 double lat_deg = 0.0;
34 if constexpr (frames::has_ra_dec_v<F>) {
35 lon_deg = azimuth_.value();
36 lat_deg = polar_.value();
37 } else if constexpr (frames::has_lon_lat_v<F>) {
38 lon_deg = azimuth_.value();
39 lat_deg = polar_.value();
40 } else if constexpr (frames::has_az_alt_v<F>) {
41 lon_deg = azimuth_.value();
42 lat_deg = polar_.value();
43 }
44
45 const double lon = lon_deg * constants::pi / 180.0;
46 const double lat = lat_deg * constants::pi / 180.0;
47 const double r = dist_.value();
48
49 const double cx = r * std::cos(lat) * std::cos(lon);
50 const double cy = r * std::cos(lat) * std::sin(lon);
51 const double cz = r * std::sin(lat);
52 return cartesian::Position<C, F, U>(U(cx), U(cy), U(cz));
53}
54
55// spherical::Position::to_frame implementation
56// Performs the transformation via a cartesian round-trip:
57// spherical(C,F) → cartesian(C,F) → cartesian(C,Target) → spherical(C,Target)
58template <typename C, typename F, typename U>
59template <typename Target>
60std::enable_if_t<siderust::frames::has_frame_transform_v<F, Target>,
63 if constexpr (std::is_same_v<F, Target>) {
64 return *this;
65 } else {
66 return to_cartesian().template to_frame<Target>(jd).to_spherical();
67 }
68}
69
70template <typename C, typename F, typename U>
71template <typename Target>
72std::enable_if_t<siderust::frames::has_frame_transform_v<F, Target>,
75 if constexpr (std::is_same_v<F, Target>) {
76 return *this;
77 } else {
78 return to_cartesian().template to_frame_with<Target>(jd, ctx).to_spherical();
79 }
80}
81
82} // namespace siderust
constexpr double pi
Circle constant pi (radians per half-turn).
Definition constants.hpp:12
tempoch::EncodedTime< Scale, Format > Time
Definition time.hpp:36
A 3D Cartesian position, compile-time tagged by center, frame, unit.
spherical::Position< C, F, U > to_spherical() const
Convert this cartesian position to a spherical Position<C,F,U>.
A spherical position (direction + distance), compile-time tagged.
cartesian::Position< C, F, U > to_cartesian() const
Convert this spherical position to a cartesian Position<C,F,U>.
std::enable_if_t< frames::has_frame_transform_v< F, Target >, Position< C, Target, U > > to_frame_with(const Time< TT, JD > &jd, const AstroContext &ctx) const
std::enable_if_t< frames::has_frame_transform_v< F, Target >, Position< C, Target, U > > to_frame(const Time< TT, JD > &jd) const
Transform this position to a different reference frame (same center).