Skip to content
210 changes: 210 additions & 0 deletions solidcore/crates/solidcore-ops/tests/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1122,3 +1122,213 @@ fn a_box_mirrored_into_an_opening_of_a_spline_loft_stays_clear_of_it() {
}
assert!(near(volume(&mirrored()), boxed), "{}", volume(&mirrored()));
}

/// Sets the calling thread's fixes: none, or every one (SolidCraft's set).
fn with_fixes(all: bool) {
if all {
all_fixes();
} else {
session::set_thread_fixes(std::iter::empty::<&str>()).unwrap();
}
}

/// `body` as SolidCraft hands it to the next feature: through its record.
fn round_tripped(body: &Body) -> Body {
let (record, _) = body.to_record().unwrap();
Body::from_record(&record, true).unwrap().0
}

/// solidcraft#12785 (solidcore #13650): a block with a hexagonal pocket through it and a
/// round hole drilled from an end face along the pocket's axis plane, ending exactly at a
/// corner of the pocket, so the hole's bottom meets the two pocket walls along a segment of
/// the corner edge (a solid touching itself along a line). The cut leaves that segment as one
/// edge of four faces; a record round trip (`to_record`, `from_record`: SolidCraft's body
/// between features) writes it as two edges of two faces each, and filling the pocket with
/// the same hexagon as a join failed there (`topology_intersector::p1::relate`, status 1001,
/// or a result that is not a closed solid). Without the hole, or without the round trip, the
/// same join works. Carried from bherbruck's
/// `boolean_join_fills_pocket_with_hole_at_its_corner.rs`.
#[allow(clippy::expect_used, clippy::panic)] // test helpers
mod pocket_with_hole_at_its_corner {
use std::f64::consts::TAU;

use solidcore_ops::boolean::{boolean, BooleanOp};
use solidcore_ops::sweep::{extrude, Curve, DraftCorner};
use solidcore_ops::{Body, CheckLevel};

use super::{round_tripped, with_fixes};

/// The hexagon's circumradius.
const R: f64 = 6.3685;
const Z: [f64; 3] = [0.0, 0.0, 1.0];

fn line(start: [f64; 3], end: [f64; 3]) -> Curve {
Curve::Line { start, end }
}

/// The regular hexagon of circumradius `R` about the origin with a corner on +X, at z = 0
/// (the reporter's sketch: the corner at (R, 0), the next at (R/2, R sqrt(3)/2)).
fn hexagon() -> Vec<Curve> {
const H: f64 = 5.515_282_784_001_197;
let corners = [
[R, 0.0, 0.0],
[R / 2.0, H, 0.0],
[-R / 2.0, H, 0.0],
[-R, 0.0, 0.0],
[-R / 2.0, -H, 0.0],
[R / 2.0, -H, 0.0],
];
(0..6)
.map(|i| line(corners[i], corners[(i + 1) % 6]))
.collect()
}

/// The block 100.29 x 32 x 10.
fn block() -> Body {
extrude(
&[
line([-50.145, -16.0, 0.0], [50.145, -16.0, 0.0]),
line([50.145, -16.0, 0.0], [50.145, 16.0, 0.0]),
line([50.145, 16.0, 0.0], [-50.145, 16.0, 0.0]),
line([-50.145, 16.0, 0.0], [-50.145, -16.0, 0.0]),
],
Z,
10.0,
0.0,
DraftCorner::Curvature,
true,
)
.unwrap()
}

/// The hexagon extruded through the block's height: the pocket's cutter and the join's
/// tool.
fn tool() -> Body {
extrude(&hexagon(), Z, 10.0, 0.0, DraftCorner::Curvature, true).unwrap()
}

/// The block with the pocket through it and the hole: 2.5 mm across, along -X from the
/// end face x = 50.145, 43.7765 deep, so it ends at the pocket's corner (R, 0).
fn target() -> Body {
let pocketed = boolean(block(), tool(), BooleanOp::Subtract)
.unwrap()
.expect("the pocketed block");
let hole = extrude(
&[Curve::Arc {
center: [50.145, 0.0, 5.0],
axis: [-1.0, 0.0, 0.0],
reference: [0.0, 1.0, 0.0],
radius: 1.25,
start: 0.0,
end: TAU,
}],
[-1.0, 0.0, 0.0],
50.145 - R,
0.0,
DraftCorner::Curvature,
true,
)
.unwrap();
boolean(pocketed, hole, BooleanOp::Subtract)
.unwrap()
.expect("the drilled block")
}

/// Joins `target` and the hexagon, checks the result and compares its volume with the
/// sum.
fn join_fills(target: Body, what: &str) {
let want = target.volume(1e-9).unwrap() + tool().volume(1e-9).unwrap();
let b = match boolean(target, tool(), BooleanOp::Union) {
Ok(Some(b)) => b,
Ok(None) => panic!("{what}: no body"),
Err(e) => panic!("{what}: {e}"),
};
let report = b.check(CheckLevel::All).unwrap();
assert!(report.is_valid(), "{what}:\n{}", report.text);
let got = b.volume(1e-9).unwrap();
assert!(
(got - want).abs() <= 1e-6 * want,
"{what}: volume {got} vs {want}"
);
}

/// The cut's own body: the corner segment is one edge of four faces, and the join works.
#[test]
fn filling_a_pocket_whose_corner_a_hole_reaches_joins() {
let target = target();
assert_eq!(target.counts().unwrap(), [15, 37, 24]);
join_fills(target, "the cut's body");
}

/// The same body after its record round trip: the corner segment is two coincident edges
/// of two faces each, and the join works as well, with no fixes and with every fix.
#[test]
fn filling_a_pocket_whose_corner_a_hole_reaches_joins_after_a_record_round_trip() {
for all in [false, true] {
with_fixes(all);
let target = round_tripped(&target());
assert_eq!(target.counts().unwrap(), [15, 38, 24]);
assert!(target.check(CheckLevel::All).unwrap().is_valid());
join_fills(target, &format!("the round-tripped body, all fixes {all}"));
}
}

/// The control: without the hole the same join works, round trip or not.
#[test]
fn filling_a_pocket_without_the_hole_joins() {
let pocketed = boolean(block(), tool(), BooleanOp::Subtract)
.unwrap()
.expect("the pocketed block");
join_fills(round_tripped(&pocketed), "the pocketed block");
}
}

