nyx_space/io/
matrices.rs

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
/*
    Nyx, blazing fast astrodynamics
    Copyright (C) 2018-onwards 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 either::Either;
use nalgebra::{Matrix2, Matrix6, Vector2, Vector6};
use serde::{Deserialize, Serialize};

// TODO: Use a macro to generate all of these implementations

#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Matrix2Serde {
    #[serde(with = "either::serde_untagged")]
    inner: Either<Diag2, Mat2>,
}

impl Matrix2Serde {
    pub fn to_matrix(&self) -> Matrix2<f64> {
        match self.inner {
            Either::Left(diag) => Matrix2::from_diagonal(&Vector2::from_iterator(diag.0)),
            Either::Right(mat2) => {
                let mut flat: [f64; 4] = [0.0; 4];
                for (i, row) in mat2.0.iter().enumerate() {
                    for (j, val) in row.iter().enumerate() {
                        flat[4 * i + j] = *val;
                    }
                }
                Matrix2::from_row_slice(&flat)
            }
        }
    }
}

#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Diag2([f64; 2]);

#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Mat2([[f64; 2]; 2]);

#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Matrix6Serde {
    #[serde(with = "either::serde_untagged")]
    inner: Either<Diag6, Mat6>,
}

impl Matrix6Serde {
    pub fn to_matrix(&self) -> Matrix6<f64> {
        match self.inner {
            Either::Left(diag) => Matrix6::from_diagonal(&Vector6::from_iterator(diag.0)),
            Either::Right(mat6) => {
                let mut flat: [f64; 36] = [0.0; 36];
                for (i, row) in mat6.0.iter().enumerate() {
                    for (j, val) in row.iter().enumerate() {
                        flat[6 * i + j] = *val;
                    }
                }
                Matrix6::from_row_slice(&flat)
            }
        }
    }
}

#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Diag6([f64; 6]);

#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Mat6([[f64; 6]; 6]);

#[test]
fn test_serde2() {
    use serde_yml;

    let m_diag = Matrix2Serde {
        inner: Either::Left(Diag2([1.0, 2.0])),
    };

    println!("Diag -- \n{}", serde_yml::to_string(&m_diag).unwrap());
    // Load from one line list
    let diag_s = "[1.0, 2.0]";
    let diag_loaded: Matrix2Serde = serde_yml::from_str(diag_s).unwrap();
    assert_eq!(diag_loaded, m_diag);

    let m_full = Matrix2Serde {
        inner: Either::Right(Mat2([[1.0, 2.0]; 2])),
    };

    // Serialization will print this as an exhaustive list of lists.
    println!("Full -- \n{}", serde_yml::to_string(&m_full).unwrap());
    // Load from list
    let full_mat = r#"
- [1.0, 2.0] # Row 1
- [1.0, 2.0] # Row 2
    "#;

    let full_loaded: Matrix2Serde = serde_yml::from_str(full_mat).unwrap();

    assert_eq!(full_loaded, m_full);
}

#[test]
fn test_serde6() {
    use serde_yml;

    let m_diag = Matrix6Serde {
        inner: Either::Left(Diag6([1.0, 2.0, 3.0, 4.0, 5.0, 6.0])),
    };

    println!("Diag -- \n{}", serde_yml::to_string(&m_diag).unwrap());
    // Load from one line list
    let diag_s = "[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]";
    let diag_loaded: Matrix6Serde = serde_yml::from_str(diag_s).unwrap();
    assert_eq!(diag_loaded, m_diag);

    let m_full = Matrix6Serde {
        inner: Either::Right(Mat6([[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]; 6])),
    };

    // Serialization will print this as an exhaustive list of lists.
    println!("Full -- \n{}", serde_yml::to_string(&m_full).unwrap());
    // Load from list
    let full_mat = r#"
- [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] # Row 1
- [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] # Row 2
- [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] # Row 3
- [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] # Row 4
- [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] # Row 5
- [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] # Row 6
    "#;

    let full_loaded: Matrix6Serde = serde_yml::from_str(full_mat).unwrap();

    assert_eq!(full_loaded, m_full);
}