diff --git a/src/query_accel.rs b/src/query_accel.rs index cb5552b..62284e6 100644 --- a/src/query_accel.rs +++ b/src/query_accel.rs @@ -65,6 +65,28 @@ impl QueryAccelerator { .flatten() } + // Approximate neighbors; neighbors considered may be outside of the radius! + pub fn query_neighbors_fast<'s, 'p: 's>( + &'s self, + query_idx: usize, + query_point: Vec2, + ) -> impl Iterator + 's { + let origin = quantize(query_point, self.radius); + + self.neighbors + .iter() + .filter_map(move |diff| { + let key = add(origin, *diff); + self.cells.get(&key).map(|cell_indices| { + cell_indices.iter().copied().filter(move |&idx| { + idx != query_idx + }) + }) + }) + .flatten() + } + + pub fn replace_point(&mut self, idx: usize, prev: Vec2, current: Vec2) { // TODO: Keep points in sorted order and use binary search! Or use hashsets for O(n)? // Find this point in our cells and remove it diff --git a/src/sim.rs b/src/sim.rs index 8657337..2b6f842 100644 --- a/src/sim.rs +++ b/src/sim.rs @@ -522,7 +522,7 @@ fn particle_interactions(particles: &mut [Particle], cfg: &LifeConfig, dt: f32) //accel.stats("Life"); for i in 0..particles.len() { - for neighbor in accel.query_neighbors(&points, i, points[i]) { + for neighbor in accel.query_neighbors_fast(i, points[i]) { let a = points[i]; let b = points[neighbor];