siderust-cpp 0.8.0
Header-only C++ wrapper for siderust
Loading...
Searching...
No Matches
sgp4.hpp
Go to the documentation of this file.
1#pragma once
2
22#include "ffi_core.hpp"
23
24#include <cstdint>
25#include <ostream>
26#include <string_view>
27#include <utility>
28
29namespace siderust {
30
33
34// ============================================================================
35// TLE handle
36// ============================================================================
37
38namespace tle {
39
45class Tle {
46public:
52 static Tle parse(std::string_view line1, std::string_view line2) {
53 const std::string l1{line1};
54 const std::string l2{line2};
55 SiderustTle *handle = nullptr;
56 check_status(siderust_tle_parse(l1.c_str(), l2.c_str(), &handle), "tle::Tle::parse");
57 return Tle{handle};
58 }
59
60 Tle(Tle &&other) noexcept : handle_(std::exchange(other.handle_, nullptr)) {}
61 Tle &operator=(Tle &&other) noexcept {
62 if (this != &other) {
63 siderust_tle_free(handle_);
64 handle_ = std::exchange(other.handle_, nullptr);
65 }
66 return *this;
67 }
68
69 Tle(const Tle &) = delete;
70 Tle &operator=(const Tle &) = delete;
71
72 ~Tle() { siderust_tle_free(handle_); }
73
75 std::uint32_t norad_id() const {
76 std::uint32_t id = 0;
77 check_status(siderust_tle_norad_id(handle_, &id), "tle::Tle::norad_id");
78 return id;
79 }
80
82 const SiderustTle *raw() const noexcept { return handle_; }
83
84private:
85 explicit Tle(SiderustTle *h) : handle_(h) {}
86 SiderustTle *handle_;
87};
88
89} // namespace tle
90
91// ============================================================================
92// SGP4 propagator
93// ============================================================================
94
95namespace sgp4 {
96
98enum class GravityModel : int {
99 Wgs72 = 0,
100 Wgs72Iau = 1,
101 Wgs84 = 2,
102};
103
105struct State {
106 double pos_km[3];
107 double vel_kms[3];
108};
109
110inline std::ostream &operator<<(std::ostream &os, const State &s) {
111 os << "SGP4 TEME (pos_km=(" << s.pos_km[0] << ", " << s.pos_km[1] << ", " << s.pos_km[2]
112 << "), vel_kms=(" << s.vel_kms[0] << ", " << s.vel_kms[1] << ", " << s.vel_kms[2] << "))";
113 return os;
114}
115
121class Propagator {
122public:
128 explicit Propagator(const tle::Tle &tle, GravityModel model = GravityModel::Wgs72) {
129 check_status(siderust_sgp4_new(tle.raw(), static_cast<int>(model), &handle_),
130 "sgp4::Propagator");
131 }
132
133 Propagator(Propagator &&other) noexcept : handle_(std::exchange(other.handle_, nullptr)) {}
134 Propagator &operator=(Propagator &&other) noexcept {
135 if (this != &other) {
136 siderust_sgp4_free(handle_);
137 handle_ = std::exchange(other.handle_, nullptr);
138 }
139 return *this;
140 }
141
142 Propagator(const Propagator &) = delete;
143 Propagator &operator=(const Propagator &) = delete;
144
145 ~Propagator() { siderust_sgp4_free(handle_); }
146
148 double epoch_jd_utc() const {
149 double jd = 0.0;
150 check_status(siderust_sgp4_epoch_jd_utc(handle_, &jd), "sgp4::Propagator::epoch_jd_utc");
151 return jd;
152 }
153
155 int gravity_model() const {
156 int m = 0;
157 check_status(siderust_sgp4_gravity_model(handle_, &m), "sgp4::Propagator::gravity_model");
158 return m;
159 }
160
165 State propagate_at(double jd_utc) const {
166 State s{};
167 check_status(siderust_sgp4_propagate_at(handle_, jd_utc, s.pos_km, s.vel_kms),
168 "sgp4::Propagator::propagate_at");
169 return s;
170 }
171
172private:
173 SiderustSgp4 *handle_ = nullptr;
174};
175
176} // namespace sgp4
177
179
180} // namespace siderust
Move-only RAII wrapper around a parsed Two-Line Element set.
Definition sgp4.hpp:45
Tle & operator=(Tle &&other) noexcept
Definition sgp4.hpp:61
const SiderustTle * raw() const noexcept
Expose the raw opaque handle (for use by Propagator constructor).
Definition sgp4.hpp:82
Tle & operator=(const Tle &)=delete
std::uint32_t norad_id() const
Return the NORAD catalog number.
Definition sgp4.hpp:75
static Tle parse(std::string_view line1, std::string_view line2)
Definition sgp4.hpp:52
Tle(Tle &&other) noexcept
Definition sgp4.hpp:60
Tle(const Tle &)=delete
Error handling and utility base for the siderust C++ wrapper.
void check_status(siderust_status_t status, const char *operation)
Definition ffi_core.hpp:111
std::ostream & operator<<(std::ostream &os, AzimuthExtremumKind kind)
Stream operator for AzimuthExtremumKind.
Definition azimuth.hpp:395