diff --git a/crates/engine/src/cmd/sketch_modify.rs b/crates/engine/src/cmd/sketch_modify.rs index f91795471..d45b69ae1 100644 --- a/crates/engine/src/cmd/sketch_modify.rs +++ b/crates/engine/src/cmd/sketch_modify.rs @@ -2703,13 +2703,31 @@ fn offset_group(sk: &mut Sketch, ids: &[String], eligible: &std::collections::Ha sel.clone() }; let (order, closed) = chain(sk, first, &pool); - // Side: toward `side` (compare both offsets' distance to it), else the sign. + // Side: toward `side`, else the sign. A positive distance offsets to the left of the walk. + // A line or an arc says directly which side `side` is on (the half-plane; inside or outside + // the circle, as for a circle above), so neither rounding of a tiny step nor an arc too small + // for the distance can turn it round; other curves compare both offsets' distance to it. let d = match side { Some(q) => { - let dist = |dd: f64| { - offset_piece(sk, first, order.iter().find(|x| x.0 == first).is_none_or(|x| x.1), dd).map(|pc| piece_dist(&pc, q)).unwrap_or(f64::MAX) + let forward = order.iter().find(|x| x.0 == first).is_none_or(|x| x.1); + let left = match sk.curves.get(first).map(|c| c.kind.clone()) { + Some(CurveKind::Line { a, b }) => { + let (a, b) = (sk.point(a).unwrap_or_default(), sk.point(b).unwrap_or_default()); + let (from, along) = if forward { (a, b - a) } else { (b, a - b) }; + Some(along.x * (q - from).y - along.y * (q - from).x >= 0.0) + } + Some(CurveKind::Arc { c, a, .. }) => { + let (c, a) = (sk.point(c).unwrap_or_default(), sk.point(a).unwrap_or_default()); + // Followed forward an arc turns anticlockwise, its centre on the left. + Some((q.dist(c) <= c.dist(a)) == forward) + } + _ => None, }; - if dist(d0.abs()) <= dist(-d0.abs()) { d0.abs() } else { -d0.abs() } + let toward = left.unwrap_or_else(|| { + let dist = |dd: f64| offset_piece(sk, first, forward, dd).map(|pc| piece_dist(&pc, q)).unwrap_or(f64::MAX); + dist(d0.abs()) <= dist(-d0.abs()) + }); + if toward { d0.abs() } else { -d0.abs() } } None => d0, }; @@ -2830,11 +2848,28 @@ fn offset_beyond(sk: &Sketch, rung: &[String]) -> Option { // an inward ladder round into a growing one instead of letting it run out of room. Its // signed distance already says which way, so leave the side to it. let index = sk.curve_index(rung.first()?)?; - if matches!(sk.curves.get(index)?.kind, CurveKind::Circle { .. }) { + let kind = &sk.curves.get(index)?.kind; + if matches!(kind, CurveKind::Circle { .. }) { return None; } let (from, to) = sk.offset_dimension_points(rung.first()?)?; - (from.dist(to) > TOL).then(|| to + (to - from)) + if from.dist(to) <= TOL { + return None; + } + let step = to - from; + // An arc's side is inside or outside its circle, and the same trap as the ring's waits + // there: a whole step toward the centre past an arc smaller than the distance lands beyond + // the centre and reads as outside, so the ladder turned round onto an earlier instance. + // Stop halfway to the centre instead, still inside, and let the arc run out of room. + if let CurveKind::Arc { c, .. } = *kind + && let Some(centre) = sk.points.get(c).map(|p| p.pos) + { + let r = to.dist(centre); + if step.dot(centre - to) > 0.0 && step.len() >= r * 0.5 { + return Some(to + step.normalized()? * (r * 0.5)); + } + } + Some(to + step) } /// What one side of a rung leaves for the next: the curves to offset again, its own signed diff --git a/crates/engine/src/cmd/sketch_modify_tests.rs b/crates/engine/src/cmd/sketch_modify_tests.rs index d6c66e862..9fdb7e99b 100644 --- a/crates/engine/src/cmd/sketch_modify_tests.rs +++ b/crates/engine/src/cmd/sketch_modify_tests.rs @@ -2685,6 +2685,59 @@ fn offset_instances_stop_where_the_shape_runs_out() { assert_eq!(o["sides"].as_array().map(Vec::len), Some(2), "{o}"); } +/// Instances picked from an arc keep going inward: a slot with r5 ends, stepped 2 mm in three +/// times from an end arc. The third step would pass that arc's centre, so the command is +/// refused there, where the third instance used to turn round onto the first (its side was +/// read past the centre, outside the arc). +#[test] +fn offset_instances_from_an_arc_stop_where_the_arc_runs_out() { + let mut s = new_sketch(); + run(&mut s, "sketch.line", json!({"points": [[-10, -5], [10, -5]]})); + run(&mut s, "sketch.arc.center_point", json!({"center": [10, 0], "start": [10, -5], "end": [10, 5]})); + run(&mut s, "sketch.line", json!({"points": [[10, 5], [-10, 5]]})); + run(&mut s, "sketch.arc.center_point", json!({"center": [-10, 0], "start": [-10, 5], "end": [-10, -5]})); + let before = s.doc.to_json(); + let error = s.execute("sketch.offset", &json!({"curves": ["a1"], "distance": 2, "side": [10, 0], "instances": 3})).unwrap_err().to_string(); + assert!(error.contains("instance 3 of 3"), "{error}"); + assert_eq!(s.doc.to_json(), before, "a refused instance leaves the design alone"); + let o = run(&mut s, "sketch.offset", json!({"curves": ["a1"], "distance": 2, "side": [10, 0], "instances": 2})); + let sk = sketch(&s); + let radii: Vec = o["sides"] + .as_array() + .unwrap() + .iter() + .map(|side| { + let id = side["curves"].as_array().unwrap().iter().filter_map(|v| v.as_str()).find(|id| id.starts_with('a')).unwrap(); + let CurveKind::Arc { c, a, .. } = sk.curves[sk.curve_index(id).unwrap()].kind else { panic!("{id} is no arc") }; + sk.points[c].pos.dist(sk.points[a].pos) + }) + .collect(); + assert!(radii.len() == 2 && (radii[0] - 3.0).abs() < 1e-6 && (radii[1] - 1.0).abs() < 1e-6, "{radii:?}"); +} + +/// `side` is read off the picked curve itself: an arc's end of a slot left a hair of radius by +/// the first instance still makes the second go on inward, and run out of room, and a tiny +/// distance far from the origin still goes the way `side` says (a step a thousandth of it is +/// lost to rounding there). +#[test] +fn offset_side_is_read_off_the_picked_curve() { + let mut s = new_sketch(); + let r = 2.0005; + run(&mut s, "sketch.line", json!({"points": [[-10, -r], [10, -r]]})); + run(&mut s, "sketch.arc.center_point", json!({"center": [10, 0], "start": [10, -r], "end": [10, r]})); + run(&mut s, "sketch.line", json!({"points": [[10, r], [-10, r]]})); + run(&mut s, "sketch.arc.center_point", json!({"center": [-10, 0], "start": [-10, r], "end": [-10, -r]})); + let error = s.execute("sketch.offset", &json!({"curves": ["a1"], "distance": 2, "side": [10, 0], "instances": 2})).unwrap_err().to_string(); + assert!(error.contains("instance 2 of 2"), "{error}"); + let mut s = new_sketch(); + run(&mut s, "sketch.line", json!({"points": [[0, 1e8], [20, 1e8]]})); + let o = run(&mut s, "sketch.offset", json!({"curves": ["l1"], "distance": 0.000001, "side": [10, 99999999]})); + let sk = sketch(&s); + let out = o["curves"][0].as_str().unwrap(); + let CurveKind::Line { a, .. } = sk.curves[sk.curve_index(out).unwrap()].kind else { panic!("{out} is no line") }; + assert!(sk.points[a].pos.y < 1e8, "below, toward `side`: {:?}", sk.points[a].pos); +} + #[test] fn offset_instances_count_must_be_a_whole_number_within_the_cap() { let mut s = new_sketch();