From c0da9fe861084b347d1d718f08b01923d864e7d6 Mon Sep 17 00:00:00 2001 From: DuBearte <130190089+DuBearte@users.noreply.github.com> Date: Sat, 24 Oct 2026 11:26:36 +0000 Subject: [PATCH] Trim cuts a line at its tangent point on a circle (#5074) (#5163) * claim: #5074 Co-Authored-By: Claude Opus 5.5 * Trim cuts a line at its tangent point on a circle (#5074) A line the solver made tangent to a circle touches it within rounding noise, and the exact intersection missed the touch whenever the noise fell outside, so trim found no cut there and deleted the whole line. Trim takes its cut points from intersections_within at its own merge distance, as profile detection does (#588). Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- crates/engine/src/cmd/sketch_modify.rs | 7 +++-- crates/engine/src/cmd/sketch_modify_tests.rs | 31 ++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/cmd/sketch_modify.rs b/crates/engine/src/cmd/sketch_modify.rs index cead75217..662631325 100644 --- a/crates/engine/src/cmd/sketch_modify.rs +++ b/crates/engine/src/cmd/sketch_modify.rs @@ -6,7 +6,7 @@ use std::f64::consts::TAU; use serde_json::{Value, json}; use solidcraft_doc::ProfileSel; use solidcraft_geom::Vec2; -use solidcraft_sketch::{ConstraintKind, Curve, CurveKind, Shape, Sketch, intersections}; +use solidcraft_sketch::{ConstraintKind, Curve, CurveKind, Shape, Sketch, intersections, intersections_within}; use super::sketch::{add_c, edit, ids_of, solve_current}; use super::{CommandSpec, in_sketch}; @@ -144,7 +144,10 @@ fn cut_points(sk: &Sketch, ci: usize) -> Vec<(f64, Vec2, usize)> { continue; } let Some(other) = sk.shape(oi) else { continue }; - let mut pts = intersections(&sh, &other); + // A line or circle the solver made tangent to a circle touches it within rounding noise, + // and the exact roots miss it whenever the noise falls outside: the touch counts as a + // cut, as profile detection counts it (#588), or trim deletes the whole curve (#5074). + let mut pts = intersections_within(&sh, &other, TOL * 10.0); // Overlapping or touching ends that the analytic intersection misses (parallel lines). if let Shape::Line { a, b } = other { pts.extend([a, b]); diff --git a/crates/engine/src/cmd/sketch_modify_tests.rs b/crates/engine/src/cmd/sketch_modify_tests.rs index f6d7326ce..e9e153305 100644 --- a/crates/engine/src/cmd/sketch_modify_tests.rs +++ b/crates/engine/src/cmd/sketch_modify_tests.rs @@ -1972,3 +1972,34 @@ fn fillet_radius_accepts_a_leading_equals_sign() { let plain = run(&mut s, "sketch.fillet", json!({"point": "c.end", "radius": "= 2 mm"})); assert!(plain["curves"].is_array()); } + +/// #5074: lines made tangent to a circle by the solver touch it within rounding noise, and the +/// noise often falls outside, so trim found no cut at the touch and deleted the whole line. Each +/// line is trimmed back to its tangent point instead: the belt shape (two lines from one point) +/// and two parallel lines. +#[test] +fn trim_cuts_a_line_at_its_tangent_point_on_a_circle() { + // (start, end, where the trim picks: past the tangent point) + let belt = [([30.0, 0.0], [-8.0, 12.0], [-7.0, 12.5]), ([30.0, 0.0], [-8.0, -12.0], [-7.0, -12.5])]; + let parallel = [([-20.0, 11.0], [20.0, 11.0], [-15.0, 10.0]), ([-20.0, -11.0], [20.0, -11.0], [-15.0, -10.0])]; + for (name, lines) in [("belt", belt), ("parallel", parallel)] { + let mut s = new_sketch(); + run(&mut s, "sketch.circle.center", json!({"center": [0, 0], "radius": 10})); + for (a, b, _) in lines { + run(&mut s, "sketch.line", json!({"points": [a, b], "connect": false, "infer": false})); + } + run(&mut s, "sketch.constraint.tangent", json!({"a": "l1", "b": "c1"})); + run(&mut s, "sketch.constraint.tangent", json!({"a": "l2", "b": "c1"})); + for (line, (_, _, at)) in ["l1", "l2"].into_iter().zip(lines) { + let r = run(&mut s, "sketch.trim", json!({"curve": line, "at": at})); + assert_eq!(r["result"]["trimmed"], line, "{name}: {line} was {}", r["result"]); + let sk = sketch(&s); + let at = |r: &str| sk.point(sk.resolve_point(r).unwrap()).unwrap(); + // The solve may size the circle to the lines (its radius isn't dimensioned). + let radius = sk.radius(sk.curve_index("c1").unwrap()).unwrap(); + let touches = [format!("{line}.start"), format!("{line}.end")].iter().any(|e| (at(e).len() - radius).abs() < 1e-6); + assert!(touches, "{name}: {line} now ends at its tangent point"); + } + assert_eq!(sketch(&s).curves.len(), 3, "{name}: the circle and both lines stay"); + } +}