1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
    Nyx, blazing fast astrodynamics
    Copyright (C) 2021 Christopher Rabotin <christopher.rabotin@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published
    by the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

use super::Bodies;
use crate::time::{Duration, TimeUnit};
use std::cmp::PartialEq;
use std::convert::TryFrom;
use std::f64::consts::PI;
use std::fmt;

#[allow(non_snake_case, clippy::upper_case_acronyms)]
#[derive(Copy, Clone, PartialEq)]
pub enum Frame {
    /// Any celestial frame which only has a GM (e.g. 3 body frames)
    Celestial {
        gm: f64,
        ephem_path: [Option<usize>; 3],
        frame_path: [Option<usize>; 3],
    },
    /// Any Geoid which has a GM, flattening value, etc.
    Geoid {
        gm: f64,
        flattening: f64,
        equatorial_radius: f64,
        semi_major_radius: f64,
        ephem_path: [Option<usize>; 3],
        frame_path: [Option<usize>; 3],
    },
    /// Velocity, Normal, Cross (called VNB in GMAT)
    VNC,
    /// Radial, Cross, Normal
    RCN,
    /// Radial, in-track, normal
    RIC,
    /// Used as a placeholder only
    Inertial,
}

impl Frame {
    pub fn is_geoid(&self) -> bool {
        matches!(self, Frame::Geoid { .. })
    }

    pub fn is_celestial(&self) -> bool {
        matches!(self, Frame::Celestial { .. })
    }

    pub fn ephem_path(&self) -> Vec<usize> {
        match self {
            Frame::Celestial { ephem_path, .. } | Frame::Geoid { ephem_path, .. } => {
                let mut path = Vec::with_capacity(3);
                for p in ephem_path.iter().flatten() {
                    path.push(*p)
                }
                path
            }
            _ => panic!("Frame is not Celestial or Geoid in kind"),
        }
    }

    pub fn frame_path(&self) -> Vec<usize> {
        match self {
            Frame::Celestial { frame_path, .. } | Frame::Geoid { frame_path, .. } => {
                let mut path = Vec::with_capacity(3);
                for p in frame_path.iter().flatten() {
                    path.push(*p)
                }
                path
            }
            _ => panic!("Frame is not Celestial or Geoid in kind"),
        }
    }

    pub fn gm(&self) -> f64 {
        match self {
            Frame::Celestial { gm, .. } | Frame::Geoid { gm, .. } => *gm,
            _ => panic!("Frame is not Celestial or Geoid in kind"),
        }
    }

    /// Allows mutuating the GM for this frame
    pub fn gm_mut(&mut self, new_gm: f64) {
        match self {
            Self::Geoid { ref mut gm, .. } | Self::Celestial { ref mut gm, .. } => *gm = new_gm,
            _ => panic!("Frame is not Celestial or Geoid in kind"),
        }
    }

    pub fn equatorial_radius(&self) -> f64 {
        match self {
            Frame::Geoid {
                equatorial_radius, ..
            } => *equatorial_radius,
            _ => panic!("Frame is not Geoid in kind"),
        }
    }

    pub fn flattening(&self) -> f64 {
        match self {
            Frame::Geoid { flattening, .. } => *flattening,
            _ => panic!("Frame is not Geoid in kind"),
        }
    }

    pub fn semi_major_radius(&self) -> f64 {
        match self {
            Frame::Geoid {
                semi_major_radius, ..
            } => *semi_major_radius,
            _ => panic!("Frame is not Geoid in kind"),
        }
    }

    /// Returns the angular velocity for _some_ planets and moons
    /// Source for Earth: G. Xu and Y. Xu, "GPS", DOI 10.1007/978-3-662-50367-6_2, 2016 (confirmed by https://hpiers.obspm.fr/eop-pc/models/constants.html)
    /// Source for everything else: https://en.wikipedia.org/w/index.php?title=Day&oldid=1008298887
    #[allow(clippy::identity_op)]
    pub fn angular_velocity(&self) -> f64 {
        let period_to_mean_motion = |dur: Duration| -> f64 { 2.0 * PI / dur.in_seconds() };
        match Bodies::try_from(self.ephem_path()).unwrap() {
            Bodies::MercuryBarycenter | Bodies::Mercury => period_to_mean_motion(
                58 * TimeUnit::Day + 15 * TimeUnit::Hour + 30 * TimeUnit::Minute,
            ),
            Bodies::VenusBarycenter | Bodies::Venus => period_to_mean_motion(243 * TimeUnit::Day),
            Bodies::Earth => 7.292_115_146_706_4e-5,
            Bodies::Luna => period_to_mean_motion(
                27 * TimeUnit::Day + 7 * TimeUnit::Hour + 12 * TimeUnit::Minute,
            ),
            Bodies::MarsBarycenter => {
                period_to_mean_motion(1 * TimeUnit::Day + 37 * TimeUnit::Minute)
            }
            Bodies::JupiterBarycenter => {
                period_to_mean_motion(9 * TimeUnit::Hour + 56 * TimeUnit::Minute)
            }
            Bodies::SaturnBarycenter => {
                period_to_mean_motion(10 * TimeUnit::Hour + 30 * TimeUnit::Minute)
            }
            Bodies::UranusBarycenter => {
                period_to_mean_motion(17 * TimeUnit::Hour + 14 * TimeUnit::Minute)
            }
            Bodies::NeptuneBarycenter => {
                period_to_mean_motion(16 * TimeUnit::Hour + 6 * TimeUnit::Minute)
            }
            _ => unimplemented!(),
        }
    }
}

impl fmt::Display for Frame {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Frame::Celestial { .. } | Frame::Geoid { .. } => {
                write!(
                    f,
                    "{} {}",
                    Bodies::try_from(self.ephem_path()).unwrap().name(),
                    match self.frame_path().len() {
                        0 | 1 => "J2000".to_string(),
                        2 => "IAU Fixed".to_string(),
                        3 => "IAU Poles Fixed".to_string(),
                        _ => "Custom".to_string(),
                    }
                )
            }
            othframe => write!(f, "{:?}", othframe),
        }
    }
}

impl fmt::Debug for Frame {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Frame::Celestial { .. } | Frame::Geoid { .. } => {
                write!(
                    f,
                    "{} {} (μ = {:.06} km^3/s^2)",
                    Bodies::try_from(self.ephem_path()).unwrap().name(),
                    match self.frame_path().len() {
                        0 | 1 => "J2000".to_string(),
                        2 => "IAU Fixed".to_string(),
                        3 => "IAU Poles Fixed".to_string(),
                        _ => "Custom".to_string(),
                    },
                    self.gm()
                )
            }
            othframe => write!(f, "{:?}", othframe),
        }
    }
}