SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Sketch: dragging a constrained line by its middle no longer stalls (#11696) (#11714)
PaddleStroke863b0777da8c
diff --git a/crates/engine/tests/sketch_drag_target.rs b/crates/engine/tests/sketch_drag_target.rsindex dc132f635c..ee2669159e 100644--- a/crates/engine/tests/sketch_drag_target.rs+++ b/crates/engine/tests/sketch_drag_target.rs@@ -219,6 +219,36 @@ fn grab(s: &solidcraft_engine::Session, line: &str, fraction: f64) -> Vec2 {a + (b - a) * fraction}+#[test]+fn a_live_line_grab_can_move_in_a_large_staircase() {+ let mut s = solidcraft_engine::Session::default();+ s.execute("sketch.create", &json!({"plane":"XY"})).unwrap();+ let mut points = vec![[10, 0], [10, 20]];+ for i in 0..43 {+ let [x, y] = points.last().copied().unwrap();+ points.push(if i % 2 == 0 { [x + 5, y] } else { [x, y + 5] });+ }+ s.execute("sketch.line", &json!({"points":points,"infer":true})).unwrap();+ s.execute("sketch.constraint.horizontal_vertical", &json!({"points":["origin","l1.start"],"mode":"horizontal"})).unwrap();++ // Rigid translation cannot move the first endpoint off Y = 0. The line can+ // follow the grab by changing its length while both endpoints move sideways.+ let target = Vec2::new(11.0, 12.0);+ for _ in 0..8 {+ let from = grab(&s, "l1", 0.5);+ let result = s.execute("sketch.drag", &json!({"entity":"l1","from":[from.x,from.y],"to":[target.x,target.y],"live":true})).unwrap();+ let after = grab(&s, "l1", 0.5);+ assert_eq!(result["sketch"]["solved"], true, "{result}");+ if after.dist(target) < 1e-6 {+ break;+ }+ assert!(after.dist(target) < from.dist(target), "the live drag stalled: {result}");+ assert_eq!(result["retry"], true, "{result}");+ }+ assert!(four_bar::point(&s, "l1.start").unwrap().dist(Vec2::new(11.0, 0.0)) < 1e-6);+ assert!(four_bar::point(&s, "l1.end").unwrap().dist(Vec2::new(11.0, 24.0)) < 1e-6);+}+#[test]fn a_free_line_grabbed_slightly_off_its_edge_still_translates_rigidly() {for from in [Vec2::new(15.0, 12.0), Vec2::new(32.0, 11.0)] {diff --git a/crates/sketch/src/cursor.rs b/crates/sketch/src/cursor.rsindex 3a22b0dcce..0fe9568fe3 100644--- a/crates/sketch/src/cursor.rs+++ b/crates/sketch/src/cursor.rs@@ -167,11 +167,7 @@ fn move_cursor_towards(break;}let (_, j, _) = local_system(&problem, &lay, &sub, &mut x, &col, n);- let null_work = n.saturating_mul(n).saturating_mul(j.rows.saturating_add(n));- if !within_budget(&mut budget, null_work) {- break;- }- let z = null_space(j);+ let Some(z) = null_space(j, &mut budget) else { break };if z.is_empty() {break;}@@ -277,7 +273,13 @@ fn in_range(sk: &Sketch) -> bool {.all(|c| !matches!(c.kind, CurveKind::Circle { r, .. } | CurveKind::Ellipse { r, .. } if !crate::model::radius_in_range(r)))}-fn null_space(j: Mat) -> Vec<Vec<f64>> {+fn null_space(j: Mat, budget: &mut Option<&mut Budget>) -> Option<Vec<Vec<f64>>> {+ // A pivot processes at most all rows and columns. A rectangular matrix has at+ // most as many pivots as its smaller dimension.+ let reduction_work = j.rows.saturating_mul(j.cols).saturating_mul(j.rows.min(j.cols));+ if !within_budget(budget, reduction_work) {+ return None;+ }// Equilibrate before the rank analysis. A near-zero-length construction line makes its// angle row's entries on that line ~1e7 while the same row's entries on the line it// measures are ~1: per-row scaling alone hides those (physically meaningful) small@@ -301,6 +303,13 @@ fn null_space(j: Mat) -> Vec<Vec<f64>> {}}let pivots = rref(&mut a, NULL_SPACE_TOL);+ // Only the free directions are orthogonalized. In a nearly determined sketch,+ // reserving work for a full basis could consume the frame before any motion.+ let free = a.cols.saturating_sub(pivots.len());+ let basis_work = a.cols.saturating_mul(free).saturating_mul(free);+ if !within_budget(budget, basis_work) {+ return None;+ }let mut z: Vec<Vec<f64>> = Vec::new();for free in (0..a.cols).filter(|c| !pivots.contains(c)) {let mut v = vec![0.0; a.cols];@@ -331,7 +340,7 @@ fn null_space(j: Mat) -> Vec<Vec<f64>> {z.push(v);}}- z+ Some(z)}/// The second weight component multiplies a 90-degree rotation of the endpoint.