From 8e55f56f47adf2c4db17b2c6a7c63b1281b9110e Mon Sep 17 00:00:00 2001 From: Khawar Zamman Wani Date: Wed, 2 Dec 2026 11:49:08 +0530 Subject: [PATCH 1/2] perf(merge): grid ring search for ANMS point thinning (#4953) ANMS compared every keypoint with every stronger one (O(n^2), n up to 12,000). Bucket the points into a uniform grid and search rings of cells outward until no unsearched cell can hold a closer stronger point. Points with no stronger predecessor skip the search. The result is identical; a test checks it against the old quadratic version. --- crates/merge/src/features.rs | 172 ++++++++++++++++++++++++++++++++--- 1 file changed, 161 insertions(+), 11 deletions(-) diff --git a/crates/merge/src/features.rs b/crates/merge/src/features.rs index 9bf770a69f..70d5690de8 100644 --- a/crates/merge/src/features.rs +++ b/crates/merge/src/features.rs @@ -163,23 +163,25 @@ fn harris(lvl: &Plane) -> Vec<(f32, f32, f32)> { } /// Adaptive non-maximal suppression: indices (into `pts`, sorted by decreasing strength) of the `max` -/// points with the largest suppression radii. +/// points with the largest suppression radii. The nearest stronger point is found by searching +/// rings of a uniform grid outward from each point, which gives exactly the brute-force result in +/// near-linear time. fn anms(pts: &[Keypoint], max: usize) -> Vec { if pts.len() <= max { return (0..pts.len()).collect(); } + let mut pmax = Vec::with_capacity(pts.len()); + let mut run = f32::NEG_INFINITY; + for p in pts { + pmax.push(run); + run = run.max(p.strength); + } + let grid = AnmsGrid::new(pts); let radii: Vec = (0..pts.len()) .into_par_iter() - .map(|i| { - let p = pts[i]; - let mut best = f32::INFINITY; - for q in &pts[..i] { - if q.strength * 0.9 > p.strength { - let d = (q.x - p.x).powi(2) + (q.y - p.y).powi(2); - best = best.min(d); - } - } - best + .map(|i| match (pts.get(i), pmax.get(i)) { + (Some(p), Some(m)) if m * 0.9 > p.strength => grid.nearest_stronger(pts, i, p), + _ => f32::INFINITY, }) .collect(); let mut idx: Vec = (0..pts.len()).collect(); @@ -188,6 +190,98 @@ fn anms(pts: &[Keypoint], max: usize) -> Vec { idx } +/// Uniform grid over the finite points; each cell lists its point indices in ascending order. +struct AnmsGrid { + minx: f64, + miny: f64, + cell: f64, + gw: usize, + gh: usize, + cells: Vec>, +} + +impl AnmsGrid { + fn new(pts: &[Keypoint]) -> Self { + let finite = || pts.iter().filter(|p| p.x.is_finite() && p.y.is_finite()); + let (mut minx, mut miny, mut maxx, mut maxy) = (f64::INFINITY, f64::INFINITY, f64::NEG_INFINITY, f64::NEG_INFINITY); + for p in finite() { + minx = minx.min(p.x as f64); + maxx = maxx.max(p.x as f64); + miny = miny.min(p.y as f64); + maxy = maxy.max(p.y as f64); + } + if minx > maxx { + (minx, miny, maxx, maxy) = (0.0, 0.0, 0.0, 0.0); + } + let (w, h, n) = (maxx - minx, maxy - miny, pts.len().max(1) as f64); + let mut cell = (w * h / n * 2.0).sqrt().max(1.0); + while ((w / cell).floor() + 1.0) * ((h / cell).floor() + 1.0) > 4.0 * n { + cell *= 1.5; + } + let (gw, gh) = ((w / cell) as usize + 1, (h / cell) as usize + 1); + let mut g = AnmsGrid { minx, miny, cell, gw, gh, cells: vec![Vec::new(); gw * gh] }; + for (i, p) in pts.iter().enumerate() { + if p.x.is_finite() && p.y.is_finite() { + let (cx, cy) = g.locate(p); + if let Some(c) = g.cells.get_mut(cy * gw + cx) { + c.push(i); + } + } + } + g + } + + fn locate(&self, p: &Keypoint) -> (usize, usize) { + let cx = (((p.x as f64 - self.minx) / self.cell) as usize).min(self.gw - 1); + let cy = (((p.y as f64 - self.miny) / self.cell) as usize).min(self.gh - 1); + (cx, cy) + } + + /// Squared distance from `p` (= `pts[i]`) to the nearest earlier point with `strength * 0.9 > p.strength`. + fn nearest_stronger(&self, pts: &[Keypoint], i: usize, p: &Keypoint) -> f32 { + let mut best = f32::INFINITY; + if !(p.x.is_finite() && p.y.is_finite()) { + return best; + } + let (cx, cy) = self.locate(p); + let (cx, cy) = (cx as isize, cy as isize); + let rmax = cx.max(self.gw as isize - 1 - cx).max(cy).max(self.gh as isize - 1 - cy); + let scan = |x: isize, y: isize, best: &mut f32| { + if x < 0 || y < 0 || x >= self.gw as isize || y >= self.gh as isize { + return; + } + let Some(list) = self.cells.get(y as usize * self.gw + x as usize) else { return }; + for &j in list.get(..list.partition_point(|&j| j < i)).unwrap_or(&[]) { + let Some(q) = pts.get(j) else { continue }; + if q.strength * 0.9 > p.strength { + let d = (q.x - p.x).powi(2) + (q.y - p.y).powi(2); + *best = best.min(d); + } + } + }; + for r in 0..=rmax { + if r == 0 { + scan(cx, cy, &mut best); + } else { + for x in (cx - r).max(0)..=(cx + r).min(self.gw as isize - 1) { + scan(x, cy - r, &mut best); + scan(x, cy + r, &mut best); + } + for y in (cy - r + 1).max(0)..=(cy + r - 1).min(self.gh as isize - 1) { + scan(cx - r, y, &mut best); + scan(cx + r, y, &mut best); + } + } + // points in rings beyond `r` are at least `r * cell` away; 1 px absorbs rounding in the cell assignment + let m = (r as f64 * self.cell - 1.0).max(0.0); + if best as f64 <= m * m { + break; + } + } + best + } +} + fn descriptor(img: &Plane, x: f32, y: f32, angle: f32) -> Option<[f32; DESC_LEN]> { let (s, c) = angle.sin_cos(); let mut d = [0f32; DESC_LEN]; @@ -291,6 +385,62 @@ mod tests { }) } + fn anms_reference(pts: &[Keypoint], max: usize) -> Vec { + if pts.len() <= max { + return (0..pts.len()).collect(); + } + let radii: Vec = (0..pts.len()) + .map(|i| { + let p = pts[i]; + let mut best = f32::INFINITY; + for q in &pts[..i] { + if q.strength * 0.9 > p.strength { + let d = (q.x - p.x).powi(2) + (q.y - p.y).powi(2); + best = best.min(d); + } + } + best + }) + .collect(); + let mut idx: Vec = (0..pts.len()).collect(); + idx.sort_by(|&a, &b| radii[b].total_cmp(&radii[a]).then(a.cmp(&b))); + idx.truncate(max); + idx + } + + #[test] + fn grid_anms_matches_quadratic_reference() { + let mut state = 0x9E37_79B9_7F4A_7C15u64; + let mut rnd = move || { + state ^= state << 13; + state ^= state >> 7; + state ^= state << 17; + (state >> 40) as f32 / (1u64 << 24) as f32 + }; + let kp = |x: f32, y: f32, strength: f32| Keypoint { x, y, level: 0, angle: 0.0, strength }; + let mut sets: Vec<(Vec, usize)> = Vec::new(); + sets.push(((0..3000).map(|_| kp(rnd() * 2000.0, rnd() * 1500.0, rnd())).collect(), 400)); + let centres: Vec<(f32, f32)> = (0..6).map(|_| (rnd() * 2000.0, rnd() * 1500.0)).collect(); + sets.push(( + (0..3000) + .map(|k| { + let (cx, cy) = centres[k % centres.len()]; + let g = |r: f32| (r - 0.5) * 60.0; + kp(cx + g(rnd() + rnd()), cy + g(rnd() + rnd()), rnd()) + }) + .collect(), + 400, + )); + sets.push(((0..3000).map(|_| kp((rnd() * 40.0).floor(), (rnd() * 30.0).floor(), rnd())).collect(), 400)); + sets.push(((0..3000).map(|_| kp(rnd() * 2000.0, rnd() * 1500.0, (rnd() * 4.0).floor() + 1.0)).collect(), 400)); + sets.push(((0..50).map(|_| kp(rnd() * 100.0, rnd() * 100.0, rnd())).collect(), 400)); + sets.push(((0..500).map(|_| kp(rnd() * 2000.0, rnd() * 1500.0, rnd())).collect(), 0)); + for (mut pts, max) in sets { + pts.sort_by(|a, b| b.strength.total_cmp(&a.strength)); + assert_eq!(anms(&pts, max), anms_reference(&pts, max)); + } + } + #[test] fn shifted_images_match_with_the_shift() { let a = pattern(400, 300, 0.0, 0.0); From 29ab3f7916eaa9a822cb16458e93738c2d83ac95 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Wed, 2 Dec 2026 10:11:58 +0100 Subject: [PATCH 2/2] merge: ANMS ring search skips ring edges outside the grid (#4953) --- crates/merge/src/features.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/crates/merge/src/features.rs b/crates/merge/src/features.rs index 70d5690de8..9e2083637f 100644 --- a/crates/merge/src/features.rs +++ b/crates/merge/src/features.rs @@ -263,13 +263,18 @@ impl AnmsGrid { if r == 0 { scan(cx, cy, &mut best); } else { - for x in (cx - r).max(0)..=(cx + r).min(self.gw as isize - 1) { - scan(x, cy - r, &mut best); - scan(x, cy + r, &mut best); + // (only the ring's edges that lie inside the grid: a long thin grid would otherwise + // walk the out-of-grid edges cell by cell on every ring) + let (xs, ys) = ((cx - r).max(0)..=(cx + r).min(self.gw as isize - 1), (cy - r + 1).max(0)..=(cy + r - 1).min(self.gh as isize - 1)); + for y in [cy - r, cy + r].into_iter().filter(|y| (0..self.gh as isize).contains(y)) { + for x in xs.clone() { + scan(x, y, &mut best); + } } - for y in (cy - r + 1).max(0)..=(cy + r - 1).min(self.gh as isize - 1) { - scan(cx - r, y, &mut best); - scan(cx + r, y, &mut best); + for x in [cx - r, cx + r].into_iter().filter(|x| (0..self.gw as isize).contains(x)) { + for y in ys.clone() { + scan(x, y, &mut best); + } } } // points in rings beyond `r` are at least `r * cell` away; 1 px absorbs rounding in the cell assignment @@ -433,6 +438,8 @@ mod tests { )); sets.push(((0..3000).map(|_| kp((rnd() * 40.0).floor(), (rnd() * 30.0).floor(), rnd())).collect(), 400)); sets.push(((0..3000).map(|_| kp(rnd() * 2000.0, rnd() * 1500.0, (rnd() * 4.0).floor() + 1.0)).collect(), 400)); + // one row of points: a grid one cell tall, where the rings' top and bottom edges are outside it + sets.push(((0..3000).map(|i| kp(32.0 + 4.0 * i as f32, 32.0, rnd())).collect(), 400)); sets.push(((0..50).map(|_| kp(rnd() * 100.0, rnd() * 100.0, rnd())).collect(), 400)); sets.push(((0..500).map(|_| kp(rnd() * 2000.0, rnd() * 1500.0, rnd())).collect(), 0)); for (mut pts, max) in sets {