Files
makeCAD/crates/kernel/src/whole_face_tests.rs
T
Yeonghun Lee 316b49cd84 Extrude from a whole face rounded by fillets: join, and cuts whose walls only touch (#2040) (#2041)
* Extrude: a join from a whole face rounded by fillets sweeps the face itself (#2040)

A face's sketch fits an edge that is no line or arc (the quarter-ellipses
where fillets meet a slanted face) as splines a hair off the body's edges,
so a join from the face touched the body along a face it did not match and
failed with "boolean Union: no result" after minutes. When the profile is
a whole planar face of the one target, the join now sweeps the body's own
face, which shares its edges and is sewn on along it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Extrude: a cut into a whole face keeps the walls that cut and moves the others off (#2040)

A cut from a whole planar face sweeps walls from each of its edges. Where
the face beside an edge turns away from the sweep (n_N . -n_P >= 0) the wall
only touches the body along that edge, or lies in the face beside it, and
the boolean failed on it with "no result". The cut now keeps the walls that
go into the body, exactly (the body's own edges), runs the outline round
the others well outside the body, and starts a little out from the face.
A cut whose walls all only touch takes a slab beyond its depth plane.

Not covered: a cutting wall tangent to the face beside a touching one (a
round's wall meeting an end face, as in the issue's part); the boolean
still fails there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Extrude: leave a whole face outlined by lines and arcs to the profile's own tool

Only a face whose outline the projection had to fit needs the body's own face as the tool. A square drawn over a box's top already meets the body exactly, and the swept face lost the names a sketch on the new top attaches to (stacked_face_tests, #489).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Run the whole cut tool from a face under the kernel guard (#2040)

Review: `builder::vertex`, `builder::line`, `Face::try_new` and
`builder::translated` ran outside `crate::guard`; only the sweep was
guarded. `cut_tool` now runs all of it under one guard.

Adds an end-to-end test of a cut into a face the body stands out past
below it (the block's top strip between its rounds): it takes only
what lies over the face.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test: the slab cut of whole_face_tool, on a spline prism (#2040)

A prism on a closed spline standing on a wider plate, cut 3 into its whole
top face: the slab takes the face's area times the depth and leaves the
plate alone. A test-only counter asserts the slab was the tool, so the test
cannot pass through the profile's own tool. Shortens the name of the
rounded block's top-face cut test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Kernel: the face's outline as a Vec now that polyline returns a closed chain (#2040)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-21 00:41:41 +00:00

111 lines
5.8 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
use solidcraft_geom::{Loop2, Plane, Region2, Seg2, Vec2, Vec3};
use super::*;
use crate::{BoolOp, boolean, box_solid, measure};
fn square(a: Vec2, b: Vec2) -> Region2 {
let p = Vec2::new;
let line = |a: Vec2, b: Vec2| Seg2::Line { a, b };
Region2 { outer: Loop2 { segs: vec![line(a, p(b.x, a.y)), line(p(b.x, a.y), b), line(b, p(a.x, b.y)), line(p(a.x, b.y), a)] }, holes: vec![] }
}
#[test]
fn a_profile_matches_the_face_it_outlines_and_no_other() {
let block = box_solid(Vec3::ZERO, Vec3::new(20.0, 10.0, 5.0)).unwrap();
let top = Plane { origin: Vec3::new(0.0, 0.0, 5.0), ..Plane::XY };
let whole = matching_face(&block, &top, &square(Vec2::new(0.0, 0.0), Vec2::new(20.0, 10.0)), &[0.0]).unwrap();
assert!((whole.normal - Vec3::Z).len() < 1e-9 && whole.height.abs() < 1e-9, "{whole:?}");
// A smaller rectangle on the same face is not the face.
assert!(matching_face(&block, &top, &square(Vec2::new(1.0, 1.0), Vec2::new(19.0, 9.0)), &[0.0]).is_none());
// Nor is the bottom face, though its outline is the same.
assert!(matching_face(&block, &top, &square(Vec2::ZERO, Vec2::new(20.0, 10.0)), &[-3.0]).is_none());
}
#[test]
fn a_cut_over_the_whole_top_takes_only_the_face_and_a_step_beside_it_does_not() {
let block = box_solid(Vec3::ZERO, Vec3::new(20.0, 10.0, 5.0)).unwrap();
let top =
matching_face(&block, &Plane { origin: Vec3::new(0.0, 0.0, 5.0), ..Plane::XY }, &square(Vec2::ZERO, Vec2::new(20.0, 10.0)), &[0.0]).unwrap();
assert!(cut_takes_only_the_face(&block, top, 2.0));
// A step standing beside the top face: a slab would take it too.
let step = box_solid(Vec3::new(20.0, 0.0, 0.0), Vec3::new(30.0, 10.0, 8.0)).unwrap();
let stepped = boolean(&block, &step, BoolOp::Union).unwrap().unwrap();
let face = matching_face(&stepped, &Plane { origin: Vec3::new(0.0, 0.0, 5.0), ..Plane::XY }, &square(Vec2::ZERO, Vec2::new(20.0, 10.0)), &[0.0])
.unwrap();
assert!(!cut_takes_only_the_face(&stepped, face, 2.0));
}
#[test]
fn a_prism_from_the_face_joins_by_its_shared_edges() {
let block = box_solid(Vec3::ZERO, Vec3::new(20.0, 10.0, 5.0)).unwrap();
let plane = Plane { origin: Vec3::new(0.0, 0.0, 5.0), ..Plane::XY };
let region = square(Vec2::ZERO, Vec2::new(20.0, 10.0));
let face = matching_face(&block, &plane, &region, &[0.0]).unwrap();
let built = crate::extrude(&plane, std::slice::from_ref(&region), 0.0, 3.0).unwrap().remove(0);
let tool = face_prism(&block, face, 3.0, true, &built).unwrap();
assert!(tool.history().is_some_and(|h| h.operands == 1));
let joined = boolean(&block, &tool, BoolOp::Union).unwrap().unwrap();
assert!((measure(&joined).unwrap().volume - 20.0 * 10.0 * 8.0).abs() < 1e-6);
}
/// The block of #2040: one side at 45°, its top (and, with `bottom`, its bottom) rounded 3.8 mm
/// at both ends.
fn rounded_block(bottom: bool) -> Body {
let p = Vec2::new;
let line = |a: Vec2, b: Vec2| Seg2::Line { a, b };
let h = 8.485281374238571;
let segs = vec![
line(p(0.0, 0.0), p(11.514718625761429, 0.0)),
line(p(11.514718625761429, 0.0), p(20.0, h)),
line(p(20.0, h), p(0.0, h)),
line(p(0.0, h), p(0.0, 0.0)),
];
let plane = Plane { origin: Vec3::ZERO, x: Vec3::X, y: Vec3::Z };
let block = crate::extrude(&plane, &[Region2 { outer: Loop2 { segs }, holes: vec![] }], 0.0, 10.0).unwrap().remove(0);
let mut edges = vec![Vec3::new(10.0, -10.0, h), Vec3::new(10.0, 0.0, h)];
if bottom {
edges.extend([Vec3::new(5.757359312880714, 0.0, 0.0), Vec3::new(5.757359312880714, -10.0, 0.0)]);
}
crate::fillet(&block, &edges, 3.8).unwrap()
}
fn slanted_face(b: &Body) -> WholeFace {
let n = Vec3::new(1.0, 0.0, -1.0).normalized().unwrap();
let face = b.solid.face_iter().position(|f| crate::blend::plane_normal(f).is_some_and(|m| m.dot(n) > 1.0 - 1e-9)).unwrap();
WholeFace { face, normal: n, height: 0.0 }
}
/// Into the slanted face, the walls from its top edge and the rounds beside it leave the body
/// (n_N · −n_P = +0.707 down to 0), those from the end faces lie in them (0), and those from the
/// bottom edge and its rounds go into it (−0.707 up to 0): the bottom ones are kept, the others
/// moved off.
#[test]
fn the_walls_into_a_slanted_face_touch_at_the_top_and_cut_at_the_bottom() {
let b = rounded_block(true);
let face = slanted_face(&b);
let mesh = b.tessellate(outline_tol(&b) * 0.2).unwrap();
let walls = walls(&mesh, face.face, face.normal, 1e-6).unwrap();
assert_eq!(walls.iter().filter(|(_, w)| *w == Wall::Cuts).count(), 3, "{walls:?}");
assert_eq!(walls.iter().filter(|(_, w)| *w == Wall::Touches).count(), 5, "{walls:?}");
assert!(!cut_takes_only_the_face(&b, face, 3.0));
assert!(cut_tool(&b, face, 3.0, None).is_some());
}
/// A cut 3 mm into the slanted face of the block rounded along its top: the walls from the top
/// are moved off it, the one from the bottom edge kept. What it takes: in the face's plane, the
/// body's section at depth t is the block's section (10 wide, h high, its top corners rounded)
/// moved down the face by t, so the cut takes √2 ∫₀³ (10 (h − t/√2) − 2 (1 − π/4) r²) dt.
#[test]
fn a_cut_into_a_slanted_face_beside_rounds_takes_what_lies_over_it() {
let b = rounded_block(false);
let face = slanted_face(&b);
let (tool, _) = cut_tool(&b, face, 3.0, None).unwrap();
let cut = boolean(&b, &tool, BoolOp::Cut).unwrap().unwrap();
assert!(cut.validity().is_empty(), "{:?}", cut.validity());
let (h, r, d, w) = (8.485281374238571_f64, 3.8_f64, 3.0_f64, 10.0_f64);
let corner = (1.0 - std::f64::consts::FRAC_PI_4) * r * r;
let taken = 2f64.sqrt() * w * h * d - w * d * d / 2.0 - 2.0 * 2f64.sqrt() * corner * d;
let v = measure(&b).unwrap().volume - measure(&cut).unwrap().volume;
assert!((v - taken).abs() < 0.05, "took {v}, expected {taken}");
}