Skip to content

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

Sketch Offset: an inward offset past a narrow neck is refused, not looped (#11055)

semmelrindede07905f7876
diff --git a/crates/engine/src/cmd/sketch_modify_tests.rs b/crates/engine/src/cmd/sketch_modify_tests.rs
index 83f3e12d31..dae7053932 100644
--- a/crates/engine/src/cmd/sketch_modify_tests.rs
+++ b/crates/engine/src/cmd/sketch_modify_tests.rs
@@ -3426,6 +3426,47 @@ fn offset_refuses_the_combinations_that_would_be_guesswork() {
assert_eq!(o["sides"].as_array().map(Vec::len), Some(2), "{o}");
}
+/// An outline with a neck: an inward offset wider than half the neck would pinch it into two,
+/// which one outline can't be. The offset is refused, and the sketch keeps what it had, where
+/// it used to be drawn as a loop back over itself (found by fuzzing). Narrower, it is made; an
+/// outline that already crosses itself (a bow tie) still gets its crossing offset.
+#[test]
+fn offset_refuses_an_outline_that_would_loop_over_itself() {
+ let necked = |s: &mut Session| {
+ run(s, "sketch.arc.three_point", json!({"start": [-18.297, -25.082], "end": [16.306, -24.151], "through": [-1.25, -15.158]}));
+ run(s, "sketch.arc.three_point", json!({"start": [16.306, -24.151], "end": [12.319, -5.785], "through": [21.544, -13.398]}));
+ run(s, "sketch.line", json!({"points": [[12.319, -5.785], [19.695, 22.256], [-2.01, 25.074], [-12.378, 20.824], [-24.959, -8.303]]}));
+ run(s, "sketch.arc.three_point", json!({"start": [-24.959, -8.303], "end": [-18.297, -25.082], "through": [-19.508, -15.851]}));
+ };
+ let mut s = new_sketch();
+ necked(&mut s);
+ let curves = sketch(&s).curves.len();
+ let e = s.execute("sketch.offset", &json!({"curves": ["a1"], "distance": 3.71, "side": [0, 0]})).unwrap_err().to_string();
+ assert!(e.contains("would cross"), "{e}");
+ assert_eq!(sketch(&s).curves.len(), curves, "a refused offset leaves the sketch alone");
+ let o = run(&mut s, "sketch.offset", json!({"curves": ["a1"], "distance": 1, "side": [0, 0]}));
+ assert_eq!(ids(&o["curves"]).len(), 7, "{o}");
+ let mut s = new_sketch();
+ run(&mut s, "sketch.line", json!({"points": [[0, 0], [20, 20], [20, 0], [0, 20]], "closed": true}));
+ let o = run(&mut s, "sketch.offset", json!({"curves": ["l1"], "distance": 1, "side": [10, -5]}));
+ assert_eq!(ids(&o["curves"]).len(), 4, "{o}");
+ // The same bow tie split where it crosses itself only touches itself there: it too keeps
+ // its offset.
+ let mut s = new_sketch();
+ run(&mut s, "sketch.line", json!({"points": [[0, 0], [10, 10], [20, 20], [20, 0], [0, 20]], "closed": true}));
+ let o = run(&mut s, "sketch.offset", json!({"curves": ["l1"], "distance": 1, "side": [10, -5]}));
+ assert_eq!(ids(&o["curves"]).len(), 5, "{o}");
+ // A dumbbell whose neck is 2 high: inward by exactly 1 the neck's two sides land on one
+ // another, which is no outline either; a little less is made.
+ let dumbbell = [[0, 0], [4, 0], [4, 1], [8, 1], [8, 0], [12, 0], [12, 4], [8, 4], [8, 3], [4, 3], [4, 4], [0, 4]];
+ for (distance, made) in [(1.0, false), (0.9, true)] {
+ let mut s = new_sketch();
+ run(&mut s, "sketch.line", json!({"points": dumbbell, "closed": true}));
+ let r = s.execute("sketch.offset", &json!({"curves": ["l1"], "distance": distance, "side": [2, 2]}));
+ assert_eq!(r.is_ok(), made, "{distance}: {r:?}");
+ }
+}
+
#[test]
fn offset_one_side_still_reports_one_set_of_curves_and_its_association() {
let mut s = new_sketch();
diff --git a/crates/sketch/src/offset.rs b/crates/sketch/src/offset.rs
index 5fdda5589d..85f4ae93ed 100644
--- a/crates/sketch/src/offset.rs
+++ b/crates/sketch/src/offset.rs
@@ -240,6 +240,7 @@ impl Sketch {
}
let bad = || SketchError::Invalid("an offset source vanished or changed shape".into());
let mut pieces = Vec::new();
+ let mut outline = Vec::new();
for (id, forward) in &offset.sources {
let i = self.curve_index(id).ok_or_else(bad)?;
let kind = &self.curves.get(i).ok_or_else(bad)?.kind;
@@ -247,11 +248,15 @@ impl Sketch {
CurveKind::Line { a, b } => {
let (a, b) = (self.point(a).ok_or_else(bad)?, self.point(b).ok_or_else(bad)?);
let (a, b) = if *forward { (a, b) } else { (b, a) };
+ outline.push((Shape::Line { a, b }, a, b, None));
let n = (b - a).normalized().ok_or_else(bad)?.perp() * offset.distance;
- (Shape::Line { a: a + n, b: b + n }, a + n, b + n, None)
+ (Shape::Line { a: a + n, b: b + n }, a + n, b + n)
}
CurveKind::Arc { c, a, b } => {
let (c, a, b) = (self.point(c).ok_or_else(bad)?, self.point(a).ok_or_else(bad)?, self.point(b).ok_or_else(bad)?);
+ let start = (a - c).angle();
+ let sweep = ((b - c).angle() - start).rem_euclid(std::f64::consts::TAU);
+ outline.push((Shape::Round { c, r: c.dist(a), start, sweep }, a, b, Some(*forward)));
let r = c.dist(a) - if *forward { offset.distance } else { -offset.distance };
if r <= 1e-9 {
return Err(SketchError::Invalid("the offset collapses an arc".into()));
@@ -259,7 +264,7 @@ impl Sketch {
let a = c + (a - c).normalized().ok_or_else(bad)? * r;
let b = c + (b - c).normalized().ok_or_else(bad)? * r;
let (a, b) = if *forward { (a, b) } else { (b, a) };
- (Shape::Round { c, r, start: 0.0, sweep: std::f64::consts::TAU }, a, b, Some(*forward))
+ (Shape::Round { c, r, start: 0.0, sweep: std::f64::consts::TAU }, a, b)
}
CurveKind::Circle { c, r } => {
let c = self.point(c).ok_or_else(bad)?;
@@ -285,7 +290,7 @@ impl Sketch {
}
// Which way a piece runs at `p`: a line along itself, an arc anticlockwise round its
// centre when it follows its source forward.
- let along = |piece: &(Shape, Vec2, Vec2, Option<bool>), p: Vec2| match (&piece.0, piece.3) {
+ let along = |i: usize, piece: &(Shape, Vec2, Vec2), p: Vec2| match (&piece.0, outline.get(i).and_then(|o| o.3)) {
(Shape::Line { a, b }, _) => *b - *a,
(Shape::Round { c, .. }, Some(true)) => (p - *c).perp(),
(Shape::Round { c, .. }, Some(false)) => (*c - p).perp(),
@@ -303,7 +308,7 @@ impl Sketch {
// both crossings are as far from the corner and rounding chose one. The pieces
// overlap there: they meet where both are cut short, not where both run on and
// cross into a loop.
- let cut_short = |p: Vec2| (p - x.2).dot(along(x, x.2)) < 0.0 && (p - y.1).dot(along(y, y.1)) > 0.0;
+ let cut_short = |p: Vec2| (p - x.2).dot(along(i, x, x.2)) < 0.0 && (p - y.1).dot(along(j, y, y.1)) > 0.0;
match found.as_slice() {
[p, q, ..] if (q.dist(guess) - p.dist(guess)).abs() <= 1e-9 * (1.0 + p.dist(*q)) && !cut_short(*p) && cut_short(*q) => Ok(*q),
[p, ..] => Ok(*p),
@@ -318,6 +323,9 @@ impl Sketch {
if !offset.closed {
joints.push(pieces.last().ok_or_else(bad)?.2);
}
+ if offset.closed && !crosses_itself(&outline, true) && crosses_itself(&cut_to_joints(&pieces, &outline, &joints), false) {
+ return Err(SketchError::Invalid("the offset curves would cross".into()));
+ }
for (i, id) in offset.outputs.iter().enumerate() {
let a = *joints.get(i).ok_or_else(bad)?;
let b = *joints.get(if offset.closed { (i + 1) % n } else { i + 1 }).ok_or_else(bad)?;
@@ -351,3 +359,92 @@ impl Sketch {
Ok(())
}
}
+
+/// A piece of a closed outline: its shape, its two ends, and for an arc whether the chain follows
+/// it forward.
+type Outline = (Shape, Vec2, Vec2, Option<bool>);
+
+/// The closed offset outline's pieces, each cut to its joints: a line between them, an arc
+/// anticlockwise from one to the other the way its source runs (`outline`: anticlockwise when
+/// followed forward).
+fn cut_to_joints(pieces: &[(Shape, Vec2, Vec2)], outline: &[Outline], joints: &[Vec2]) -> Vec<Outline> {
+ let n = pieces.len();
+ (0..n)
+ .filter_map(|i| {
+ let (a, b) = (*joints.get(i)?, *joints.get((i + 1) % n)?);
+ Some(match (pieces.get(i)?, outline.get(i)?.3) {
+ ((Shape::Round { c, r, .. }, _, _), Some(ccw)) => {
+ let (from, to) = if ccw { (a, b) } else { (b, a) };
+ let start = (from - *c).angle();
+ (Shape::Round { c: *c, r: *r, start, sweep: ((to - *c).angle() - start).rem_euclid(std::f64::consts::TAU) }, a, b, Some(ccw))
+ }
+ _ => (Shape::Line { a, b }, a, b, None),
+ })
+ })
+ .collect()
+}
+
+/// Whether a closed outline of lines and arcs crosses itself away from the ends its pieces
+/// share: two of its pieces cross, or two lines lie along each other. With `touching`, pieces
+/// that aren't neighbours meeting at all count too (an outline split where it crosses itself
+/// only touches itself there). A simple outline whose offset crosses itself is refused, not drawn
+/// as a loop: an inward offset wider than a neck of the shape pinches it into two, which one
+/// outline can't be (an outline that already crosses or touches itself, a figure eight, keeps
+/// its crossing offset). Outlines of more than 1000 pieces aren't checked.
+fn crosses_itself(outline: &[Outline], touching: bool) -> bool {
+ let n = outline.len();
+ if !(2..=1000).contains(&n) {
+ return false;
+ }
+ // Tolerances from the outline's own size, plus the coordinates' rounding.
+ let ends = || outline.iter().flat_map(|(_, a, b, _)| [*a, *b]);
+ let (lo, hi) = ends().fold((Vec2::new(f64::MAX, f64::MAX), Vec2::new(f64::MIN, f64::MIN)), |(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 reach = ends().fold(0.0f64, |m, p| m.max(p.x.abs()).max(p.y.abs()));
+ let extent = (hi.x - lo.x).max(hi.y - lo.y).max(1e-9);
+ let tol = 1e-9 * extent + 1e-12 * reach;
+ let near = 1e-6 * extent + 1e-12 * reach;
+ let bounds = |s: &Shape| match *s {
+ Shape::Line { a, b } => (a.x.min(b.x), a.y.min(b.y), a.x.max(b.x), a.y.max(b.y)),
+ Shape::Round { c, r, .. } => (c.x - r, c.y - r, c.x + r, c.y + r),
+ };
+ for i in 0..n {
+ for j in i + 1..n {
+ let (Some(x), Some(y)) = (outline.get(i), outline.get(j)) else { continue };
+ let (bx, by) = (bounds(&x.0), bounds(&y.0));
+ if bx.2 < by.0 - tol || by.2 < bx.0 - tol || bx.3 < by.1 - tol || by.3 < bx.1 - tol {
+ continue;
+ }
+ let neighbours = j == i + 1 || (i == 0 && j == n - 1);
+ // Neighbours meet at their shared end, where a tangent pair's crossings are rounding
+ // noise.
+ let shared: Vec<Vec2> = [x.1, x.2].into_iter().filter(|p| p.dist(y.1).min(p.dist(y.2)) <= tol).collect();
+ let on = |piece: &Outline, p: Vec2, with_ends: bool| {
+ piece.0.param_on(p, tol).is_some() || (with_ends && (piece.1.dist(p) <= tol || piece.2.dist(p) <= tol))
+ };
+ let with_ends = touching && !neighbours;
+ if with_ends && !shared.is_empty() {
+ return true;
+ }
+ if intersections(&x.0, &y.0)
+ .into_iter()
+ .any(|p| on(x, p, with_ends) && on(y, p, with_ends) && (with_ends || shared.iter().all(|s| s.dist(p) > near)))
+ {
+ return true;
+ }
+ // Two lines along one another: no crossing point, but they overlap.
+ if let (Shape::Line { a, b }, Shape::Line { a: c, b: d }) = (x.0, y.0)
+ && let Some(u) = (b - a).normalized()
+ {
+ let off = |p: Vec2| ((p - a).x * u.y - (p - a).y * u.x).abs();
+ let along = |p: Vec2| (p - a).dot(u);
+ let (s0, s1) = (along(c).min(along(d)), along(c).max(along(d)));
+ if off(c) <= tol && off(d) <= tol && s1.min(a.dist(b)) - s0.max(0.0) > near {
+ return true;
+ }
+ }
+ }
+ }
+ false
+}