feat(shapeops): face and edge history from booleans

and_with_history / or_with_history return, with the solid, the operand face
each result face is a region of (and whether it is that face untouched) and,
for each result edge, the operand edge it is (a piece of) or the pair of
operand faces whose intersection made it. Faces and edges are recorded where
they are created (face division, intersection edges, split edges, edges
rebuilt on a moved vertex), resolved to operand terms at creation, so the
passes folding in extra shells compose without bookkeeping. and / or wrap
the same pass and drop the history; a missing record never fails them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01J53sR6gEaVt56soFDpTck8
This commit is contained in:
Iraklis KourtisandClaude Opus 5.5 authored and bherbruck committed 2026-10-12 11:52:53 -04:00
1 parent c87efa1f35
commit cc4955a8da
12 files changed
+363 -47

No files matched your search

+49 -28
View File
@@ -1,3 +1,4 @@
use super::history::{History, Lineage};
use super::*;
use truck_geometry::prelude::IntersectionCurve;
use truck_meshalgo::prelude::*;
@@ -51,6 +52,7 @@ fn process_one_pair_of_shells<C: ShapeOpsCurve<S>, S: ShapeOpsSurface>(
shell0: &Shell<Point3, C, S>,
shell1: &Shell<Point3, C, S>,
tol: f64,
lineage: &mut Lineage<C, S>,
) -> Option<[Shell<Point3, C, S>; 2]> {
nonpositive_tolerance!(tol);
// SolveCraft: robust meshing (boundaries from earlier operations lie on surfaces only
@@ -61,10 +63,10 @@ fn process_one_pair_of_shells<C: ShapeOpsCurve<S>, S: ShapeOpsSurface>(
geom_loops_store0: loops_store0,
geom_loops_store1: loops_store1,
..
} = crate::tr!(loops_store::create_loops_stores(shell0, &poly_shell0, shell1, &poly_shell1, tol), "loops stores");
let mut cls0 = crate::tr!(divide_face::divide_faces(shell0, &loops_store0, tol), "divide faces 0");
} = crate::tr!(loops_store::create_loops_stores(shell0, &poly_shell0, shell1, &poly_shell1, tol, lineage), "loops stores");
let mut cls0 = crate::tr!(divide_face::divide_faces(shell0, &loops_store0, tol, lineage), "divide faces 0");
cls0.integrate_by_component();
let mut cls1 = crate::tr!(divide_face::divide_faces(shell1, &loops_store1, tol), "divide faces 1");
let mut cls1 = crate::tr!(divide_face::divide_faces(shell1, &loops_store1, tol, lineage), "divide faces 1");
cls1.integrate_by_component();
let [mut and0, mut or0, unknown0] = cls0.and_or_unknown();
unknown0.into_iter().try_for_each(|face| {
@@ -109,21 +111,7 @@ pub fn and<C: ShapeOpsCurve<S>, S: ShapeOpsSurface>(
solid1: &Solid<Point3, C, S>,
tol: f64,
) -> Option<Solid<Point3, C, S>> {
let mut iter0 = solid0.boundaries().iter();
let mut iter1 = solid1.boundaries().iter();
let shell0 = iter0.next()?;
let shell1 = iter1.next()?;
let [mut and_shell, _] = process_one_pair_of_shells(shell0, shell1, tol)?;
for shell in iter0 {
let [res, _] = process_one_pair_of_shells(&and_shell, shell, tol)?;
and_shell = res;
}
for shell in iter1 {
let [res, _] = process_one_pair_of_shells(&and_shell, shell, tol)?;
and_shell = res;
}
let boundaries = components_in_order(&and_shell);
Solid::try_new(boundaries).ok()
boolean(solid0, solid1, tol, |[and, _]| and).map(|(solid, _)| solid)
}
/// OR operation between two solids.
@@ -132,21 +120,54 @@ pub fn or<C: ShapeOpsCurve<S>, S: ShapeOpsSurface>(
solid1: &Solid<Point3, C, S>,
tol: f64,
) -> Option<Solid<Point3, C, S>> {
boolean(solid0, solid1, tol, |[_, or]| or).map(|(solid, _)| solid)
}
/// SolveCraft: AND operation between two solids, with where each face and edge of the result
/// came from (operand 0 is `solid0`, operand 1 is `solid1`). The operands must not share faces
/// or edges.
pub fn and_with_history<C: ShapeOpsCurve<S>, S: ShapeOpsSurface>(
solid0: &Solid<Point3, C, S>,
solid1: &Solid<Point3, C, S>,
tol: f64,
) -> Option<(Solid<Point3, C, S>, History)> {
let (solid, history) = boolean(solid0, solid1, tol, |[and, _]| and)?;
Some((solid, history?))
}
/// SolveCraft: OR operation between two solids, with where each face and edge of the result
/// came from (operand 0 is `solid0`, operand 1 is `solid1`). The operands must not share faces
/// or edges.
pub fn or_with_history<C: ShapeOpsCurve<S>, S: ShapeOpsSurface>(
solid0: &Solid<Point3, C, S>,
solid1: &Solid<Point3, C, S>,
tol: f64,
) -> Option<(Solid<Point3, C, S>, History)> {
let (solid, history) = boolean(solid0, solid1, tol, |[_, or]| or)?;
Some((solid, history?))
}
/// Combines the operands' first shells, then folds in their other shells one at a time
/// (`pick` chooses the AND or the OR shell of each pass). The history is `None` when a face or
/// edge of the result was not recorded; the result stands without it.
fn boolean<C: ShapeOpsCurve<S>, S: ShapeOpsSurface>(
solid0: &Solid<Point3, C, S>,
solid1: &Solid<Point3, C, S>,
tol: f64,
pick: fn([Shell<Point3, C, S>; 2]) -> Shell<Point3, C, S>,
) -> Option<(Solid<Point3, C, S>, Option<History>)> {
let mut lineage = Lineage::new([solid0, solid1]);
let mut iter0 = solid0.boundaries().iter();
let mut iter1 = solid1.boundaries().iter();
let shell0 = iter0.next()?;
let shell1 = iter1.next()?;
let [_, mut or_shell] = process_one_pair_of_shells(shell0, shell1, tol)?;
for shell in iter0 {
let [_, res] = process_one_pair_of_shells(&or_shell, shell, tol)?;
or_shell = res;
let mut shell = pick(crate::tr!(process_one_pair_of_shells(shell0, shell1, tol, &mut lineage), "first shells"));
for other in iter0.chain(iter1) {
shell = pick(crate::tr!(process_one_pair_of_shells(&shell, other, tol, &mut lineage), "another shell"));
}
for shell in iter1 {
let [_, res] = process_one_pair_of_shells(&or_shell, shell, tol)?;
or_shell = res;
}
let boundaries = components_in_order(&or_shell);
Solid::try_new(boundaries).ok()
let solid = crate::tr!(Solid::try_new(components_in_order(&shell)).ok(), "result solid");
let history = lineage.history(&solid);
Some((solid, history))
}
// SolveCraft: truck's connected components come out in hash order of face addresses, so the
+129
View File
@@ -19,3 +19,132 @@ fn punched_cube() {
let file = std::fs::File::create("punched-cube.obj").unwrap();
obj::write(&poly, file).unwrap();
}
// SolveCraft: boolean history.
use crate::{EdgeOrigin, FaceIndex, History};
fn cuboid(origin: Point3, size: Vector3) -> Solid {
let v = builder::vertex(origin);
let e = builder::tsweep(&v, size.x * Vector3::unit_x());
let f = builder::tsweep(&e, size.y * Vector3::unit_y());
builder::tsweep(&f, size.z * Vector3::unit_z())
}
/// Distinct edges in order of first use along the faces.
fn edges_in_order(solid: &Solid) -> Vec<Edge> {
let mut seen = std::collections::HashSet::new();
solid.face_iter().flat_map(Face::edge_iter).filter(|e| seen.insert(e.id())).collect()
}
fn on_surface(surface: &Surface, pt: Point3, tol: f64) -> bool {
surface.search_nearest_parameter(pt, None, 100).is_some_and(|(u, v)| surface.subs(u, v).distance(pt) < tol)
}
fn on_curve(curve: &Curve, pt: Point3, tol: f64) -> bool {
curve.search_nearest_parameter(pt, None, 100).is_some_and(|t| curve.subs(t).distance(pt) < tol)
}
/// Checks `history` against the geometry of `result` and the operands; returns how many result
/// faces are kept operand faces and the intersection edges.
fn check(result: &Solid, operands: [&Solid; 2], history: &History, tol: f64) -> (usize, Vec<[FaceIndex; 2]>) {
let operand_faces = operands.map(|s| s.face_iter().collect::<Vec<_>>());
let face = |f: FaceIndex| operand_faces[f.operand][f.face];
assert_eq!(history.faces.len(), result.face_iter().count());
for (out, origin) in result.face_iter().zip(&history.faces) {
let src = face(origin.source);
assert_eq!(out.orientation(), src.orientation());
assert_eq!(out.id() == src.id(), origin.kept);
assert!(out.vertex_iter().all(|v| on_surface(&src.surface(), v.point(), tol)));
}
let edges = edges_in_order(result);
assert_eq!(history.edges.len(), edges.len());
let operand_edges = operands.map(edges_in_order);
let mut meets = Vec::new();
for (out, origin) in edges.iter().zip(&history.edges) {
let ends = [out.front().point(), out.back().point()];
match *origin {
EdgeOrigin::Edge { operand, edge } => {
let src = &operand_edges[operand][edge];
assert!(ends.iter().all(|&p| on_curve(&src.curve(), p, tol)));
}
EdgeOrigin::Meet { a, b } => {
assert!(a < b);
for f in [a, b] {
assert!(ends.iter().all(|&p| on_surface(&face(f).surface(), p, tol)));
}
meets.push([a, b]);
}
}
}
(history.faces.iter().filter(|o| o.kept).count(), meets)
}
#[test]
fn box_union_box_history() {
let a = cuboid(Point3::origin(), Vector3::new(1.0, 1.0, 1.0));
let b = cuboid(Point3::new(0.5, 0.25, 0.25), Vector3::new(1.0, 0.5, 0.5));
let (or, history) = crate::or_with_history(&a, &b, 0.05).unwrap();
let (kept, meets) = check(&or, [&a, &b], &history, 0.05);
// a's five faces away from b and b's far end are kept; a's face at x = 1 gets a hole and
// b's four sides are trimmed where they enter a.
assert_eq!(history.faces.len(), 11);
assert_eq!(kept, 6);
let pieces: Vec<_> = history.faces.iter().filter(|o| !o.kept).map(|o| o.source).collect();
assert_eq!(pieces.iter().filter(|f| f.operand == 0).count(), 1);
assert_eq!(pieces.iter().filter(|f| f.operand == 1).count(), 4);
// b's sides meet a's face at x = 1 along four new edges.
assert_eq!(meets.len(), 4);
assert!(meets.iter().all(|[x, y]| x.operand == 0 && y.operand == 1 && *x == pieces[0]));
// a's 12 edges, b's 4 far-end edges and the 4 trimmed edges along x.
assert_eq!(history.edges.len(), 24);
let from_b = history.edges.iter().filter(|o| matches!(o, EdgeOrigin::Edge { operand: 1, .. })).count();
assert_eq!(from_b, 8);
}
#[test]
fn box_minus_cylinder_history() {
let cube = cuboid(Point3::origin(), Vector3::new(1.0, 1.0, 1.0));
let v = builder::vertex(Point3::new(0.5, 0.25, -0.5));
let w = builder::rsweep(&v, Point3::new(0.5, 0.5, 0.0), Vector3::unit_z(), Rad(7.0));
let f = builder::try_attach_plane(&[w]).unwrap();
let mut cylinder = builder::tsweep(&f, Vector3::unit_z() * 2.0);
cylinder.not();
let (cut, history) = crate::and_with_history(&cube, &cylinder, 0.05).unwrap();
let (kept, meets) = check(&cut, [&cube, &cylinder], &history, 0.05);
// The cube's four sides are kept; its top and bottom get holes; only the cylinder's wall
// remains of the tool, trimmed to the cube.
assert_eq!(kept, 4);
assert!(history.faces.iter().filter(|o| o.source.operand == 1).all(|o| !o.kept));
// The holes' rims: the wall meets the top and the bottom.
assert!(!meets.is_empty());
assert!(meets.iter().all(|[x, y]| x.operand == 0 && y.operand == 1));
let rims: std::collections::BTreeSet<_> = meets.iter().map(|[x, _]| *x).collect();
assert_eq!(rims.len(), 2);
}
/// A cube [0, 3]³ with a cubic void [1, 2]³: two shells, the void's faces after the outer ones.
fn hollow_cube() -> Solid {
let mut void = cuboid(Point3::new(1.0, 1.0, 1.0), Vector3::new(1.0, 1.0, 1.0));
void.not();
let outer = cuboid(Point3::origin(), Vector3::new(3.0, 3.0, 3.0));
Solid::new(outer.into_boundaries().into_iter().chain(void.into_boundaries()).collect())
}
#[test]
fn multi_shell_operand_history() {
let hollow = hollow_cube();
// Covers the upper half of the hollow cube: its bottom crosses both shells.
let slab = cuboid(Point3::new(-0.5, -0.5, 1.5), Vector3::new(4.0, 4.0, 2.5));
let (and, history) = crate::and_with_history(&hollow, &slab, 0.05).unwrap();
let (kept, meets) = check(&and, [&hollow, &slab], &history, 0.05);
// The outer top and the void's top (numbered after the outer shell's six faces) are kept.
assert_eq!(kept, 2);
assert!(history.faces.iter().any(|o| o.kept && o.source.operand == 0 && o.source.face >= 6));
// The slab's bottom meets the four outer sides and the void's four sides.
let outer = meets.iter().filter(|[x, _]| x.operand == 0 && x.face < 6).count();
let void = meets.iter().filter(|[x, _]| x.operand == 0 && x.face >= 6).count();
assert_eq!((outer, void), (4, 4), "{meets:?}");
let slab_faces: std::collections::BTreeSet<_> = meets.iter().map(|[_, y]| *y).collect();
assert_eq!(slab_faces.len(), 1);
}