BREP_boolean 0.35.0

Rust B-rep CAD kernel (next.BREP.io): booleans and healing: imprint, fragment, boolean assembly, sewing and face merging.
Documentation
//! Check mode's comparison of a closed-form result with the pipeline's: the
//! two must be the same body up to numbering, order and how a curve or a
//! pcurve is parametrised.
use crate::topology::{BrepSolid, HashMap};
use crate::Vec3;

/// Every way the body `built` differs from the body `general`; empty where
/// they agree.
pub(super) fn body_differences(built: &BrepSolid, general: &BrepSolid) -> Vec<String> {
    let mut found = Vec::new();
    let scale = general.vertices.iter().map(|vertex| vertex.point.length()).fold(1.0, f64::max);
    let grain = 1e-6 * scale;
    let counts = |solid: &BrepSolid| {
        (
            solid.shells.len(),
            solid.shells.iter().map(|shell| shell.faces.len()).sum::<usize>(),
            solid.edges.iter().filter(|edge| !edge.degenerate).count(),
            solid.vertices.len(),
            solid.genus,
        )
    };
    if counts(built) != counts(general) {
        found.push(format!("shells/faces/edges/vertices/genus {:?} against the pipeline's {:?}", counts(built), counts(general)));
    }
    let (edges_built, edges_general) = (edge_keys(built, grain), edge_keys(general, grain));
    multiset("edge", edges_built.values().cloned().collect(), edges_general.values().cloned().collect(), &mut found);
    multiset("face", face_keys(built, &edges_built), face_keys(general, &edges_general), &mut found);
    match (crate::solid_mass_properties(built), crate::solid_mass_properties(general)) {
        (Ok(a), Ok(b)) => {
            if (a.volume - b.volume).abs() > 1e-9 * b.volume.abs().max(1e-300) {
                found.push(format!("volume {:.12e} against {:.12e}", a.volume, b.volume));
            }
            if (a.surface_area - b.surface_area).abs() > 1e-9 * b.surface_area.abs().max(1e-300) {
                found.push(format!("area {:.12e} against {:.12e}", a.surface_area, b.surface_area));
            }
        }
        (a, b) => {
            if a.is_ok() != b.is_ok() {
                found.push(format!("mass properties {:?} against {:?}", a.map(|m| m.volume), b.map(|m| m.volume)));
            }
        }
    }
    found
}

fn snapped(point: Vec3, grain: f64) -> [i64; 3] {
    [(point.x / grain).round() as i64, (point.y / grain).round() as i64, (point.z / grain).round() as i64]
}

/// An edge as the points it joins and three points along it, whichever way
/// it runs, with its name.
fn edge_keys(solid: &BrepSolid, grain: f64) -> HashMap<u64, String> {
    let points: HashMap<u64, Vec3> = solid.vertices.iter().map(|vertex| (vertex.id, vertex.point)).collect();
    solid
        .edges
        .iter()
        .filter(|edge| !edge.degenerate)
        .map(|edge| {
            let at = |fraction: f64| {
                edge.curve.evaluate(edge.t0 + (edge.t1 - edge.t0) * fraction).map(|point| snapped(point, grain)).unwrap_or([i64::MAX; 3])
            };
            let end = |id: u64| points.get(&id).map(|point| snapped(*point, grain)).unwrap_or([i64::MAX; 3]);
            let forward = [end(edge.start_vertex_id), at(0.25), at(0.5), at(0.75), end(edge.end_vertex_id)];
            let mut backward = forward;
            backward.reverse();
            (edge.id, format!("{:?} {:?}", forward.min(backward), edge.name))
        })
        .collect()
}

/// A face as its carrier kind, sense, name and loops, each loop the sorted
/// keys of its edges.
fn face_keys(solid: &BrepSolid, edges: &HashMap<u64, String>) -> Vec<String> {
    solid
        .shells
        .iter()
        .flat_map(|shell| &shell.faces)
        .map(|face| {
            let mut loops: Vec<Vec<&str>> = face
                .loops
                .iter()
                .map(|ring| {
                    let mut keys: Vec<&str> =
                        ring.coedges.iter().map(|coedge| edges.get(&coedge.edge_id).map_or("degenerate", String::as_str)).collect();
                    keys.sort_unstable();
                    keys
                })
                .collect();
            loops.sort_unstable();
            format!("{} sense={} name={:?} loops={:?}", super::carrier_kind(&face.surface), face.same_sense, face.name, loops)
        })
        .collect()
}

fn multiset(what: &str, mut built: Vec<String>, mut general: Vec<String>, found: &mut Vec<String>) {
    built.sort_unstable();
    general.sort_unstable();
    if built == general {
        return;
    }
    let only = |a: &[String], b: &[String]| a.iter().find(|key| b.binary_search(key).is_err()).cloned();
    if let Some(key) = only(&built, &general) {
        found.push(format!("a built {what} the pipeline has not: {key}"));
    }
    if let Some(key) = only(&general, &built) {
        found.push(format!("a pipeline {what} not built: {key}"));
    }
    if built.len() != general.len() {
        found.push(format!("{} {what}s against {}", built.len(), general.len()));
    }
}