Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(lox-orbits): expose concrete KeplerianElements #199

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions crates/lox-orbits/src/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use crate::states::State;

#[derive(Debug, Clone, PartialEq)]
pub(crate) struct KeplerianElements {
pub struct KeplerianElements {
pub semi_major_axis: f64,
pub eccentricity: f64,
pub inclination: f64,
Expand All @@ -29,6 +29,36 @@
pub true_anomaly: f64,
}

impl KeplerianElements {
pub fn semi_parameter(&self) -> f64 {
if is_circular(self.eccentricity) {
self.semi_major_axis

Check warning on line 35 in crates/lox-orbits/src/elements.rs

View check run for this annotation

Codecov / codecov/patch

crates/lox-orbits/src/elements.rs#L33-L35

Added lines #L33 - L35 were not covered by tests
} else {
self.semi_major_axis * (1.0 - self.eccentricity.powi(2))

Check warning on line 37 in crates/lox-orbits/src/elements.rs

View check run for this annotation

Codecov / codecov/patch

crates/lox-orbits/src/elements.rs#L37

Added line #L37 was not covered by tests
}
}

Check warning on line 39 in crates/lox-orbits/src/elements.rs

View check run for this annotation

Codecov / codecov/patch

crates/lox-orbits/src/elements.rs#L39

Added line #L39 was not covered by tests

pub fn to_perifocal(&self, grav_param: f64) -> (DVec3, DVec3) {
let semiparameter = self.semi_parameter();
let (sin_nu, cos_nu) = self.true_anomaly.sin_cos();
let sqrt_mu_p = (grav_param / semiparameter).sqrt();

let pos =
DVec3::new(cos_nu, sin_nu, 0.0) * (semiparameter / (1.0 + self.eccentricity * cos_nu));
let vel = DVec3::new(-sin_nu, self.eccentricity + cos_nu, 0.0) * sqrt_mu_p;

(pos, vel)
}

Check warning on line 51 in crates/lox-orbits/src/elements.rs

View check run for this annotation

Codecov / codecov/patch

crates/lox-orbits/src/elements.rs#L41-L51

Added lines #L41 - L51 were not covered by tests

pub fn to_cartesian(&self, grav_param: f64) -> (DVec3, DVec3) {
let (pos, vel) = self.to_perifocal(grav_param);
let rot = DMat3::from_rotation_z(self.longitude_of_ascending_node)
* DMat3::from_rotation_x(self.inclination)
* DMat3::from_rotation_z(self.argument_of_periapsis);
(rot * pos, rot * vel)
}

Check warning on line 59 in crates/lox-orbits/src/elements.rs

View check run for this annotation

Codecov / codecov/patch

crates/lox-orbits/src/elements.rs#L53-L59

Added lines #L53 - L59 were not covered by tests
}

#[derive(Debug, Clone, PartialEq)]
pub struct Keplerian<T: TimeScale, O: TryPointMass, R: ReferenceFrame> {
time: Time<T>,
Expand Down Expand Up @@ -165,6 +195,7 @@
self.semi_major_axis * (1.0 - self.eccentricity.powi(2))
}
}

pub fn to_perifocal(&self) -> (DVec3, DVec3) {
let grav_param = self.gravitational_parameter();
let semiparameter = self.semiparameter();
Expand All @@ -191,7 +222,7 @@
O: TryPointMass + Clone,
R: ReferenceFrame + Clone,
{
pub(crate) fn to_cartesian(&self) -> State<T, O, R> {
pub fn to_cartesian(&self) -> State<T, O, R> {
let (pos, vel) = self.to_perifocal();
let rot = DMat3::from_rotation_z(self.longitude_of_ascending_node)
* DMat3::from_rotation_x(self.inclination)
Expand Down
Loading