diff --git a/crates/engine/src/cmd/sketch_drag.rs b/crates/engine/src/cmd/sketch_drag.rs index e039c330c..f8920ed8b 100644 --- a/crates/engine/src/cmd/sketch_drag.rs +++ b/crates/engine/src/cmd/sketch_drag.rs @@ -48,6 +48,10 @@ const SWEEP_EPS: f64 = 1e-6; const SHRINK_FROM: f64 = 1e-3; const SHRINK_TO: f64 = 1e-4; +/// A step that moves some point further than this many times the sketch's size and the +/// drag's way has thrown the geometry to a far solution: it is taken as failed. +const FLUNG: f64 = 1000.0; + /// What a drag moves, from where it is (`t = 0`) to where the cursor puts it (`t = 1`). enum Path { /// Points slide by `d` (a dragged point by its way to the cursor). @@ -176,6 +180,10 @@ pub(super) fn drag_entity( let line_grab = if with.is_empty() { LineGrab::new(sk, e, from) } else { None }; let start = line_grab.and_then(|g| g.point(sk)).or_else(|| if let Ent::Point(q) = e { sk.point(q) } else { None }); let mut frame = live.then(frame_budget); + let reach = FLUNG * (extent(sk) + path.length()).max(1.0); + // The sketch as the drag found it: no step may press one of its lines to no length. + let mut as_found = Geometry::default(); + as_found.take(sk); // Only success matters to these solves: the degrees of freedom of a sketch with the entity // held are not the sketch's. Each is seeded on the last solved state (`last`), so what the // constraints pin down keeps the orientation it had there. A failed or wrapping step is @@ -211,6 +219,8 @@ pub(super) fn drag_entity( Step::Failed } else if last.sweep_wrapped(sk) { Step::Wrapped + } else if last.flung(sk, reach) || as_found.squashed(sk) { + Step::Failed } else { return Step::Solved; }; @@ -342,7 +352,11 @@ pub(super) fn drag_entity( let valid = |before: &Sketch, after: &Sketch| { let mut geometry = Geometry::default(); geometry.take(before); - !geometry.sweep_wrapped(after) && !geometry.shrank(after) && solidcraft_sketch::fillets_kept(before, after) + !geometry.sweep_wrapped(after) + && !geometry.shrank(after) + && !geometry.flung(after, reach) + && !as_found.squashed(after) + && solidcraft_sketch::fillets_kept(before, after) }; let followed = match (e, line_grab) { (Ent::Point(q), _) => solidcraft_sketch::move_point_towards(sk, q, to, max_step, frame.as_mut(), valid), @@ -582,6 +596,23 @@ impl Geometry { } } + /// Did the step press a line to (nearly) no length: under 1/1000 of what it had when this + /// geometry was taken? A line with no length has no direction left for the constraints on + /// it, so an end dragged past the point where its line would have to turn over stops there. + fn squashed(&self, sk: &Sketch) -> bool { + self.curves.iter().any(|(kind, _)| { + let CurveKind::Line { a, b } = *kind else { return false }; + let (Some(a0), Some(b0)) = (self.pos.get(a), self.pos.get(b)) else { return false }; + let before = a0.dist(*b0); + before > 1e-3 && sk.point(a).zip(sk.point(b)).is_some_and(|(a1, b1)| a1.dist(b1) < before * 1e-3) + }) + } + + /// Did the step throw some point further than `reach` (or to no number at all)? + fn flung(&self, sk: &Sketch, reach: f64) -> bool { + sk.points.iter().zip(&self.pos).any(|(p, was)| !(p.pos.dist(*was) <= reach)) + } + /// Did the step drive some arc's end through its start: the sweep jumped through 0 / a full /// turn, or ran from clear of them onto one? An arc already that close may still move. /// Measured on the arc's ends as they were before the step (a solve may swap them). @@ -827,6 +858,29 @@ impl Group<'_> { } } +/// The size of the sketch: the diagonal of its points' box and its circles' diameters. +fn extent(sk: &Sketch) -> f64 { + let (lo, hi) = sk + .points + .iter() + .map(|p| p.pos) + .filter(|p| p.x.is_finite() && p.y.is_finite()) + .fold((Vec2::new(f64::INFINITY, f64::INFINITY), Vec2::new(f64::NEG_INFINITY, f64::NEG_INFINITY)), |(lo, hi), p| { + (Vec2::new(lo.x.min(p.x), lo.y.min(p.y)), Vec2::new(hi.x.max(p.x), hi.y.max(p.y))) + }); + let radius = sk + .curves + .iter() + .filter_map(|c| match c.kind { + CurveKind::Circle { r, .. } | CurveKind::Ellipse { r, .. } => Some(r.abs()), + _ => None, + }) + .filter(|r| r.is_finite()) + .fold(0.0, f64::max); + let diagonal = if lo.x <= hi.x { lo.dist(hi) } else { 0.0 }; + diagonal + 2.0 * radius +} + /// The smallest size that can make a constrained shape take another solution (of the geometry /// in `group`, or of the whole sketch), or infinity when there is none: every length the /// constraints hold. That is driving length dimensions (offsets along an axis or from a line diff --git a/crates/engine/tests/sketch_drag_target.rs b/crates/engine/tests/sketch_drag_target.rs index d425c03c4..1d72ac72a 100644 --- a/crates/engine/tests/sketch_drag_target.rs +++ b/crates/engine/tests/sketch_drag_target.rs @@ -446,3 +446,53 @@ fn a_linkage_held_by_redundant_parallelograms_follows_the_cursor() { assert!(parallelogram_error(&s) < 1e-6); } } + +#[test] +fn a_drag_that_would_fling_points_far_away_leaves_them_near_the_sketch() { + let mut s = solidcraft_engine::Session::default(); + for (id, params) in [ + ("sketch.create", json!({"plane":"XY"})), + ("sketch.line", json!({"points":[[2,-27],[-2,22],[-10,-10],[2,13]]})), + ("sketch.rectangle.two_point", json!({"p0":[-12,1],"p1":[-18,28]})), + ("sketch.line", json!({"points":[[6,-21],[-12,-27],[-25,-29],[16,5]]})), + ("sketch.rectangle.two_point", json!({"p0":[-21,0],"p1":[-23,-3]})), + ("sketch.line", json!({"points":[[24,23],[-7,7],[0,29],[-6,-7]]})), + ("sketch.dimension", json!({"entities":["l16","l12"],"type":"angle","value":31})), + ] { + s.execute(id, ¶ms).unwrap(); + } + s.execute("sketch.drag", &json!({"entity":"p8","from":[-23.0,-0.534780330115472],"to":[23,-8]})).unwrap(); + for i in 0..30 { + let Ok(p) = four_bar::point(&s, &format!("p{i}")) else { continue }; + assert!(p.len() < 1000.0, "p{i} was thrown to {p:?}"); + } +} + +/// A line held at its midpoint, with an angle to a horizontal line from there: its end dragged +/// past the midpoint, where the line would have to turn over, pressed it to no length (the angle +/// then held no direction). The drag now stops just short of that, the line still pointing the +/// way the angle says. +#[test] +fn an_end_dragged_past_its_lines_midpoint_leaves_the_line_a_length() { + for live in [false, true] { + let mut s = solidcraft_engine::Session::default(); + for (id, params) in [ + ("sketch.create", json!({"plane":"XY"})), + ("sketch.line", json!({"points":[[-10,-6],[10,6]],"infer":false})), + ("sketch.constraint.midpoint", json!({"line":"l1","point":"origin"})), + ("sketch.line", json!({"points":["origin",[15,0]],"infer":false})), + ("sketch.constraint.horizontal_vertical", json!({"line":"l2"})), + ("sketch.dimension", json!({"entities":["l1","l2"],"type":"angle"})), + ("sketch.drag", json!({"entity":"l1.start","from":[-10,-6],"to":[12,4],"live":live})), + ] { + s.execute(id, ¶ms).unwrap(); + } + if live { + s.execute("sketch.drag_end", &json!({})).unwrap(); + } + let (a, b) = (four_bar::point(&s, "l1.start").unwrap(), four_bar::point(&s, "l1.end").unwrap()); + assert!(a.dist(b) > 1e-3, "l1 is left {} mm long (live {live})", a.dist(b)); + let d = b - a; + assert!((d.y.atan2(d.x) - 6f64.atan2(10.0)).abs() < 1e-6, "l1 turned to {d:?} (live {live})"); + } +}