filtration_domination/points/
output.rs

1//! Utilities to write point clouds to disk.
2use num::Float;
3
4use crate::points::PointCloud;
5
6/// Write the point cloud to the given writer.
7pub fn write_point_cloud<T: Float + std::fmt::Display, W: std::io::Write, const N: usize>(
8    cloud: &PointCloud<T, N>,
9    w: &mut W,
10) -> std::io::Result<()> {
11    for p in cloud.0.iter() {
12        for i in 0..N {
13            write!(w, "{}", p.0[i])?;
14            if i == N - 1 {
15                writeln!(w)?;
16            } else {
17                write!(w, ", ")?;
18            }
19        }
20    }
21    Ok(())
22}
23
24#[cfg(test)]
25mod tests {
26    use crate::points::output::write_point_cloud;
27    use crate::points::{Point, PointCloud};
28
29    #[test]
30    fn write_point_cloud_happy_case() {
31        let f: PointCloud<f64, 2> = PointCloud(vec![Point([2., 1.]), Point([0., -2.14])]);
32        let mut buf = Vec::new();
33        write_point_cloud(&f, &mut buf).unwrap();
34        let out = String::from_utf8(buf).unwrap();
35        assert_eq!(out, "2, 1\n0, -2.14\n")
36    }
37}