use crate::{BooleanOperation, BooleanOptions, BrepSolid, KernelRefusal};
fn window_retry_on() -> bool {
crate::switch!("BREP_BOOLEAN_WINDOW_RETRY", true)
}
fn window_retry_check() -> bool {
crate::debug_flag!("BREP_BOOLEAN_WINDOW_RETRY_CHECK")
}
fn windowed(solid: &BrepSolid) -> Option<BrepSolid> {
let mut out = solid.clone();
let mut changed = 0usize;
for face in out.shells.iter_mut().flat_map(|shell| shell.faces.iter_mut()) {
if face.surface.analytic().is_some() || face.surface.is_affine().unwrap_or(true) {
continue;
}
let Some(window) = crate::imprint::restricted_carrier(face) else { continue };
if window.lifted {
continue;
}
face.surface = window.surface;
changed += 1;
}
(changed > 0).then(|| {
out.mass_properties_cache = Default::default();
out
})
}
pub(super) fn retry(
first: &BrepSolid,
second: &BrepSolid,
operation: BooleanOperation,
options: &BooleanOptions,
refused: KernelRefusal,
run: impl FnOnce(&BrepSolid, &BrepSolid, BooleanOperation, &BooleanOptions) -> Result<BrepSolid, KernelRefusal>,
) -> Result<BrepSolid, KernelRefusal> {
let (on, check) = (window_retry_on(), window_retry_check());
if !(on || check) {
return Err(refused);
}
let (left, right) = (windowed(first), windowed(second));
if left.is_none() && right.is_none() {
return Err(refused);
}
let answer = run(left.as_ref().unwrap_or(first), right.as_ref().unwrap_or(second), operation, options);
if check {
match &answer {
Ok(_) => eprintln!("BOOLEAN_WINDOW_RETRY check: refused ({refused}); on the windows it builds"),
Err(other) => eprintln!("BOOLEAN_WINDOW_RETRY check: refused ({refused}); on the windows refused too ({other})"),
}
}
match answer {
Ok(solid) if on => Ok(solid),
_ => Err(refused),
}
}