Skip to content

Commit

Permalink
Create FrameUid to pass around a small thing
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherRabotin committed Sep 17, 2023
1 parent bd4e4e7 commit b9c5b1e
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 115 deletions.
6 changes: 3 additions & 3 deletions src/ephemerides/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use snafu::prelude::*;

use crate::{
errors::PhysicsError, math::interpolation::InterpolationError, naif::daf::DAFError,
prelude::Frame, NaifId,
prelude::FrameUid, NaifId,
};

pub mod paths;
Expand All @@ -33,8 +33,8 @@ pub enum EphemerisError {
"Could not translate from {from} to {to}: no common origin found at epoch {epoch}"
))]
TranslationOrigin {
from: Frame,
to: Frame,
from: FrameUid,
to: FrameUid,
epoch: Epoch,
},
#[snafu(display("no ephemeris data loaded (must call load_spk)"))]
Expand Down
4 changes: 2 additions & 2 deletions src/ephemerides/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ impl<'a> Almanac<'a> {
// path is which brings the non zero-length path back to the file root.
if from_len == 0 && to_len == 0 {
Err(EphemerisError::TranslationOrigin {
from: from_frame,
to: to_frame,
from: from_frame.into(),
to: to_frame.into(),
epoch,
})

Check warning on line 154 in src/ephemerides/paths.rs

View check run for this annotation

Codecov / codecov/patch

src/ephemerides/paths.rs#L150-L154

Added lines #L150 - L154 were not covered by tests
} else if from_len != 0 && to_len == 0 {
Expand Down
23 changes: 8 additions & 15 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use hifitime::Epoch;
use snafu::prelude::*;

use crate::prelude::Frame;
use crate::prelude::FrameUid;
use crate::structure::semver::Semver;
use crate::NaifId;
use core::convert::From;
Expand Down Expand Up @@ -41,11 +41,6 @@ pub enum AniseError {
NoInterpolationData,
/// Raised to prevent overwriting an existing file
FileExists,
/// Raised if a transformation is requested but the frames have no common origin
DisjointFrames {
from_frame: Frame,
to_frame: Frame,
},
/// Raised if the ephemeris or orientation is deeper to the context origin than this library supports
MaxTreeDepth,
/// Raised if there is no interpolation data for the requested epoch, i.e. ephemeris/orientation starts after or ends before the requested epoch
Expand Down Expand Up @@ -107,7 +102,10 @@ pub enum IntegrityError {
/// The lookup table is broken somehow
LookupTable,
/// Raised if a transformation is requested but the frames have no common origin
DisjointRoots { from_frame: Frame, to_frame: Frame },
DisjointRoots {
from_frame: FrameUid,
to_frame: FrameUid,
},
#[snafu(display(
"data for {variable} in {dataset} decoded as subnormal double (data malformed?)"
))]
Expand Down Expand Up @@ -145,8 +143,8 @@ pub enum PhysicsError {
#[snafu(display("frames {frame1} and {frame2} differ while {action}"))]
FrameMismatch {
action: &'static str,
frame1: Frame,
frame2: Frame,
frame1: FrameUid,
frame2: FrameUid,
},
#[snafu(display("origins {from1} and {from2} differ while {action}"))]
OriginMismatch {
Expand All @@ -160,7 +158,7 @@ pub enum PhysicsError {
MissingFrameData {
action: &'static str,
data: &'static str,
frame: Frame,
frame: FrameUid,
},
#[snafu(display("parabolic orbits are physically impossible and the eccentricity calculated to be within {limit:e} of 1.0"))]
ParabolicEccentricity { limit: f64 },
Expand Down Expand Up @@ -209,11 +207,6 @@ impl fmt::Display for AniseError {
f,
"ANISE error: operation aborted to prevent overwriting an existing file"
),
Self::DisjointFrames { from_frame: from, to_frame: to } => write!(
f,
"ANISE error: frame {} and {} do not share a common origin",
to, from
),
Self::MaxTreeDepth => write!(
f,
"ANISE error: the ephemeris or orientation is deeper to the context origin than this library supports"
Expand Down
12 changes: 6 additions & 6 deletions src/frames/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ impl Frame {

/// Returns the gravitational parameters of this frame, if defined
pub fn mu_km3_s2(&self) -> Result<f64, PhysicsError> {
Ok(self.mu_km3_s2.ok_or(PhysicsError::MissingFrameData {
self.mu_km3_s2.ok_or(PhysicsError::MissingFrameData {
action: "retrieving mean equatorial radius",
data: "shape",
frame: *self,
})?)
frame: self.into(),
})
}

Check warning on line 94 in src/frames/frame.rs

View check run for this annotation

Codecov / codecov/patch

src/frames/frame.rs#L88-L94

Added lines #L88 - L94 were not covered by tests

/// Returns the mean equatorial radius in km, if defined
Expand All @@ -100,7 +100,7 @@ impl Frame {
.ok_or(PhysicsError::MissingFrameData {
action: "retrieving mean equatorial radius",
data: "shape",
frame: *self,
frame: self.into(),
})?
.mean_equatorial_radius_km())
}

Check warning on line 106 in src/frames/frame.rs

View check run for this annotation

Codecov / codecov/patch

src/frames/frame.rs#L97-L106

Added lines #L97 - L106 were not covered by tests
Expand All @@ -112,7 +112,7 @@ impl Frame {
.ok_or(PhysicsError::MissingFrameData {
action: "retrieving semi major axis radius",
data: "shape",
frame: *self,
frame: self.into(),
})?

Check warning on line 116 in src/frames/frame.rs

View check run for this annotation

Codecov / codecov/patch

src/frames/frame.rs#L109-L116

Added lines #L109 - L116 were not covered by tests
.semi_major_equatorial_radius_km)
}

Check warning on line 118 in src/frames/frame.rs

View check run for this annotation

Codecov / codecov/patch

src/frames/frame.rs#L118

Added line #L118 was not covered by tests
Expand All @@ -123,7 +123,7 @@ impl Frame {
.ok_or(PhysicsError::MissingFrameData {
action: "retrieving flattening ratio",
data: "shape",
frame: *self,
frame: self.into(),
})?
.flattening())

Check warning on line 128 in src/frames/frame.rs

View check run for this annotation

Codecov / codecov/patch

src/frames/frame.rs#L120-L128

Added lines #L120 - L128 were not covered by tests
}
Expand Down
86 changes: 0 additions & 86 deletions src/frames/geodetic_frame.rs

This file was deleted.

53 changes: 53 additions & 0 deletions src/frames/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,58 @@
* Documentation: https://nyxspace.com/
*/

use crate::{
constants::{
celestial_objects::celestial_name_from_id, orientations::orientation_name_from_id,
},
NaifId,
};
use core::fmt;

pub mod frame;
pub use frame::Frame;

/// A unique frame reference that only contains enough information to build the actual Frame object.
/// It cannot be used for any computations, is it be used in any structure apart from error structures.
///
/// # Usage note
/// You should almost always prefer Frame over FrameRef unless you will
#[derive(Copy, Clone, Debug, PartialEq, Eq)]

Check warning on line 27 in src/frames/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/frames/mod.rs#L27

Added line #L27 was not covered by tests
pub struct FrameUid {
pub ephemeris_id: NaifId,
pub orientation_id: NaifId,
}

impl From<Frame> for FrameUid {
fn from(frame: Frame) -> Self {
Self {
ephemeris_id: frame.ephemeris_id,
orientation_id: frame.orientation_id,
}
}

Check warning on line 39 in src/frames/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/frames/mod.rs#L34-L39

Added lines #L34 - L39 were not covered by tests
}

impl From<&Frame> for FrameUid {
fn from(frame: &Frame) -> Self {
Self {
ephemeris_id: frame.ephemeris_id,
orientation_id: frame.orientation_id,
}
}

Check warning on line 48 in src/frames/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/frames/mod.rs#L43-L48

Added lines #L43 - L48 were not covered by tests
}

impl fmt::Display for FrameUid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
let body_name = match celestial_name_from_id(self.ephemeris_id) {
Some(name) => name.to_string(),
None => format!("body {}", self.ephemeris_id),

Check warning on line 55 in src/frames/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/frames/mod.rs#L52-L55

Added lines #L52 - L55 were not covered by tests
};

let orientation_name = match orientation_name_from_id(self.orientation_id) {
Some(name) => name.to_string(),
None => format!("orientation {}", self.orientation_id),

Check warning on line 60 in src/frames/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/frames/mod.rs#L58-L60

Added lines #L58 - L60 were not covered by tests
};

write!(f, "{body_name} {orientation_name}")
}

Check warning on line 64 in src/frames/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/frames/mod.rs#L63-L64

Added lines #L63 - L64 were not covered by tests
}
6 changes: 3 additions & 3 deletions src/orientations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use snafu::prelude::*;

use crate::{
errors::PhysicsError, math::interpolation::InterpolationError, naif::daf::DAFError,
prelude::Frame,
prelude::FrameUid,
};

#[derive(Debug, Snafu)]

Check warning on line 19 in src/orientations/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/orientations/mod.rs#L19

Added line #L19 was not covered by tests
Expand All @@ -29,8 +29,8 @@ pub enum OrientationError {
"Could not rotate from {from} to {to}: no common origin found at epoch {epoch}"
))]
RotationOrigin {
from: Frame,
to: Frame,
from: FrameUid,
to: FrameUid,
epoch: Epoch,
},
#[snafu(display("no oreitnation data loaded (must call load_bpc or DataSet::from_bytes)"))]
Expand Down

0 comments on commit b9c5b1e

Please sign in to comment.