Skip to main content

brep_boolean/csg/boolean/
dedicated.rs

1//! Dedicated booleans: routes a crate that builds a kind of solid registers
2//! for booleans of two such solids (the swept tori of `BREP_construction`,
3//! `BREP_SWEEP_TUBE_BOOLEAN`). A route recognises its operands itself and
4//! answers `None` for any other pair. That is a miss, and the general boolean
5//! runs as before. With no route registered nothing changes.
6use super::{BooleanOperation, BrepSolid, KernelRefusal};
7
8/// A dedicated boolean: `None` where it does not recognise both operands.
9pub type DedicatedBoolean = fn(&BrepSolid, &BrepSolid, BooleanOperation) -> Option<Result<BrepSolid, KernelRefusal>>;
10
11static ROUTES: std::sync::RwLock<Vec<DedicatedBoolean>> = std::sync::RwLock::new(Vec::new());
12
13/// Register `route` (once; registering it again is a no-op).
14pub fn register_dedicated_boolean(route: DedicatedBoolean) {
15    if let Ok(mut routes) = ROUTES.write() {
16        if !routes.iter().any(|known| *known as usize == route as usize) {
17            routes.push(route);
18        }
19    }
20}
21
22/// The switch every dedicated route runs under (`BREP_SWEEP_TUBE_BOOLEAN`,
23/// default off). Off, a boolean reads only this cached flag here: the
24/// registry is not read and the exact-sections scope is not consulted.
25pub fn dedicated_booleans_on() -> bool {
26    crate::switch!("BREP_SWEEP_TUBE_BOOLEAN", false)
27}
28
29/// The first registered route's answer for this pair, if any recognises it.
30pub(super) fn dedicated(first: &BrepSolid, second: &BrepSolid, operation: BooleanOperation) -> Option<Result<BrepSolid, KernelRefusal>> {
31    if !dedicated_booleans_on() {
32        return None;
33    }
34    let routes: Vec<DedicatedBoolean> = ROUTES.read().ok()?.clone();
35    routes.into_iter().find_map(|route| route(first, second, operation))
36}