ifc_lite_geometry/
output_policy.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//! The process-wide geometry policy that can change output, read through the
6//! SAME accessors the pipeline calls, plus this crate's resolved features.
7//!
8//! A result cache keys on [`effective_output_policy`] rather than on the
9//! environment: every `IFC_LITE_*` knob here is read once into a `OnceLock`
10//! (or per call) by its accessor, and several have programmatic overrides
11//! (`local_frame_set_enabled_override`, `rect_fast::param_set_enabled_override`,
12//! `GeometryRouter::set_build_dedup_extra_override`, `kernel::budget::set_cap`
13//! / `set_element_cap`, `set_alt_triangulator`). Reading the environment again
14//! would disagree with a `OnceLock` initialised before the variable changed,
15//! and would miss the overrides entirely.
16//!
17//! Every programmatic setter bumps a mutation generation
18//! ([`policy_generation`]), so a cache can tell that a policy changed during a
19//! pass even when it was changed back.
20//!
21//! A new process-global output knob belongs here; the processing crate's
22//! `result_cache_policy_ratchet` test fails on an unclassified `IFC_LITE_*`
23//! variable.
24
25#[path = "output_policy_generation.rs"]
26mod generation;
27pub(crate) use generation::mutate;
28pub use generation::{coherent_output_policy, policy_generation};
29
30/// Every feature of this crate and whether this build enabled it. Cargo
31/// resolves features per build, so a downstream binary can enable, say,
32/// `csg_topology_gate` without the processing crate's own features changing.
33/// Kept in step with `Cargo.toml` by a test below.
34pub const BUILD_FEATURES: &[(&str, bool)] = &[
35    ("retained-runtime", cfg!(feature = "retained-runtime")),
36    ("opening-perf-trace", cfg!(feature = "opening-perf-trace")),
37    ("debug_geometry", cfg!(feature = "debug_geometry")),
38    ("csg_capture", cfg!(feature = "csg_capture")),
39    ("wasm_source_capture", cfg!(feature = "wasm_source_capture")),
40    ("observability", cfg!(feature = "observability")),
41    ("triangulation-alt", cfg!(feature = "triangulation-alt")),
42    ("test-support", cfg!(feature = "test-support")),
43    ("csg_topology_gate", cfg!(feature = "csg_topology_gate")),
44    ("csg_manifold_gate", cfg!(feature = "csg_manifold_gate")),
45];
46
47/// The effective value of every process-global geometry output knob.
48#[non_exhaustive]
49#[derive(Debug, Clone, Copy, PartialEq)]
50pub struct OutputPolicy {
51    /// `rect_fast::enabled` (`IFC_LITE_RECT_FAST`).
52    pub rect_fast: bool,
53    /// `rect_fast::param_enabled` (override, `IFC_LITE_RECT_PARAM`).
54    pub rect_param: bool,
55    /// Process-wide local-frame fallback (override, `IFC_LITE_LOCAL_FRAME`),
56    /// used by every router built without an explicit frame policy.
57    pub local_frame: bool,
58    /// Coaxial void union (`IFC_LITE_VOID_UNION`).
59    pub void_union: bool,
60    /// 2D boolean void path (`IFC_LITE_VOID_2D`).
61    pub void_2d: bool,
62    /// Prism cut path (`IFC_LITE_PRISM_CUT`).
63    pub prism_cut: bool,
64    /// Extra build-time item dedup (override, `IFC_LITE_DEDUP_EXTRA`).
65    pub dedup_extra: bool,
66    /// Half-space section cap disabled (`IFC_LITE_HALFSPACE_CAP_OFF`).
67    pub halfspace_cap_off: bool,
68    /// Preview small-cut skip ratio (`IFC_LITE_FAST_CUT_RATIO`).
69    pub fast_cut_ratio: f64,
70    /// Per-boolean escalation cap as snapshotted (`u64::MAX` = unbounded).
71    pub csg_op_cap: u64,
72    /// Per-element escalation cap as snapshotted (`u64::MAX` = unbounded).
73    pub csg_element_cap: u64,
74    /// Alternate triangulator oracle armed (feature `triangulation-alt`).
75    pub alt_triangulator: bool,
76}
77
78/// Read the policy now, through the pipeline's own accessors.
79pub fn effective_output_policy() -> OutputPolicy {
80    OutputPolicy {
81        rect_fast: crate::rect_fast::enabled(),
82        rect_param: crate::rect_fast::param_enabled(),
83        local_frame: crate::router::transforms::local_frame_enabled(),
84        void_union: crate::router::voids::coaxial_union::enabled(),
85        void_2d: crate::router::voids::bool2d_path::enabled(),
86        prism_cut: crate::router::voids::prism_cut::enabled(),
87        dedup_extra: crate::GeometryRouter::build_dedup_extra_enabled(),
88        halfspace_cap_off: crate::processors::boolean::halfspace_cap::disabled(),
89        fast_cut_ratio: crate::processors::boolean::cut_heuristics::fast_cut_skip_ratio(),
90        csg_op_cap: crate::kernel::budget::effective_op_cap(),
91        csg_element_cap: crate::kernel::budget::effective_element_cap(),
92        #[cfg(feature = "triangulation-alt")]
93        alt_triangulator: crate::triangulation::alt_oracle::armed(),
94        #[cfg(not(feature = "triangulation-alt"))]
95        alt_triangulator: false,
96    }
97}
98
99impl OutputPolicy {
100    /// A canonical, versioned byte encoding: fixed field order, explicit
101    /// widths, floats as raw bits, followed by [`BUILD_FEATURES`].
102    pub fn canonical_bytes(&self) -> Vec<u8> {
103        let Self {
104            rect_fast,
105            rect_param,
106            local_frame,
107            void_union,
108            void_2d,
109            prism_cut,
110            dedup_extra,
111            halfspace_cap_off,
112            fast_cut_ratio,
113            csg_op_cap,
114            csg_element_cap,
115            alt_triangulator,
116        } = *self;
117        let mut out = b"ifc-lite-geometry-output-policy-v1".to_vec();
118        let flags =
119            [rect_fast, rect_param, local_frame, void_union, void_2d, prism_cut, dedup_extra, halfspace_cap_off, alt_triangulator];
120        out.extend(flags.iter().map(|f| u8::from(*f)));
121        out.extend_from_slice(&fast_cut_ratio.to_bits().to_le_bytes());
122        out.extend_from_slice(&csg_op_cap.to_le_bytes());
123        out.extend_from_slice(&csg_element_cap.to_le_bytes());
124        out.extend_from_slice(&(BUILD_FEATURES.len() as u64).to_le_bytes());
125        for (name, on) in BUILD_FEATURES {
126            out.extend_from_slice(&(name.len() as u64).to_le_bytes());
127            out.extend_from_slice(name.as_bytes());
128            out.push(u8::from(*on));
129        }
130        out
131    }
132}
133
134#[cfg(test)]
135mod tests {
136    use super::*;
137
138    /// A feature added to `Cargo.toml` without a row above would let a build
139    /// that enables it share cache entries with one that does not.
140    #[test]
141    fn build_features_list_every_cargo_feature() {
142        let manifest = include_str!("../Cargo.toml");
143        let section = manifest.split("\n[features]\n").nth(1).expect("[features] section");
144        let section = section.split("\n[").next().unwrap_or(section);
145        let mut declared: Vec<&str> = section
146            .lines()
147            .map(str::trim)
148            .filter(|l| !l.is_empty() && !l.starts_with('#'))
149            .filter_map(|l| l.split('=').next().map(str::trim))
150            .filter(|name| *name != "default")
151            .collect();
152        declared.sort_unstable();
153        let mut listed: Vec<&str> = BUILD_FEATURES.iter().map(|(n, _)| *n).collect();
154        listed.sort_unstable();
155        assert_eq!(listed, declared);
156    }
157
158    #[test]
159    fn canonical_bytes_see_every_field() {
160        let base = effective_output_policy();
161        let variants: [fn(&mut OutputPolicy); 12] = [
162            |p| p.rect_fast = !p.rect_fast,
163            |p| p.rect_param = !p.rect_param,
164            |p| p.local_frame = !p.local_frame,
165            |p| p.void_union = !p.void_union,
166            |p| p.void_2d = !p.void_2d,
167            |p| p.prism_cut = !p.prism_cut,
168            |p| p.dedup_extra = !p.dedup_extra,
169            |p| p.halfspace_cap_off = !p.halfspace_cap_off,
170            |p| p.fast_cut_ratio = -p.fast_cut_ratio,
171            |p| p.csg_op_cap ^= 1,
172            |p| p.csg_element_cap ^= 1,
173            |p| p.alt_triangulator = !p.alt_triangulator,
174        ];
175        for (i, change) in variants.iter().enumerate() {
176            let mut p = base;
177            change(&mut p);
178            assert_ne!(p.canonical_bytes(), base.canonical_bytes(), "field {i} not encoded");
179        }
180    }
181}