From 9c2b8a2589aff9ee4e6988c267b16d074cbae618 Mon Sep 17 00:00:00 2001 From: 2ez4gcx <108074836+2ez4gcx@users.noreply.github.com> Date: Mon, 2 Nov 2026 18:15:46 +0700 Subject: [PATCH] TRIM: a pick on a cut takes the piece next to that cut, by index The split point was moved 2 x 4 ULP off a cut the pick was on. When two cuts are closer than that (1e9 out, a segment a few ULP long) it moved past the other cut too, and the long segment before was trimmed instead of the short one picked. The cut the pick is on (the nearest within rounding) now bounds the piece directly: with the next cut when the pick is at its segment's start, with the previous one otherwise. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/modify/trim.rs | 21 ++++++++++--------- .../tests/it/break_trim_polyline_rules.rs | 19 +++++++++++++++++ 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/crates/engine/src/cmd/modify/trim.rs b/crates/engine/src/cmd/modify/trim.rs index 383e4c50a..e5bef9b8e 100644 --- a/crates/engine/src/cmd/modify/trim.rs +++ b/crates/engine/src/cmd/modify/trim.rs @@ -370,18 +370,19 @@ fn trim_polyline(p: &cadcraft_doc::LwPolyline, cut: &[Segment], pick: Vec2, itse } // A pick on a cut (a vertex picked exactly: the same length within rounding) takes the piece // of the segment it was found on, rather than neither piece (which would erase the whole - // polyline): the cuts are split just past that cut when the pick is at its segment's start, - // just before it otherwise. A pick off every cut splits them where it is. + // polyline): from that cut to the next when the pick is at its segment's start, from the one + // before to that cut otherwise. A pick off every cut takes the piece between the cuts either + // side of it. let on = 4.0 * f64::EPSILON * (total + 1.0); - let split = if !ts.iter().any(|t| (t - tp).abs() <= on) { - tp - } else if at_end > 0.0 { - tp + 2.0 * on - } else { - tp - 2.0 * on + let on_cut = ts.iter().enumerate().map(|(i, t)| (i, (t - tp).abs())).filter(|(_, d)| *d <= on).min_by(|a, b| a.1.total_cmp(&b.1)).map(|(i, _)| i); + let (lo, hi) = match on_cut { + Some(i) if at_end > 0.0 => (ts.get(i).copied().unwrap_or(f64::NEG_INFINITY), ts.get(i + 1).copied().unwrap_or(f64::INFINITY)), + Some(i) => (i.checked_sub(1).and_then(|j| ts.get(j)).copied().unwrap_or(f64::NEG_INFINITY), ts.get(i).copied().unwrap_or(f64::INFINITY)), + None => ( + ts.iter().copied().filter(|t| *t < tp).fold(f64::NEG_INFINITY, f64::max), + ts.iter().copied().filter(|t| *t > tp).fold(f64::INFINITY, f64::min), + ), }; - let lo = ts.iter().copied().filter(|t| *t < split).fold(f64::NEG_INFINITY, f64::max); - let hi = ts.iter().copied().filter(|t| *t > split).fold(f64::INFINITY, f64::min); let mk = |vs: Vec| lwpoly(vs, false); let mut out = Vec::new(); if p.closed { diff --git a/crates/engine/tests/it/break_trim_polyline_rules.rs b/crates/engine/tests/it/break_trim_polyline_rules.rs index f5d05257d..7fe34c08c 100644 --- a/crates/engine/tests/it/break_trim_polyline_rules.rs +++ b/crates/engine/tests/it/break_trim_polyline_rules.rs @@ -336,6 +336,25 @@ fn a_pick_inside_a_segment_stays_on_its_side_of_a_cut() { expect(&s, p, &[(true, false, &[(0.0, 0.0, 0.0), (1e9, 0.0, 0.0)])]); } +#[test] +fn a_pick_on_a_vertex_next_to_a_very_short_segment_takes_that_segment() { + // 1e9 out, a segment 5e-7 long, a few roundings of the length, so both its ends are within + // rounding of a pick on the vertex it ends at: that pick takes it, not the long segment before. + let mut s = Session::new(); + let p = pline(&mut s, json!([[0, 0], [1e9, 0], [1e9, 5e-7], [1e9, 10]]), false); + trim(&mut s, p, [1e9, 5e-7], None); + expect(&s, p, &[(true, false, &[(0.0, 0.0, 0.0), (1e9, 0.0, 0.0)]), (false, false, &[(1e9, 5e-7, 0.0), (1e9, 10.0, 0.0)])]); +} + +#[test] +fn a_pick_on_a_closed_polylines_start_vertex_takes_its_first_segment() { + // Not measured. The pick is at the start of the first segment, which it was found on. + let mut s = Session::new(); + let p = pline(&mut s, json!([[0, 0], [10, 0], [10, 10], [0, 10]]), true); + trim(&mut s, p, [0.0, 0.0], None); + expect(&s, p, &[(true, false, &[(10.0, 0.0, 0.0), (10.0, 10.0, 0.0), (0.0, 10.0, 0.0), (0.0, 0.0, 0.0)])]); +} + #[test] fn a_piece_whose_ends_only_come_near_stays_open_far_from_the_origin() { // 1e9 out, as survey coordinates in millimetres are: the piece left runs from the line's cut