|
| 1 | +use nalgebra::Vector3; |
| 2 | +use splashsurf_lib::neighborhood_search::*; |
| 3 | +use splashsurf_lib::AxisAlignedBoundingBox3d; |
| 4 | + |
| 5 | +fn sort_neighborhood_lists(neighborhood_list: &mut Vec<Vec<usize>>) { |
| 6 | + for neighbors in neighborhood_list.iter_mut() { |
| 7 | + neighbors.sort_unstable(); |
| 8 | + } |
| 9 | +} |
| 10 | + |
| 11 | +fn generate_simple_test_cases(search_radius: f32) -> Vec<(Vec<Vector3<f32>>, Vec<Vec<usize>>)> { |
| 12 | + vec![ |
| 13 | + ( |
| 14 | + vec![ |
| 15 | + Vector3::new(1.0, 1.0, 1.0), |
| 16 | + Vector3::new( |
| 17 | + 1.0 + search_radius, |
| 18 | + 1.0 + search_radius, |
| 19 | + 1.0 + search_radius, |
| 20 | + ), |
| 21 | + ], |
| 22 | + vec![Vec::new(), Vec::new()], |
| 23 | + ), |
| 24 | + ( |
| 25 | + vec![ |
| 26 | + Vector3::new(1.0, 1.0, 1.0), |
| 27 | + Vector3::new(1.0 + 0.9999 * search_radius, 1.0, 1.0), |
| 28 | + ], |
| 29 | + vec![vec![1], vec![0]], |
| 30 | + ), |
| 31 | + ( |
| 32 | + vec![ |
| 33 | + Vector3::new(1.0, 1.0, 1.0), |
| 34 | + Vector3::new(1.0 + search_radius, 1.0, 1.0), |
| 35 | + ], |
| 36 | + vec![vec![1], vec![0]], |
| 37 | + ), |
| 38 | + ( |
| 39 | + vec![ |
| 40 | + Vector3::new(1.0, 1.0, 1.0), |
| 41 | + Vector3::new(1.0 + search_radius * 1.0001, 1.0, 1.0), |
| 42 | + ], |
| 43 | + vec![Vec::new(), Vec::new()], |
| 44 | + ), |
| 45 | + ( |
| 46 | + vec![ |
| 47 | + Vector3::new(1.0, 1.0, 1.0), |
| 48 | + Vector3::new(1.0 + 0.9 * search_radius, 1.0, 1.0), |
| 49 | + Vector3::new(1.0 - 0.9 * search_radius, 1.0, 1.0), |
| 50 | + Vector3::new(1.0, 1.0 + 0.2 * search_radius, 1.0), |
| 51 | + ], |
| 52 | + vec![vec![1, 2, 3], vec![0, 3], vec![0, 3], vec![0, 1, 2]], |
| 53 | + ), |
| 54 | + ( |
| 55 | + vec![ |
| 56 | + Vector3::new(1.0, 1.0, 1.0), |
| 57 | + Vector3::new(1.0 + 0.9 * search_radius, 1.0, 1.0), |
| 58 | + Vector3::new(1.0 - 0.9 * search_radius, 1.0, 1.0), |
| 59 | + Vector3::new(1.0 - 0.8 * search_radius, 1.0, -0.2 * search_radius), |
| 60 | + Vector3::new(1.0, 1.0 + 0.2 * search_radius, 1.0), |
| 61 | + Vector3::new(1.0, 1.0 - 0.2 * search_radius, 1.0), |
| 62 | + Vector3::new(1.0, 1.0 + 0.2 * search_radius, 1.0 + 0.2 * search_radius), |
| 63 | + Vector3::new(1.0, 1.0 - 0.2 * search_radius, 1.0 - 0.2 * search_radius), |
| 64 | + ], |
| 65 | + vec![ |
| 66 | + vec![1, 2, 4, 5, 6, 7], |
| 67 | + vec![0, 4, 5, 6, 7], |
| 68 | + vec![0, 4, 5, 6, 7], |
| 69 | + vec![], |
| 70 | + vec![0, 1, 2, 5, 6, 7], |
| 71 | + vec![0, 1, 2, 4, 6, 7], |
| 72 | + vec![0, 1, 2, 4, 5, 7], |
| 73 | + vec![0, 1, 2, 4, 5, 6], |
| 74 | + ], |
| 75 | + ), |
| 76 | + ] |
| 77 | +} |
| 78 | + |
| 79 | +#[test] |
| 80 | +fn test_neighborhood_search_naive_simple() { |
| 81 | + let search_radius: f32 = 0.3; |
| 82 | + |
| 83 | + for (particles, mut solution) in generate_simple_test_cases(search_radius) { |
| 84 | + let mut nl = Vec::new(); |
| 85 | + neighborhood_search_naive(particles.as_slice(), search_radius, &mut nl); |
| 86 | + |
| 87 | + sort_neighborhood_lists(&mut nl); |
| 88 | + sort_neighborhood_lists(&mut solution); |
| 89 | + |
| 90 | + assert_eq!( |
| 91 | + nl, solution, |
| 92 | + "neighborhood_search_naive failed. Search radius: {}, input: {:?}", |
| 93 | + search_radius, particles |
| 94 | + ); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +#[test] |
| 99 | +fn test_neighborhood_search_spatial_hashing_simple() { |
| 100 | + let search_radius: f32 = 0.3; |
| 101 | + |
| 102 | + for (particles, mut solution) in generate_simple_test_cases(search_radius) { |
| 103 | + let mut nl = Vec::new(); |
| 104 | + let mut domain = AxisAlignedBoundingBox3d::from_points(particles.as_slice()); |
| 105 | + domain.grow_uniformly(search_radius); |
| 106 | + neighborhood_search_spatial_hashing::<i32, f32>( |
| 107 | + &domain, |
| 108 | + particles.as_slice(), |
| 109 | + search_radius, |
| 110 | + &mut nl, |
| 111 | + ); |
| 112 | + |
| 113 | + sort_neighborhood_lists(&mut nl); |
| 114 | + sort_neighborhood_lists(&mut solution); |
| 115 | + |
| 116 | + assert_eq!( |
| 117 | + nl, solution, |
| 118 | + "neighborhood_search_spatial_hashing failed. Search radius: {}, input: {:?}", |
| 119 | + search_radius, particles |
| 120 | + ); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +#[cfg(feature = "vtk_extras")] |
| 125 | +mod tests_from_files { |
| 126 | + use super::super::io; |
| 127 | + use super::*; |
| 128 | + use splashsurf_lib::AxisAlignedBoundingBox3d; |
| 129 | + |
| 130 | + #[test] |
| 131 | + fn test_compare_free_particles_125() { |
| 132 | + let search_radius: f32 = 2.0 * 0.5; |
| 133 | + |
| 134 | + let file = "../data/free_particles_125_particles.vtk"; |
| 135 | + let particles = io::vtk::particles_from_vtk::<f32, _>(file).unwrap(); |
| 136 | + |
| 137 | + let mut domain = AxisAlignedBoundingBox3d::par_from_points(particles.as_slice()); |
| 138 | + domain.scale_uniformly(1.5); |
| 139 | + |
| 140 | + let mut nl_naive = Vec::new(); |
| 141 | + let mut nl_hashed = Vec::new(); |
| 142 | + let mut nl_hashed_par = Vec::new(); |
| 143 | + |
| 144 | + neighborhood_search_naive(particles.as_slice(), search_radius, &mut nl_naive); |
| 145 | + neighborhood_search_spatial_hashing::<i64, f32>( |
| 146 | + &domain, |
| 147 | + particles.as_slice(), |
| 148 | + search_radius, |
| 149 | + &mut nl_hashed, |
| 150 | + ); |
| 151 | + neighborhood_search_spatial_hashing_parallel::<i64, f32>( |
| 152 | + &domain, |
| 153 | + particles.as_slice(), |
| 154 | + search_radius, |
| 155 | + &mut nl_hashed_par, |
| 156 | + ); |
| 157 | + |
| 158 | + sort_neighborhood_lists(&mut nl_naive); |
| 159 | + sort_neighborhood_lists(&mut nl_hashed); |
| 160 | + sort_neighborhood_lists(&mut nl_hashed_par); |
| 161 | + |
| 162 | + assert_eq!(nl_hashed, nl_naive, "result of neighborhood_search_spatial_hashing does not match neighborhood_search_naive, file: {:?}", file); |
| 163 | + assert_eq!(nl_hashed_par, nl_naive, "result of neighborhood_search_spatial_hashing_parallel does not match neighborhood_search_naive, file: {:?}", file); |
| 164 | + } |
| 165 | + |
| 166 | + #[test] |
| 167 | + #[cfg_attr(debug_assertions, ignore)] |
| 168 | + fn test_compare_free_particles_1000() { |
| 169 | + let search_radius: f32 = 2.0 * 0.5; |
| 170 | + |
| 171 | + let file = "../data/free_particles_1000_particles.vtk"; |
| 172 | + let particles = io::vtk::particles_from_vtk::<f32, _>(file).unwrap(); |
| 173 | + |
| 174 | + let mut domain = AxisAlignedBoundingBox3d::par_from_points(particles.as_slice()); |
| 175 | + domain.scale_uniformly(1.5); |
| 176 | + |
| 177 | + let mut nl_naive = Vec::new(); |
| 178 | + let mut nl_hashed = Vec::new(); |
| 179 | + let mut nl_hashed_par = Vec::new(); |
| 180 | + |
| 181 | + neighborhood_search_naive(particles.as_slice(), search_radius, &mut nl_naive); |
| 182 | + neighborhood_search_spatial_hashing::<i64, f32>( |
| 183 | + &domain, |
| 184 | + particles.as_slice(), |
| 185 | + search_radius, |
| 186 | + &mut nl_hashed, |
| 187 | + ); |
| 188 | + neighborhood_search_spatial_hashing_parallel::<i64, f32>( |
| 189 | + &domain, |
| 190 | + particles.as_slice(), |
| 191 | + search_radius, |
| 192 | + &mut nl_hashed_par, |
| 193 | + ); |
| 194 | + |
| 195 | + sort_neighborhood_lists(&mut nl_naive); |
| 196 | + sort_neighborhood_lists(&mut nl_hashed); |
| 197 | + sort_neighborhood_lists(&mut nl_hashed_par); |
| 198 | + |
| 199 | + assert_eq!(nl_hashed, nl_naive, "result of neighborhood_search_spatial_hashing does not match neighborhood_search_naive, file: {:?}", file); |
| 200 | + assert_eq!(nl_hashed_par, nl_naive, "result of neighborhood_search_spatial_hashing_parallel does not match neighborhood_search_naive, file: {:?}", file); |
| 201 | + } |
| 202 | + |
| 203 | + #[test] |
| 204 | + #[cfg_attr(debug_assertions, ignore)] |
| 205 | + fn test_compare_cube_2366() { |
| 206 | + let search_radius: f32 = 4.0 * 0.025; |
| 207 | + |
| 208 | + let file = "../data/cube_2366_particles.vtk"; |
| 209 | + let particles = io::vtk::particles_from_vtk::<f32, _>(file).unwrap(); |
| 210 | + |
| 211 | + let mut domain = AxisAlignedBoundingBox3d::par_from_points(particles.as_slice()); |
| 212 | + domain.scale_uniformly(1.5); |
| 213 | + |
| 214 | + let mut nl_naive = Vec::new(); |
| 215 | + let mut nl_hashed = Vec::new(); |
| 216 | + let mut nl_hashed_par = Vec::new(); |
| 217 | + |
| 218 | + neighborhood_search_naive(particles.as_slice(), search_radius, &mut nl_naive); |
| 219 | + neighborhood_search_spatial_hashing::<i64, f32>( |
| 220 | + &domain, |
| 221 | + particles.as_slice(), |
| 222 | + search_radius, |
| 223 | + &mut nl_hashed, |
| 224 | + ); |
| 225 | + neighborhood_search_spatial_hashing_parallel::<i64, f32>( |
| 226 | + &domain, |
| 227 | + particles.as_slice(), |
| 228 | + search_radius, |
| 229 | + &mut nl_hashed_par, |
| 230 | + ); |
| 231 | + |
| 232 | + sort_neighborhood_lists(&mut nl_naive); |
| 233 | + sort_neighborhood_lists(&mut nl_hashed); |
| 234 | + sort_neighborhood_lists(&mut nl_hashed_par); |
| 235 | + |
| 236 | + assert_eq!(nl_hashed, nl_naive, "result of neighborhood_search_spatial_hashing does not match neighborhood_search_naive, file: {:?}", file); |
| 237 | + assert_eq!(nl_hashed_par, nl_naive, "result of neighborhood_search_spatial_hashing_parallel does not match neighborhood_search_naive, file: {:?}", file); |
| 238 | + } |
| 239 | +} |
0 commit comments