Skip to content

Commit 5bc735d

Browse files
committed
Rename neighborhood search functions, add naive neighborhood search
1 parent f77cf72 commit 5bc735d

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

splashsurf_lib/src/neighborhood_search.rs

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ pub fn search<I: Index, R: Real>(
2626
) -> Vec<Vec<usize>> {
2727
let mut particle_neighbor_lists = Vec::new();
2828
if enable_multi_threading {
29-
parallel_search::<I, R>(
29+
neighborhood_search_spatial_hashing_parallel::<I, R>(
3030
domain,
3131
particle_positions,
3232
search_radius,
3333
&mut particle_neighbor_lists,
3434
)
3535
} else {
36-
sequential_search::<I, R>(
36+
neighborhood_search_spatial_hashing::<I, R>(
3737
domain,
3838
particle_positions,
3939
search_radius,
@@ -53,14 +53,14 @@ pub fn search_inplace<I: Index, R: Real>(
5353
particle_neighbor_lists: &mut Vec<Vec<usize>>,
5454
) {
5555
if enable_multi_threading {
56-
parallel_search::<I, R>(
56+
neighborhood_search_spatial_hashing_parallel::<I, R>(
5757
domain,
5858
particle_positions,
5959
search_radius,
6060
particle_neighbor_lists,
6161
)
6262
} else {
63-
sequential_search::<I, R>(
63+
neighborhood_search_spatial_hashing::<I, R>(
6464
domain,
6565
particle_positions,
6666
search_radius,
@@ -69,6 +69,29 @@ pub fn search_inplace<I: Index, R: Real>(
6969
}
7070
}
7171

72+
/// Performs a naive neighborhood search with `O(N^2)` complexity, only recommended for testing
73+
#[inline(never)]
74+
pub fn neighborhood_search_naive<R: Real>(
75+
particle_positions: &[Vector3<R>],
76+
search_radius: R,
77+
neighborhood_list: &mut Vec<Vec<usize>>,
78+
) {
79+
profile!("neighborhood_search_naive");
80+
81+
init_neighborhood_list(neighborhood_list, particle_positions.len());
82+
let search_radius_squared = search_radius * search_radius;
83+
84+
for (idx_i, pos_i) in particle_positions.iter().enumerate() {
85+
let neighbors_i = neighborhood_list.get_mut(idx_i).unwrap();
86+
for (idx_j, pos_j) in particle_positions.iter().enumerate() {
87+
let is_neighbor = (pos_j - pos_i).norm_squared() <= search_radius_squared;
88+
if is_neighbor && idx_j != idx_i {
89+
neighbors_i.push(idx_j);
90+
}
91+
}
92+
}
93+
}
94+
7295
/// Allocates enough storage for the given number of particles and clears all existing neighborhood lists
7396
fn init_neighborhood_list(neighborhood_list: &mut Vec<Vec<usize>>, new_len: usize) {
7497
let old_len = neighborhood_list.len();
@@ -99,15 +122,15 @@ fn par_init_neighborhood_list(neighborhood_list: &mut Vec<Vec<usize>>, new_len:
99122

100123
/// Performs a neighborhood search, returning the indices of all neighboring particles in the given search radius per particle, sequential implementation
101124
#[inline(never)]
102-
pub fn sequential_search<I: Index, R: Real>(
125+
pub fn neighborhood_search_spatial_hashing<I: Index, R: Real>(
103126
domain: &AxisAlignedBoundingBox3d<R>,
104127
particle_positions: &[Vector3<R>],
105128
search_radius: R,
106129
neighborhood_list: &mut Vec<Vec<usize>>,
107130
) {
108131
// TODO: Use ArrayStorage from femproto instead of Vec of Vecs?
109132
// FIXME: Replace unwraps?
110-
profile!("neighborhood_search");
133+
profile!("neighborhood_search_spatial_hashing");
111134

112135
assert!(
113136
search_radius > R::zero(),
@@ -179,15 +202,15 @@ pub fn sequential_search<I: Index, R: Real>(
179202
}
180203
}
181204

182-
/// Performs a neighborhood search, returning the indices of all neighboring particles in the given search radius per particle, multi-thread implementation
205+
/// Performs a neighborhood search, returning the indices of all neighboring particles in the given search radius per particle, multi-threaded implementation
183206
#[inline(never)]
184-
pub fn parallel_search<I: Index, R: Real>(
207+
pub fn neighborhood_search_spatial_hashing_parallel<I: Index, R: Real>(
185208
domain: &AxisAlignedBoundingBox3d<R>,
186209
particle_positions: &[Vector3<R>],
187210
search_radius: R,
188211
neighborhood_list: &mut Vec<Vec<usize>>,
189212
) {
190-
profile!("par_neighborhood_search");
213+
profile!("neighborhood_search_spatial_hashing_parallel");
191214

192215
assert!(
193216
search_radius > R::zero(),

0 commit comments

Comments
 (0)