fix: sketch.intersect keeps a small round between short walls (#9962)

- section() kept the loose fit whenever it had fewer curves, even when it had merged 2 mm walls and an R2 round into one R2.85 arc that misses the walls, leaving the outline open
- The tight fit is now also kept when the loose one strays from the exact crossings (on planes and rulings) and the tight one does not; capped work, other curves count as near
- Test: a box with R2 corner rounds and four tapered grooves stopping 2 mm short, cut at Y=20

Co-authored-by: P'tite Tête <asso@ptitetete.com>
This commit is contained in:
P'tite Tête
2026-11-04 21:27:05 +00:00
committed by GitHub
parent 4ab1640dc8
commit 315d8cfeb7
2 changed files with 71 additions and 1 deletions
+30 -1
View File
@@ -1619,7 +1619,36 @@ pub(crate) fn section(plane: &Plane, m: &Mesh, face: Option<u32>) -> Vec<LinkGeo
// where they are) are kept when they take no more curves than the loose fit.
let close = refine(fit_segments(&segs, size, true));
let plain = |v: &[LinkGeom]| v.iter().all(|g| matches!(g, LinkGeom::Line(..) | LinkGeom::Arc { .. } | LinkGeom::Circle(..)));
if plain(&close) && close.len() <= lossy.len() { close } else { lossy }
// A loose fit with fewer curves can still be wrong: short faces beside a small round (2 mm
// walls each side of an R2 corner) fit within its tolerance as one larger arc. The exact
// crossings (on planes and rulings) show it: one of them off every curve of the fit.
// Any other curve counts as near, and past a work limit nothing is checked: the loose fit
// stands, as before.
let tight = size * 1e-4;
let checkable = exact.len().saturating_mul(close.len()) <= 4_000_000;
let strays = |v: &[LinkGeom]| exact.iter().any(|p| !v.iter().any(|g| plain_distance(g, *p).is_none_or(|d| d <= tight)));
if plain(&close) && (close.len() <= lossy.len() || (checkable && strays(&lossy) && !strays(&close))) { close } else { lossy }
}
/// How far `p` lies from a line, arc or circle; None for any other curve.
fn plain_distance(g: &LinkGeom, p: Vec2) -> Option<f64> {
match *g {
LinkGeom::Line(a, b) => {
let d = b - a;
let t = if d.len2() > 0.0 { ((p - a).dot(d) / d.len2()).clamp(0.0, 1.0) } else { 0.0 };
Some(p.dist(a.lerp(b, t)))
}
LinkGeom::Circle(c, r) => Some((p.dist(c) - r).abs()),
LinkGeom::Arc { c, a, b } => {
let (s0, sw) = ((a - c).angle(), ((b - c).angle() - (a - c).angle()).rem_euclid(std::f64::consts::TAU));
if ((p - c).angle() - s0).rem_euclid(std::f64::consts::TAU) <= sw + 1e-9 {
Some((p.dist(c) - c.dist(a)).abs())
} else {
Some(p.dist(a).min(p.dist(b)))
}
}
_ => None,
}
}
/// The segments where the plane crosses the mesh's triangles (in plane coordinates, each with
@@ -1395,6 +1395,47 @@ fn whole_body_intersect_of_a_filleted_plate_keeps_exact_lines_and_arcs() {
assert!(arcs.iter().all(|r| (r - 2.).abs() < 5e-3), "{arcs:?}");
}
#[test]
fn whole_body_intersect_keeps_a_small_round_between_short_walls() {
use solidcraft_sketch::CurveKind;
// A tapered groove on each side stops 2 mm short of the R2 corner rounds: walls of 2 mm, a
// round, walls of 2 mm, which a loose fit takes for one larger arc that misses the walls.
let mut s = Session::default();
run(&mut s, "solid.box", json!({"length": 150, "width": 55, "height": 150, "corner": [-75, 0, -75], "body_name": "Part"}));
run(&mut s, "solid.fillet", json!({"edges": [[75, 27.5, 75], [-75, 27.5, 75], [75, 27.5, -75], [-75, 27.5, -75]], "radius": 2}));
for (plane, offset, direction, rect) in [
("YZ", 75, "negative", [[7, -71], [48, 71]]),
("YZ", -75, "positive", [[7, -71], [48, 71]]),
("XY", 75, "negative", [[-71, 7], [71, 48]]),
("XY", -75, "positive", [[-71, 7], [71, 48]]),
] {
run(&mut s, "sketch.create", json!({"plane": plane, "offset": offset}));
run(&mut s, "sketch.rectangle.two_point", json!({"p0": rect[0], "p1": rect[1]}));
run(&mut s, "sketch.finish", json!({}));
run(&mut s, "solid.extrude", json!({"distance": 6, "direction": direction, "taper": "-56.31 deg", "operation": "cut", "targets": ["Part"]}));
}
let sketch = run(&mut s, "sketch.create", json!({"plane": "XZ", "offset": 20, "project_edges": false}))["sketch"].as_u64().unwrap();
run(&mut s, "sketch.intersect", json!({"refs": [{"body": "Part"}]}));
let inspect = run(&mut s, "sketch.inspect", json!({}));
let state = s.model.state();
let sk = &state.sketch(sketch).unwrap().sketch;
let pos = |i: usize| sk.points[i].pos;
let arcs: Vec<f64> = sk
.curves
.iter()
.filter_map(|c| match c.kind {
CurveKind::Arc { c, a, .. } => Some(pos(c).dist(pos(a))),
_ => None,
})
.collect();
assert_eq!(arcs.len(), 4, "{arcs:?}");
assert!(arcs.iter().all(|r| (r - 2.).abs() < 5e-3), "{arcs:?}");
// Square less the corners' (4 - pi) r^2 and four trapezoid grooves (142 + 124) / 2 * 6.
assert_eq!(inspect["profiles"].as_array().map(Vec::len), Some(1), "one closed outline");
let area = inspect["profiles"][0]["area_mm2"].as_f64().unwrap();
let expected = 150. * 150. - (4. - std::f64::consts::PI) * 4. - 4. * 798.;
assert!((area - expected).abs() < 0.5, "{area} vs {expected}");
}
#[test]
fn a_rebuilt_projection_keeps_user_constraints_without_a_warning() {
for dimensioned in [false, true] {
let mut s = Session::default();