honeycomb_core/cmap/dim3/
serialize.rs

1use crate::{
2    cmap::{CMap3, DartIdType},
3    geometry::CoordsFloat,
4};
5
6/// **Serialization methods**
7impl<T: CoordsFloat> CMap3<T> {
8    // --- Custom
9
10    /// Serialize the map under a custom format.
11    ///
12    /// The format specification is described in the [user guide]().
13    pub fn serialize(&self, mut writer: impl std::fmt::Write) {
14        let n_darts = self.n_darts();
15
16        writeln!(writer, "[META]").expect("E: couldn't write to file");
17        writeln!(
18            writer,
19            "{} {} {}",
20            env!("CARGO_PKG_VERSION"), // indicates which version was used to generate the file
21            3,
22            n_darts - 1
23        )
24        .expect("E: couldn't write to file");
25        writeln!(writer).expect("E: couldn't write to file"); // not required, but nice
26
27        writeln!(writer, "[BETAS]").expect("E: couldn't write to file");
28        let width = n_darts.to_string().len();
29        let mut b0 = String::with_capacity(self.n_darts() * 2);
30        let mut b1 = String::with_capacity(self.n_darts() * 2);
31        let mut b2 = String::with_capacity(self.n_darts() * 2);
32        let mut b3 = String::with_capacity(self.n_darts() * 2);
33        std::thread::scope(|s| {
34            s.spawn(|| {
35                // convoluted bc this prevents ephemeral allocs
36                use std::fmt::Write;
37                let mut buf = String::new();
38                (0..n_darts as DartIdType).for_each(|d| {
39                    write!(&mut buf, "{:>width$} ", self.beta::<0>(d)).unwrap();
40                    b0.push_str(buf.as_str());
41                    buf.clear();
42                });
43            });
44            s.spawn(|| {
45                // convoluted bc this prevents ephemeral allocs
46                use std::fmt::Write;
47                let mut buf = String::new();
48                (0..n_darts as DartIdType).for_each(|d| {
49                    write!(&mut buf, "{:>width$} ", self.beta::<1>(d)).unwrap();
50                    b1.push_str(buf.as_str());
51                    buf.clear();
52                });
53            });
54            s.spawn(|| {
55                // convoluted bc this prevents ephemeral allocs
56                use std::fmt::Write;
57                let mut buf = String::new();
58                (0..n_darts as DartIdType).for_each(|d| {
59                    write!(&mut buf, "{:>width$} ", self.beta::<2>(d)).unwrap();
60                    b2.push_str(buf.as_str());
61                    buf.clear();
62                });
63            });
64            s.spawn(|| {
65                // convoluted bc this prevents ephemeral allocs
66                use std::fmt::Write;
67                let mut buf = String::new();
68                (0..n_darts as DartIdType).for_each(|d| {
69                    write!(&mut buf, "{:>width$} ", self.beta::<3>(d)).unwrap();
70                    b3.push_str(buf.as_str());
71                    buf.clear();
72                });
73            });
74        });
75        writeln!(writer, "{}", b0.trim()).expect("E: couldn't write to file");
76        writeln!(writer, "{}", b1.trim()).expect("E: couldn't write to file");
77        writeln!(writer, "{}", b2.trim()).expect("E: couldn't write to file");
78        writeln!(writer, "{}", b3.trim()).expect("E: couldn't write to file");
79        writeln!(writer).expect("E: couldn't write to file"); // not required, but nice
80
81        writeln!(writer, "[UNUSED]").expect("E: couldn't write to file");
82        self.unused_darts
83            .iter()
84            .enumerate()
85            .filter(|(_, v)| v.read_atomic())
86            .for_each(|(i, _)| {
87                write!(writer, "{i} ").unwrap();
88            });
89        writeln!(writer).expect("E: couldn't write to file"); // required
90        writeln!(writer).expect("E: couldn't write to file"); // not required, but nice
91
92        writeln!(writer, "[VERTICES]").expect("E: couldn't write to file");
93        self.iter_vertices().for_each(|v| {
94            if let Some(val) = self.force_read_vertex(v) {
95                writeln!(
96                    writer,
97                    "{v} {} {} {}",
98                    val.0.to_f64().unwrap(),
99                    val.1.to_f64().unwrap(),
100                    val.2.to_f64().unwrap(),
101                )
102                .expect("E: couldn't write to file");
103            }
104        });
105    }
106}