/// Two unit boxes touching along the edge x = y = 1 in one body (two solids), after a record
/// round trip (`split_shared` writes the edge once per solid, so the operand carries it as two
/// coincident edges, which the boolean glues back into the one edge the kernel's union shares).
/// Booleans on it give the exact volumes, whether the tool reaches the shared edge or not,
/// with no fixes and with every fix: subtract `[0.5, 1.5]^2 x [0, 0.5]` (an eighth of a unit
/// from each box, across the edge) leaves 1.75; intersect `[0.5, 1.5]^2 x [0, 1]` keeps 0.5;
/// union with a far unit box gives 3.
#[test]
fn two_solids_touching_along_an_edge_survive_a_round_trip_and_booleans() {
for all in [false, true] {
with_fixes(all);
let touching = || {
let a = make_box([0.0, 0.0, 0.0], [1.0, 1.0, 1.0]).unwrap();
let b = make_box([1.0, 1.0, 0.0], [1.0, 1.0, 1.0]).unwrap();
round_tripped(&boolean(a, b, BooleanOp::Union).unwrap().unwrap())
};
let cases = [
(
BooleanOp::Subtract,
make_box([0.5, 0.5, 0.0], [1.0, 1.0, 0.5]).unwrap(),
1.75,
),
(
BooleanOp::Intersect,
make_box([0.5, 0.5, 0.0], [1.0, 1.0, 1.0]).unwrap(),
0.5,
),
(
BooleanOp::Union,
make_box([5.0, 0.0, 0.0], [1.0, 1.0, 1.0]).unwrap(),
3.0,
),
];
for (op, tool, want) in cases {
let r = boolean(touching(), tool, op).unwrap().unwrap();
let report = r.check(CheckLevel::All).unwrap();
assert!(
report.is_valid(),
"all fixes {all}, {op:?}:\n{}",
report.text
);
assert!(
near(volume(&r), want),
"all fixes {all}, {op:?}: {} vs {want}",
volume(&r)
);
}
}
}
Loading
Loading