Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

6.3 KB
Raw Blame
//! 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 crate::Cause) -> KernelError {
        KernelError::smlib(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]))
    }
}