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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
extern crate num;
use self::num::traits::real::Real;
use crate::celestia::Orbit;
use crate::dimensions::{
allocator::Allocator, DefaultAllocator, DimName, Matrix3, Vector3, Vector6, VectorN, U3,
};
use std::f64;
pub fn tilde_matrix(v: &Vector3<f64>) -> Matrix3<f64> {
Matrix3::new(
0.0,
-v[(2, 0)],
v[(1, 0)],
v[(2, 0)],
0.0,
-v[(0, 0)],
-v[(1, 0)],
v[(0, 0)],
0.0,
)
}
pub fn is_diagonal(m: &Matrix3<f64>) -> bool {
for i in 1..2 {
for j in 0..i {
if i == j && (m[(i, j)] - m[(0, 0)]) > f64::EPSILON
|| i != j
&& (m[(i, j)].abs() > f64::EPSILON
|| (m[(i, j)] - m[(j, i)]).abs() > f64::EPSILON)
{
return false;
}
}
}
true
}
pub fn between_0_360(angle: f64) -> f64 {
let mut bounded = angle;
while bounded > 360.0 {
bounded -= 360.0;
}
while bounded < 0.0 {
bounded += 360.0;
}
bounded
}
pub fn between_pm_180(angle: f64) -> f64 {
between_pm_x(angle, 180.0)
}
pub fn between_pm_x(angle: f64, x: f64) -> f64 {
let mut bounded = angle;
while bounded > x {
bounded -= 2.0 * x;
}
while bounded < -x {
bounded += 2.0 * x;
}
bounded
}
pub fn kronecker<T: Real>(a: T, b: T) -> T {
if (a - b).abs() <= T::epsilon() {
T::one()
} else {
T::zero()
}
}
pub fn r1(angle: f64) -> Matrix3<f64> {
let (s, c) = angle.sin_cos();
Matrix3::new(1.0, 0.0, 0.0, 0.0, c, s, 0.0, -s, c)
}
pub fn r2(angle: f64) -> Matrix3<f64> {
let (s, c) = angle.sin_cos();
Matrix3::new(c, 0.0, -s, 0.0, 1.0, 0.0, s, 0.0, c)
}
pub fn r3(angle: f64) -> Matrix3<f64> {
let (s, c) = angle.sin_cos();
Matrix3::new(c, s, 0.0, -s, c, 0.0, 0.0, 0.0, 1.0)
}
pub fn rotv(v: &Vector3<f64>, axis: &Vector3<f64>, theta: f64) -> Vector3<f64> {
let k_hat = axis / axis.norm();
v * theta.cos() + k_hat.cross(&v) * theta.sin() + k_hat.dot(&v) * k_hat * (1.0 - theta.cos())
}
pub fn perpv(a: &Vector3<f64>, b: &Vector3<f64>) -> Vector3<f64> {
let big_a = a[0].abs().max(a[1].abs().max(a[2].abs()));
let big_b = b[0].abs().max(b[1].abs().max(b[2].abs()));
if big_a < f64::EPSILON {
Vector3::zeros()
} else if big_b < f64::EPSILON {
*a
} else {
let a_scl = a / big_a;
let b_scl = b / big_b;
let v = projv(&a_scl, &b_scl);
big_a * (a_scl - v)
}
}
pub fn projv(a: &Vector3<f64>, b: &Vector3<f64>) -> Vector3<f64> {
b * a.dot(&b) / b.dot(&b)
}
pub fn rss_errors<N: DimName>(prop_err: &VectorN<f64, N>, cur_state: &VectorN<f64, N>) -> f64
where
DefaultAllocator: Allocator<f64, N>,
{
let mut v = 0.0;
for i in 0..N::dim() {
v += (prop_err[i] - cur_state[i]).powi(2);
}
v.sqrt()
}
pub fn rss_orbit_errors(prop_err: &Orbit, cur_state: &Orbit) -> (f64, f64) {
(
rss_errors(&prop_err.radius(), &cur_state.radius()),
rss_errors(&prop_err.velocity(), &cur_state.velocity()),
)
}
pub fn rss_orbit_vec_errors(prop_err: &Vector6<f64>, cur_state: &Vector6<f64>) -> (f64, f64) {
let err_radius = (prop_err.fixed_rows::<U3>(0) - cur_state.fixed_rows::<U3>(0)).norm();
let err_velocity = (prop_err.fixed_rows::<U3>(3) - cur_state.fixed_rows::<U3>(3)).norm();
(err_radius, err_velocity)
}
pub fn normalize(x: f64, min_x: f64, max_x: f64) -> f64 {
2.0 * (x - min_x) / (max_x - min_x) - 1.0
}
pub fn denormalize(xp: f64, min_x: f64, max_x: f64) -> f64 {
(max_x - min_x) * (xp + 1.0) / 2.0 + min_x
}
pub fn capitalize(s: &str) -> String {
let mut c = s.chars();
match c.next() {
None => String::new(),
Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
}
}
#[test]
fn test_tilde_matrix() {
let vec = Vector3::new(1.0, 2.0, 3.0);
let rslt = Matrix3::new(0.0, -3.0, 2.0, 3.0, 0.0, -1.0, -2.0, 1.0, 0.0);
assert_eq!(tilde_matrix(&vec), rslt);
}
#[test]
fn test_diagonality() {
assert_eq!(
is_diagonal(&Matrix3::new(10.0, 0.0, 0.0, 1.0, 5.0, 0.0, 0.0, 0.0, 2.0)),
false,
"lower triangular"
);
assert_eq!(
is_diagonal(&Matrix3::new(10.0, 1.0, 0.0, 1.0, 5.0, 0.0, 0.0, 0.0, 2.0)),
false,
"symmetric but not diag"
);
assert_eq!(
is_diagonal(&Matrix3::new(10.0, 1.0, 0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 2.0)),
false,
"upper triangular"
);
assert_eq!(
is_diagonal(&Matrix3::new(10.0, 0.0, 0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 2.0)),
true,
"diagonal"
);
}
#[test]
fn test_perpv() {
assert_eq!(
perpv(&Vector3::new(6.0, 6.0, 6.0), &Vector3::new(2.0, 0.0, 0.0)),
Vector3::new(0.0, 6.0, 6.0)
);
assert_eq!(
perpv(&Vector3::new(6.0, 6.0, 6.0), &Vector3::new(-3.0, 0.0, 0.0)),
Vector3::new(0.0, 6.0, 6.0)
);
assert_eq!(
perpv(&Vector3::new(6.0, 6.0, 0.0), &Vector3::new(0.0, 7.0, 0.0)),
Vector3::new(6.0, 0.0, 0.0)
);
assert_eq!(
perpv(&Vector3::new(6.0, 0.0, 0.0), &Vector3::new(0.0, 0.0, 9.0)),
Vector3::new(6.0, 0.0, 0.0)
);
}
#[test]
fn test_projv() {
assert_eq!(
projv(&Vector3::new(6.0, 6.0, 6.0), &Vector3::new(2.0, 0.0, 0.0)),
Vector3::new(6.0, 0.0, 0.0)
);
assert_eq!(
projv(&Vector3::new(6.0, 6.0, 6.0), &Vector3::new(-3.0, 0.0, 0.0)),
Vector3::new(6.0, 0.0, 0.0)
);
assert_eq!(
projv(&Vector3::new(6.0, 6.0, 0.0), &Vector3::new(0.0, 7.0, 0.0)),
Vector3::new(0.0, 6.0, 0.0)
);
assert_eq!(
projv(&Vector3::new(6.0, 0.0, 0.0), &Vector3::new(0.0, 0.0, 9.0)),
Vector3::new(0.0, 0.0, 0.0)
);
}
#[test]
fn test_angle_bounds() {
assert!((between_pm_180(181.0) - -179.0).abs() < std::f64::EPSILON);
assert!((between_0_360(-179.0) - 181.0).abs() < std::f64::EPSILON);
}