1use std::{
26 fs::File,
27 io::{prelude::*, BufReader},
28};
29
30use crate::{io::*, *};
31
32pub fn assert_files_equal(filepath1: &str, filepath2: &str) {
36 let mut f1 = File::open(filepath1).unwrap();
37 let mut f2 = File::open(filepath2).unwrap();
38
39 let mut s1 = String::new();
40 f1.read_to_string(&mut s1).unwrap();
41
42 let mut s2 = String::new();
43 f2.read_to_string(&mut s2).unwrap();
44
45 assert!(s1 == s2);
46}
47
48pub fn test_filter_2d<F, P>(f: F, path_expected: &str, unique_identifier: &str)
50where
51 F: IsFilter<P>,
52 P: IsBuildable2D + Clone,
53{
54 let path_tmp = ["tests/tmp/tmp", unique_identifier, ".xyz"].join("");
55 let filter = FilterRandomAccessible::new(f);
56
57 let mut view = View::Full;
58 let mut pc = PointCloud2D::<P>::new();
59 let mut f = BufReader::new(File::open("tests/data/test_square.xy").unwrap());
60 load_xy(&mut f, &mut pc).unwrap();
61
62 filter.filter(&pc, &mut view);
63
64 pc.apply_view(&view).unwrap();
65
66 let mut f_tmp = File::create(&path_tmp).unwrap();
67 save_xy(&mut f_tmp, &pc, " ", "\n").unwrap();
68 assert_files_equal(path_expected, &path_tmp);
69}
70
71pub fn test_filter_3d<F, P>(f: F, path_expected: &str, unique_identifier: &str)
73where
74 F: IsFilter<P>,
75 P: IsBuildable3D + Clone,
76{
77 let path_tmp = ["tests/tmp/tmp", unique_identifier, ".xyz"].join("");
78 let filter = FilterRandomAccessible::new(f);
79
80 let mut view = View::Full;
81 let mut pc = PointCloud3D::<P>::new();
82 let mut f = BufReader::new(File::open("tests/data/test_cube.xyz").unwrap());
83 load_xyz(&mut f, &mut pc).unwrap();
84
85 filter.filter(&pc, &mut view);
86
87 pc.apply_view(&view).unwrap();
88
89 let mut f_tmp = File::create(&path_tmp).unwrap();
90 save_xyz(&mut f_tmp, &pc, " ", "\n").unwrap();
91 assert_files_equal(path_expected, &path_tmp);
92}
93
94pub fn write_expected_filter_2d<F, P>(f: F, path_expected: &str)
96where
97 F: IsFilter<P>,
98 P: IsBuildable2D + Clone,
99{
100 let filter = FilterRandomAccessible::new(f);
101
102 let mut view = View::Full;
103 let mut pc = PointCloud2D::<P>::new();
104 let mut f = BufReader::new(File::open("tests/data/test_square.xy").unwrap());
105 load_xy(&mut f, &mut pc).unwrap();
106
107 filter.filter(&pc, &mut view);
108
109 let mut f_expected = File::create(path_expected).unwrap();
110 pc.apply_view(&view).unwrap();
111 save_xy(&mut f_expected, &pc, " ", "\n").unwrap();
112}
113
114pub fn write_expected_filter_3d<F, P>(f: F, path_expected: &str)
116where
117 F: IsFilter<P>,
118 P: IsBuildable3D + Clone,
119{
120 let filter = FilterRandomAccessible::new(f);
121
122 let mut view = View::Full;
123 let mut pc = PointCloud3D::<P>::new();
124 let mut f = BufReader::new(File::open("tests/data/test_cube.xyz").unwrap());
125 load_xyz(&mut f, &mut pc).unwrap();
126
127 filter.filter(&pc, &mut view);
128
129 let mut f_expected = File::create(path_expected).unwrap();
130 pc.apply_view(&view).unwrap();
131 save_xyz(&mut f_expected, &pc, " ", "\n").unwrap();
132}