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