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
use std::convert::TryFrom;
use nalgebra::{Affine2, Vector2};
use ndarray::Array2;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use crate::{cell_map::Bounds, CellMap, CellMapParams, Error, Layer};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CellMapFile<L, T>
where
L: Layer,
{
pub num_layers: usize,
pub layers: Vec<L>,
pub cell_bounds: Bounds,
pub cell_size: Vector2<f64>,
pub cell_boundary_precision: f64,
pub from_parent_angle_rad: f64,
pub from_parent_translation: Vector2<f64>,
pub from_parent_matrix: Affine2<f64>,
pub data: Vec<Array2<T>>,
}
impl<L, T> CellMapFile<L, T>
where
L: Layer,
{
pub fn into_cell_map(self) -> Result<CellMap<L, T>, Error> {
let params = CellMapParams {
cell_size: self.cell_size,
cell_bounds: self.cell_bounds,
rotation_in_parent_rad: self.from_parent_angle_rad,
position_in_parent: self.from_parent_translation,
cell_boundary_precision: self.cell_boundary_precision,
};
CellMap::new_from_data(params, self.data)
}
}
impl<L, T> CellMapFile<L, T>
where
L: Layer + Serialize,
T: Clone + Serialize,
{
pub(crate) fn new(map: &CellMap<L, T>) -> Self {
Self {
num_layers: L::NUM_LAYERS,
layers: L::all(),
cell_bounds: map.metadata.cell_bounds,
cell_size: map.metadata.cell_size,
cell_boundary_precision: map.metadata.cell_boundary_precision,
from_parent_angle_rad: map.params.rotation_in_parent_rad,
from_parent_translation: map.params.position_in_parent,
from_parent_matrix: map.metadata.to_parent.inverse(),
data: map.data.clone(),
}
}
}
impl<L, T> CellMapFile<L, T>
where
L: Layer + Serialize,
T: Serialize,
{
#[cfg(feature = "json")]
pub fn write_json<P: AsRef<std::path::Path>>(&self, path: P) -> Result<(), Error> {
let file = std::fs::OpenOptions::new()
.create(true)
.append(false)
.truncate(true)
.write(true)
.open(path)
.map_err(Error::IoError)?;
serde_json::to_writer_pretty(file, &self).map_err(Error::JsonError)?;
Ok(())
}
}
impl<L, T> CellMapFile<L, T>
where
L: Layer + DeserializeOwned,
T: DeserializeOwned,
{
#[cfg(feature = "json")]
pub fn from_json<P: AsRef<std::path::Path>>(path: P) -> Result<Self, Error> {
let file = std::fs::File::open(path).map_err(Error::IoError)?;
let map_file: CellMapFile<L, T> =
serde_json::from_reader(&file).map_err(Error::JsonError)?;
Ok(map_file)
}
}
impl<L, T> From<CellMap<L, T>> for CellMapFile<L, T>
where
L: Layer + Serialize,
T: Clone + Serialize,
{
fn from(map: CellMap<L, T>) -> Self {
Self::new(&map)
}
}
impl<L, T> TryFrom<CellMapFile<L, T>> for CellMap<L, T>
where
L: Layer,
{
type Error = Error;
fn try_from(value: CellMapFile<L, T>) -> Result<Self, Self::Error> {
value.into_cell_map()
}
}