Skip to content

Commit a2be670

Browse files
committed
Optimize and parallelize Hausdorff distance.
1 parent 1e9daaa commit a2be670

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/metric.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use rand::{
1212
distr::{Distribution, weighted::WeightedIndex},
1313
};
1414
use rand_chacha::ChaCha8Rng;
15+
use rayon::iter::{IntoParallelIterator, ParallelIterator};
1516

1617
use crate::{mesh::Mesh, ops};
1718

@@ -109,19 +110,22 @@ fn hausdorff_element(a: &[PointInTriangle], b: &[PointInTriangle]) -> f64 {
109110

110111
// For each vertex a ∈ Sample(A), compute the minimum distance to B. The
111112
// Hausdorff distance from A to B is the supremum of these minimums.
112-
a.iter()
113+
//
114+
// PARALLEL: Executing the outer loops in parallel provides a nice speedup
115+
// since the KD-tree lookup and triangle distance test are quite expensive.
116+
a.into_par_iter()
113117
.flat_map(|a_sample| {
114118
// Find the point on the surface of B which is closest to a.
115119
let a_pt = a_sample.1;
116120

117121
// The closest point in B is probably within one of the faces of the
118122
// nearest N vertices.
119-
let b_samples =
120-
b_index.nearest_n::<SquaredEuclidean>(&[a_pt.x, a_pt.y, a_pt.z], N_LOOKUP);
121-
b_samples
123+
b_index
124+
.nearest_n::<SquaredEuclidean>(&[a_pt.x, a_pt.y, a_pt.z], N_LOOKUP)
122125
.into_iter()
123126
.map(|b_sample| {
124-
let tri_in_b = b[b_sample.item as usize].0;
127+
let tri_in_b = &b[b_sample.item as usize].0;
128+
125129
tri_in_b.distance_to_local_point(&a_pt, true)
126130
})
127131
.min_by(|a, b| a.total_cmp(b))

0 commit comments

Comments
 (0)