From 96509412e9718a38efeb32565f67b3a22cb5ac7d Mon Sep 17 00:00:00 2001 From: dudedagger42 <76926918+dudedagger42@users.noreply.github.com> Date: Mon, 2 Nov 2026 02:01:42 +0000 Subject: [PATCH] Extrude: a Cut To the far side of a curved wall takes the wall (#8634) A Cut To Object on a curved face ran the tool through the target and kept the tool less the body, the piece at the start. From outside a curved wall to its inside face that piece is the air before the wall: the cut took nothing, or the tool lay in the curved face and the boolean failed on the coincident faces. Where the target's material ends at the picked face under the whole profile (rays over the profile cross it once, material before, none after), the tool now ends flat halfway across the gap beyond the face. It takes what lies before the face and meets no face of the body. Otherwise the trim is as before. Co-authored-by: Claude Opus 5.5 --- crates/doc/src/eval.rs | 88 ++++++++++++++++++++++++++++++- crates/engine/src/extent_tests.rs | 53 +++++++++++++++++++ 2 files changed, 140 insertions(+), 1 deletion(-) diff --git a/crates/doc/src/eval.rs b/crates/doc/src/eval.rs index 60984be81..8533da1e7 100644 --- a/crates/doc/src/eval.rs +++ b/crates/doc/src/eval.rs @@ -5488,7 +5488,26 @@ fn feature_tools_with_constructor( let (mut lo, mut hi) = if a <= b { (a, b) } else { (b, a) }; // To a curved (or slanted) face: run right through the target body, then keep what // lies before its surface (the tool less the body, the piece at the start). - let curved = curved_face(&tst, to_face, Some(n)); + let mut curved = curved_face(&tst, to_face, Some(n)); + // A Cut to the far side of a curved wall (a window through a duct's side): ending the + // tool on that face leaves it lying in the wall's face, where the boolean fails, and + // the tool less the body keeps only the air before the wall, which cuts nothing. Where + // the target's material ends at the face under the whole profile, the tool ends flat + // in the gap beyond it instead: it takes what lies before the face and meets no face. + if *operation == Operation::Cut + && off == 0.0 + && let (Some(bi), Some((_, fi))) = (curved, to_face) + && let Some(tb) = tst.bodies.get(bi) + && let Some(end) = cut_end_past_face(tb, fi, &ss.plane, ®ions, if hi > 0.0 { 1.0 } else { -1.0 }) + && (if hi > 0.0 { end > lo } else { end < hi }) + { + if hi > 0.0 { + hi = end; + } else { + lo = end; + } + curved = None; + } if let Some(bi) = curved && let Some(tb) = tst.bodies.get(bi) { @@ -7135,6 +7154,73 @@ fn trim_to_body(tools: Vec, target: &ModelBody, dist_to_start: impl Fn(V Ok(out) } +/// For a Cut To the curved face `fi` of `target`: the height above `plane` (along its normal) at +/// which a flat-ended tool over `regions`, travelling along `travel` × the normal (±1), stops in +/// the gap beyond that face. Rays over the profiles must each cross the face once, with material +/// just before it and none just after; the end is halfway across the narrowest gap to the next +/// material (at most the usual run past a curved target). `None` when that does not hold. +fn cut_end_past_face(target: &ModelBody, fi: usize, plane: &Plane, regions: &[Region2], travel: f64) -> Option { + let m = target.mesh(); + let n = plane.normal(); + let dir = n * travel; + let size = target.body.size(); + let eps = (size * 1e-4).max(1e-6); + let margin = size * 0.05 + 1.0; + // Sample points: the outline of each profile and a grid over its inside. + let mut samples: Vec = Vec::new(); + for r in regions { + let ring: Vec = r.outer.polyline(1e-2).into_iter().collect(); + let step = (ring.len() / 64).max(1); + samples.extend(ring.iter().step_by(step).copied()); + let (mut lo, mut hi) = (Vec2::new(f64::MAX, f64::MAX), Vec2::new(f64::MIN, f64::MIN)); + for q in &ring { + lo = Vec2::new(lo.x.min(q.x), lo.y.min(q.y)); + hi = Vec2::new(hi.x.max(q.x), hi.y.max(q.y)); + } + for i in 0..12 { + for j in 0..12 { + let q = Vec2::new(lo.x + (hi.x - lo.x) * (i as f64 + 0.5) / 12.0, lo.y + (hi.y - lo.y) * (j as f64 + 0.5) / 12.0); + if r.contains(q) { + samples.push(q); + } + } + } + } + let height = |q: Vec3| (q - plane.origin).dot(n); + let reach = size * 4.0 + 1.0; + let (mut face_end, mut gap_end) = (f64::MIN, f64::MAX); + for q2 in samples { + let q = plane.to_world(q2); + // Every crossing of the line through `q`, in travel order, from well before the body. + let back = travel * height(q) + reach + size; + let mut o = q - dir * back; + let mut hits: Vec<(f64, usize)> = Vec::new(); + while let Some((t, tri)) = m.raycast(o, dir) { + let at = o + dir * t; + hits.push((travel * height(at), *m.tri_face.get(tri)? as usize)); + o = at + dir * eps; + if hits.len() > 256 { + return None; + } + } + let on_face: Vec = (0..hits.len()).filter(|&k| hits[k].1 == fi).collect(); + let [k] = on_face[..] else { return None }; + let e = hits[k].0; + let at = |t: f64| plane.origin + n * (travel * t) + (q - plane.origin - n * height(q)); + if !m.contains(at(e - eps * 10.0)) || m.contains(at(e + eps * 10.0)) { + return None; + } + face_end = face_end.max(e); + let next = hits.iter().map(|h| h.0).filter(|&t| t > e + eps * 10.0).fold(f64::MAX, f64::min); + gap_end = gap_end.min(next); + } + if face_end == f64::MIN || gap_end - face_end < eps * 100.0 { + return None; + } + let end = face_end + ((gap_end - face_end) * 0.5).min(margin); + Some(travel * end) +} + /// One point on each face of a body (a triangle centre, so it lies on the face). fn face_samples(b: &ModelBody) -> Vec { let m = b.mesh(); diff --git a/crates/engine/src/extent_tests.rs b/crates/engine/src/extent_tests.rs index 852a271ef..8a836ce2d 100644 --- a/crates/engine/src/extent_tests.rs +++ b/crates/engine/src/extent_tests.rs @@ -181,3 +181,56 @@ fn extrude_to_a_cylinder_beside_another_body() { assert!((end + (2.75f64 * 2.75 - 1.5 * 1.5).sqrt()).abs() < 1e-3, "{b}"); assert!((b["bbox"]["min"][0].as_f64().unwrap_or(0.0) + 98.0).abs() < 1e-6, "{b}"); } + +/// A window cut To Object through the side of a tube (r 20 outside, 18 inside), up to the inside +/// face: it takes the wall under the profile and nothing else. Before, the tool ended in the +/// curved inner face: the tool less the tube kept only the air before the wall and cut nothing, +/// or the boolean failed on the coincident curved faces. Picking the outside face from inside +/// the wall cuts back to it. +#[test] +fn cut_to_the_far_side_of_a_curved_wall() { + let tube = || { + let mut s = Session::default(); + run(&mut s, "solid.cylinder", json!({"radius": 20, "height": 20, "body_name": "Tube"})); + run(&mut s, "sketch.create", json!({"plane": "XY", "name": "Bore"})); + run(&mut s, "sketch.circle.center", json!({"center": [0, 0], "radius": 18})); + run(&mut s, "sketch.finish", json!({})); + run(&mut s, "solid.extrude", json!({"sketch": "Bore", "distance": 20, "operation": "cut"})); + s + }; + let volume = |s: &mut Session| body(s, "Tube")["volume_mm3"].as_f64().unwrap_or(0.0); + let wall = |x: f64, r: f64| (r * r - x * x).sqrt(); + let n = 2000; + let between = |r0: f64, r1: f64| -> f64 { + (0..n).map(|i| -3.0 + 6.0 * (i as f64 + 0.5) / n as f64).map(|x| (wall(x, r1) - wall(x, r0)) * 4.0 * 6.0 / n as f64).sum() + }; + // From outside the tube, through the wall, to its inside face. + let mut s = tube(); + let whole = volume(&mut s); + run(&mut s, "sketch.create", json!({"plane": {"origin": [0, -30, 0], "x_dir": [1, 0, 0], "y_dir": [0, 0, 1]}, "name": "Window"})); + run(&mut s, "sketch.rectangle.two_point", json!({"p0": [-3, 8], "p1": [3, 12]})); + run(&mut s, "sketch.finish", json!({})); + run(&mut s, "solid.extrude", json!({"sketch": "Window", "to": [0, -18, 10], "operation": "cut"})); + let removed = whole - volume(&mut s); + assert!(s.model.results.iter().all(|r| r.error.is_none()), "{:?}", s.model.results); + // The same window cut 20 mm, ending in the bore: the tube's own facets set its volume. + let mut plain = tube(); + run(&mut plain, "sketch.create", json!({"plane": {"origin": [0, -30, 0], "x_dir": [1, 0, 0], "y_dir": [0, 0, 1]}, "name": "Window"})); + run(&mut plain, "sketch.rectangle.two_point", json!({"p0": [-3, 8], "p1": [3, 12]})); + run(&mut plain, "sketch.finish", json!({})); + run(&mut plain, "solid.extrude", json!({"sketch": "Window", "distance": -20, "operation": "cut"})); + let reference = whole - volume(&mut plain); + assert!((removed - reference).abs() < reference * 1e-6, "removed {removed}, a 20 mm cut {reference}"); + let want = between(18.0, 20.0); + assert!((removed - want).abs() < want * 2e-2, "removed {removed}, want {want}"); + // From inside the wall (y = -19) back out to the outside face. + let mut s = tube(); + run(&mut s, "sketch.create", json!({"plane": {"origin": [0, -19, 0], "x_dir": [1, 0, 0], "y_dir": [0, 0, 1]}, "name": "Window"})); + run(&mut s, "sketch.rectangle.two_point", json!({"p0": [-3, 8], "p1": [3, 12]})); + run(&mut s, "sketch.finish", json!({})); + run(&mut s, "solid.extrude", json!({"sketch": "Window", "to": [0, -20, 10], "operation": "cut"})); + let removed = whole - volume(&mut s); + let want: f64 = (0..n).map(|i| -3.0 + 6.0 * (i as f64 + 0.5) / n as f64).map(|x| (wall(x, 20.0) - 19.0) * 4.0 * 6.0 / n as f64).sum(); + assert!((removed - want).abs() < want * 2e-2, "removed {removed}, want {want}"); + assert!(s.model.results.iter().all(|r| r.error.is_none()), "{:?}", s.model.results); +}