Skip to content

Commit

Permalink
An optimization for the particle life impl
Browse files Browse the repository at this point in the history
  • Loading branch information
Masterchef365 committed Dec 19, 2023
1 parent 9e6d154 commit 2ad02e4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/query_accel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item = usize> + '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
Expand Down
2 changes: 1 addition & 1 deletion src/sim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down

0 comments on commit 2ad02e4

Please sign in to comment.