use nalgebra::{SMatrix, SVector};
use serde_json::Value;
use std::collections::HashMap;
use std::ops::Index;
use crate::time::Epoch;
use crate::utils::BraheError;
use crate::math::{
CovarianceInterpolationConfig, interpolate_covariance_sqrt_smatrix,
interpolate_covariance_two_wasserstein_smatrix, interpolate_lagrange_svector,
};
use super::traits::{
CovarianceInterpolationMethod, InterpolatableTrajectory, InterpolationConfig,
InterpolationMethod, Trajectory, TrajectoryEvictionPolicy,
};
pub type STrajectory3 = STrajectory<3>;
pub type STrajectory4 = STrajectory<4>;
pub type STrajectory6 = STrajectory<6>;
#[derive(Debug, Clone, PartialEq)]
pub struct STrajectory<const R: usize> {
pub epochs: Vec<Epoch>,
pub states: Vec<SVector<f64, R>>,
pub covariances: Option<Vec<SMatrix<f64, R, R>>>,
pub interpolation_method: InterpolationMethod,
pub covariance_interpolation_method: CovarianceInterpolationMethod,
pub eviction_policy: TrajectoryEvictionPolicy,
max_size: Option<usize>,
max_age: Option<f64>,
pub metadata: HashMap<String, Value>,
}
impl<const R: usize> STrajectory<R> {
pub fn new() -> Self {
Self {
epochs: Vec::new(),
states: Vec::new(),
covariances: None,
interpolation_method: InterpolationMethod::Linear,
covariance_interpolation_method: CovarianceInterpolationMethod::TwoWasserstein,
eviction_policy: TrajectoryEvictionPolicy::None,
max_size: None,
max_age: None,
metadata: HashMap::new(),
}
}
pub fn with_interpolation_method(mut self, interpolation_method: InterpolationMethod) -> Self {
self.interpolation_method = interpolation_method;
self
}
pub fn with_eviction_policy_max_size(mut self, max_size: usize) -> Self {
if max_size < 1 {
panic!("Maximum size must be >= 1");
}
self.eviction_policy = TrajectoryEvictionPolicy::KeepCount;
self.max_size = Some(max_size);
self.max_age = None;
self
}
pub fn with_eviction_policy_max_age(mut self, max_age: f64) -> Self {
if max_age <= 0.0 {
panic!("Maximum age must be > 0.0");
}
self.eviction_policy = TrajectoryEvictionPolicy::KeepWithinDuration;
self.max_age = Some(max_age);
self.max_size = None;
self
}
fn apply_eviction_policy(&mut self) {
match self.eviction_policy {
TrajectoryEvictionPolicy::None => {
}
TrajectoryEvictionPolicy::KeepCount => {
if let Some(max_size) = self.max_size
&& self.epochs.len() > max_size
{
let to_remove = self.epochs.len() - max_size;
self.epochs.drain(0..to_remove);
self.states.drain(0..to_remove);
if let Some(ref mut covs) = self.covariances {
covs.drain(0..to_remove);
}
}
}
TrajectoryEvictionPolicy::KeepWithinDuration => {
if let Some(max_age) = self.max_age
&& let Some(&last_epoch) = self.epochs.last()
{
let mut indices_to_keep = Vec::new();
for (i, &epoch) in self.epochs.iter().enumerate() {
if (last_epoch - epoch).abs() <= max_age {
indices_to_keep.push(i);
}
}
let new_epochs: Vec<Epoch> =
indices_to_keep.iter().map(|&i| self.epochs[i]).collect();
let new_states: Vec<SVector<f64, R>> =
indices_to_keep.iter().map(|&i| self.states[i]).collect();
self.epochs = new_epochs;
self.states = new_states;
if let Some(ref mut covs) = self.covariances {
let new_covs: Vec<SMatrix<f64, R, R>> =
indices_to_keep.iter().map(|&i| covs[i]).collect();
*covs = new_covs;
}
}
}
}
}
pub fn dimension(&self) -> usize {
R
}
pub fn to_matrix(&self) -> Result<nalgebra::DMatrix<f64>, BraheError> {
if self.states.is_empty() {
return Err(BraheError::Error(
"Cannot convert empty trajectory to matrix".to_string(),
));
}
let n_epochs = self.states.len();
let n_elements = 6;
let mut matrix = nalgebra::DMatrix::<f64>::zeros(n_epochs, n_elements);
for (row_idx, state) in self.states.iter().enumerate() {
for col_idx in 0..n_elements {
matrix[(row_idx, col_idx)] = state[col_idx];
}
}
Ok(matrix)
}
pub fn enable_covariance_storage(&mut self) {
if self.covariances.is_none() {
self.covariances = Some(vec![SMatrix::<f64, R, R>::zeros(); self.states.len()]);
}
}
pub fn add_with_covariance(
&mut self,
epoch: Epoch,
state: SVector<f64, R>,
covariance: SMatrix<f64, R, R>,
) {
if self.covariances.is_none() {
self.enable_covariance_storage();
}
let mut insert_idx = self.epochs.len();
for (i, existing_epoch) in self.epochs.iter().enumerate() {
if epoch < *existing_epoch {
insert_idx = i;
break;
}
}
self.epochs.insert(insert_idx, epoch);
self.states.insert(insert_idx, state);
if let Some(ref mut covs) = self.covariances {
covs.insert(insert_idx, covariance);
}
self.apply_eviction_policy();
}
pub fn set_covariance_at(&mut self, index: usize, covariance: SMatrix<f64, R, R>) {
if index >= self.states.len() {
panic!(
"Index {} out of bounds for trajectory with {} states",
index,
self.states.len()
);
}
if self.covariances.is_none() {
self.enable_covariance_storage();
}
if let Some(ref mut covs) = self.covariances {
covs[index] = covariance;
}
}
pub fn covariance_at(&self, epoch: Epoch) -> Option<SMatrix<f64, R, R>> {
let covs = self.covariances.as_ref()?;
if self.epochs.is_empty() {
return None;
}
if let Some((idx, _)) = self.epochs.iter().enumerate().find(|(_, e)| **e == epoch) {
return Some(covs[idx]);
}
let (idx_before, idx_after) = self.find_surrounding_indices(epoch)?;
if self.epochs[idx_before] == epoch {
return Some(covs[idx_before]);
}
if self.epochs[idx_after] == epoch {
return Some(covs[idx_after]);
}
let t0 = self.epochs[idx_before] - self.epoch_initial()?;
let t1 = self.epochs[idx_after] - self.epoch_initial()?;
let t = epoch - self.epoch_initial()?;
let alpha = (t - t0) / (t1 - t0);
let cov0 = covs[idx_before];
let cov1 = covs[idx_after];
let cov = match self.covariance_interpolation_method {
CovarianceInterpolationMethod::MatrixSquareRoot => {
interpolate_covariance_sqrt_smatrix(cov0, cov1, alpha)
}
CovarianceInterpolationMethod::TwoWasserstein => {
interpolate_covariance_two_wasserstein_smatrix(cov0, cov1, alpha)
}
};
Some(cov)
}
fn epoch_initial(&self) -> Option<Epoch> {
self.epochs.first().copied()
}
fn find_surrounding_indices(&self, epoch: Epoch) -> Option<(usize, usize)> {
if self.epochs.is_empty() {
return None;
}
if epoch < self.epochs[0] || epoch > *self.epochs.last()? {
return None;
}
for i in 0..self.epochs.len() - 1 {
if self.epochs[i] <= epoch && epoch <= self.epochs[i + 1] {
return Some((i, i + 1));
}
}
None
}
}
impl<const R: usize> Default for STrajectory<R> {
fn default() -> Self {
Self::new()
}
}
impl<const R: usize> Index<usize> for STrajectory<R> {
type Output = SVector<f64, R>;
fn index(&self, index: usize) -> &Self::Output {
&self.states[index]
}
}
pub struct STrajectoryIterator<'a, const R: usize> {
trajectory: &'a STrajectory<R>,
index: usize,
}
impl<'a, const R: usize> Iterator for STrajectoryIterator<'a, R> {
type Item = (Epoch, SVector<f64, R>);
fn next(&mut self) -> Option<Self::Item> {
if self.index < self.trajectory.len() {
let result = self.trajectory.get(self.index).ok();
self.index += 1;
result
} else {
None
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let remaining = self.trajectory.len() - self.index;
(remaining, Some(remaining))
}
}
impl<'a, const R: usize> ExactSizeIterator for STrajectoryIterator<'a, R> {
fn len(&self) -> usize {
self.trajectory.len() - self.index
}
}
impl<'a, const R: usize> IntoIterator for &'a STrajectory<R> {
type Item = (Epoch, SVector<f64, R>);
type IntoIter = STrajectoryIterator<'a, R>;
fn into_iter(self) -> Self::IntoIter {
STrajectoryIterator {
trajectory: self,
index: 0,
}
}
}
impl<const R: usize> Trajectory for STrajectory<R> {
type StateVector = SVector<f64, R>;
fn from_data(epochs: Vec<Epoch>, states: Vec<Self::StateVector>) -> Result<Self, BraheError> {
if epochs.len() != states.len() {
return Err(BraheError::Error(
"Epochs and states vectors must have the same length".to_string(),
));
}
if epochs.is_empty() {
return Err(BraheError::Error(
"Cannot create trajectory from empty data".to_string(),
));
}
let mut indices: Vec<usize> = (0..epochs.len()).collect();
indices.sort_by(|&i, &j| epochs[i].partial_cmp(&epochs[j]).unwrap());
let sorted_epochs: Vec<Epoch> = indices.iter().map(|&i| epochs[i]).collect();
let sorted_states: Vec<SVector<f64, R>> = indices.iter().map(|&i| states[i]).collect();
Ok(Self {
epochs: sorted_epochs,
states: sorted_states,
covariances: None,
interpolation_method: InterpolationMethod::Linear, covariance_interpolation_method: CovarianceInterpolationMethod::TwoWasserstein,
eviction_policy: TrajectoryEvictionPolicy::None,
max_size: None,
max_age: None,
metadata: HashMap::new(),
})
}
fn add(&mut self, epoch: Epoch, state: Self::StateVector) {
let mut insert_idx = self.epochs.len();
for (i, existing_epoch) in self.epochs.iter().enumerate() {
if epoch < *existing_epoch {
insert_idx = i;
break;
}
}
self.epochs.insert(insert_idx, epoch);
self.states.insert(insert_idx, state);
if let Some(ref mut covs) = self.covariances {
covs.insert(insert_idx, SMatrix::<f64, R, R>::zeros());
}
self.apply_eviction_policy();
}
fn epoch_at_idx(&self, index: usize) -> Result<Epoch, BraheError> {
if index >= self.epochs.len() {
return Err(BraheError::Error(format!(
"Index {} out of bounds for trajectory with {} epochs",
index,
self.epochs.len()
)));
}
Ok(self.epochs[index])
}
fn state_at_idx(&self, index: usize) -> Result<Self::StateVector, BraheError> {
if index >= self.states.len() {
return Err(BraheError::Error(format!(
"Index {} out of bounds for trajectory with {} states",
index,
self.states.len()
)));
}
Ok(self.states[index])
}
fn nearest_state(&self, epoch: &Epoch) -> Result<(Epoch, Self::StateVector), BraheError> {
if self.epochs.is_empty() {
return Err(BraheError::Error(
"Cannot find nearest state in empty trajectory".to_string(),
));
}
let mut nearest_idx = 0;
let mut min_diff = f64::MAX;
for (i, existing_epoch) in self.epochs.iter().enumerate() {
let diff = (*epoch - *existing_epoch).abs();
if diff < min_diff {
min_diff = diff;
nearest_idx = i;
}
if i > 0 && existing_epoch > epoch && diff > min_diff {
break;
}
}
Ok((self.epochs[nearest_idx], self.states[nearest_idx]))
}
fn len(&self) -> usize {
self.states.len()
}
fn is_empty(&self) -> bool {
self.states.is_empty()
}
fn start_epoch(&self) -> Option<Epoch> {
self.epochs.first().copied()
}
fn end_epoch(&self) -> Option<Epoch> {
self.epochs.last().copied()
}
fn timespan(&self) -> Option<f64> {
if self.epochs.len() < 2 {
None
} else {
Some(*self.epochs.last().unwrap() - *self.epochs.first().unwrap())
}
}
fn first(&self) -> Option<(Epoch, Self::StateVector)> {
if self.epochs.is_empty() {
None
} else {
Some((self.epochs[0], self.states[0]))
}
}
fn last(&self) -> Option<(Epoch, Self::StateVector)> {
if self.epochs.is_empty() {
None
} else {
let last_index = self.epochs.len() - 1;
Some((self.epochs[last_index], self.states[last_index]))
}
}
fn clear(&mut self) {
self.epochs.clear();
self.states.clear();
}
fn remove_epoch(&mut self, epoch: &Epoch) -> Result<Self::StateVector, BraheError> {
if let Some(index) = self.epochs.iter().position(|e| e == epoch) {
let removed_state = self.states.remove(index);
self.epochs.remove(index);
Ok(removed_state)
} else {
Err(BraheError::Error(
"Epoch not found in trajectory".to_string(),
))
}
}
fn remove(&mut self, index: usize) -> Result<(Epoch, Self::StateVector), BraheError> {
if index >= self.states.len() {
return Err(BraheError::Error(format!(
"Index {} out of bounds for trajectory with {} states",
index,
self.states.len()
)));
}
let removed_epoch = self.epochs.remove(index);
let removed_state = self.states.remove(index);
Ok((removed_epoch, removed_state))
}
fn get(&self, index: usize) -> Result<(Epoch, Self::StateVector), BraheError> {
if index >= self.states.len() {
return Err(BraheError::Error(format!(
"Index {} out of bounds for trajectory with {} states",
index,
self.states.len()
)));
}
Ok((self.epochs[index], self.states[index]))
}
fn index_before_epoch(&self, epoch: &Epoch) -> Result<usize, BraheError> {
if self.epochs.is_empty() {
return Err(BraheError::Error(
"Cannot get index from empty trajectory".to_string(),
));
}
if epoch < &self.epochs[0] {
return Err(BraheError::Error(
"Epoch is before all states in trajectory".to_string(),
));
}
for i in (0..self.epochs.len()).rev() {
if &self.epochs[i] <= epoch {
return Ok(i);
}
}
Err(BraheError::Error(
"Failed to find index before epoch".to_string(),
))
}
fn index_after_epoch(&self, epoch: &Epoch) -> Result<usize, BraheError> {
if self.epochs.is_empty() {
return Err(BraheError::Error(
"Cannot get index from empty trajectory".to_string(),
));
}
if epoch > self.epochs.last().unwrap() {
return Err(BraheError::Error(
"Epoch is after all states in trajectory".to_string(),
));
}
for i in 0..self.epochs.len() {
if &self.epochs[i] >= epoch {
return Ok(i);
}
}
Err(BraheError::Error(
"Failed to find index after epoch".to_string(),
))
}
fn set_eviction_policy_max_size(&mut self, max_size: usize) -> Result<(), BraheError> {
if max_size < 1 {
return Err(BraheError::Error("Maximum size must be >= 1".to_string()));
}
self.eviction_policy = TrajectoryEvictionPolicy::KeepCount;
self.max_size = Some(max_size);
self.max_age = None;
self.apply_eviction_policy();
Ok(())
}
fn set_eviction_policy_max_age(&mut self, max_age: f64) -> Result<(), BraheError> {
if max_age <= 0.0 {
return Err(BraheError::Error("Maximum age must be > 0.0".to_string()));
}
self.eviction_policy = TrajectoryEvictionPolicy::KeepWithinDuration;
self.max_age = Some(max_age);
self.max_size = None;
self.apply_eviction_policy();
Ok(())
}
fn get_eviction_policy(&self) -> TrajectoryEvictionPolicy {
self.eviction_policy
}
}
impl<const R: usize> InterpolationConfig for STrajectory<R> {
fn with_interpolation_method(mut self, method: InterpolationMethod) -> Self {
self.interpolation_method = method;
self
}
fn set_interpolation_method(&mut self, method: InterpolationMethod) {
self.interpolation_method = method;
}
fn get_interpolation_method(&self) -> InterpolationMethod {
self.interpolation_method
}
}
impl<const R: usize> CovarianceInterpolationConfig for STrajectory<R> {
fn with_covariance_interpolation_method(
mut self,
method: CovarianceInterpolationMethod,
) -> Self {
self.covariance_interpolation_method = method;
self
}
fn set_covariance_interpolation_method(&mut self, method: CovarianceInterpolationMethod) {
self.covariance_interpolation_method = method;
}
fn get_covariance_interpolation_method(&self) -> CovarianceInterpolationMethod {
self.covariance_interpolation_method
}
}
impl<const R: usize> InterpolatableTrajectory for STrajectory<R> {
fn interpolate(&self, epoch: &Epoch) -> Result<SVector<f64, R>, BraheError> {
if let Some(start) = self.start_epoch()
&& *epoch < start
{
return Err(BraheError::OutOfBoundsError(format!(
"Cannot interpolate: epoch {} is before trajectory start {}",
epoch, start
)));
}
if let Some(end) = self.end_epoch()
&& *epoch > end
{
return Err(BraheError::OutOfBoundsError(format!(
"Cannot interpolate: epoch {} is after trajectory end {}",
epoch, end
)));
}
let idx1 = self.index_before_epoch(epoch)?;
let idx2 = self.index_after_epoch(epoch)?;
if idx1 == idx2 {
return self.state_at_idx(idx1);
}
let method = self.get_interpolation_method();
let required = method.min_points_required();
if self.len() < required {
return Err(BraheError::Error(format!(
"{:?} requires {} points, trajectory has {}",
method,
required,
self.len()
)));
}
let ref_epoch = self.start_epoch().unwrap();
match method {
InterpolationMethod::Linear => self.interpolate_linear(epoch),
InterpolationMethod::Lagrange { degree } => {
let n_points = degree + 1;
let (start_idx, end_idx) =
compute_interpolation_window(self.len(), idx1, idx2, n_points)?;
let times: Vec<f64> = (start_idx..=end_idx)
.map(|i| self.epochs[i] - ref_epoch)
.collect();
let values: Vec<SVector<f64, R>> =
(start_idx..=end_idx).map(|i| self.states[i]).collect();
let t = *epoch - ref_epoch;
Ok(interpolate_lagrange_svector(×, &values, t))
}
InterpolationMethod::HermiteCubic | InterpolationMethod::HermiteQuintic => {
Err(BraheError::Error(format!(
"{:?} interpolation requires 6D orbital states with position/velocity \
structure. STrajectory<{}> cannot use Hermite methods. Use \
SOrbitTrajectory or DOrbitTrajectory for orbital states, or use \
Linear/Lagrange interpolation for generic systems.",
self.interpolation_method, R
)))
}
}
}
}
fn compute_interpolation_window(
len: usize,
idx1: usize,
idx2: usize,
n_points: usize,
) -> Result<(usize, usize), BraheError> {
if len < n_points {
return Err(BraheError::Error(format!(
"Need {} points for interpolation, trajectory has {}",
n_points, len
)));
}
let center = (idx1 + idx2) / 2;
let half_window = n_points / 2;
let mut start_idx = center.saturating_sub(half_window);
let mut end_idx = start_idx + n_points - 1;
if end_idx >= len {
end_idx = len - 1;
start_idx = end_idx.saturating_sub(n_points - 1);
}
Ok((start_idx, end_idx))
}
#[cfg(test)]
#[cfg_attr(coverage_nightly, coverage(off))]
mod tests {
use super::*;
use crate::time::{Epoch, TimeSystem};
use crate::utils::testing::setup_global_test_eop;
use approx::assert_abs_diff_eq;
use nalgebra as na;
use nalgebra::Vector6;
fn create_test_trajectory() -> STrajectory6 {
let epochs = vec![
Epoch::from_jd(2451545.0, TimeSystem::UTC),
Epoch::from_jd(2451545.1, TimeSystem::UTC),
Epoch::from_jd(2451545.2, TimeSystem::UTC),
];
let states = vec![
Vector6::new(7000e3, 0.0, 0.0, 0.0, 7.5e3, 0.0),
Vector6::new(7100e3, 1000e3, 500e3, 100.0, 7.6e3, 50.0),
Vector6::new(7200e3, 2000e3, 1000e3, 200.0, 7.7e3, 100.0),
];
STrajectory6::from_data(epochs, states).unwrap()
}
#[test]
fn test_strajectory_strajectory_new() {
let trajectory = STrajectory6::new();
assert_eq!(trajectory.len(), 0);
assert_eq!(trajectory.interpolation_method, InterpolationMethod::Linear);
assert!(trajectory.is_empty());
}
#[test]
fn test_strajectory_with_interpolation_method() {
let traj = STrajectory6::new().with_interpolation_method(InterpolationMethod::Linear);
assert_eq!(traj.get_interpolation_method(), InterpolationMethod::Linear);
assert_eq!(traj.len(), 0);
let mut traj = STrajectory6::new().with_interpolation_method(InterpolationMethod::Linear);
let t0 = Epoch::from_jd(2451545.0, TimeSystem::UTC);
let state = Vector6::new(1.0, 2.0, 3.0, 4.0, 5.0, 6.0);
traj.add(t0, state);
assert_eq!(traj.len(), 1);
assert_eq!(traj.get_interpolation_method(), InterpolationMethod::Linear);
}
#[test]
fn test_strajectory_with_eviction_policy_max_size_builder() {
let traj = STrajectory6::new().with_eviction_policy_max_size(5);
assert_eq!(
traj.get_eviction_policy(),
TrajectoryEvictionPolicy::KeepCount
);
assert_eq!(traj.len(), 0);
}
#[test]
fn test_strajectory_with_eviction_policy_max_age_builder() {
let traj = STrajectory6::new().with_eviction_policy_max_age(300.0);
assert_eq!(
traj.get_eviction_policy(),
TrajectoryEvictionPolicy::KeepWithinDuration
);
assert_eq!(traj.len(), 0);
}
#[test]
fn test_strajectory_builder_pattern_chaining() {
let mut traj = STrajectory6::new()
.with_interpolation_method(InterpolationMethod::Linear)
.with_eviction_policy_max_size(10);
assert_eq!(traj.get_interpolation_method(), InterpolationMethod::Linear);
assert_eq!(
traj.get_eviction_policy(),
TrajectoryEvictionPolicy::KeepCount
);
let t0 = Epoch::from_datetime(2023, 1, 1, 12, 0, 0.0, 0.0, TimeSystem::UTC);
for i in 0..15 {
let epoch = t0 + (i as f64 * 60.0);
let state = Vector6::new(7000e3 + i as f64 * 1000.0, 0.0, 0.0, 0.0, 7.5e3, 0.0);
traj.add(epoch, state);
}
assert_eq!(traj.len(), 10);
}
#[test]
fn test_strajectory_dimension() {
let traj = STrajectory6::new();
assert_eq!(traj.dimension(), 6);
let traj3: STrajectory<3> = STrajectory::new();
assert_eq!(traj3.dimension(), 3);
let traj4: STrajectory<4> = STrajectory::new();
assert_eq!(traj4.dimension(), 4);
let traj12: STrajectory<12> = STrajectory::new();
assert_eq!(traj12.dimension(), 12);
}
#[test]
fn test_strajectory_to_matrix() {
let t0 = Epoch::from_jd(2451545.0, TimeSystem::UTC);
let epochs = vec![t0, t0 + 60.0, t0 + 120.0];
let states = vec![
Vector6::new(1.0, 2.0, 3.0, 4.0, 5.0, 6.0),
Vector6::new(11.0, 12.0, 13.0, 14.0, 15.0, 16.0),
Vector6::new(21.0, 22.0, 23.0, 24.0, 25.0, 26.0),
];
let traj = STrajectory6::from_data(epochs, states).unwrap();
let matrix = traj.to_matrix().unwrap();
assert_eq!(matrix.nrows(), 3);
assert_eq!(matrix.ncols(), 6);
assert_eq!(matrix[(0, 0)], 1.0);
assert_eq!(matrix[(0, 1)], 2.0);
assert_eq!(matrix[(0, 2)], 3.0);
assert_eq!(matrix[(0, 3)], 4.0);
assert_eq!(matrix[(0, 4)], 5.0);
assert_eq!(matrix[(0, 5)], 6.0);
assert_eq!(matrix[(1, 0)], 11.0);
assert_eq!(matrix[(1, 1)], 12.0);
assert_eq!(matrix[(1, 2)], 13.0);
assert_eq!(matrix[(1, 3)], 14.0);
assert_eq!(matrix[(1, 4)], 15.0);
assert_eq!(matrix[(1, 5)], 16.0);
assert_eq!(matrix[(2, 0)], 21.0);
assert_eq!(matrix[(2, 1)], 22.0);
assert_eq!(matrix[(2, 2)], 23.0);
assert_eq!(matrix[(2, 3)], 24.0);
assert_eq!(matrix[(2, 4)], 25.0);
assert_eq!(matrix[(2, 5)], 26.0);
assert_eq!(matrix[(0, 0)], 1.0);
assert_eq!(matrix[(1, 0)], 11.0);
assert_eq!(matrix[(2, 0)], 21.0);
}
#[test]
fn test_strajectory_default() {
let trajectory = STrajectory6::default();
assert_eq!(trajectory.len(), 0);
assert_eq!(trajectory.interpolation_method, InterpolationMethod::Linear);
assert!(trajectory.is_empty());
}
#[test]
fn test_strajectory_index_index() {
let trajectory = create_test_trajectory();
let state0 = &trajectory[0];
assert_abs_diff_eq!(state0[0], 7000e3, epsilon = 1.0);
let state1 = &trajectory[1];
assert_abs_diff_eq!(state1[0], 7100e3, epsilon = 1.0);
let state2 = &trajectory[2];
assert_abs_diff_eq!(state2[0], 7200e3, epsilon = 1.0);
}
#[test]
#[should_panic]
fn test_strajectory_index_index_out_of_bounds() {
let trajectory = create_test_trajectory();
let _ = &trajectory[10]; }
#[test]
fn test_strajectory_intoiterator_into_iter() {
let trajectory = create_test_trajectory();
let mut count = 0;
for (epoch, state) in &trajectory {
match count {
0 => {
assert_eq!(epoch.jd(), 2451545.0);
assert_abs_diff_eq!(state[0], 7000e3, epsilon = 1.0);
}
1 => {
assert_eq!(epoch.jd(), 2451545.1);
assert_abs_diff_eq!(state[0], 7100e3, epsilon = 1.0);
}
2 => {
assert_eq!(epoch.jd(), 2451545.2);
assert_abs_diff_eq!(state[0], 7200e3, epsilon = 1.0);
}
_ => panic!("Too many iterations"),
}
count += 1;
}
assert_eq!(count, 3);
}
#[test]
fn test_strajectory_intoiterator_into_iter_empty() {
let trajectory = STrajectory6::new();
let mut count = 0;
for _ in &trajectory {
count += 1;
}
assert_eq!(count, 0);
}
#[test]
fn test_strajectory_iterator_iterator_size_hint() {
let trajectory = create_test_trajectory();
let iter = trajectory.into_iter();
let (lower, upper) = iter.size_hint();
assert_eq!(lower, 3);
assert_eq!(upper, Some(3));
}
#[test]
fn test_strajectory_iterator_iterator_len() {
let trajectory = create_test_trajectory();
let iter = trajectory.into_iter();
assert_eq!(iter.len(), 3);
}
#[test]
fn test_strajectory_trajectory_add() {
let mut trajectory = STrajectory6::new();
let epoch1 = Epoch::from_jd(2451545.0, TimeSystem::UTC);
let state1 = Vector6::new(7000e3, 0.0, 0.0, 0.0, 7.5e3, 0.0);
trajectory.add(epoch1, state1);
let epoch3 = Epoch::from_jd(2451545.2, TimeSystem::UTC);
let state3 = Vector6::new(7200e3, 0.0, 0.0, 0.0, 7.7e3, 0.0);
trajectory.add(epoch3, state3);
let epoch2 = Epoch::from_jd(2451545.1, TimeSystem::UTC);
let state2 = Vector6::new(7100e3, 0.0, 0.0, 0.0, 7.6e3, 0.0);
trajectory.add(epoch2, state2);
assert_eq!(trajectory.len(), 3);
assert_eq!(trajectory.epochs[0].jd(), 2451545.0);
assert_eq!(trajectory.epochs[1].jd(), 2451545.1);
assert_eq!(trajectory.epochs[2].jd(), 2451545.2);
}
#[test]
fn test_strajectory_trajectory_add_append() {
let mut trajectory = STrajectory6::new();
let epoch = Epoch::from_jd(2451545.0, TimeSystem::UTC);
let state1 = Vector6::new(7000e3, 0.0, 0.0, 0.0, 7.5e3, 0.0);
trajectory.add(epoch, state1);
assert_eq!(trajectory.len(), 1);
assert_eq!(trajectory.states[0][0], 7000e3);
let state2 = Vector6::new(7100e3, 100e3, 50e3, 10.0, 7.6e3, 5.0);
trajectory.add(epoch, state2);
assert_eq!(trajectory.len(), 2); assert_eq!(trajectory.states[0][0], 7000e3); assert_eq!(trajectory.states[1][0], 7100e3); }
#[test]
fn test_strajectory_trajectory_state() {
let trajectory = create_test_trajectory();
let state0 = trajectory.state_at_idx(0).unwrap();
assert_eq!(state0[0], 7000e3);
let state1 = trajectory.state_at_idx(1).unwrap();
assert_eq!(state1[0], 7100e3);
let state2 = trajectory.state_at_idx(2).unwrap();
assert_eq!(state2[0], 7200e3);
assert!(trajectory.state_at_idx(10).is_err());
}
#[test]
fn test_strajectory_trajectory_epoch() {
let trajectory = create_test_trajectory();
let epoch0 = trajectory.epoch_at_idx(0).unwrap();
assert_eq!(epoch0.jd(), 2451545.0);
let epoch1 = trajectory.epoch_at_idx(1).unwrap();
assert_eq!(epoch1.jd(), 2451545.1);
let epoch2 = trajectory.epoch_at_idx(2).unwrap();
assert_eq!(epoch2.jd(), 2451545.2);
assert!(trajectory.epoch_at_idx(10).is_err());
}
#[test]
fn test_strajectory_trajectory_nearest_state() {
let trajectory = create_test_trajectory();
let (epoch, state) = trajectory
.nearest_state(&Epoch::from_jd(2451545.0, TimeSystem::UTC))
.unwrap();
assert_eq!(epoch.jd(), 2451545.0);
assert_eq!(state[0], 7000e3);
let (epoch, state) = trajectory
.nearest_state(&Epoch::from_jd(2451545.05, TimeSystem::UTC))
.unwrap();
assert_eq!(epoch.jd(), 2451545.0);
assert_eq!(state[0], 7000e3);
let (epoch, state) = trajectory
.nearest_state(&Epoch::from_jd(2451545.0999, TimeSystem::UTC))
.unwrap();
assert_eq!(epoch.jd(), 2451545.1);
assert_eq!(state[0], 7100e3);
let (epoch, state) = trajectory
.nearest_state(&Epoch::from_jd(2451545.3, TimeSystem::UTC))
.unwrap();
assert_eq!(epoch.jd(), 2451545.2);
assert_eq!(state[0], 7200e3);
}
#[test]
fn test_strajectory_trajectory_len() {
let mut trajectory = STrajectory6::new();
assert_eq!(trajectory.len(), 0);
trajectory.add(
Epoch::from_jd(2451545.0, TimeSystem::UTC),
Vector6::new(7000e3, 0.0, 0.0, 0.0, 7.5e3, 0.0),
);
assert_eq!(trajectory.len(), 1);
trajectory.add(
Epoch::from_jd(2451545.1, TimeSystem::UTC),
Vector6::new(7100e3, 0.0, 0.0, 0.0, 7.6e3, 0.0),
);
assert_eq!(trajectory.len(), 2);
}
#[test]
fn test_strajectory_trajectory_is_empty() {
let mut trajectory = STrajectory6::new();
assert!(trajectory.is_empty());
trajectory.add(
Epoch::from_jd(2451545.0, TimeSystem::UTC),
Vector6::new(7000e3, 0.0, 0.0, 0.0, 7.5e3, 0.0),
);
assert!(!trajectory.is_empty());
trajectory.clear();
assert!(trajectory.is_empty());
}
#[test]
fn test_strajectory_trajectory_start_epoch() {
let trajectory = create_test_trajectory();
let start = trajectory.start_epoch().unwrap();
assert_eq!(start.jd(), 2451545.0);
let empty_trajectory = STrajectory6::new();
assert!(empty_trajectory.start_epoch().is_none());
}
#[test]
fn test_strajectory_trajectory_end_epoch() {
let trajectory = create_test_trajectory();
let end = trajectory.end_epoch().unwrap();
assert_eq!(end.jd(), 2451545.2);
let empty_trajectory = STrajectory6::new();
assert!(empty_trajectory.end_epoch().is_none());
}
#[test]
fn test_strajectory_trajectory_timespan() {
let trajectory = create_test_trajectory();
let span = trajectory.timespan().unwrap();
assert_abs_diff_eq!(span, 0.2 * 86400.0, epsilon = 1.0);
let mut single_trajectory = STrajectory6::new();
single_trajectory.add(
Epoch::from_jd(2451545.0, TimeSystem::UTC),
Vector6::new(7000e3, 0.0, 0.0, 0.0, 7.5e3, 0.0),
);
assert!(single_trajectory.timespan().is_none());
let empty_trajectory = STrajectory6::new();
assert!(empty_trajectory.timespan().is_none());
}
#[test]
fn test_strajectory_trajectory_first() {
let empty_trajectory = STrajectory6::new();
assert!(empty_trajectory.first().is_none());
let mut single_trajectory = STrajectory6::new();
let epoch = Epoch::from_jd(2451545.0, TimeSystem::UTC);
let state = Vector6::new(7000e3, 0.0, 0.0, 0.0, 7.5e3, 0.0);
single_trajectory.add(epoch, state);
let (first_epoch, first_state) = single_trajectory.first().unwrap();
assert_eq!(first_epoch.jd(), 2451545.0);
assert_eq!(first_state[0], 7000e3);
let trajectory = create_test_trajectory();
let (first_epoch, first_state) = trajectory.first().unwrap();
assert_eq!(first_epoch.jd(), 2451545.0);
assert_eq!(first_state[0], 7000e3);
}
#[test]
fn test_strajectory_trajectory_last() {
let empty_trajectory = STrajectory6::new();
assert!(empty_trajectory.last().is_none());
let mut single_trajectory = STrajectory6::new();
let epoch = Epoch::from_jd(2451545.0, TimeSystem::UTC);
let state = Vector6::new(7000e3, 0.0, 0.0, 0.0, 7.5e3, 0.0);
single_trajectory.add(epoch, state);
let (last_epoch, last_state) = single_trajectory.last().unwrap();
assert_eq!(last_epoch.jd(), 2451545.0);
assert_eq!(last_state[0], 7000e3);
let trajectory = create_test_trajectory();
let (last_epoch, last_state) = trajectory.last().unwrap();
assert_eq!(last_epoch.jd(), 2451545.2);
assert_eq!(last_state[0], 7200e3);
}
#[test]
fn test_strajectory_trajectory_clear() {
let mut trajectory = create_test_trajectory();
assert_eq!(trajectory.len(), 3);
assert!(!trajectory.is_empty());
trajectory.clear();
assert_eq!(trajectory.len(), 0);
assert!(trajectory.is_empty());
assert!(trajectory.start_epoch().is_none());
assert!(trajectory.end_epoch().is_none());
}
#[test]
fn test_strajectory_trajectory_remove_epoch() {
let mut trajectory = create_test_trajectory();
let epoch_to_remove = Epoch::from_jd(2451545.1, TimeSystem::UTC);
let removed_state = trajectory.remove_epoch(&epoch_to_remove).unwrap();
assert_eq!(removed_state[0], 7100e3);
assert_eq!(trajectory.len(), 2);
let non_existent_epoch = Epoch::from_jd(2451546.0, TimeSystem::UTC);
assert!(trajectory.remove_epoch(&non_existent_epoch).is_err());
}
#[test]
fn test_strajectory_trajectory_remove() {
let mut trajectory = create_test_trajectory();
let (removed_epoch, removed_state) = trajectory.remove(1).unwrap();
assert_eq!(removed_epoch.jd(), 2451545.1);
assert_eq!(removed_state[0], 7100e3);
assert_eq!(trajectory.len(), 2);
assert!(trajectory.remove(10).is_err());
}
#[test]
fn test_strajectory_trajectory_get() {
let trajectory = create_test_trajectory();
let (epoch, state) = trajectory.get(0).unwrap();
assert_eq!(epoch.jd(), 2451545.0);
assert_eq!(state[0], 7000e3);
assert!(trajectory.get(10).is_err());
}
#[test]
fn test_strajectory_trajectory_index_before_epoch() {
let t0 = Epoch::from_jd(2451545.0, TimeSystem::UTC);
let epochs = vec![t0, t0 + 60.0, t0 + 120.0];
let states = vec![
Vector6::new(1.0, 0.0, 0.0, 0.0, 0.0, 0.0),
Vector6::new(2.0, 0.0, 0.0, 0.0, 0.0, 0.0),
Vector6::new(3.0, 0.0, 0.0, 0.0, 0.0, 0.0),
];
let traj = STrajectory6::from_data(epochs, states).unwrap();
let before_t0 = t0 - 10.0;
assert!(traj.index_before_epoch(&before_t0).is_err());
let t0_plus_30 = t0 + 30.0;
let idx = traj.index_before_epoch(&t0_plus_30).unwrap();
assert_eq!(idx, 0);
let t0_plus_60 = t0 + 60.0;
let idx = traj.index_before_epoch(&t0_plus_60).unwrap();
assert_eq!(idx, 1);
let t0_plus_90 = t0 + 90.0;
let idx = traj.index_before_epoch(&t0_plus_90).unwrap();
assert_eq!(idx, 1);
let t0_plus_120 = t0 + 120.0;
let idx = traj.index_before_epoch(&t0_plus_120).unwrap();
assert_eq!(idx, 2);
let t0_plus_150 = t0 + 150.0;
let idx = traj.index_before_epoch(&t0_plus_150).unwrap();
assert_eq!(idx, 2);
}
#[test]
fn test_strajectory_trajectory_index_after_epoch() {
let t0 = Epoch::from_jd(2451545.0, TimeSystem::UTC);
let epochs = vec![t0, t0 + 60.0, t0 + 120.0];
let states = vec![
Vector6::new(1.0, 0.0, 0.0, 0.0, 0.0, 0.0),
Vector6::new(2.0, 0.0, 0.0, 0.0, 0.0, 0.0),
Vector6::new(3.0, 0.0, 0.0, 0.0, 0.0, 0.0),
];
let traj = STrajectory6::from_data(epochs, states).unwrap();
let before_t0 = t0 - 30.0;
let idx = traj.index_after_epoch(&before_t0).unwrap();
assert_eq!(idx, 0);
let idx = traj.index_after_epoch(&t0).unwrap();
assert_eq!(idx, 0);
let t0_plus_30 = t0 + 30.0;
let idx = traj.index_after_epoch(&t0_plus_30).unwrap();
assert_eq!(idx, 1);
let t0_plus_60 = t0 + 60.0;
let idx = traj.index_after_epoch(&t0_plus_60).unwrap();
assert_eq!(idx, 1);
let t0_plus_90 = t0 + 90.0;
let idx = traj.index_after_epoch(&t0_plus_90).unwrap();
assert_eq!(idx, 2);
let t0_plus_120 = t0 + 120.0;
let idx = traj.index_after_epoch(&t0_plus_120).unwrap();
assert_eq!(idx, 2);
let t0_plus_150 = t0 + 150.0;
assert!(traj.index_after_epoch(&t0_plus_150).is_err());
}
#[test]
fn test_strajectory_trajectory_state_before_epoch() {
let t0 = Epoch::from_jd(2451545.0, TimeSystem::UTC);
let epochs = vec![t0, t0 + 60.0, t0 + 120.0];
let states = vec![
Vector6::new(1000.0, 100.0, 10.0, 1.0, 0.1, 0.01),
Vector6::new(2000.0, 200.0, 20.0, 2.0, 0.2, 0.02),
Vector6::new(3000.0, 300.0, 30.0, 3.0, 0.3, 0.03),
];
let traj = STrajectory6::from_data(epochs, states).unwrap();
let before_t0 = t0 - 10.0;
assert!(traj.state_before_epoch(&before_t0).is_err());
let t0_plus_30 = t0 + 30.0;
let (epoch, state) = traj.state_before_epoch(&t0_plus_30).unwrap();
assert_eq!(epoch, t0);
assert_eq!(state[0], 1000.0);
assert_eq!(state[1], 100.0);
let t0_plus_60 = t0 + 60.0;
let (epoch, state) = traj.state_before_epoch(&t0_plus_60).unwrap();
assert_eq!(epoch, t0 + 60.0);
assert_eq!(state[0], 2000.0);
assert_eq!(state[1], 200.0);
let t0_plus_90 = t0 + 90.0;
let (epoch, state) = traj.state_before_epoch(&t0_plus_90).unwrap();
assert_eq!(epoch, t0 + 60.0);
assert_eq!(state[0], 2000.0);
assert_eq!(state[1], 200.0);
let t0_plus_150 = t0 + 150.0;
let (epoch, state) = traj.state_before_epoch(&t0_plus_150).unwrap();
assert_eq!(epoch, t0 + 120.0);
assert_eq!(state[0], 3000.0);
assert_eq!(state[1], 300.0);
let t0_plus_45 = t0 + 45.0;
let idx = traj.index_before_epoch(&t0_plus_45).unwrap();
let (expected_epoch, expected_state) = traj.get(idx).unwrap();
let (actual_epoch, actual_state) = traj.state_before_epoch(&t0_plus_45).unwrap();
assert_eq!(actual_epoch, expected_epoch);
assert_eq!(actual_state, expected_state);
}
#[test]
fn test_strajectory_trajectory_state_after_epoch() {
let t0 = Epoch::from_jd(2451545.0, TimeSystem::UTC);
let epochs = vec![t0, t0 + 60.0, t0 + 120.0];
let states = vec![
Vector6::new(1000.0, 100.0, 10.0, 1.0, 0.1, 0.01),
Vector6::new(2000.0, 200.0, 20.0, 2.0, 0.2, 0.02),
Vector6::new(3000.0, 300.0, 30.0, 3.0, 0.3, 0.03),
];
let traj = STrajectory6::from_data(epochs, states).unwrap();
let after_t0_120 = t0 + 150.0;
assert!(traj.state_after_epoch(&after_t0_120).is_err());
let before_t0 = t0 - 30.0;
let (epoch, state) = traj.state_after_epoch(&before_t0).unwrap();
assert_eq!(epoch, t0);
assert_eq!(state[0], 1000.0);
assert_eq!(state[1], 100.0);
let (epoch, state) = traj.state_after_epoch(&t0).unwrap();
assert_eq!(epoch, t0);
assert_eq!(state[0], 1000.0);
assert_eq!(state[1], 100.0);
let t0_plus_30 = t0 + 30.0;
let (epoch, state) = traj.state_after_epoch(&t0_plus_30).unwrap();
assert_eq!(epoch, t0 + 60.0);
assert_eq!(state[0], 2000.0);
assert_eq!(state[1], 200.0);
let t0_plus_60 = t0 + 60.0;
let (epoch, state) = traj.state_after_epoch(&t0_plus_60).unwrap();
assert_eq!(epoch, t0 + 60.0);
assert_eq!(state[0], 2000.0);
assert_eq!(state[1], 200.0);
let t0_plus_90 = t0 + 90.0;
let (epoch, state) = traj.state_after_epoch(&t0_plus_90).unwrap();
assert_eq!(epoch, t0 + 120.0);
assert_eq!(state[0], 3000.0);
assert_eq!(state[1], 300.0);
let t0_plus_45 = t0 + 45.0;
let idx = traj.index_after_epoch(&t0_plus_45).unwrap();
let (expected_epoch, expected_state) = traj.get(idx).unwrap();
let (actual_epoch, actual_state) = traj.state_after_epoch(&t0_plus_45).unwrap();
assert_eq!(actual_epoch, expected_epoch);
assert_eq!(actual_state, expected_state);
}
#[test]
fn test_strajectory_trajectory_get_eviction_policy() {
let mut traj = STrajectory6::new();
assert_eq!(traj.get_eviction_policy(), TrajectoryEvictionPolicy::None);
traj.set_eviction_policy_max_size(10).unwrap();
assert_eq!(
traj.get_eviction_policy(),
TrajectoryEvictionPolicy::KeepCount
);
traj.set_eviction_policy_max_age(100.0).unwrap();
assert_eq!(
traj.get_eviction_policy(),
TrajectoryEvictionPolicy::KeepWithinDuration
);
}
#[test]
fn test_strajectory_set_eviction_policy_max_size() {
let mut traj = STrajectory6::new();
let t0 = Epoch::from_datetime(2023, 1, 1, 12, 0, 0.0, 0.0, TimeSystem::UTC);
for i in 0..5 {
let epoch = t0 + (i as f64 * 60.0);
let state = Vector6::new(7000e3 + i as f64 * 1000.0, 0.0, 0.0, 0.0, 7.5e3, 0.0);
traj.add(epoch, state);
}
assert_eq!(traj.len(), 5);
traj.set_eviction_policy_max_size(3).unwrap();
assert_eq!(traj.len(), 3);
let first_state = traj.state_at_idx(0).unwrap();
assert_abs_diff_eq!(first_state[0], 7000e3 + 2000.0, epsilon = 1.0);
let new_epoch = t0 + 5.0 * 60.0;
let new_state = Vector6::new(7000e3 + 5000.0, 0.0, 0.0, 0.0, 7.5e3, 0.0);
traj.add(new_epoch, new_state);
assert_eq!(traj.len(), 3);
assert_eq!(traj.state_at_idx(0).unwrap()[0], 7000e3 + 3000.0);
assert!(traj.set_eviction_policy_max_size(0).is_err());
}
#[test]
fn test_strajectory_set_eviction_policy_max_age() {
let mut traj = STrajectory6::new();
let t0 = Epoch::from_datetime(2023, 1, 1, 12, 0, 0.0, 0.0, TimeSystem::UTC);
for i in 0..6 {
let epoch = t0 + (i as f64 * 60.0); let state = Vector6::new(7000e3 + i as f64 * 1000.0, 0.0, 0.0, 0.0, 7.5e3, 0.0);
traj.add(epoch, state);
}
assert_eq!(traj.len(), 6);
traj.set_eviction_policy_max_age(240.0).unwrap();
assert_eq!(traj.len(), 5);
assert_eq!(traj.epoch_at_idx(0).unwrap(), t0 + 60.0);
assert_eq!(traj.state_at_idx(0).unwrap()[0], 7000e3 + 1000.0);
traj.set_eviction_policy_max_age(239.0).unwrap();
assert_eq!(traj.len(), 4);
assert_eq!(traj.epoch_at_idx(0).unwrap(), t0 + 120.0);
assert_eq!(traj.state_at_idx(0).unwrap()[0], 7000e3 + 2000.0);
assert!(traj.set_eviction_policy_max_age(0.0).is_err());
assert!(traj.set_eviction_policy_max_age(-10.0).is_err());
}
#[test]
fn test_strajectory_interpolatable_get_interpolation_method() {
let mut traj = STrajectory6::new();
assert_eq!(traj.get_interpolation_method(), InterpolationMethod::Linear);
traj.set_interpolation_method(InterpolationMethod::Linear);
assert_eq!(traj.get_interpolation_method(), InterpolationMethod::Linear);
}
#[test]
fn test_strajectory_interpolatable_interpolate_linear() {
setup_global_test_eop();
let t0 = Epoch::from_jd(2451545.0, TimeSystem::UTC);
let epochs = vec![t0, t0 + 60.0, t0 + 120.0];
let states = vec![
Vector6::new(7000e3, 0.0, 0.0, 0.0, 7.5e3, 0.0),
Vector6::new(7060e3, 0.0, 0.0, 0.0, 7.5e3, 0.0),
Vector6::new(7120e3, 0.0, 0.0, 0.0, 7.5e3, 0.0),
];
let traj = STrajectory6::from_data(epochs, states).unwrap();
let t_mid = t0 + 30.0;
let state_mid = traj.interpolate_linear(&t_mid).unwrap();
assert_abs_diff_eq!(state_mid[0], 7030e3, epsilon = 1e-6);
assert_abs_diff_eq!(state_mid[1], 0.0, epsilon = 1e-6);
assert_abs_diff_eq!(state_mid[2], 0.0, epsilon = 1e-6);
assert_abs_diff_eq!(state_mid[3], 0.0, epsilon = 1e-6);
assert_abs_diff_eq!(state_mid[4], 7.5e3, epsilon = 1e-6);
assert_abs_diff_eq!(state_mid[5], 0.0, epsilon = 1e-6);
let state_t0 = traj.interpolate_linear(&t0).unwrap();
assert_abs_diff_eq!(state_t0[0], 7000e3, epsilon = 1e-6);
let state_t60 = traj.interpolate_linear(&(t0 + 60.0)).unwrap();
assert_abs_diff_eq!(state_t60[0], 7060e3, epsilon = 1e-6);
let state_t120 = traj.interpolate_linear(&(t0 + 120.0)).unwrap();
assert_abs_diff_eq!(state_t120[0], 7120e3, epsilon = 1e-6);
let t_90 = t0 + 90.0;
let state_90 = traj.interpolate_linear(&t_90).unwrap();
assert_abs_diff_eq!(state_90[0], 7090e3, epsilon = 1e-6);
assert_abs_diff_eq!(state_90[1], 0.0, epsilon = 1e-6);
assert_abs_diff_eq!(state_90[2], 0.0, epsilon = 1e-6);
assert_abs_diff_eq!(state_90[3], 0.0, epsilon = 1e-6);
assert_abs_diff_eq!(state_90[4], 7.5e3, epsilon = 1e-6);
assert_abs_diff_eq!(state_90[5], 0.0, epsilon = 1e-6);
let single_epoch = vec![t0];
let single_state = vec![Vector6::new(8000e3, 100.0, 200.0, 1.0, 2.0, 3.0)];
let single_traj = STrajectory6::from_data(single_epoch, single_state).unwrap();
let result = single_traj.interpolate_linear(&t0).unwrap();
assert_abs_diff_eq!(result[0], 8000e3, epsilon = 1e-6);
assert_abs_diff_eq!(result[1], 100.0, epsilon = 1e-6);
assert_abs_diff_eq!(result[2], 200.0, epsilon = 1e-6);
assert_abs_diff_eq!(result[3], 1.0, epsilon = 1e-6);
assert_abs_diff_eq!(result[4], 2.0, epsilon = 1e-6);
assert_abs_diff_eq!(result[5], 3.0, epsilon = 1e-6);
let different_epoch = t0 + 10.0;
assert!(single_traj.interpolate_linear(&different_epoch).is_err());
let empty_traj = STrajectory6::new();
assert!(empty_traj.interpolate_linear(&t0).is_err());
}
#[test]
fn test_strajectory_interpolatable_interpolate() {
setup_global_test_eop();
let t0 = Epoch::from_jd(2451545.0, TimeSystem::UTC);
let epochs = vec![t0, t0 + 60.0, t0 + 120.0];
let states = vec![
Vector6::new(7000e3, 0.0, 0.0, 0.0, 7.5e3, 0.0),
Vector6::new(7060e3, 0.0, 0.0, 0.0, 7.5e3, 0.0),
Vector6::new(7120e3, 0.0, 0.0, 0.0, 7.5e3, 0.0),
];
let traj = STrajectory6::from_data(epochs, states).unwrap();
let t_test = t0 + 30.0;
let result_interpolate = traj.interpolate(&t_test).unwrap();
let result_linear = traj.interpolate_linear(&t_test).unwrap();
assert_abs_diff_eq!(result_interpolate[0], result_linear[0], epsilon = 1e-6);
assert_abs_diff_eq!(result_interpolate[1], result_linear[1], epsilon = 1e-6);
assert_abs_diff_eq!(result_interpolate[2], result_linear[2], epsilon = 1e-6);
assert_abs_diff_eq!(result_interpolate[3], result_linear[3], epsilon = 1e-6);
assert_abs_diff_eq!(result_interpolate[4], result_linear[4], epsilon = 1e-6);
assert_abs_diff_eq!(result_interpolate[5], result_linear[5], epsilon = 1e-6);
assert_abs_diff_eq!(result_interpolate[0], 7030e3, epsilon = 1e-6);
}
#[test]
fn test_strajectory_interpolate_before_start() {
setup_global_test_eop();
let t0 = Epoch::from_jd(2451545.0, TimeSystem::UTC);
let epochs = vec![t0, t0 + 60.0, t0 + 120.0];
let states = vec![
Vector6::new(7000e3, 0.0, 0.0, 0.0, 7.5e3, 0.0),
Vector6::new(7060e3, 0.0, 0.0, 0.0, 7.5e3, 0.0),
Vector6::new(7120e3, 0.0, 0.0, 0.0, 7.5e3, 0.0),
];
let traj = STrajectory6::from_data(epochs, states).unwrap();
let before_start = t0 - 10.0;
let result = traj.interpolate_linear(&before_start);
assert!(result.is_err());
match result {
Err(BraheError::OutOfBoundsError(_)) => {} _ => panic!("Expected OutOfBoundsError for interpolation before start"),
}
let result = traj.interpolate(&before_start);
assert!(result.is_err());
match result {
Err(BraheError::OutOfBoundsError(_)) => {} _ => panic!("Expected OutOfBoundsError for interpolation before start"),
}
}
#[test]
fn test_strajectory_interpolate_after_end() {
setup_global_test_eop();
let t0 = Epoch::from_jd(2451545.0, TimeSystem::UTC);
let epochs = vec![t0, t0 + 60.0, t0 + 120.0];
let states = vec![
Vector6::new(7000e3, 0.0, 0.0, 0.0, 7.5e3, 0.0),
Vector6::new(7060e3, 0.0, 0.0, 0.0, 7.5e3, 0.0),
Vector6::new(7120e3, 0.0, 0.0, 0.0, 7.5e3, 0.0),
];
let traj = STrajectory6::from_data(epochs, states).unwrap();
let after_end = t0 + 130.0;
let result = traj.interpolate_linear(&after_end);
assert!(result.is_err());
match result {
Err(BraheError::OutOfBoundsError(_)) => {} _ => panic!("Expected OutOfBoundsError for interpolation after end"),
}
let result = traj.interpolate(&after_end);
assert!(result.is_err());
match result {
Err(BraheError::OutOfBoundsError(_)) => {} _ => panic!("Expected OutOfBoundsError for interpolation after end"),
}
}
#[test]
fn test_strajectory_interpolate_empty_trajectory() {
setup_global_test_eop();
let traj = STrajectory6::new();
let t = Epoch::from_jd(2451545.0, TimeSystem::UTC);
let result = traj.interpolate_linear(&t);
assert!(result.is_err());
match result {
Err(BraheError::Error(msg)) => {
assert!(msg.contains("empty trajectory"));
}
_ => panic!("Expected Error for empty trajectory"),
}
let result = traj.interpolate(&t);
assert!(result.is_err());
}
#[test]
fn test_strajectory_interpolate_single_state_exact_match() {
setup_global_test_eop();
let t0 = Epoch::from_jd(2451545.0, TimeSystem::UTC);
let state0 = Vector6::new(7000e3, 0.0, 0.0, 0.0, 7.5e3, 0.0);
let mut traj = STrajectory6::new();
traj.add(t0, state0);
let result = traj.interpolate_linear(&t0).unwrap();
assert_abs_diff_eq!(result[0], state0[0], epsilon = 1e-6);
assert_abs_diff_eq!(result[1], state0[1], epsilon = 1e-6);
assert_abs_diff_eq!(result[2], state0[2], epsilon = 1e-6);
let result = traj.interpolate(&t0).unwrap();
assert_abs_diff_eq!(result[0], state0[0], epsilon = 1e-6);
}
#[test]
fn test_strajectory_interpolate_single_state_no_match() {
setup_global_test_eop();
let t0 = Epoch::from_jd(2451545.0, TimeSystem::UTC);
let state0 = Vector6::new(7000e3, 0.0, 0.0, 0.0, 7.5e3, 0.0);
let mut traj = STrajectory6::new();
traj.add(t0, state0);
let t_different = t0 + 60.0;
let result = traj.interpolate_linear(&t_different);
assert!(result.is_err());
match result {
Err(BraheError::Error(msg)) => {
assert!(msg.contains("single state"));
}
_ => panic!("Expected Error for single state with non-matching epoch"),
}
let result = traj.interpolate(&t_different);
assert!(result.is_err());
}
#[test]
fn test_strajectory_covariance_interpolation_config() {
setup_global_test_eop();
let traj = STrajectory6::new();
assert_eq!(
traj.get_covariance_interpolation_method(),
CovarianceInterpolationMethod::TwoWasserstein
);
let traj = STrajectory6::new()
.with_covariance_interpolation_method(CovarianceInterpolationMethod::MatrixSquareRoot);
assert_eq!(
traj.get_covariance_interpolation_method(),
CovarianceInterpolationMethod::MatrixSquareRoot
);
let mut traj = STrajectory6::new();
traj.set_covariance_interpolation_method(CovarianceInterpolationMethod::MatrixSquareRoot);
assert_eq!(
traj.get_covariance_interpolation_method(),
CovarianceInterpolationMethod::MatrixSquareRoot
);
traj.set_covariance_interpolation_method(CovarianceInterpolationMethod::TwoWasserstein);
assert_eq!(
traj.get_covariance_interpolation_method(),
CovarianceInterpolationMethod::TwoWasserstein
);
}
#[test]
fn test_strajectory_covariance_interpolation_methods() {
setup_global_test_eop();
let t0 = Epoch::from_jd(2451545.0, TimeSystem::UTC);
let t1 = t0 + 60.0;
let state1 = Vector6::new(7000e3, 0.0, 0.0, 0.0, 7.5e3, 0.0);
let state2 = Vector6::new(7100e3, 0.0, 0.0, 0.0, 7.6e3, 0.0);
let cov1 = na::SMatrix::<f64, 6, 6>::from_diagonal(&na::Vector6::new(
100.0, 100.0, 100.0, 1.0, 1.0, 1.0,
));
let cov2 = na::SMatrix::<f64, 6, 6>::from_diagonal(&na::Vector6::new(
200.0, 200.0, 200.0, 2.0, 2.0, 2.0,
));
let mut traj = STrajectory6::new();
traj.enable_covariance_storage();
traj.add(t0, state1);
traj.add(t1, state2);
traj.set_covariance_at(0, cov1);
traj.set_covariance_at(1, cov2);
traj.set_covariance_interpolation_method(CovarianceInterpolationMethod::MatrixSquareRoot);
let t_mid = t0 + 30.0;
let cov_sqrt = traj.covariance_at(t_mid).unwrap();
for i in 0..6 {
assert!(cov_sqrt[(i, i)] > 0.0);
for j in 0..6 {
assert_abs_diff_eq!(cov_sqrt[(i, j)], cov_sqrt[(j, i)], epsilon = 1e-10);
}
}
assert!(cov_sqrt[(0, 0)] > 100.0 && cov_sqrt[(0, 0)] < 200.0);
traj.set_covariance_interpolation_method(CovarianceInterpolationMethod::TwoWasserstein);
let cov_wasserstein = traj.covariance_at(t_mid).unwrap();
for i in 0..6 {
assert!(cov_wasserstein[(i, i)] > 0.0);
for j in 0..6 {
assert_abs_diff_eq!(
cov_wasserstein[(i, j)],
cov_wasserstein[(j, i)],
epsilon = 1e-10
);
}
}
assert!(cov_wasserstein[(0, 0)] > 100.0 && cov_wasserstein[(0, 0)] < 200.0);
assert_abs_diff_eq!(cov_sqrt[(0, 0)], cov_wasserstein[(0, 0)], epsilon = 1e-6);
}
#[test]
fn test_strajectory_covariance_at_exact_epochs() {
setup_global_test_eop();
let t0 = Epoch::from_jd(2451545.0, TimeSystem::UTC);
let t1 = t0 + 60.0;
let state1 = Vector6::new(7000e3, 0.0, 0.0, 0.0, 7.5e3, 0.0);
let state2 = Vector6::new(7100e3, 0.0, 0.0, 0.0, 7.6e3, 0.0);
let cov1 = na::SMatrix::<f64, 6, 6>::identity() * 100.0;
let cov2 = na::SMatrix::<f64, 6, 6>::identity() * 200.0;
let mut traj = STrajectory6::new();
traj.enable_covariance_storage();
traj.add(t0, state1);
traj.add(t1, state2);
traj.set_covariance_at(0, cov1);
traj.set_covariance_at(1, cov2);
let result = traj.covariance_at(t0).unwrap();
assert_abs_diff_eq!(result[(0, 0)], 100.0, epsilon = 1e-10);
let result = traj.covariance_at(t1).unwrap();
assert_abs_diff_eq!(result[(0, 0)], 200.0, epsilon = 1e-10);
}
}