SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Sketch: a projected face follows its body when the body is resized (#12086) (#12732)
PaddleStroke28fed4d6dd86
diff --git a/crates/sketch/src/link.rs b/crates/sketch/src/link.rsindex d048bbeb80..a68a8fcb3e 100644--- a/crates/sketch/src/link.rs+++ b/crates/sketch/src/link.rs@@ -1078,7 +1078,10 @@ impl Sketch {// thicker moves the inner corner by more than a tenth of the face) is still the same// boundary when two witnesses agree: the new boundary was read in the same order// with the same curves, and each point read in that order is, among the points on- // the same loop with lines the same way, clearly the nearest to where it was.+ // the same loop with lines leaving them the same ways, clearly the nearest to where+ // it was. The two end corners of a thin arm have the same lines; the lines leave them+ // in opposite senses, which tells them apart when the box the points are compared in+ // shrinks with the face (#12086).same_order_agrees = same&& loop_roles.as_ref().is_some_and(|roles| {(0..points.len()).all(|k| {@@ -1090,6 +1093,7 @@ impl Sketch {old_roles.get(k) == fresh_roles.get(*i)&& roles.old.get(k) == roles.fresh.get(*i)&& roles.old_lines.get(k).zip(roles.fresh_lines.get(*i)).is_some_and(|(x, y)| loops::same_lines(x, y))+ && roles.old_leaving.get(k).zip(roles.fresh_leaving.get(*i)).is_some_and(|(x, y)| loops::same_leaving(x, y))}).map(|(i, q)| (i, p.dist(*q))).collect();diff --git a/crates/sketch/src/link_identity_tests.rs b/crates/sketch/src/link_identity_tests.rsindex 31c91d2292..d3d8d9f7c9 100644--- a/crates/sketch/src/link_identity_tests.rs+++ b/crates/sketch/src/link_identity_tests.rs@@ -458,3 +458,60 @@ fn rebuilt_face_keeps_a_users_circle_on_its_projected_centre() {}assert_eq!(sk.link_curves(&link).len(), 4 + 3, "the projection is the new outline and holes");}++/// #12086: the bottom face of a holder, an arm 2 thick along a hole's ring, projected into a+/// sketch. The ring's diameter `d` drives the outline: the arm's end corners are 2 apart, at+/// (55 + s, -9 + s) and (55 + s, -7 + s), with `s` = (10 - d) / 2. Made smaller (d 10 to 8),+/// the outline's box shrinks with it, and in box-relative terms each end corner lands nearer+/// to where the other one goes. The lines leave the two corners in different directions, so they+/// are not confused: the projection follows, keeping its ids and a point drawn on a corner.+#[test]+fn a_face_with_a_thin_arm_follows_its_body_made_smaller() {+ let holder = |d: f64| -> Vec<LinkGeom> {+ let (r, s) = (d / 2.0, (10.0 - d) / 2.0);+ let p = |x: f64, y: f64| Vec2::new(x, y);+ let hole = r - 2.0;+ vec![+ LinkGeom::Line(p(-r, 0.0), p(-r, -67.0 + s)),+ LinkGeom::Line(p(-r, -67.0 + s), p(-3.0 + s, -67.0 + s)),+ LinkGeom::Arc { c: Vec2::ZERO, a: p(r, 0.0), b: p(-r, 0.0) },+ LinkGeom::Line(p(-3.0 + s, -67.0 + s), p(-3.0 + s, -9.0 + s)),+ LinkGeom::Line(p(-3.0 + s, -9.0 + s), p(55.0 + s, -9.0 + s)),+ LinkGeom::Line(p(r, -7.0 + s), p(r, 0.0)),+ LinkGeom::Line(p(55.0 + s, -7.0 + s), p(r, -7.0 + s)),+ LinkGeom::Line(p(55.0 + s, -9.0 + s), p(55.0 + s, -7.0 + s)),+ LinkGeom::Arc { c: Vec2::ZERO, a: p(-hole, 0.0), b: p(hole, 0.0) },+ LinkGeom::Arc { c: Vec2::ZERO, a: p(hole, 0.0), b: p(-hole, 0.0) },+ ]+ };+ let mut sk = Sketch::new();+ let link = sk.add_link(LinkKind::Project, face(), &holder(10.0)).unwrap();+ let ids: Vec<String> = sk.link_curves(&link).iter().map(|c| sk.curves[*c].id.clone()).collect();+ let corner = |sk: &Sketch, at: Vec2| sk.link_points(&link).into_iter().find(|p| sk.points[*p].pos.dist(at) < 1e-9).unwrap();+ let upper = corner(&sk, Vec2::new(55.0, -7.0));+ let lower = corner(&sk, Vec2::new(55.0, -9.0));+ let free = sk.add_point(Vec2::new(55.0, -7.0), None).unwrap();+ sk.add_constraint(ConstraintKind::Coincident { p: free, q: upper }, None).unwrap();++ assert_eq!(sk.update_link(&link, &holder(8.0)).map_err(|e| e.to_string()), Ok(false), "the same outline, moved");+ assert!(!sk.link(&link).unwrap().lost);+ assert_eq!(ids, sk.link_curves(&link).iter().map(|c| sk.curves[*c].id.clone()).collect::<Vec<_>>());+ assert!(sk.points[upper].pos.dist(Vec2::new(56.0, -6.0)) < 1e-9, "{:?}", sk.points[upper].pos);+ assert!(sk.points[lower].pos.dist(Vec2::new(56.0, -8.0)) < 1e-9, "{:?}", sk.points[lower].pos);+ assert!(solve(&mut sk).ok());+ assert!(sk.points[free].pos.dist(Vec2::new(56.0, -6.0)) < 1e-7, "{:?}", sk.points[free].pos);+}++/// The lines at a point leave it the same ways only when each direction matches a different+/// one: two nearly parallel lines at a sharp corner don't both match one line while the other+/// line turned round.+#[test]+fn leaving_directions_match_one_to_one() {+ let (right, left, up) = (Vec2::new(1.0, 0.0), Vec2::new(-1.0, 0.0), Vec2::new(0.0, 1.0));+ let near_right = Vec2::new(0.995, 0.0998);+ assert!(super::loops::same_leaving(&[right, up], &[up, right]), "the same lines, read in another order");+ assert!(super::loops::same_leaving(&[right, near_right], &[near_right, right]));+ assert!(!super::loops::same_leaving(&[right, near_right], &[right, left]), "one line turned round");+ assert!(!super::loops::same_leaving(&[right, up], &[right, up, left]));+ assert!(super::loops::same_leaving(&[], &[]));+}diff --git a/crates/sketch/src/link_loops.rs b/crates/sketch/src/link_loops.rsindex 5c732652aa..64da82b40c 100644--- a/crates/sketch/src/link_loops.rs+++ b/crates/sketch/src/link_loops.rs@@ -21,11 +21,41 @@ pub(super) struct Roles {pub fresh_normal: Vec<Vec2>,pub old_lines: Vec<Vec<Vec2>>,pub fresh_lines: Vec<Vec<Vec2>>,+ /// The directions the lines at each point leave it in (as `old_lines`, but with their sense):+ /// the two end corners of a thin arm have the same lines, leaving them opposite ways.+ pub old_leaving: Vec<Vec<Vec2>>,+ pub fresh_leaving: Vec<Vec<Vec2>>,}pub(super) fn same_lines(a: &[Vec2], b: &[Vec2]) -> bool {a.len() == b.len() && a.iter().all(|x| b.iter().any(|y| x.dot(*y).abs() > 0.98))}++/// Whether the lines leave two points the same ways ([`Roles::old_leaving`]): each direction+/// of one point matches a different direction of the other. At a sharp corner two nearly+/// parallel directions must not both match one direction while the other line turned round.+/// A point with more than 8 lines is never matched: the search stays small.+pub(super) fn same_leaving(a: &[Vec2], b: &[Vec2]) -> bool {+ fn assign(a: &[Vec2], b: &[Vec2], used: &mut [bool]) -> bool {+ let Some((x, rest)) = a.split_first() else { return true };+ for (i, y) in b.iter().enumerate() {+ if used.get(i).copied().unwrap_or(true) || x.dot(*y) <= 0.98 {+ continue;+ }+ if let Some(slot) = used.get_mut(i) {+ *slot = true;+ }+ if assign(rest, b, used) {+ return true;+ }+ if let Some(slot) = used.get_mut(i) {+ *slot = false;+ }+ }+ false+ }+ a.len() == b.len() && a.len() <= 8 && assign(a, b, &mut vec![false; b.len()])+}fn on(s: &Shape, p: Vec2, tol: f64) -> bool {match *s {Shape::Line { a, b } => {@@ -312,8 +342,10 @@ pub(super) fn roles(old_curves: &[LCurve], a: &[Vec2], fresh_curves: &[LCurve],};let ((old, old_normal), (fresh, fresh_normal)) =(membership(&previous, old_curves, a, Some(&mapping))?, membership(&incoming, fresh_curves, b, None)?);- let line_roles = |loops: &[Boundary], curves: &[LCurve], pts: &[Vec2]| -> Option<Vec<Vec<Vec2>>> {+ type Lines = Vec<Vec<Vec2>>;+ let line_roles = |loops: &[Boundary], curves: &[LCurve], pts: &[Vec2]| -> Option<(Lines, Lines)> {let mut roles = vec![Vec::new(); pts.len()];+ let mut leaving = vec![Vec::new(); pts.len()];for loop_ in loops {let size = loop_.bounds.max - loop_.bounds.min;for i in &loop_.curves {@@ -322,12 +354,14 @@ pub(super) fn roles(old_curves: &[LCurve], a: &[Vec2], fresh_curves: &[LCurve],let direction = Vec2::new(d.x / size.x.max(1e-9), d.y / size.y.max(1e-9)).normalized()?;roles.get_mut(a)?.push(direction);roles.get_mut(b)?.push(direction);+ leaving.get_mut(a)?.push(direction);+ leaving.get_mut(b)?.push(direction * -1.0);}}}- Some(roles)+ Some((roles, leaving))};- let old_lines = line_roles(&previous, old_curves, a)?;- let fresh_lines = line_roles(&incoming, fresh_curves, b)?;- Some(Roles { old, fresh, old_normal, fresh_normal, old_lines, fresh_lines })+ let (old_lines, old_leaving) = line_roles(&previous, old_curves, a)?;+ let (fresh_lines, fresh_leaving) = line_roles(&incoming, fresh_curves, b)?;+ Some(Roles { old, fresh, old_normal, fresh_normal, old_lines, fresh_lines, old_leaving, fresh_leaving })}