ifc_lite_diff/
split_grid.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5//! Bounded uniform-grid candidate lookup and deterministic proximity clusters.
6use std::collections::HashMap;
7use crate::model::EntityAabb;
8use crate::split_geometry::boxes_touch;
9
10pub struct BoxGrid {
11    count: usize,
12    cell: f64,
13    buckets: HashMap<[i64; 3], Vec<usize>>,
14    scan: bool,
15}
16impl BoxGrid {
17    pub fn new(boxes: &[EntityAabb]) -> Self {
18        let mut cell = 0.0;
19        for b in boxes {
20            for axis in 0..3 {
21                let extent = b.max[axis] - b.min[axis];
22                if extent > cell { cell = extent; }
23            }
24        }
25        if cell <= 0.0 { cell = 1.0; }
26        let mut grid = Self { count: boxes.len(), cell, buckets: HashMap::new(), scan: false };
27        for (index, b) in boxes.iter().enumerate() {
28            let Some((lo, hi)) = grid.range(b, 0.0) else {
29                // Extreme coordinates cannot be stepped safely as JS numbers.
30                // A scan retains the candidate-superset contract.
31                grid.scan = true;
32                grid.buckets.clear();
33                break;
34            };
35            for x in lo[0]..=hi[0] { for y in lo[1]..=hi[1] { for z in lo[2]..=hi[2] {
36                grid.buckets.entry([x, y, z]).or_default().push(index);
37            } } }
38        }
39        grid
40    }
41    fn range(&self, b: &EntityAabb, pad: f64) -> Option<([i64; 3], [i64; 3])> {
42        let mut lo = [0; 3];
43        let mut hi = [0; 3];
44        let mut cells = 1.0;
45        for axis in 0..3 {
46            let a = ((b.min[axis] - pad) / self.cell).floor();
47            let z = ((b.max[axis] + pad) / self.cell).floor();
48            if !a.is_finite() || !z.is_finite() || a.abs() >= 4503599627370496.0 || z.abs() >= 4503599627370496.0 { return None; }
49            cells *= z - a + 1.0;
50            lo[axis] = a as i64;
51            hi[axis] = z as i64;
52        }
53        if !cells.is_finite() || cells > 4096.0 { return None; }
54        Some((lo, hi))
55    }
56    pub fn query(&self, b: &EntityAabb, pad: f64) -> Vec<usize> {
57        if self.scan { return (0..self.count).collect(); }
58        let Some((lo, hi)) = self.range(b, pad) else { return (0..self.count).collect(); };
59        let mut found = Vec::new();
60        for x in lo[0]..=hi[0] { for y in lo[1]..=hi[1] { for z in lo[2]..=hi[2] {
61            if let Some(indices) = self.buckets.get(&[x, y, z]) { found.extend_from_slice(indices); }
62        } } }
63        found.sort_unstable();
64        found.dedup();
65        found
66    }
67}
68fn find(parent: &mut [usize], i: usize) -> usize {
69    let mut root = i;
70    while parent[root] != root { root = parent[root]; }
71    let mut walk = i;
72    while parent[walk] != root {
73        let next = parent[walk];
74        parent[walk] = root;
75        walk = next;
76    }
77    root
78}
79pub fn cluster_by_proximity(boxes: &[EntityAabb], paddings: &[f64]) -> Vec<Vec<usize>> {
80    let mut parent: Vec<_> = (0..boxes.len()).collect();
81    let grid = BoxGrid::new(boxes);
82    let mut reach = 0.0;
83    for &padding in paddings { if padding > reach { reach = padding; } }
84    reach *= 2.0;
85    for i in 0..boxes.len() {
86        for j in grid.query(&boxes[i], reach) {
87            if j <= i || !boxes_touch(&boxes[i], &boxes[j], paddings[i].max(paddings[j])) { continue; }
88            let a = find(&mut parent, i);
89            let b = find(&mut parent, j);
90            if a != b { parent[a] = b; }
91        }
92    }
93    let mut by_root: HashMap<usize, Vec<usize>> = HashMap::new();
94    for i in 0..boxes.len() { by_root.entry(find(&mut parent, i)).or_default().push(i); }
95    let mut components: Vec<_> = by_root.into_values().collect();
96    components.sort_by_key(|members| members[0]);
97    components
98}