ifc_lite_geometry/kernel/
mod.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//! Pure-Rust exact mesh-arrangement CSG kernel — predicate foundation.
6//!
7//! This layer provides exact, platform-deterministic geometric predicates over
8//! a mix of EXPLICIT input points and IMPLICIT intersection points (LPI =
9//! line∩plane, TPI = three planes) carried symbolically and never materialised
10//! to a float decision.
11//!
12//! Determinism: signs are integer parity over deterministic arithmetic. The
13//! explicit path goes through `geometry-predicates` (FMA-free, const error
14//! bounds). The EXACT (BigRational) tier is correct by construction and is the
15//! oracle for the faster interval/fixed-width tiers, each verified `≡` exact.
16
17pub mod arrangement;
18pub mod broadphase;
19pub mod budget;
20pub mod coplanar;
21pub mod fixed;
22// Public because `fixed::Lam` (a `pub type`) and the `pub` cached-lambda
23// predicates expose `FixedInt` exactly as they previously exposed the external
24// `bnum::types::I512`; a narrower visibility would trip `private_interfaces`.
25pub mod fixed_int;
26pub mod interner;
27pub mod interval;
28pub mod manifest;
29pub mod mesh_bridge;
30pub mod mesh_volume;
31pub(crate) mod near_band;
32pub(crate) mod plane_weld;
33pub mod predicates;
34pub mod rational;
35pub(crate) mod signed_volume;
36pub mod retriangulate;
37mod retriangulate_audit;
38mod retriangulate_cleanup;
39mod retriangulate_recover;
40#[cfg(test)]
41mod retriangulate_recover_tests;
42pub mod tritri;
43
44/// Three-valued exact sign.
45#[derive(Clone, Copy, PartialEq, Eq, Debug)]
46pub enum Sign {
47    Negative,
48    Zero,
49    Positive,
50}
51
52impl Sign {
53    #[inline]
54    pub fn from_f64(x: f64) -> Sign {
55        if x < 0.0 {
56            Sign::Negative
57        } else if x > 0.0 {
58            Sign::Positive
59        } else {
60            Sign::Zero
61        }
62    }
63
64    /// Flip the sign (Zero is fixed). Used by the per-configuration denominator
65    /// flip in [`assemble_sign`].
66    #[inline]
67    pub fn flip(self) -> Sign {
68        match self {
69            Sign::Positive => Sign::Negative,
70            Sign::Negative => Sign::Positive,
71            Sign::Zero => Sign::Zero,
72        }
73    }
74}
75
76/// Which axis to drop when projecting a 3D predicate to 2D (orient2d).
77#[derive(Clone, Copy, PartialEq, Eq, Debug)]
78pub enum DropAxis {
79    X,
80    Y,
81    Z,
82}
83
84/// A point that is either an explicit input coordinate or an implicit
85/// intersection point carried symbolically over the original input coords.
86#[derive(Clone, Debug)]
87pub enum ImplicitPoint {
88    Explicit([f64; 3]),
89    /// Line `PQ` ∩ plane `RST`.
90    Lpi(Lpi),
91    /// Three planes concurrent (each a triangle: 3 points).
92    Tpi(Tpi),
93}
94
95/// Line–plane implicit point: line through `p,q` ∩ plane through `r,s,t`.
96#[derive(Clone, Copy, Debug)]
97pub struct Lpi {
98    pub p: [f64; 3],
99    pub q: [f64; 3],
100    pub r: [f64; 3],
101    pub s: [f64; 3],
102    pub t: [f64; 3],
103}
104
105/// Three-plane implicit point: `planes[i]` is a triangle (3 points) defining a plane.
106#[derive(Clone, Copy, Debug)]
107pub struct Tpi {
108    pub planes: [[[f64; 3]; 3]; 3],
109}
110
111/// Combine the sign of the homogenised determinant `Λ′` with the
112/// per-configuration denominator flip.
113///
114/// When an implicit point `(λ/d)` enters a determinant row, clearing the
115/// denominator multiplies the determinant by `d` (degree = the denominator's
116/// multiplicity in that configuration). The geometric sign therefore equals
117/// `sign(Λ′)` flipped once per NEGATIVE odd-multiplicity denominator.
118/// `den_signs` lists ONLY the odd-multiplicity denominator signs — squared
119/// denominators (e.g. the TPI `III` orient3d case, `D′=(d1d2d3d4)²`) cannot
120/// change the sign and MUST NOT be included. Getting this wrong silently
121/// inverts inside/outside for ~half of real cuts (the per-config rule, not a
122/// blanket XOR over all negatives — see the spec's REFUTATION-FIX).
123///
124/// A `Zero` denominator means a degenerate / at-infinity construction (e.g. the
125/// LPI line is parallel to the plane, `d=0`): the predicate is undefined, so we
126/// return `Zero`. Valid implicit points (built only for genuinely-crossing
127/// edges) never have a zero denominator.
128#[inline]
129pub fn assemble_sign(lambda_det_sign: Sign, den_signs: &[Sign]) -> Sign {
130    let mut negatives = 0u32;
131    for &d in den_signs {
132        match d {
133            Sign::Negative => negatives += 1,
134            Sign::Zero => return Sign::Zero,
135            Sign::Positive => {}
136        }
137    }
138    if negatives % 2 == 1 {
139        lambda_det_sign.flip()
140    } else {
141        lambda_det_sign
142    }
143}