Files
solidcraft/crates/kernel/src/smlib_boolean.rs
T
jyvikoandClaude Opus 5.5 4030b795c4 Kernel: a validation mode decides when sweeps, booleans and blends are checked
Validation::EachStep checks every solid sweep, boolean and blend result
in SMLib as before; EachFeature leaves those checks to the end of the
feature and says a check was left; Off skips them. The evaluation installs
the mode around a feature (validating), as watch installs a watch; outside
it every step is checked as always, and the kernel's worker threads run
under the caller's mode.

Under EachFeature a body decoded from a step keeps that step's SMLib
result (for its own record only), and check_kept checks it as the step's
own check would have, then lets it go; any other kept body is checked as
SMLib imports its record. The record can pass where the result fails: a
fillet's surface of revolution whose polar curve SMLib's check refuses
(fillets/1080-fillet-sloped-basin-1-mm-rim-r0-5-4-edges) is rebuilt on
import, and its round meets a wall at 72 degrees; checking the record
alone kept that round.

The largest-size search of a blend checks every candidate as it is built,
whatever the mode: a candidate fits only as a body the check accepts.
Surface operations and stitching keep their checks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 13:52:54 -04:00

130 lines
6.4 KiB
Rust

//! Booleans through SMLib: each operand's schema 4 record built in SMLib and tagged as operand 0
//! or 1, `SmBrep` boolean, the result exported to schema 4 and decoded as a kernel body. The result's history is SMLib's own (its tags carried through the
//! operation), renumbered from SMLib's face and edge order into the operands' and the result's
//! ([`crate::history::History`]'s contract). A result SMLib's own check or the record's geometry
//! check refuses is an error, never a body.
pub(crate) use smlib::{boolean as smlib_boolean, union_all as smlib_union_all};
mod smlib {
use solidcraft_smlib::{BooleanOp, CheckLevel};
use crate::body::Body;
use crate::history::History;
use crate::native::{body_from_smlib_ordered, smlib_body_ordered};
use crate::ops::BoolOp;
use crate::validation::{Checked, Step};
use crate::{KernelError, Result, record};
fn failed(what: &str, e: impl std::fmt::Display) -> KernelError {
KernelError::Failed(format!("SMLib boolean: {what}: {e}"))
}
/// `a` and `b` combined by `op` in SMLib, with the history SMLib's tags give (none where a
/// body's face and edge order cannot be matched to SMLib's). `None`: the result is empty.
pub(crate) fn boolean(a: &Body, b: &Body, op: BoolOp) -> Result<Option<Body>> {
a.require_brep("a boolean")?;
b.require_brep("a boolean")?;
let (sa, oa) = smlib_body_ordered(a)?;
let (sb, ob) = smlib_body_ordered(b)?;
let ta = sa.tagged(0).map_err(|e| failed("tag", e))?;
let tb = sb.tagged(1).map_err(|e| failed("tag", e))?;
let kind = match op {
BoolOp::Union => BooleanOp::Union,
BoolOp::Cut => BooleanOp::Subtract,
BoolOp::Intersect => BooleanOp::Intersect,
};
let Some(r) = ta.boolean(&tb, kind).map_err(|e| failed("operation", e))? else { return Ok(None) };
let step = checked(&r, &[&sa, &sb])?;
let (body, or) = body_from_smlib_ordered(&r)?;
let history = match (oa, ob, or) {
(Some(oa), Some(ob), Some(or)) => r.history().map_err(|e| failed("history", e))?.history(&[oa, ob], &or).map(Some).unwrap_or_else(|e| {
log::debug!(target: "smlib", "boolean history: {e}");
None
}),
_ => None,
};
Ok(Some(left(with_history(body, history, a, b), step, r)))
}
/// The union of `bodies` in one SMLib call, each tagged as its own operand, checked once:
/// the result's history is over `bodies` in order. At least one body.
pub(crate) fn union_all(bodies: &[&Body]) -> Result<Body> {
if bodies.len() > solidcraft_smlib::MAX_UNION_BREPS {
return Err(KernelError::Invalid(format!("a union takes at most {} bodies", solidcraft_smlib::MAX_UNION_BREPS)));
}
let mut operands = Vec::with_capacity(bodies.len());
let mut tagged = Vec::with_capacity(bodies.len());
let mut orders = Some(Vec::with_capacity(bodies.len()));
for (i, body) in bodies.iter().enumerate() {
body.require_brep("a union")?;
let (s, order) = smlib_body_ordered(body)?;
tagged.push(s.tagged(i).map_err(|e| failed("tag", e))?);
operands.push(s);
orders = orders.zip(order).map(|(mut v, o)| {
v.push(o);
v
});
}
let refs: Vec<&solidcraft_smlib::Brep> = tagged.iter().collect();
let r = solidcraft_smlib::Brep::union_all(&refs).map_err(|e| failed("operation", e))?;
let operand_refs: Vec<&solidcraft_smlib::Brep> = operands.iter().collect();
let step = checked(&r, &operand_refs)?;
let (body, or) = body_from_smlib_ordered(&r)?;
let history = match (orders, or) {
(Some(orders), Some(or)) => r.history().map_err(|e| failed("history", e))?.history(&orders, &or).map(Some).unwrap_or_else(|e| {
log::debug!(target: "smlib", "union history: {e}");
None
}),
_ => None,
};
Ok(left(body.with_checked_history(history, &record::counts(bodies)), step, r))
}
/// `body`, decoded from `r`, keeping `r` for the check at the end of its feature when the
/// step left it there.
fn left(body: Body, step: Step, r: solidcraft_smlib::Brep) -> Body {
if step == Step::Defer { body.left_unchecked("SMLib boolean", Checked::Combined, r) } else { body }
}
/// An error unless SMLib's check accepts `r`, the result of combining `operands` (checked
/// here unless the evaluation's validation mode leaves it to the feature's end,
/// `crate::validation`): what was done with the check.
fn checked(r: &solidcraft_smlib::Brep, operands: &[&solidcraft_smlib::Brep]) -> Result<Step> {
let step = crate::validation::step();
if step != Step::Check {
return Ok(step);
}
// Solids touching each other along an edge or at a vertex come back as one non-manifold
// body, which the export splits into separate solids: SMLib's check is of those, imported
// as given (its check of the touching arrangement itself refuses the radial order of
// faces round an edge they share).
let check = crate::validation::check_result(r, Checked::Combined).map_err(|e| KernelError::Failed(format!("SMLib boolean: {e}")))?;
if check.is_valid() {
return Ok(step);
}
// Whether an operand failed it already says whose the fault is.
let failing: Vec<String> = operands
.iter()
.enumerate()
.filter(|(_, s)| s.check(CheckLevel::All).is_ok_and(|c| !c.is_valid()))
.map(|(i, _)| match (operands.len(), i) {
(2, 0) => "the first operand".to_string(),
(2, _) => "the second operand".to_string(),
_ => format!("operand {i}"),
})
.collect();
let what = if failing.is_empty() {
"SMLib's check refuses the result".to_string()
} else {
format!("SMLib's check refuses the result ({} already fails it)", failing.join(" and "))
};
Err(failed(&what, check))
}
/// `body` with `history` where it describes `body` made from `a` and `b`.
fn with_history(body: Body, history: Option<History>, a: &Body, b: &Body) -> Body {
body.with_checked_history(history, &record::counts(&[a, b]))
}
}