Files
solidcraft/crates/kernel/src/seam_faces.rs
T
7734ba2300 Offset Face: split a periodic face off its seam first, as booleans already do (#6226) (#6227)
* claim: #6226

* Offset Face: split a face off its seam and try again, as booleans do (#6226)

A hole's wall arrives from a STEP file as one whole turn bounded by one wire that
runs along its seam twice, where SolidCraft builds the same wall as two halves.
Offset Face heals the body before it moves anything, and healing such a face
fails inside truck with `This wire is not simple`, so the panic guard turned the
whole operation into a kernel internal error. Since importing is how a part with
a hole usually arrives, Offset Face did not work on imported parts with holes at
all.

`seam_faces` already answers this for booleans: split every such face in two
along a plane through its axis, run the operation, and compose the history back
over the body as it was given. This gives the offset path the same retry.

- `seam_faces::one_body` is `boolean`'s answer for an operation that takes one
  body, with `needed_one` beside `needed`.
- `offset_faces` now does its healing, its face picking and its offset inside one
  closure, so the retry runs the whole of it on the split body; the first attempt
  is guarded, so the failure is an error the retry can act on rather than an
  unwind. The faces are picked per body, because the pieces are numbered
  differently once a face is split — the points do not move, so they pick the
  same surface either way.

The regression test is in `seam_faces_tests.rs`, beside the boolean ones: a
20 x 20 x 10 block with a 6 mm hole, read back from STEP, whose top moves 2 mm
and lands on the same volume as moving the built body's top, with its history
intact.

Checked against `main` before and after: the three kernel tests that fail here
(`blend::…seam_twice`, `step_in::…parallel_body_preparation`,
`tests::a_box_cut_round_a_small_hole…`) fail the same way without this change.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: CarlosKeese <dudukeese@gmail.com>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-10-25 18:49:44 +00:00

119 lines
5.9 KiB
Rust

//! Faces that run along a seam edge twice: a whole turn of a cylinder or a cone bounded by one
//! wire, as STEP files write them (the wire goes round one end, along the seam, round the other
//! end and back along the seam). SolidCraft builds its own round faces in pieces with no such
//! seam, and a boolean that has to divide one of these faces can fail where the same bodies with
//! the face in two halves work: an intersection loop lying wholly inside the face is a hole in a
//! piece whose outer wire visits the seam's vertices twice, which the boolean refuses. So when the
//! first tries at the boolean of the bodies as they are fail, each such face is split in two along
//! a plane through its axis, a quarter turn either side of the seam (the solids are unchanged), and
//! the boolean is asked again; the result's history is composed back over the operands as they
//! were given.
use solidcraft_geom::{Plane, Vec3};
use crate::body::{Body, from_p3};
use crate::boolean_record as recorded;
use crate::history::History;
use crate::splitface::{SplitTool, split_any};
use crate::{BoolOp, FaceSurface, Result, face_surface};
/// Most faces split before the boolean.
const MAX_SPLITS: usize = 64;
thread_local! {
/// Set while the boolean of the split bodies runs (no second round of splitting).
static SPLITTING: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}
/// The first face of `body` whose wire runs along a seam edge twice, on a cylinder or a cone,
/// with the plane that splits it in two: through its axis, square to the seam.
fn next_split(body: &Body) -> Option<(usize, Plane)> {
body.solid.face_iter().enumerate().find_map(|(i, face)| {
let mut seen = std::collections::HashSet::new();
let seam = face.absolute_boundaries().iter().flat_map(|w| w.edge_iter()).find(|e| !seen.insert(e.id()))?;
let (origin, axis) = match face_surface(body, i)? {
FaceSurface::Cylinder { origin, axis, .. } => (origin, axis),
FaceSurface::Cone { apex, axis, .. } => (apex, axis),
_ => return None,
};
let p = from_p3(seam.front().point());
let off = p - origin;
let radial: Vec3 = (off - axis * off.dot(axis)).normalized()?;
Some((i, Plane::from_normal(origin, radial)?))
})
}
/// Whether either body has a face for [`boolean`] to split.
pub(crate) fn needed(a: &Body, b: &Body) -> bool {
!a.is_mesh() && !b.is_mesh() && !SPLITTING.with(|c| c.get()) && (next_split(a).is_some() || next_split(b).is_some())
}
/// Whether `body` has a face for [`one_body`] to split.
pub(crate) fn needed_one(body: &Body) -> bool {
!body.is_mesh() && !SPLITTING.with(|c| c.get()) && next_split(body).is_some()
}
/// `body` with every face that runs along a seam edge twice split in two, and its history over
/// `body`. `None` when a split fails.
fn halved(body: &Body) -> Option<(Body, Option<History>)> {
let (mut x, mut h) = (body.clone(), Some(History::identity(body)));
for _ in 0..MAX_SPLITS {
let Some((face, plane)) = next_split(&x) else { return Some((x, h)) };
let (mut done, _) = split_any(&x, &[face], &SplitTool::Plane(plane)).ok()?;
// The split is no authored boundary: a later feature may still merge what it separates.
done.regularization_protected = x.regularization_protected;
let through = recorded::through(&done, 0, h.as_ref());
(x, h) = (done, through);
}
next_split(&x).is_none().then_some((x, h))
}
/// The boolean `op` of `a` and `b` with their faces that run along a seam edge twice split in
/// two, `run` doing the boolean of the split bodies. `None` when neither body has such a face, a
/// split fails, or the boolean of the split bodies gave no (checked) result: the caller then
/// keeps what it had.
pub(crate) fn boolean(a: &Body, b: &Body, op: BoolOp, run: &dyn Fn(&Body, &Body, BoolOp) -> Result<Option<Body>>) -> Option<Result<Option<Body>>> {
if !needed(a, b) {
return None;
}
let ((x, hx), (y, hy)) = (halved(a)?, halved(b)?);
SPLITTING.with(|c| c.set(true));
let result = crate::guard("boolean of faces split off their seams", || run(&x, &y, op));
SPLITTING.with(|c| c.set(false));
let Ok(result) = result else { return None };
// Held to what a boolean can do, as a retry's result is (see `boolean_guard`).
crate::boolean_guard::check_retry(a, b, op, result.as_ref()).ok()?;
let counts = crate::record::counts(&[a, b]);
Some(Ok(result.map(|body| {
// Each step's history over the operands `[a, b]`.
let history = recorded::of_pair(&body, &recorded::placed(hx.clone(), 0, 2), &recorded::placed(hy.clone(), 1, 2));
body.with_checked_history(history, &counts)
})))
}
/// `run` over `body` with every face that runs along a seam edge twice split in two, for an
/// operation that takes one body. `None` when the body has no such face, a split fails, or the
/// run over the split body fails: the caller then keeps the error it had.
///
/// The same answer as [`boolean`] gives, for the same reason — an operation that has to divide
/// such a face can fail where the body with the face in two halves works — and it matters most
/// to a body read from a STEP file, which is where a whole-turn face comes from. The result's
/// history is composed back over `body` as it was given, so a caller that knows `body`'s faces
/// still recognises them: the split is no authored boundary.
pub(crate) fn one_body(body: &Body, what: &str, run: &dyn Fn(&Body) -> Result<Body>) -> Option<Result<Body>> {
if !needed_one(body) {
return None;
}
let (split, over) = halved(body)?;
SPLITTING.with(|c| c.set(true));
let result = crate::guard(what, || run(&split));
SPLITTING.with(|c| c.set(false));
let out = result.ok()?;
let history = recorded::through(&out, 0, over.as_ref());
Some(Ok(out.with_checked_history(history, &crate::record::counts(&[body]))))
}
#[cfg(test)]
#[path = "seam_faces_tests.rs"]
mod tests;