ifc_lite_diff/
geometry_compare.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//! Shared geometry comparison arithmetic, preserving JS evaluation order (#8042).
6use crate::model::EntityAabb;
7
8#[derive(Debug, Clone, Copy)]
9pub struct GeometryTolerances {
10    pub move_tolerance: f64,
11    pub reshape_tolerance: f64,
12    pub max_move_distance: f64,
13}
14impl Default for GeometryTolerances {
15    fn default() -> Self {
16        Self { move_tolerance: 2e-3, reshape_tolerance: 1e-3, max_move_distance: 10.0 }
17    }
18}
19/// Nonfinite or negative settings use the same defaults as the TS boundary.
20pub fn positive_or(value: Option<f64>, fallback: f64) -> f64 {
21    value.filter(|v| v.is_finite() && *v >= 0.0).unwrap_or(fallback)
22}
23impl GeometryTolerances {
24    pub fn resolve(movement: Option<f64>, reshape: Option<f64>, maximum: Option<f64>) -> Self {
25        let defaults = Self::default();
26        Self {
27            move_tolerance: positive_or(movement, defaults.move_tolerance),
28            reshape_tolerance: positive_or(reshape, defaults.reshape_tolerance),
29            max_move_distance: positive_or(maximum, defaults.max_move_distance),
30        }
31    }
32}
33
34pub fn usable_aabb(aabb: &EntityAabb) -> bool {
35    aabb.min.iter().chain(&aabb.max).all(|value| value.is_finite())
36}
37pub fn centre(aabb: &EntityAabb) -> [f64; 3] {
38    std::array::from_fn(|axis| (aabb.min[axis] + aabb.max[axis]) / 2.0)
39}
40pub fn centre_distance(a: [f64; 3], b: [f64; 3]) -> f64 {
41    let dx = b[0] - a[0];
42    let dy = b[1] - a[1];
43    let dz = b[2] - a[2];
44    (dx * dx + dy * dy + dz * dz).sqrt()
45}
46pub fn size_agrees(a: &EntityAabb, b: &EntityAabb, tolerance: f64) -> bool {
47    for axis in 0..3 {
48        let size_a = a.max[axis] - a.min[axis];
49        let size_b = b.max[axis] - b.min[axis];
50        if (size_a - size_b).abs() > tolerance { return false; }
51    }
52    true
53}
54
55#[derive(Debug, Clone, Copy, PartialEq, Eq)]
56pub enum GeometryChangeKind { Moved, Reshaped }
57#[derive(Debug, Clone, Copy, PartialEq)]
58pub struct GeometryChange {
59    pub kind: GeometryChangeKind,
60    pub distance: Option<f64>,
61}
62pub fn classify_geometry_change(
63    base: Option<&EntityAabb>, head: Option<&EntityAabb>, tolerances: GeometryTolerances,
64) -> GeometryChange {
65    let (Some(a), Some(b)) = (base.filter(|a| usable_aabb(a)), head.filter(|b| usable_aabb(b))) else {
66        return GeometryChange { kind: GeometryChangeKind::Moved, distance: None };
67    };
68    let raw_distance = centre_distance(centre(a), centre(b));
69    let distance = if raw_distance > tolerances.move_tolerance { raw_distance } else { 0.0 };
70    let kind = if !size_agrees(a, b, tolerances.reshape_tolerance) || distance <= 0.0 {
71        GeometryChangeKind::Reshaped
72    } else { GeometryChangeKind::Moved };
73    GeometryChange { kind, distance: Some(distance) }
74}