ifc_lite_diff/
split_geometry.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//! Box predicates for split/merge evidence (#8042).
6use crate::model::EntityAabb;
7
8pub fn containment_padding(b: &EntityAabb, min: f64, ratio: f64) -> f64 {
9    let dx = b.max[0] - b.min[0];
10    let dy = b.max[1] - b.min[1];
11    let dz = b.max[2] - b.min[2];
12    min.max(ratio * (dx * dx + dy * dy + dz * dz).sqrt())
13}
14pub fn box_within(inner: &EntityAabb, outer: &EntityAabb, pad: f64) -> bool {
15    (0..3).all(|axis| !(inner.min[axis] < outer.min[axis] - pad || inner.max[axis] > outer.max[axis] + pad))
16}
17pub fn boxes_touch(a: &EntityAabb, b: &EntityAabb, pad: f64) -> bool {
18    (0..3).all(|axis| !(a.min[axis] - pad > b.max[axis] + pad || b.min[axis] - pad > a.max[axis] + pad))
19}
20pub fn box_union(boxes: &[EntityAabb]) -> EntityAabb {
21    let mut result = EntityAabb { min: [f64::INFINITY; 3], max: [f64::NEG_INFINITY; 3] };
22    for b in boxes {
23        for axis in 0..3 {
24            if b.min[axis] < result.min[axis] { result.min[axis] = b.min[axis]; }
25            if b.max[axis] > result.max[axis] { result.max[axis] = b.max[axis]; }
26        }
27    }
28    result
29}
30pub fn sorted_extents(b: &EntityAabb) -> [f64; 3] {
31    let mut extents = std::array::from_fn(|axis| b.max[axis] - b.min[axis]);
32    extents.sort_by(f64::total_cmp);
33    extents
34}
35pub fn extent_coverage(whole: &EntityAabb, pieces: &[EntityAabb], pad: f64) -> bool {
36    if pieces.is_empty() { return false; }
37    for axis in 0..3 {
38        let mut intervals: Vec<_> = pieces.iter().map(|p| (p.min[axis], p.max[axis])).collect();
39        intervals.sort_by(|a, b| a.0.total_cmp(&b.0));
40        let mut cursor = whole.min[axis];
41        for (lo, hi) in intervals {
42            if lo > cursor + pad { return false; }
43            if hi > cursor { cursor = hi; }
44            if cursor >= whole.max[axis] - pad { break; }
45        }
46        if cursor < whole.max[axis] - pad { return false; }
47    }
48    true
49}
50fn box_volume(b: &EntityAabb) -> f64 {
51    (b.max[0] - b.min[0]).max(0.0) * (b.max[1] - b.min[1]).max(0.0) * (b.max[2] - b.min[2]).max(0.0)
52}
53pub fn box_iou(a: &EntityAabb, b: &EntityAabb) -> f64 {
54    let mut intersection = 1.0;
55    for axis in 0..3 {
56        let lo = a.min[axis].max(b.min[axis]);
57        let hi = a.max[axis].min(b.max[axis]);
58        if hi <= lo { return 0.0; }
59        intersection *= hi - lo;
60    }
61    let union = box_volume(a) + box_volume(b) - intersection;
62    if union > 0.0 { intersection / union } else { 0.0 }
63}
64pub fn any_coincident(boxes: &[EntityAabb], limit: f64) -> bool {
65    boxes.iter().enumerate().any(|(i, a)| boxes[i + 1..].iter().any(|b| box_iou(a, b) >= limit))
66}