Sketch: a constraint that would shrink a curve to a point is refused (#8988)
A constraint whose only solution squeezes curves to a point (a rectangle's corner made the midpoint of its opposite side, or two opposite corners made coincident) collapsed the whole rectangle while the sketch said solved. A solve that shrinks a line or circle from over 1e-3 to under 1e-6 now counts as one that fails, as one that throws points far away does: a strict edit is refused, another keeps the sketch from before its solve, and the free solve after the held ones in apply_in_order refuses the constraint. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -342,12 +342,32 @@ fn settle(strict: bool, rep: SolveReport, sk: &mut Sketch, unsolved: Sketch) ->
|
||||
"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 {
|
||||
// A solve that shrinks a line or circle to a point met its constraints only by squeezing
|
||||
// that curve away (a corner made the midpoint of the opposite side of its rectangle).
|
||||
let gone = shrunk(&unsolved, sk);
|
||||
if strict && let Some(curve) = &gone {
|
||||
return Err(EngineError::Other(format!("that would shrink {curve} to a point: its constraints can only be met without it")));
|
||||
}
|
||||
if !rep.keeps_orientation() || far || gone.is_some() {
|
||||
*sk = unsolved;
|
||||
}
|
||||
Ok(rep)
|
||||
}
|
||||
|
||||
/// The first line or circle that solving `before` into `after` shrank to a point: longer (or
|
||||
/// wider) than 1e-3 before and shorter than 1e-6 after. A length a dimension asks for is never
|
||||
/// that short.
|
||||
fn shrunk(before: &Sketch, after: &Sketch) -> Option<String> {
|
||||
let size = |sk: &Sketch, i: usize| match sk.curves.get(i).map(|c| &c.kind) {
|
||||
Some(CurveKind::Line { .. }) => line_pts(sk, i).map(|(a, b)| a.dist(b)),
|
||||
Some(CurveKind::Arc { .. } | CurveKind::Circle { .. }) => sk.radius(i),
|
||||
_ => None,
|
||||
};
|
||||
(0..before.curves.len().min(after.curves.len()))
|
||||
.find(|&i| matches!((size(before, i), size(after, i)), (Some(b), Some(a)) if b > 1e-3 && a < 1e-6))
|
||||
.and_then(|i| after.curves.get(i).map(|c| c.id.clone()))
|
||||
}
|
||||
|
||||
/// 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;
|
||||
@@ -1781,6 +1801,9 @@ pub(super) fn apply_in_order(sk: &mut Sketch, rep: &SolveReport, first: &[Ent],
|
||||
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"));
|
||||
}
|
||||
if let Some(curve) = shrunk(sk, &trial) {
|
||||
return Err(bad(cmd, format!("that would shrink {curve} to a point: its constraints can only be met without it")));
|
||||
}
|
||||
*sk = trial;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
@@ -1281,6 +1281,24 @@ fn a_constraint_never_throws_the_sketch_far_away() {
|
||||
assert!(far < 1e4, "a point went {far} mm away");
|
||||
}
|
||||
|
||||
/// A constraint that can only be met by shrinking curves to a point is refused, as one that
|
||||
/// throws the sketch far away is: making a rectangle's corner the midpoint of the opposite side,
|
||||
/// or two opposite corners coincident, squeezed the whole rectangle to a point while the sketch
|
||||
/// said solved (found by a local sketch fuzzer).
|
||||
#[test]
|
||||
fn a_constraint_that_would_shrink_a_rectangle_to_a_point_is_refused() {
|
||||
for (id, p) in
|
||||
[("sketch.constraint.midpoint", json!({"line": "l4", "point": "p2"})), ("sketch.constraint.coincident", json!({"a": "p1", "b": "p3"}))]
|
||||
{
|
||||
let mut s = new_sketch();
|
||||
run(&mut s, "sketch.rectangle.two_point", json!({"p0": [-30, 4], "p1": [11, 27]}));
|
||||
let before = sketch(&s);
|
||||
let e = s.execute(id, &p).unwrap_err().to_string();
|
||||
assert!(e.contains("shrink") && e.contains("to a point"), "{id} {p}: {e}");
|
||||
assert_eq!(sketch(&s), before, "{id} {p}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn coincident_circles_lie_on_each_other() {
|
||||
let mut s = new_sketch();
|
||||
|
||||
Reference in New Issue
Block a user