siderust-cpp
Header-only C++ wrapper for siderust
Loading...
Searching...
No Matches
bodies.hpp
Go to the documentation of this file.
1#pragma once
2
8#include "coordinates.hpp"
9#include "ffi_core.hpp"
10#include <optional>
11#include <string>
12#include <utility>
13
14namespace siderust {
15
16// ============================================================================
17// ProperMotion
18// ============================================================================
19
24 double pm_ra_deg_yr;
27
28 ProperMotion(double ra, double dec,
30 : pm_ra_deg_yr(ra), pm_dec_deg_yr(dec), convention(conv) {}
31
32 siderust_proper_motion_t to_c() const {
34 static_cast<siderust_ra_convention_t>(convention)};
35 }
36};
37
38// ============================================================================
39// Orbit
40// ============================================================================
41
45struct Orbit {
52 double epoch_jd;
53
54 static Orbit from_c(const siderust_orbit_t& c) {
55 return {c.semi_major_axis_au, c.eccentricity, c.inclination_deg,
56 c.lon_ascending_node_deg, c.arg_perihelion_deg,
57 c.mean_anomaly_deg, c.epoch_jd};
58 }
59};
60
61// ============================================================================
62// Planet
63// ============================================================================
64
68struct Planet {
69 double mass_kg;
70 double radius_km;
72
73 static Planet from_c(const siderust_planet_t& c) {
74 return {c.mass_kg, c.radius_km, Orbit::from_c(c.orbit)};
75 }
76};
77
78namespace detail {
79
81 siderust_planet_t out;
82 check_status(siderust_planet_mercury(&out), "MERCURY");
83 return Planet::from_c(out);
84}
85
87 siderust_planet_t out;
88 check_status(siderust_planet_venus(&out), "VENUS");
89 return Planet::from_c(out);
90}
91
93 siderust_planet_t out;
94 check_status(siderust_planet_earth(&out), "EARTH");
95 return Planet::from_c(out);
96}
97
99 siderust_planet_t out;
100 check_status(siderust_planet_mars(&out), "MARS");
101 return Planet::from_c(out);
102}
103
105 siderust_planet_t out;
106 check_status(siderust_planet_jupiter(&out), "JUPITER");
107 return Planet::from_c(out);
108}
109
111 siderust_planet_t out;
112 check_status(siderust_planet_saturn(&out), "SATURN");
113 return Planet::from_c(out);
114}
115
117 siderust_planet_t out;
118 check_status(siderust_planet_uranus(&out), "URANUS");
119 return Planet::from_c(out);
120}
121
123 siderust_planet_t out;
124 check_status(siderust_planet_neptune(&out), "NEPTUNE");
125 return Planet::from_c(out);
126}
127
128} // namespace detail
129
138
139// Backward-compatible function aliases.
140inline Planet mercury() { return MERCURY; }
141inline Planet venus() { return VENUS; }
142inline Planet earth() { return EARTH; }
143inline Planet mars() { return MARS; }
144inline Planet jupiter() { return JUPITER; }
145inline Planet saturn() { return SATURN; }
146inline Planet uranus() { return URANUS; }
147inline Planet neptune() { return NEPTUNE; }
148
149// ============================================================================
150// Star (RAII)
151// ============================================================================
152
158class Star {
159 SiderustStar* m_handle = nullptr;
160
161 explicit Star(SiderustStar* h) : m_handle(h) {}
162
163 public:
164 Star() = default;
166 if (m_handle)
167 siderust_star_free(m_handle);
168 }
169
170 // Move-only
171 Star(Star&& o) noexcept : m_handle(o.m_handle) { o.m_handle = nullptr; }
172 Star& operator=(Star&& o) noexcept {
173 if (this != &o) {
174 if (m_handle)
175 siderust_star_free(m_handle);
176 m_handle = o.m_handle;
177 o.m_handle = nullptr;
178 }
179 return *this;
180 }
181 Star(const Star&) = delete;
182 Star& operator=(const Star&) = delete;
183
185 explicit operator bool() const { return m_handle != nullptr; }
186
188 const SiderustStar* c_handle() const { return m_handle; }
189
190 // -- Factory methods --
191
198 static Star catalog(const std::string& name) {
199 SiderustStar* h = nullptr;
200 check_status(siderust_star_catalog(name.c_str(), &h),
201 "Star::catalog");
202 return Star(h);
203 }
204
218 static Star create(const std::string& name,
219 double distance_ly,
220 double mass_solar,
221 double radius_solar,
222 double luminosity_solar,
223 double ra_deg,
224 double dec_deg,
225 double epoch_jd,
226 const std::optional<ProperMotion>& pm = std::nullopt) {
227 SiderustStar* h = nullptr;
228 const siderust_proper_motion_t* pm_ptr = nullptr;
229 siderust_proper_motion_t pm_c{};
230 if (pm.has_value()) {
231 pm_c = pm->to_c();
232 pm_ptr = &pm_c;
233 }
234 check_status(siderust_star_create(
236 luminosity_solar, ra_deg, dec_deg, epoch_jd, pm_ptr, &h),
237 "Star::create");
238 return Star(h);
239 }
240
241 // -- Accessors --
242
243 std::string name() const {
244 char buf[256];
245 uintptr_t written = 0;
246 check_status(siderust_star_name(m_handle, buf, sizeof(buf), &written),
247 "Star::name");
248 return std::string(buf, written);
249 }
250
251 double distance_ly() const { return siderust_star_distance_ly(m_handle); }
252 double mass_solar() const { return siderust_star_mass_solar(m_handle); }
253 double radius_solar() const { return siderust_star_radius_solar(m_handle); }
254 double luminosity_solar() const { return siderust_star_luminosity_solar(m_handle); }
255};
256
257inline const Star VEGA = Star::catalog("VEGA");
258inline const Star SIRIUS = Star::catalog("SIRIUS");
259inline const Star POLARIS = Star::catalog("POLARIS");
260inline const Star CANOPUS = Star::catalog("CANOPUS");
261inline const Star ARCTURUS = Star::catalog("ARCTURUS");
262inline const Star RIGEL = Star::catalog("RIGEL");
263inline const Star BETELGEUSE = Star::catalog("BETELGEUSE");
264inline const Star PROCYON = Star::catalog("PROCYON");
265inline const Star ALDEBARAN = Star::catalog("ALDEBARAN");
266inline const Star ALTAIR = Star::catalog("ALTAIR");
267
268} // namespace siderust
RAII handle to a Star (opaque Rust object).
Definition bodies.hpp:158
Star & operator=(const Star &)=delete
Star & operator=(Star &&o) noexcept
Definition bodies.hpp:172
Star()=default
Star(Star &&o) noexcept
Definition bodies.hpp:171
double mass_solar() const
Definition bodies.hpp:252
const SiderustStar * c_handle() const
Access the raw C handle (for passing to altitude functions).
Definition bodies.hpp:188
double distance_ly() const
Definition bodies.hpp:251
Star(const Star &)=delete
static Star catalog(const std::string &name)
Look up a star from the built-in catalog.
Definition bodies.hpp:198
static Star create(const std::string &name, double distance_ly, double mass_solar, double radius_solar, double luminosity_solar, double ra_deg, double dec_deg, double epoch_jd, const std::optional< ProperMotion > &pm=std::nullopt)
Create a custom star.
Definition bodies.hpp:218
double luminosity_solar() const
Definition bodies.hpp:254
std::string name() const
Definition bodies.hpp:243
double radius_solar() const
Definition bodies.hpp:253
Coordinate module umbrella.
Error handling and utility base for the siderust C++ wrapper.
Planet make_planet_uranus()
Definition bodies.hpp:116
Planet make_planet_earth()
Definition bodies.hpp:92
Planet make_planet_saturn()
Definition bodies.hpp:110
Planet make_planet_mars()
Definition bodies.hpp:98
Planet make_planet_mercury()
Definition bodies.hpp:80
Planet make_planet_jupiter()
Definition bodies.hpp:104
Planet make_planet_neptune()
Definition bodies.hpp:122
Planet make_planet_venus()
Definition bodies.hpp:86
const Star PROCYON
Definition bodies.hpp:264
Planet jupiter()
Definition bodies.hpp:144
const Planet NEPTUNE
Definition bodies.hpp:137
void check_status(siderust_status_t status, const char *operation)
Definition ffi_core.hpp:81
Planet uranus()
Definition bodies.hpp:146
const Planet SATURN
Definition bodies.hpp:135
Planet mercury()
Definition bodies.hpp:140
const Star SIRIUS
Definition bodies.hpp:258
const Planet MERCURY
Definition bodies.hpp:130
const Planet URANUS
Definition bodies.hpp:136
const Star ALTAIR
Definition bodies.hpp:266
const Planet VENUS
Definition bodies.hpp:131
const Star ARCTURUS
Definition bodies.hpp:261
const Planet MARS
Definition bodies.hpp:133
const Star BETELGEUSE
Definition bodies.hpp:263
const Star RIGEL
Definition bodies.hpp:262
const Star ALDEBARAN
Definition bodies.hpp:265
Planet venus()
Definition bodies.hpp:141
const Star POLARIS
Definition bodies.hpp:259
const Planet JUPITER
Definition bodies.hpp:134
const Star VEGA
Definition bodies.hpp:257
Planet neptune()
Definition bodies.hpp:147
Planet earth()
Definition bodies.hpp:142
const Planet EARTH
Definition bodies.hpp:132
const Star CANOPUS
Definition bodies.hpp:260
Planet saturn()
Definition bodies.hpp:145
Planet mars()
Definition bodies.hpp:143
Keplerian orbital elements.
Definition bodies.hpp:45
double inclination_deg
Definition bodies.hpp:48
double mean_anomaly_deg
Definition bodies.hpp:51
double semi_major_axis_au
Definition bodies.hpp:46
static Orbit from_c(const siderust_orbit_t &c)
Definition bodies.hpp:54
double epoch_jd
Definition bodies.hpp:52
double lon_ascending_node_deg
Definition bodies.hpp:49
double eccentricity
Definition bodies.hpp:47
double arg_perihelion_deg
Definition bodies.hpp:50
Planet data (value type, copyable).
Definition bodies.hpp:68
double radius_km
Definition bodies.hpp:70
static Planet from_c(const siderust_planet_t &c)
Definition bodies.hpp:73
Proper motion for a star (equatorial).
Definition bodies.hpp:23
double pm_dec_deg_yr
Dec proper motion (deg/yr).
Definition bodies.hpp:25
ProperMotion(double ra, double dec, RaConvention conv=RaConvention::MuAlphaStar)
Definition bodies.hpp:28
double pm_ra_deg_yr
RA proper motion (deg/yr).
Definition bodies.hpp:24
RaConvention convention
RA rate convention.
Definition bodies.hpp:26
siderust_proper_motion_t to_c() const
Definition bodies.hpp:32