Skip to main content

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 predicates;
31pub mod rational;
32mod signed_volume;
33pub mod retriangulate;
34mod retriangulate_audit;
35mod retriangulate_cleanup;
36mod retriangulate_recover;
37#[cfg(test)]
38mod retriangulate_recover_tests;
39pub mod tritri;
40
41/// Three-valued exact sign.
42#[derive(Clone, Copy, PartialEq, Eq, Debug)]
43pub enum Sign {
44    Negative,
45    Zero,
46    Positive,
47}
48
49impl Sign {
50    #[inline]
51    pub fn from_f64(x: f64) -> Sign {
52        if x < 0.0 {
53            Sign::Negative
54        } else if x > 0.0 {
55            Sign::Positive
56        } else {
57            Sign::Zero
58        }
59    }
60
61    /// Flip the sign (Zero is fixed). Used by the per-configuration denominator
62    /// flip in [`assemble_sign`].
63    #[inline]
64    pub fn flip(self) -> Sign {
65        match self {
66            Sign::Positive => Sign::Negative,
67            Sign::Negative => Sign::Positive,
68            Sign::Zero => Sign::Zero,
69        }
70    }
71}
72
73/// Which axis to drop when projecting a 3D predicate to 2D (orient2d).
74#[derive(Clone, Copy, PartialEq, Eq, Debug)]
75pub enum DropAxis {
76    X,
77    Y,
78    Z,
79}
80
81/// A point that is either an explicit input coordinate or an implicit
82/// intersection point carried symbolically over the original input coords.
83#[derive(Clone, Debug)]
84pub enum ImplicitPoint {
85    Explicit([f64; 3]),
86    /// Line `PQ` ∩ plane `RST`.
87    Lpi(Lpi),
88    /// Three planes concurrent (each a triangle: 3 points).
89    Tpi(Tpi),
90}
91
92/// Line–plane implicit point: line through `p,q` ∩ plane through `r,s,t`.
93#[derive(Clone, Copy, Debug)]
94pub struct Lpi {
95    pub p: [f64; 3],
96    pub q: [f64; 3],
97    pub r: [f64; 3],
98    pub s: [f64; 3],
99    pub t: [f64; 3],
100}
101
102/// Three-plane implicit point: `planes[i]` is a triangle (3 points) defining a plane.
103#[derive(Clone, Copy, Debug)]
104pub struct Tpi {
105    pub planes: [[[f64; 3]; 3]; 3],
106}
107
108/// Combine the sign of the homogenised determinant `Λ′` with the
109/// per-configuration denominator flip.
110///
111/// When an implicit point `(λ/d)` enters a determinant row, clearing the
112/// denominator multiplies the determinant by `d` (degree = the denominator's
113/// multiplicity in that configuration). The geometric sign therefore equals
114/// `sign(Λ′)` flipped once per NEGATIVE odd-multiplicity denominator.
115/// `den_signs` lists ONLY the odd-multiplicity denominator signs — squared
116/// denominators (e.g. the TPI `III` orient3d case, `D′=(d1d2d3d4)²`) cannot
117/// change the sign and MUST NOT be included. Getting this wrong silently
118/// inverts inside/outside for ~half of real cuts (the per-config rule, not a
119/// blanket XOR over all negatives — see the spec's REFUTATION-FIX).
120///
121/// A `Zero` denominator means a degenerate / at-infinity construction (e.g. the
122/// LPI line is parallel to the plane, `d=0`): the predicate is undefined, so we
123/// return `Zero`. Valid implicit points (built only for genuinely-crossing
124/// edges) never have a zero denominator.
125#[inline]
126pub fn assemble_sign(lambda_det_sign: Sign, den_signs: &[Sign]) -> Sign {
127    let mut negatives = 0u32;
128    for &d in den_signs {
129        match d {
130            Sign::Negative => negatives += 1,
131            Sign::Zero => return Sign::Zero,
132            Sign::Positive => {}
133        }
134    }
135    if negatives % 2 == 1 {
136        lambda_det_sign.flip()
137    } else {
138        lambda_det_sign
139    }
140}