Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Sketch: an edit whose solve throws points far away is not taken (#8931)

semmelrinde8b1ee94296e6
diff --git a/crates/engine/src/cmd/sketch.rs b/crates/engine/src/cmd/sketch.rs
index 3b2ac12e04..4f924f33c3 100644
--- a/crates/engine/src/cmd/sketch.rs
+++ b/crates/engine/src/cmd/sketch.rs
@@ -333,12 +333,42 @@ fn settle(strict: bool, rep: SolveReport, sk: &mut Sketch, unsolved: Sketch) ->
if strict && !rep.keeps_orientation() {
return Err(EngineError::Other(rep.failure(|f| format!("that would over-constrain the sketch (conflicts with {f})"))));
}
- if !rep.keeps_orientation() {
+ // A solve that throws points far beyond the sketch found a far solution of its constraints,
+ // not the one the edit means (a line squeezed to a point under a direction constraint, an
+ // angle between sides that can't take it): it counts as one that fails.
+ let far = flung(&unsolved, sk);
+ if strict && far {
+ return Err(EngineError::Other(
+ "that would throw points of the sketch far away: its constraints have no solution near the sketch as it is".into(),
+ ));
+ }
+ if !rep.keeps_orientation() || far {
*sk = unsolved;
}
Ok(rep)
}
+/// A solve moving some point further than this many times the sketch's size (and its largest
+/// dimension value) has thrown the geometry to a far solution.
+const FLUNG: f64 = 1000.0;
+
+/// Did solving `before` into `after` throw some point further than [`FLUNG`] times the sketch's
+/// size, or to no number at all?
+fn flung(before: &Sketch, after: &Sketch) -> bool {
+ let (lo, hi) = before
+ .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 size = if lo.x <= hi.x { lo.dist(hi) } else { 0.0 };
+ let longest = after.driving_values().values().map(|v| v.abs()).filter(|v| v.is_finite()).fold(0.0, f64::max);
+ let reach = FLUNG * (size + longest).max(1.0);
+ after.points.iter().zip(&before.points).any(|(a, b)| !(a.pos.dist(b.pos) <= reach))
+}
+
/// The sketch solves of `edit` and drags, counted in tests (see [`solve_with`]).
pub(super) fn counted_solve(
sk: &mut Sketch,
@@ -1736,7 +1766,7 @@ pub(super) fn apply_in_order(sk: &mut Sketch, rep: &SolveReport, first: &[Ent],
}));
let ok = solve_with(&mut trial, &h, None, Analysis::StatusOnly, None).ok();
trial.constraints.retain(|c| c.id != TEMP);
- if ok && !collapsed(sk, &trial) {
+ if ok && !collapsed(sk, &trial) && !flung(sk, &trial) {
*sk = trial;
return Ok(());
}
@@ -1748,6 +1778,9 @@ pub(super) fn apply_in_order(sk: &mut Sketch, rep: &SolveReport, first: &[Ent],
if first_movable && !second_locked {
let mut trial = sk.clone();
if solve_with(&mut trial, &Hold::default(), None, Analysis::StatusOnly, None).ok() {
+ if flung(sk, &trial) {
+ return Err(bad(cmd, "that would throw points of the sketch far away: its constraints have no solution near the sketch as it is"));
+ }
*sk = trial;
return Ok(());
}
diff --git a/crates/engine/src/cmd/sketch_constraints_tests.rs b/crates/engine/src/cmd/sketch_constraints_tests.rs
index 1759f8e6ab..9007390687 100644
--- a/crates/engine/src/cmd/sketch_constraints_tests.rs
+++ b/crates/engine/src/cmd/sketch_constraints_tests.rs
@@ -1232,6 +1232,55 @@ fn coincident_refuses_a_lines_two_ends() {
run(&mut s, "sketch.constraint.coincident", json!({"a":"l1.end","b":"l2.end"}));
}
+/// A constraint whose solve finds only a far solution is not allowed to throw points far away:
+/// the held solve is tried without it, or the constraint is refused. Here a centre that is
+/// already the midpoint of two lines is made the midpoint of a third, and the solve threw two
+/// line ends to x = -2.2e8 mm while the sketch said solved (found by a local sketch fuzzer).
+#[test]
+fn a_constraint_never_throws_the_sketch_far_away() {
+ let mut s = Session::default();
+ for (id, p) in [
+ ("sketch.create", json!({"plane": "XY"})),
+ ("sketch.arc.three_point", json!({"end": [-15, 6], "start": [-24, -21], "through": [-12, -1]})),
+ ("sketch.arc.three_point", json!({"end": [-18, -27], "start": [3, 13], "through": [22, 21]})),
+ ("sketch.dimension", json!({"entities": ["a1.start", "a2.center"], "value": 29})),
+ ("sketch.dimension", json!({"entities": ["a2.end", "a1.start"], "value": 38})),
+ ("sketch.circle.center", json!({"center": [-23, -10], "radius": 7})),
+ ("sketch.constraint.coincident", json!({"a": "a2.start", "b": "a1.end"})),
+ ("sketch.rectangle.two_point", json!({"p0": [22, -9], "p1": [19, -10]})),
+ ("sketch.line", json!({"ids": ["l16"], "points": [[-14, -29], [-5, 5]]})),
+ ("sketch.circle.center", json!({"center": [-14, 19], "radius": 14})),
+ ("sketch.constraint.fix", json!({"entity": "c1.center"})),
+ ("sketch.rectangle.two_point", json!({"p0": [-10, 23], "p1": [16, 13]})),
+ ("sketch.line", json!({"ids": ["l22"], "points": [[-8, 21], [25, 14]]})),
+ ("sketch.circle.center", json!({"center": [6, 10], "radius": 10})),
+ ("sketch.circle.center", json!({"center": [8, -12], "radius": 11})),
+ ("sketch.rectangle.two_point", json!({"p0": [-4, -7], "p1": [-5, -27]})),
+ ("sketch.constraint.concentric", json!({"a": "a1", "b": "c4"})),
+ ("sketch.rectangle.two_point", json!({"p0": [-12, -14], "p1": [-1, -17]})),
+ ("sketch.constraint.midpoint", json!({"line": "l12", "point": "c4.center"})),
+ ("sketch.constraint.midpoint", json!({"line": "l14", "point": "c4.center"})),
+ ("sketch.rectangle.two_point", json!({"p0": [23, 22], "p1": [-28, 8]})),
+ ("sketch.constraint.fix", json!({"entity": "p15"})),
+ ("sketch.dimension", json!({"entities": ["p17", "a1.start"], "value": 35})),
+ ("sketch.constraint.coincident", json!({"a": "p19", "b": "p12"})),
+ ("sketch.constraint.tangent", json!({"a": "c1", "b": "l11"})),
+ ("sketch.line", json!({"closed": true, "points": [[8, -29], [-5, 24], [-9, -6], [26, -8]]})),
+ ("sketch.constraint.perpendicular", json!({"a": "l15", "b": "l17"})),
+ ("sketch.dimension", json!({"entities": ["l12"], "value": 13})),
+ ("sketch.dimension", json!({"entities": ["l26", "l17"], "type": "angle", "value": 14})),
+ ("sketch.constraint.fix", json!({"entity": "p18"})),
+ ("sketch.constraint.tangent", json!({"a": "l23", "b": "c4"})),
+ ("sketch.constraint.coincident", json!({"a": "origin", "b": "l23.start"})),
+ ("sketch.constraint.midpoint", json!({"line": "l18", "point": "c4.center"})),
+ ] {
+ let _ = s.execute(id, &p);
+ }
+ let sk = sketch(&s);
+ let far = sk.points.iter().map(|p| p.pos.len()).fold(0.0_f64, f64::max);
+ assert!(far < 1e4, "a point went {far} mm away");
+}
+
#[test]
fn coincident_circles_lie_on_each_other() {
let mut s = new_sketch();