From 97e0ff9050170ab6b5fc4adede9c65db37154196 Mon Sep 17 00:00:00 2001 From: Kai Evans Date: Sat, 24 Oct 2026 20:46:01 +0000 Subject: [PATCH] Sketch: a face seen edge-on projects as one line end to end (#5375) A face orthogonal to the sketch plane projected every edge onto the same line: coincident and overlapping pieces, one per notch, that the face boundary check then refused on the next evaluation ("boundaries cross, touch or have ambiguous topology") and marked lost. A link layout whose straight pieces all fall on one line is now one line between its two farthest points; vertices along it can be projected one by one. The face-boundary and loop-identity checks skip such a silhouette, which encloses nothing. Coincident lines are laid out once. --- crates/engine/src/cmd/sketch_project_tests.rs | 32 +++++++++++++++++ crates/sketch/src/link.rs | 33 +++++++++++++++-- crates/sketch/src/link_identity_tests.rs | 35 +++++++++++++++++-- 3 files changed, 95 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/cmd/sketch_project_tests.rs b/crates/engine/src/cmd/sketch_project_tests.rs index 376cb3c60..d277073ca 100644 --- a/crates/engine/src/cmd/sketch_project_tests.rs +++ b/crates/engine/src/cmd/sketch_project_tests.rs @@ -677,3 +677,35 @@ fn a_face_sketch_saved_with_fitted_letters_reopens_and_builds_the_same() { sketch.curves.iter().filter(|c| c.link.is_some() && matches!(c.kind, solidcraft_sketch::CurveKind::Spline { control: true, .. })).count(); assert!(pieces > 0, "{pieces}"); } + +#[test] +fn a_face_seen_edge_on_projects_as_its_silhouette_and_follows_edits() { + let mut s = Session::default(); + box_part(&mut s); + run(&mut s, "sketch.create", json!({"plane": "XY", "offset": 30})); + // The +x wall of the 40 x 30 x 20 box, seen from above: one line from (40, 0) to (40, 30). + let r = run(&mut s, "sketch.project", json!({"refs": [{"face": [40, 15, 10]}]})); + let link = r["links"][0]["link"].as_str().unwrap().to_string(); + assert_eq!(ids(&r["links"][0]["curves"]).len(), 1, "{r}"); + let si = inspect(&mut s); + let c = linked_curves(&si).into_iter().find(|c| c["link"] == link.as_str()).unwrap(); + let xs: Vec = [&c["start_at"][0], &c["end_at"][0]].iter().map(|v| v.as_f64().unwrap()).collect(); + assert!(xs.iter().all(|x| (x - 40.0).abs() < 1e-6), "{c}"); + run(&mut s, "sketch.finish", json!({})); + let sketch = s.doc.features.last().unwrap().id; + let lost = |s: &Session| s.model.state().sketch(sketch).unwrap().sketch.link(&link).unwrap().lost; + assert!(!lost(&s), "the silhouette is current after the sketch is evaluated"); + assert!( + s.model.results.iter().all(|r| r.warning.is_none()), + "{:?}", + s.model.results.iter().filter_map(|r| r.warning.clone()).collect::>() + ); + // The wall follows the width. + run(&mut s, "parameters.change", json!({"name": "width", "expression": "50 mm"})); + assert!(!lost(&s), "{:?}", s.model.results.iter().filter_map(|r| r.warning.clone()).collect::>()); + let st = s.model.state(); + let sk = &st.sketch(sketch).unwrap().sketch; + let ci = sk.link_curves(&link)[0]; + let solidcraft_sketch::CurveKind::Line { a, b } = sk.curves[ci].kind else { panic!("not a line") }; + assert!((sk.point(a).unwrap().x - 50.0).abs() < 1e-6 && (sk.point(b).unwrap().x - 50.0).abs() < 1e-6); +} diff --git a/crates/sketch/src/link.rs b/crates/sketch/src/link.rs index 2690f934f..d2b9dc2d1 100644 --- a/crates/sketch/src/link.rs +++ b/crates/sketch/src/link.rs @@ -294,6 +294,8 @@ fn layout(geom: &[LinkGeom]) -> Result { pts.push(p); pts.len() - 1 } + // A face seen edge-on projects its edges onto one another; one line stands for them. + let same_line = |cs: &[LCurve], a: usize, b: usize| cs.iter().any(|k| matches!(*k, LCurve::Line(x, y) if (x, y) == (a, b) || (x, y) == (b, a))); for g in geom.iter().filter(|g| g.finite()) { match g.clone() { LinkGeom::Point(p) => { @@ -304,7 +306,7 @@ fn layout(geom: &[LinkGeom]) -> Result { } LinkGeom::Line(a, b) => { let (a, b) = (pt(&mut l.pts, a), pt(&mut l.pts, b)); - if a != b { + if a != b && !same_line(&l.curves, a, b) { l.curves.push(LCurve::Line(a, b)); } } @@ -356,9 +358,33 @@ fn layout(geom: &[LinkGeom]) -> Result { }; let curves = l.curves.clone(); l.lone.retain(|i| !used(*i, &curves)); + // A face seen edge-on: every edge falls on one line, and one line end to end stands for + // them all (its vertices can be projected one by one). + if l.lone.is_empty() && l.curves.len() > 1 && flat(&l.curves, &l.pts) { + let (a, b) = extent(&l.pts); + l = Layout { pts: vec![a, b], lone: Vec::new(), curves: vec![LCurve::Line(0, 1)] }; + } Ok(l) } +/// The two points farthest apart. +fn extent(pts: &[Vec2]) -> (Vec2, Vec2) { + let farthest = |from: Vec2| pts.iter().copied().max_by(|p, q| p.dist(from).total_cmp(&q.dist(from))).unwrap_or(from); + let a = farthest(pts.first().copied().unwrap_or(Vec2::ZERO)); + (a, farthest(a)) +} + +/// Straight pieces along one line: a face seen edge-on, whose silhouette encloses nothing. +fn flat(curves: &[LCurve], pts: &[Vec2]) -> bool { + if !curves.iter().all(|c| matches!(c, LCurve::Line(..))) { + return false; + } + let (a, b) = extent(pts); + let Some(dir) = (b - a).normalized() else { return true }; + let tol = MERGE_TOL.max(1e-9 * a.dist(b)); + pts.iter().all(|p| (*p - a).cross(dir).abs() <= tol) +} + fn same_shape(a: &LCurve, b: &LCurve) -> bool { matches!( (a, b), @@ -681,6 +707,7 @@ impl Sketch { && lay.curves.len() <= 512 && lay.pts.len() <= 512 && lay.curves.iter().all(|c| matches!(c, LCurve::Line(..) | LCurve::Arc(..) | LCurve::Circle(..))) + && !flat(&lay.curves, &lay.pts) && !loops::valid(&lay.curves, &lay.pts) { self.set_link_lost(id, true); @@ -827,7 +854,9 @@ impl Sketch { }; let loop_roles_required = self.link(id).is_some_and(|l| matches!(l.source, LinkSource::Face { .. })) && cur.iter().chain(&lay.curves).all(|c| matches!(c, LCurve::Line(..) | LCurve::Arc(..) | LCurve::Circle(..))) - && lay.lone.is_empty(); + && lay.lone.is_empty() + && !flat(cur, &old) + && !flat(&lay.curves, &lay.pts); let loop_roles = loop_roles_required.then(|| loops::roles(cur, &old, &lay.curves, &lay.pts)).flatten(); if loop_roles_required && loop_roles.is_none() { self.set_link_lost(id, true); diff --git a/crates/sketch/src/link_identity_tests.rs b/crates/sketch/src/link_identity_tests.rs index bae16d887..543470a64 100644 --- a/crates/sketch/src/link_identity_tests.rs +++ b/crates/sketch/src/link_identity_tests.rs @@ -103,11 +103,12 @@ fn geometric_refresh_updates_conic_and_ellipse_scalar_values() { #[test] fn many_curves_sharing_few_points_do_not_enter_permutation_matching() { let mut sk = Sketch::new(); - let geom = vec![LinkGeom::Line(Vec2::new(1.0, 1.0), Vec2::new(2.0, 2.0)); 513]; + let arc = |c: Vec2| LinkGeom::Arc { c, a: c + Vec2::X, b: c + Vec2::Y }; + let geom = vec![arc(Vec2::new(1.0, 1.0)); 513]; let link = sk.add_link(LinkKind::Project, face(), &geom).unwrap(); - assert_eq!(sk.link_points(&link).len(), 2); + assert_eq!(sk.link_points(&link).len(), 3); let before = sk.clone(); - let shifted = vec![LinkGeom::Line(Vec2::new(3.0, 3.0), Vec2::new(4.0, 4.0)); 513]; + let shifted = vec![arc(Vec2::new(3.0, 3.0)); 513]; assert!(sk.update_link(&link, &shifted).is_err()); assert_eq!(sk.points, before.points); assert_eq!(sk.curves, before.curves); @@ -331,3 +332,31 @@ fn huge_loop_does_not_make_nearby_bowtie_edges_topologically_adjacent() { assert_eq!(sk.curves, saved.curves); assert!(sk.link(&link).unwrap().lost); } + +#[test] +fn edge_on_face_lays_out_as_one_silhouette_line_and_stays_current() { + let mut sk = Sketch::new(); + // A vertical wall seen from above: its top and bottom edges project onto the same segment, + // its vertical edges onto points. + let (a, b) = (Vec2::new(40.0, 0.0), Vec2::new(40.0, 30.0)); + let wall = vec![LinkGeom::Line(a, b), LinkGeom::Line(b, b), LinkGeom::Line(b, a), LinkGeom::Line(a, a)]; + let link = sk.add_link(LinkKind::Project, face(), &wall).unwrap(); + assert_eq!(sk.link_curves(&link).len(), 1); + assert!(!sk.update_link(&link, &wall).unwrap(), "the same silhouette keeps its curve"); + assert!(!sk.link(&link).unwrap().lost); + // A notched wall seen edge-on: overlapping pieces along one line, still one line end to end. + let notched = [(0.0, 12.0), (8.0, 20.0), (18.0, 30.0), (5.0, 25.0)] + .into_iter() + .flat_map(|(y0, y1)| [LinkGeom::Line(Vec2::new(40.0, y0), Vec2::new(40.0, y1)), LinkGeom::Line(Vec2::new(40.0, y1), Vec2::new(40.0, y0))]) + .collect::>(); + assert!(!sk.update_link(&link, ¬ched).unwrap(), "the same line keeps its curve"); + let [ci] = sk.link_curves(&link)[..] else { panic!("one line") }; + let CurveKind::Line { a, b } = sk.curves[ci].kind else { panic!("a line") }; + let ends = [sk.points[a].pos, sk.points[b].pos]; + assert!(ends.contains(&Vec2::new(40.0, 0.0)) && ends.contains(&Vec2::new(40.0, 30.0)), "{ends:?}"); + assert!(!sk.link(&link).unwrap().lost); + // Turned to face the sketch, the wall is its rectangle again. + assert!(sk.update_link(&link, &square(10.0)).unwrap()); + assert_eq!(sk.link_curves(&link).len(), 4); + assert!(!sk.link(&link).unwrap().lost); +}