Sketch: the same constraint twice is one constraint (#5224) (#8850)

* claim: #5224

* Sketch: the same constraint twice is one constraint (#5224)
This commit is contained in:
Colin Faulkingham
2026-10-29 15:31:18 +00:00
committed by GitHub
parent 4cd90e6806
commit 4e78acc7a2
2 changed files with 65 additions and 1 deletions
@@ -49,6 +49,37 @@ fn redundant_dimensions_are_refused_driven_ones_measure() {
assert_eq!(g["fully_constrained"], true);
}
/// The same constraint between the same two lines, picked again (in either order), adds nothing:
/// the one already there is reported back as redundant, and the sketch keeps one glyph (#5224).
#[test]
fn the_same_constraint_twice_adds_nothing() {
let mut s = new_sketch();
let a = ids(&run(&mut s, "sketch.line", json!({"points": [[1, 1], [10, 1]]}))["curves"])[0].clone();
let b = ids(&run(&mut s, "sketch.line", json!({"points": [[0, 5], [10, 7]]}))["curves"])[0].clone();
let first = run(&mut s, "sketch.constraint.parallel", json!({"a": a, "b": b}));
for (x, y) in [(&a, &b), (&b, &a), (&a, &b)] {
let r = run(&mut s, "sketch.constraint.parallel", json!({"a": x, "b": y}));
assert_eq!(r["redundant"], true, "{r}");
assert_eq!(r["constraint"], first["constraint"], "the one already there: {r}");
}
let e = s.execute("sketch.constraint.parallel", &json!({"a": a, "b": b, "if_redundant": "refuse"})).unwrap_err().to_string();
assert!(e.contains("already determined"), "{e}");
let parallels = sketch(&s).constraints.iter().filter(|c| matches!(c.kind, ConstraintKind::Parallel { .. })).count();
assert_eq!(parallels, 1);
let g = run(&mut s, "sketch.glyphs", json!({}));
assert_eq!(g["glyphs"].as_array().map(Vec::len), Some(1), "{g}");
// The sketch model itself hands back the one already there.
let mut sk = sketch(&s);
let n = sk.constraints.len();
let k = sk.constraints.iter().find(|c| matches!(c.kind, ConstraintKind::Parallel { .. })).unwrap().clone();
let (ia, ib) = match k.kind {
ConstraintKind::Parallel { a, b } => (a, b),
_ => (0, 0),
};
assert_eq!(sk.add_constraint(ConstraintKind::Parallel { a: ib, b: ia }, None).unwrap(), k.id);
assert_eq!(sk.constraints.len(), n);
}
/// `measured` is the dimension on the solved sketch: a trough drawn 99.84 long and dimensioned
/// 100 reports 100, not the length it had before solving.
#[test]
+34 -1
View File
@@ -963,15 +963,48 @@ impl Sketch {
}
pub fn add_constraint(&mut self, kind: ConstraintKind, param: Option<String>) -> Result<String> {
self.validate(&kind)?;
if let Some(same) = self.same_constraint(&kind) {
return Ok(same.to_string());
}
if self.constraints.len() >= MAX_CONSTRAINTS {
return Err(SketchError::TooLarge);
}
self.validate(&kind)?;
let id = self.fresh("k")?;
self.constraints.push(Constraint { id: id.clone(), kind, param, driven: false, text: None });
Ok(id)
}
/// The id of a geometric (not dimensional) constraint that already says what `kind` says: the
/// same kind on the same entities, in either order where the order doesn't matter.
pub fn same_constraint(&self, kind: &ConstraintKind) -> Option<&str> {
use ConstraintKind::*;
let pair = |x: usize, y: usize| if x <= y { (x, y) } else { (y, x) };
let key = |k: &ConstraintKind| -> Option<(u8, usize, usize, usize)> {
Some(match *k {
Coincident { p, q } => (0, pair(p, q).0, pair(p, q).1, 0),
PointOnCurve { p, c } => (1, p, c, 0),
Horizontal { l } => (2, l, 0, 0),
Vertical { l } => (3, l, 0, 0),
HorizontalPoints { p, q } => (4, pair(p, q).0, pair(p, q).1, 0),
VerticalPoints { p, q } => (5, pair(p, q).0, pair(p, q).1, 0),
Parallel { a, b } => (6, pair(a, b).0, pair(a, b).1, 0),
Perpendicular { a, b } => (7, pair(a, b).0, pair(a, b).1, 0),
Collinear { a, b } => (8, pair(a, b).0, pair(a, b).1, 0),
Tangent { a, b } => (9, pair(a, b).0, pair(a, b).1, 0),
Equal { a, b } => (10, pair(a, b).0, pair(a, b).1, 0),
Concentric { a, b } => (11, pair(a, b).0, pair(a, b).1, 0),
Midpoint { p, l } => (12, p, l, 0),
Symmetric { p, q, l } => (13, pair(p, q).0, pair(p, q).1, l),
Fix { p } => (14, p, 0, 0),
Smooth { a, b } => (15, pair(a, b).0, pair(a, b).1, 0),
_ => return None,
})
};
let want = key(kind)?;
self.constraints.iter().find(|c| key(&c.kind) == Some(want)).map(|c| c.id.as_str())
}
/// Check indices and entity kinds of a constraint.
pub fn validate(&self, k: &ConstraintKind) -> Result<()> {
use ConstraintKind::*;