SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Sketch drag: a scripted drag solves the steps past a failed one at once (#6606) (#6607)
semmelrindecf7fbeaba177
diff --git a/crates/engine/src/cmd/sketch_drag.rs b/crates/engine/src/cmd/sketch_drag.rsindex e020b6fc68..0337546f11 100644--- a/crates/engine/src/cmd/sketch_drag.rs+++ b/crates/engine/src/cmd/sketch_drag.rs@@ -36,6 +36,8 @@ const MAX_STEPS: usize = 64;/// After a step fails, the way to it from the last solved state is halved until what is left is/// shorter than a step over this; at most `MAX_STEPS` such solves per drag.const HALVINGS: usize = 16;+/// Steps of a drag that isn't live solved at once past a failed step (see [`drag_entity`]).+const AHEAD: usize = 8;/// An arc whose sweep gets this close (radians) to 0 or a full turn has run into its start.const SWEEP_EPS: f64 = 1e-6;@@ -173,11 +175,9 @@ pub(super) fn drag_entity(// undone (a failed solve leaves the step placed). Also returns whether the decomposition ran// out of budget.let mut solves = Solves::default();- let mut step = |sk: &mut Sketch, last: &Geometry, t: f64, solves: &mut Solves| {+ // Whether the sketch follows step `t` (with the tangent lines' far ends held, else free).+ let held = |sk: &mut Sketch, last: &Geometry, t: f64, budget: &mut Budget| {path.apply(sk, t);- let mut own = Budget::full();- let budget = frame.as_mut().unwrap_or(&mut own);- let spent = budget.spent();let kept = keep.as_ref().is_some_and(|keep| {let ok = counted_solve(sk, keep, Some(&last.pos), Analysis::StatusOnly, Some(&mut *budget)).ok() && !last.sweep_wrapped(sk);if !ok {@@ -186,7 +186,13 @@ pub(super) fn drag_entity(}ok});- let ok = kept || counted_solve(sk, &hold, Some(&last.pos), Analysis::StatusOnly, Some(&mut *budget)).ok();+ kept || counted_solve(sk, &hold, Some(&last.pos), Analysis::StatusOnly, Some(&mut *budget)).ok()+ };+ let mut step = |sk: &mut Sketch, last: &Geometry, t: f64, solves: &mut Solves| {+ let mut own = Budget::full();+ let budget = frame.as_mut().unwrap_or(&mut own);+ let spent = budget.spent();+ let ok = held(sk, last, t, budget);solves.count += 1;solves.work += budget.spent() - spent;solves.cut |= budget.exhausted();@@ -209,6 +215,11 @@ pub(super) fn drag_entity(// The steps: `k` of `dt` from `base`; once a seed ran out of budget, the rest of the way in// short steps.let (mut base, mut dt, mut k, mut t) = (0.0, 1.0 / n as f64, 0, 0.0);+ // The `k`-th step from `base`; the last ends exactly at the cursor.+ let at = |base: f64, dt: f64, k: usize| {+ let t = base + dt * k as f64;+ if 1.0 - t < 0.5 * dt { 1.0 } else { t }+ };let mut shortened = false;let mut wrapped = false;let mut out_of_frame = false;@@ -226,12 +237,39 @@ pub(super) fn drag_entity(let m = steps(rest * short as f64, 1.0);(base, dt, k) = (t, rest / m as f64, 0);}- k += 1;- t = base + dt * k as f64;- // The last step ends exactly at the cursor.- if 1.0 - t < 0.5 * dt {- t = 1.0;+ // Past a failed step, every next one starts from the last solved state again and a failed+ // one leaves nothing for the one after. Unless the drag is live (its steps share the+ // frame's budget), the next few are solved at once, on all cores, and those that fail+ // in a row are taken as tried: the walk goes on where it would have, one by one, at the+ // first of them that doesn't fail, without the seconds of failing solves before it.+ #[cfg(test)]+ let ahead = last_drag::ahead();+ #[cfg(not(test))]+ let ahead = true;+ if ahead && !live && failed.is_some() && line_grab.is_none() {+ let ahead: Vec<f64> =+ (1..=AHEAD).map(|j| at(base, dt, k + j)).scan(false, |done, t| (!std::mem::replace(done, t >= 1.0)).then_some(t)).collect();+ let tried = solidcraft_kernel::map_on_threads(&ahead, AHEAD, |&t| {+ let mut sk = sk.clone();+ let mut budget = Budget::full();+ let ok = held(&mut sk, &last, t, &mut budget);+ (ok, budget.spent(), budget.exhausted())+ });+ let failing = tried.iter().take_while(|r| matches!(r, Some((false, _, false)))).flatten().collect::<Vec<_>>();+ if !failing.is_empty() {+ for (_, work, _) in &failing {+ solves.count += 1;+ solves.work += work;+ }+ k += failing.len();+ t = at(base, dt, k);+ #[cfg(test)]+ last_drag::add_skipped(failing.len());+ continue 'walk;+ }}+ k += 1;+ t = at(base, dt, k);match step(sk, &last, t, &mut solves) {Step::Solved => {(good, failed) = (t, None);@@ -369,6 +407,8 @@ pub(super) mod last_drag {static LAST: Cell<Solves> = const { Cell::new(Solves { count: 0, work: 0, cut: false, numeric: false }) };static FRAME: Cell<Option<usize>> = const { Cell::new(None) };static NUMERIC: Cell<Option<usize>> = const { Cell::new(None) };+ static AHEAD: Cell<bool> = const { Cell::new(true) };+ static SKIPPED: Cell<usize> = const { Cell::new(0) };}pub fn set(s: Solves) {@@ -417,6 +457,34 @@ pub(super) mod last_drag {let _restore = RestoreNumeric(NUMERIC.with(|c| c.replace(Some(work))));f()}++ pub fn ahead() -> bool {+ AHEAD.with(Cell::get)+ }++ /// Failed steps taken as tried from the steps solved at once, since the last call.+ pub fn skipped() -> usize {+ SKIPPED.with(|c| c.replace(0))+ }++ pub fn add_skipped(n: usize) {+ SKIPPED.with(|c| c.set(c.get() + n));+ }++ /// Restores solving steps ahead when dropped, also when the test panics.+ struct RestoreAhead(bool);++ impl Drop for RestoreAhead {+ fn drop(&mut self) {+ AHEAD.with(|c| c.set(self.0));+ }+ }++ /// Run `f` with every step of a drag solved one by one, as before steps were solved ahead.+ pub fn one_by_one<R>(f: impl FnOnce() -> R) -> R {+ let _restore = RestoreAhead(AHEAD.with(|c| c.replace(false)));+ f()+ }}/// How one held solve of a drag went.diff --git a/crates/engine/src/cmd/sketch_ux_tests.rs b/crates/engine/src/cmd/sketch_ux_tests.rsindex d2e2e38ddc..acd4cb6820 100644--- a/crates/engine/src/cmd/sketch_ux_tests.rs+++ b/crates/engine/src/cmd/sketch_ux_tests.rs@@ -1492,6 +1492,29 @@ fn a_live_frame_stops_once_its_numerical_solves_are_spent() {assert!(reached > 0.0 && reached < 1.0 && r["retry"] == json!(true), "{r}");}+/// A drag from a script (not live) past the arm's reach: the steps after a failed one are solved+/// at once and those that fail in a row are taken as tried. The drag ends where solving every+/// step one by one ends it, with the same held solves counted.+#[test]+fn a_scripted_drag_solves_the_steps_past_a_failure_at_once_and_ends_where_one_by_one_does() {+ let start = hung_arm();+ let h = pt(&start, "eh.end");+ let far = hung_hand_target(&start, 2.0, 1.5);+ let drag = |s: &mut Session| run(s, "sketch.drag", json!({"entity": "eh.end", "from": [h.x, h.y], "to": [far.x, far.y]}));+ let points = |s: &Session| s.doc.sketch(s.active_sketch.unwrap()).unwrap().points.iter().map(|p| (p.id.clone(), p.pos)).collect::<Vec<_>>();+ let mut one = hung_arm();+ let r1 = super::drag::last_drag::one_by_one(|| drag(&mut one));+ let solves1 = super::drag::last_drag::get();+ super::drag::last_drag::skipped();+ let mut ahead = hung_arm();+ let r2 = drag(&mut ahead);+ let solves2 = super::drag::last_drag::get();+ assert!(super::drag::last_drag::skipped() > 0, "steps were solved ahead");+ assert_eq!(r2, r1);+ assert_eq!(points(&ahead), points(&one));+ assert_eq!((solves2.count, solves2.work), (solves1.count, solves1.work));+}+/// All the held solves of one live frame, halvings included, share one decomposition budget:/// together they do at most a frame's work, and one solve's more.#[test]