use std::{
fs::File,
io::{prelude::*, BufReader},
};
use crate::{io::*, *};
pub fn assert_files_equal(filepath1: &str, filepath2: &str) {
let mut f1 = File::open(filepath1).unwrap();
let mut f2 = File::open(filepath2).unwrap();
let mut s1 = String::new();
f1.read_to_string(&mut s1).unwrap();
let mut s2 = String::new();
f2.read_to_string(&mut s2).unwrap();
assert!(s1 == s2);
}
pub fn test_filter_2d<F, P>(f: F, path_expected: &str, unique_identifier: &str)
where
F: IsFilter<P>,
P: IsBuildable2D + Clone,
{
let path_tmp = ["tests/tmp/tmp", unique_identifier, ".xyz"].join("");
let filter = FilterRandomAccessible::new(f);
let mut view = View::Full;
let mut pc = PointCloud2D::<P>::new();
let mut f = BufReader::new(File::open("tests/data/test_square.xy").unwrap());
load_xy(&mut f, &mut pc).unwrap();
filter.filter(&pc, &mut view);
pc.apply_view(&view).unwrap();
let mut f_tmp = File::create(&path_tmp).unwrap();
save_xy(&mut f_tmp, &pc, " ", "\n").unwrap();
assert_files_equal(path_expected, &path_tmp);
}
pub fn test_filter_3d<F, P>(f: F, path_expected: &str, unique_identifier: &str)
where
F: IsFilter<P>,
P: IsBuildable3D + Clone,
{
let path_tmp = ["tests/tmp/tmp", unique_identifier, ".xyz"].join("");
let filter = FilterRandomAccessible::new(f);
let mut view = View::Full;
let mut pc = PointCloud3D::<P>::new();
let mut f = BufReader::new(File::open("tests/data/test_cube.xyz").unwrap());
load_xyz(&mut f, &mut pc).unwrap();
filter.filter(&pc, &mut view);
pc.apply_view(&view).unwrap();
let mut f_tmp = File::create(&path_tmp).unwrap();
save_xyz(&mut f_tmp, &pc, " ", "\n").unwrap();
assert_files_equal(path_expected, &path_tmp);
}
pub fn write_expected_filter_2d<F, P>(f: F, path_expected: &str)
where
F: IsFilter<P>,
P: IsBuildable2D + Clone,
{
let filter = FilterRandomAccessible::new(f);
let mut view = View::Full;
let mut pc = PointCloud2D::<P>::new();
let mut f = BufReader::new(File::open("tests/data/test_square.xy").unwrap());
load_xy(&mut f, &mut pc).unwrap();
filter.filter(&pc, &mut view);
let mut f_expected = File::create(path_expected).unwrap();
pc.apply_view(&view).unwrap();
save_xy(&mut f_expected, &pc, " ", "\n").unwrap();
}
pub fn write_expected_filter_3d<F, P>(f: F, path_expected: &str)
where
F: IsFilter<P>,
P: IsBuildable3D + Clone,
{
let filter = FilterRandomAccessible::new(f);
let mut view = View::Full;
let mut pc = PointCloud3D::<P>::new();
let mut f = BufReader::new(File::open("tests/data/test_cube.xyz").unwrap());
load_xyz(&mut f, &mut pc).unwrap();
filter.filter(&pc, &mut view);
let mut f_expected = File::create(path_expected).unwrap();
pc.apply_view(&view).unwrap();
save_xyz(&mut f_expected, &pc, " ", "\n").unwrap();
}