SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Sketch drag: a linkage with redundant parallelogram constraints still moves (#4585) (#4983)
HiroKenEz60efbe39e31d
diff --git a/crates/engine/tests/sketch_drag_target.rs b/crates/engine/tests/sketch_drag_target.rsindex a122b06117..0daef68b00 100644--- a/crates/engine/tests/sketch_drag_target.rs+++ b/crates/engine/tests/sketch_drag_target.rs@@ -262,3 +262,81 @@ fn the_coupler_edge_follows_with_both_ends_free_to_rotate() {}}}++/// Two parallelogram linkages, each held parallel and equal on both pairs of sides (two+/// redundant constraints each), the upper one hanging off the lower one's moving side and tied+/// to a fixed pivot by a 30 mm rod: one degree of freedom (#4585).+fn parallelograms() -> solidcraft_engine::Session {+ let mut s = solidcraft_engine::Session::default();+ let run = |s: &mut solidcraft_engine::Session, id: &str, p: serde_json::Value| {+ s.execute(id, &p).unwrap_or_else(|e| panic!("{id} {p}: {e}"));+ };+ run(&mut s, "sketch.create", json!({"plane":"XY"}));+ let at = |deg: f64, r: f64| Vec2::new(deg.to_radians().cos(), deg.to_radians().sin()) * r;+ let p1 = at(147.7, 80.0);+ let m = p1 + Vec2::new(0.0, 20.0);+ let r = m + at(20.0, 80.0);+ let (c, t) = (m + Vec2::new(0.0, 20.0), r + Vec2::new(0.0, 20.0));+ let g = r + at(-70.0, 30.0);+ let xy = |p: Vec2| [p.x, p.y];+ let (o, q) = (xy(Vec2::ZERO), xy(Vec2::new(0.0, 20.0)));+ run(&mut s, "sketch.line", json!({"points":[o, xy(p1), xy(m), q, o],"ids":["bot","left","top","right"],"infer":false}));+ run(&mut s, "sketch.line", json!({"points":["left.end", xy(r), xy(t), xy(c), "left.end"],"ids":["ubot","uright","utop","uleft"],"infer":false}));+ run(&mut s, "sketch.line", json!({"points":["ubot.end", xy(g)],"ids":["rod"],"infer":false}));+ run(&mut s, "sketch.constraint.fix", json!({"entities":["bot.start", "rod.end"]}));+ run(&mut s, "sketch.constraint.horizontal_vertical", json!({"line":"right"}));+ for (line, value) in [("right", 20.0), ("bot", 80.0), ("ubot", 80.0), ("uleft", 20.0), ("rod", 30.0)] {+ run(&mut s, "sketch.dimension", json!({"entities":[line],"value":value}));+ }+ for (a, b) in [("top", "bot"), ("left", "right"), ("utop", "ubot"), ("uright", "uleft")] {+ run(&mut s, "sketch.constraint.parallel", json!({"a":a,"b":b}));+ run(&mut s, "sketch.constraint.equal", json!({"a":a,"b":b}));+ }+ run(&mut s, "sketch.constraint.parallel", json!({"a":"uleft","b":"left"}));+ s+}++/// The parallelograms' lengths, directions and fixed points, as the largest error.+fn parallelogram_error(s: &solidcraft_engine::Session) -> f64 {+ let p = |name: &str| four_bar::point(s, name).unwrap();+ let side = |line: &str| p(&format!("{line}.end")) - p(&format!("{line}.start"));+ let mut error = p("bot.start").len().max(p("right.start").dist(Vec2::new(0.0, 20.0)));+ for (line, value) in [("bot", 80.0), ("ubot", 80.0), ("uleft", 20.0), ("rod", 30.0)] {+ error = error.max((side(line).len() - value).abs());+ }+ for (a, b) in [("top", "bot"), ("left", "right"), ("utop", "ubot"), ("uright", "uleft")] {+ error = error.max((side(a).len() - side(b).len()).abs()).max(side(a).cross(side(b)).abs() / side(b).len());+ }+ error+}++#[test]+fn a_linkage_held_by_redundant_parallelograms_follows_the_cursor() {+ // Each held parallelogram's parallel and equal sides are dependent only at an exact+ // solution: a pose corrected to the solver's tolerance must still show the linkage's freedom.+ for live in [true, false] {+ let mut s = parallelograms();+ let report = s.execute("sketch.solve", &json!({})).unwrap();+ assert_eq!((&report["status"], &report["dof"]), (&json!("solved"), &json!(1)), "{report}");+ // The lower parallelogram's free corner, its moving sides and the upper one's side, each+ // dragged in turn from where the last drag left the linkage, follows most of the way+ // (the rest is off the path the linkage allows).+ for (entity, along) in [("bot.end", None), ("bot", Some(0.5)), ("left", Some(0.5)), ("ubot", Some(0.5)), ("bot.end", None)] {+ let from = along.map_or_else(|| four_bar::point(&s, entity).unwrap(), |f| grab(&s, entity, f));+ let to = from + Vec2::new(2.0, 2.0);+ let r = s.execute("sketch.drag", &json!({"entity":entity,"from":[from.x,from.y],"to":[to.x,to.y],"live":live})).unwrap();+ let reached = r["reached"].as_f64().unwrap();+ assert!(reached > 0.8, "live {live}: {entity} reached {reached}: {r}");+ assert!(parallelogram_error(&s) < 1e-6, "live {live}: {entity}: {}", parallelogram_error(&s));+ assert_eq!(r["sketch"]["solved"], json!(true));+ }+ // The lower parallelogram's free corner lands on its 80 mm circle, as close to the+ // cursor as the circle allows.+ let from = four_bar::point(&s, "bot.end").unwrap();+ let to = from + Vec2::new(1.0, 2.0);+ s.execute("sketch.drag", &json!({"entity":"bot.end","from":[from.x,from.y],"to":[to.x,to.y],"live":live})).unwrap();+ let at = four_bar::point(&s, "bot.end").unwrap();+ assert!(at.dist(to * (80.0 / to.len())) < 1e-4, "live {live}: {at:?}");+ assert!(parallelogram_error(&s) < 1e-6);+ }+}diff --git a/crates/sketch/src/cursor.rs b/crates/sketch/src/cursor.rsindex 099d772c8c..19649fa2a4 100644--- a/crates/sketch/src/cursor.rs+++ b/crates/sketch/src/cursor.rs@@ -15,6 +15,12 @@ const MAX_VARIABLES: usize = 400;const MAX_STEPS: usize = 96;const CORRECTION_ITERS: usize = 16;const TOTAL_CORRECTION_ITERS: usize = 256;+/// Pivot tolerance of the null space, on rows scaled to a largest entry of 1. Redundant+/// constraints (a parallelogram held both parallel and equal on each pair of sides) are+/// dependent only at an exact solution; a pose corrected to `TOL` leaves their rows independent+/// by about that much, and a tighter tolerance counts that as rank and finds no freedom (#4585).+/// The solver's rank analysis, which reports the sketch's degrees of freedom, uses 1e-7 too.+const NULL_SPACE_TOL: f64 = 1e-7;/// Move a point toward `target` using freedom left by the design constraints. `max_step`/// bounds the full variable displacement before constraint correction; `valid` can reject@@ -266,7 +272,7 @@ fn null_space(mut j: Mat) -> Vec<Vec<f64>> {}}}- let pivots = rref(&mut j, 1e-9);+ let pivots = rref(&mut j, NULL_SPACE_TOL);let mut z: Vec<Vec<f64>> = Vec::new();for free in (0..j.cols).filter(|c| !pivots.contains(c)) {let mut v = vec![0.0; j.cols];