diff --git a/crates/engine/src/cmd/modify/trim.rs b/crates/engine/src/cmd/modify/trim.rs index b8ec3965d..c7fba40f4 100644 --- a/crates/engine/src/cmd/modify/trim.rs +++ b/crates/engine/src/cmd/modify/trim.rs @@ -291,11 +291,16 @@ const STRAIGHT: f64 = 1e-8; const MAX_SELF_PAIRS: usize = 1 << 24; const MAX_SELF_CROSSINGS: usize = 1 << 16; -/// How near a cut a pick is on it, along the object: AutoCAD 2024 trims nothing for a pick within -/// 5e-7 of a cut on an open polyline and trims for one 5.1e-7 away, from 1e-3 to 1e9 long (around -/// a closed rectangle it reached a little further, to 1e-6). +/// How near a cut a pick is on it, along the object. AutoCAD 2024 trims nothing for a pick that +/// near one of a polyline's own cuts, and passes over a cut by another edge, the trim running on +/// across it. On open polylines from 1e-3 to 1e9 long it was within 5e-7, and 5.1e-7 away trimmed +/// at the cut (around a closed rectangle it reached a little further, to 1e-6). const ON_CUT: f64 = 5e-7; +/// The refusal when every cut was passed over ([`ON_CUT`]): the object meets an edge, at the pick, +/// so Quick mode leaves it in place rather than erasing it as one no edge crosses. +const ON_THE_CUT: &str = "The pick is on the cut: nothing to trim."; + /// AutoCAD 2024's refusal to trim a closed polyline cut at one point only. pub(crate) const CUT_ONCE: &str = "Curve must intersect cutting edges at least twice."; @@ -384,8 +389,8 @@ fn self_cuts(segs: &[Segment], closed: bool, k: usize) -> Result> { } /// Trim a polyline at the piece containing `pick`, between the cuts by `cut` and, when it is one -/// of the cutting edges (`itself`), by its own ([`self_cuts`]). `None`: the pick is on a cut -/// ([`ON_CUT`]), which trims nothing. +/// of the cutting edges (`itself`), by its own ([`self_cuts`]). `None`: the pick is on one of its +/// own cuts ([`ON_CUT`]), which trims nothing. fn trim_polyline(p: &cadcraft_doc::LwPolyline, cut: &[Segment], pick: Vec2, itself: bool) -> Result>> { let pl = Polyline { vertices: p.vertices.clone(), closed: p.closed }; let segs = pl.segments(); @@ -408,27 +413,31 @@ fn trim_polyline(p: &cadcraft_doc::LwPolyline, cut: &[Segment], pick: Vec2, itse } acc += len; } - if itself { - ts.extend(self_cuts(&segs, p.closed, k)?); - } + let mut own = if itself { self_cuts(&segs, p.closed, k)? } else { Vec::new() }; // An open polyline's ends cut nothing; a closed one's start vertex cuts it like any other (a // cut there is at 0 or at its length, which the pieces below take as one). if !p.closed { ts.retain(|t| *t > 1e-9 && *t < total - 1e-9); - } - ts.sort_by(f64::total_cmp); - if ts.is_empty() { - return Err(EngineError::Other("Object does not intersect a cutting edge.".into())); + own.retain(|t| *t > 1e-9 && *t < total - 1e-9); } // How far apart two lengths along the polyline are, round its start too when it is closed. let apart = |s: f64, t: f64| if p.closed { (s - t).abs().min(total - (s - t).abs()) } else { (s - t).abs() }; + // A pick on one of its own cuts trims nothing; a cut by another edge there is passed over, and + // the piece runs on across it. + if own.iter().any(|t| apart(*t, tp) <= ON_CUT) { + return Ok(None); + } + let found = ts.len(); + ts.retain(|t| apart(*t, tp) > ON_CUT); + let passed = ts.len() < found; + ts.extend(own); + ts.sort_by(f64::total_cmp); + if ts.is_empty() { + return Err(EngineError::Other(if passed { ON_THE_CUT } else { "Object does not intersect a cutting edge." }.into())); + } if p.closed && ts.first().is_some_and(|f| ts.iter().all(|t| apart(*f, *t) <= ON_CUT)) { return Err(EngineError::Other(CUT_ONCE.into())); } - // A pick on a cut trims nothing. - if ts.iter().any(|t| apart(*t, tp) <= ON_CUT) { - return Ok(None); - } let lo = ts.iter().copied().filter(|t| *t < tp).fold(f64::NEG_INFINITY, f64::max); let hi = ts.iter().copied().filter(|t| *t > tp).fold(f64::INFINITY, f64::min); let mk = |vs: Vec| lwpoly(vs, false); diff --git a/crates/engine/tests/it/break_trim_polyline_rules.rs b/crates/engine/tests/it/break_trim_polyline_rules.rs index 41bf5ae88..82c369756 100644 --- a/crates/engine/tests/it/break_trim_polyline_rules.rs +++ b/crates/engine/tests/it/break_trim_polyline_rules.rs @@ -8,8 +8,9 @@ //! Quick mode) is also cut at its own vertices, where it crosses itself and where a vertex of it //! touches another of its segments. A piece whose ends meet there is left closed. A vertex //! between two line segments in line (within 1e-8, straight on or doubling back) doesn't cut it. -//! - A pick within 5e-7 of a cut along the polyline, whatever its size, trims nothing. An edge -//! through a closed polyline's start vertex cuts it there. +//! - A pick within 5e-7 of one of the polyline's own cuts, whatever its size, trims nothing; a cut +//! by another edge that near the pick is passed over. An edge through a closed polyline's start +//! vertex cuts it there. //! //! Quick mode was measured in AutoCAD 2024 itself, picking on screen: its scripted TRIM runs //! Standard mode only. @@ -325,16 +326,34 @@ fn a_pick_half_a_unit_from_a_vertex_1e9_out_trims_the_segment_it_is_on() { // ---------------- a pick on a cut ---------------- -/// The polyline `v`, with a line from `a` to `b` when given, trimmed at `pick` with every object -/// as an edge, or only the line when `only_line`; returns the polyline's handle. -fn trimmed(v: Value, closed: bool, edge: Option<([f64; 2], [f64; 2])>, only_line: bool, pick: [f64; 2]) -> (Session, Handle) { +type Edge = ([f64; 2], [f64; 2]); + +/// The polyline `v`, with lines `edges`, trimmed at `pick` with every object as an edge, or only +/// the lines when `only_lines`; the polyline's handle and whether the trim was made. +fn attempt(v: Value, closed: bool, edges: &[Edge], only_lines: bool, pick: [f64; 2]) -> (Session, Handle, bool) { let mut s = Session::new(); let p = pline(&mut s, v, closed); - let l = edge.map(|(a, b)| line(&mut s, a, b)); - trim(&mut s, p, pick, l.filter(|_| only_line).as_ref().map(std::slice::from_ref)); + let ls: Vec = edges.iter().map(|(a, b)| line(&mut s, *a, *b).hex()).collect(); + let mut params = json!({ "handle": p.hex(), "pick": pick }); + if only_lines { + params["edges"] = json!(ls); + } + let made = s.execute("trim", ¶ms).is_ok(); + (s, p, made) +} + +/// [`attempt`] with at most one line, which must trim. +fn trimmed(v: Value, closed: bool, edge: Option, only_line: bool, pick: [f64; 2]) -> (Session, Handle) { + let (s, p, made) = attempt(v, closed, edge.as_slice(), only_line, pick); + assert!(made, "{pick:?}"); (s, p) } +/// A vertical line through x = `x`. +fn at(x: f64) -> Edge { + ([x, -5.0], [x, 5.0]) +} + #[test] fn a_pick_on_a_cut_trims_nothing() { // The vertex (10, 0) is the only cut. A pick whose nearest point is within 5e-7 of it, on it @@ -370,31 +389,57 @@ fn a_pick_on_a_cut_is_within_the_same_distance_at_any_size() { } #[test] -fn a_pick_on_a_cut_by_another_edge_trims_nothing() { - // The line x = 5 is the edge: picks on it and 4e-7 past it leave the polyline as it was, one - // 6e-7 past it trims the rest. - let v = || json!([[0, 0], [10, 0], [10, 10]]); - let edge = Some(([5.0, -5.0], [5.0, 5.0])); +fn a_cut_by_another_edge_at_the_pick_is_passed_over() { + // A pick within 5e-7 of a cut by another edge runs the piece on across it, to the next cut + // either side; 6e-7 away the cut holds. + let flat = || json!([[0, 0], [10, 0]]); + let piece = |a: f64, b: f64| [(a, 0.0, 0.0), (b, 0.0, 0.0)]; + for pick in [[3.0, 0.0], [3.0000004, 0.0]] { + let (s, p, _) = attempt(flat(), false, &[at(3.0), at(7.0)], true, pick); + expect(&s, p, &[(true, false, &piece(7.0, 10.0))]); + } + let (s, p, _) = attempt(flat(), false, &[at(3.0), at(7.0)], true, [3.0000006, 0.0]); + expect(&s, p, &[(true, false, &piece(0.0, 3.0)), (false, false, &piece(7.0, 10.0))]); + // Lines at 3, 5 and 7: picked on 5, 3 to 7 goes; picked on 7, 5 to the end. + let (s, p, _) = attempt(flat(), false, &[at(3.0), at(5.0), at(7.0)], true, [5.0, 0.0]); + expect(&s, p, &[(true, false, &piece(0.0, 3.0)), (false, false, &piece(7.0, 10.0))]); + let (s, p, _) = attempt(flat(), false, &[at(3.0), at(5.0), at(7.0)], true, [7.0, 0.0]); + expect(&s, p, &[(true, false, &piece(0.0, 5.0))]); + // Every object an edge: on to the polyline's own vertex (10, 0). + let (s, p, _) = attempt(json!([[0, 0], [10, 0], [10, 10]]), false, &[at(5.0)], false, [5.0, 0.0]); + expect(&s, p, &[(true, false, &[(10.0, 0.0, 0.0), (10.0, 10.0, 0.0)])]); + // A closed rectangle cut at x = 30 and 70 top and bottom, picked on the bottom at 30: round + // its start vertex, from 30 on the top to 70 on the bottom. + let tall = |x: f64| ([x, -5.0], [x, 15.0]); + let (s, p, _) = attempt(json!([[0, 0], [100, 0], [100, 10], [0, 10]]), true, &[tall(30.0), tall(70.0)], true, [30.0, 0.0]); + expect(&s, p, &[(true, false, &[(70.0, 0.0, 0.0), (100.0, 0.0, 0.0), (100.0, 10.0, 0.0), (30.0, 10.0, 0.0)])]); + // One line only, picked on its cut or 4e-7 past it: no cut is left, and nothing is trimmed. for pick in [[5.0, 0.0], [5.0000004, 0.0]] { - let (s, p) = trimmed(v(), false, edge, true, pick); + let (s, p, made) = attempt(json!([[0, 0], [10, 0], [10, 10]]), false, &[at(5.0)], true, pick); + assert!(!made); expect(&s, p, &[(true, false, &[(0.0, 0.0, 0.0), (10.0, 0.0, 0.0), (10.0, 10.0, 0.0)])]); } - let (s, p) = trimmed(v(), false, edge, true, [5.0000006, 0.0]); - expect(&s, p, &[(true, false, &[(0.0, 0.0, 0.0), (5.0, 0.0, 0.0)])]); + // In Quick mode too: the polyline meets the line, so it isn't erased as one no edge crosses. + let mut s = Session::new(); + let p = pline(&mut s, json!([[0, 0], [10, 0]]), false); + line(&mut s, [5.0, -5.0], [5.0, 5.0]); + assert!(s.execute("trim", &json!({ "handle": p.hex(), "pick": [5, 0], "mode": "quick" })).is_err()); + expect(&s, p, &[(true, false, &piece(0.0, 10.0))]); } #[test] fn a_pick_on_a_vertex_an_edge_runs_through_trims_nothing() { - // The line's cut is found on the segments either side of the vertex, a rounding apart: the - // pick on the vertex still leaves the polyline as it was, rather than splitting it there. + // The line's cut is found on the segments either side of the vertex, a rounding apart. Both + // are passed over, so no cut is left and the polyline stays as it was, not split there. let cases = [ ([[5.5, 1.1], [23.3, 7.7], [30.1, 29.9]], ([23.3, 7.7], [45.7, 2.3])), ([[5.5, 1.1], [23.3, 7.7], [30.1, 29.9]], ([2.0 * 23.3 - 45.7, 2.0 * 7.7 - 2.3], [45.7, 2.3])), ([[1.3, 2.7], [37.9, 11.3], [55.1, 48.7]], ([4.5, 43.5], [71.3, -20.9])), ]; for (v, edge) in cases { - let (s, p) = trimmed(json!(v), false, Some(edge), true, v[1]); + let (s, p, made) = attempt(json!(v), false, &[edge], true, v[1]); let whole: Vec = v.iter().map(|q| (q[0], q[1], 0.0)).collect(); + assert!(!made); expect(&s, p, &[(true, false, &whole)]); } } @@ -402,7 +447,8 @@ fn a_pick_on_a_vertex_an_edge_runs_through_trims_nothing() { #[test] fn an_edge_through_a_closed_polylines_start_vertex_cuts_it_there() { // The line cuts the rectangle at (0, 0) and at (10, 10) on its top. The pick takes the piece - // it is on, whichever vertex the rectangle starts at; 4e-7 from (0, 0) it trims nothing. + // it is on, whichever vertex the rectangle starts at. 4e-7 from (0, 0) that cut is passed + // over, which leaves one, and the trim is refused. let rect = || json!([[0, 0], [100, 0], [100, 10], [0, 10]]); let edge = Some(([-5.0, -5.0], [15.0, 15.0])); let left: [Vertex; 3] = [(10.0, 10.0, 0.0), (0.0, 10.0, 0.0), (0.0, 0.0, 0.0)]; @@ -413,7 +459,8 @@ fn an_edge_through_a_closed_polylines_start_vertex_cuts_it_there() { let (s, p) = trimmed(rect(), true, edge, true, [0.0, 5.0]); expect(&s, p, &[(true, false, &[(0.0, 0.0, 0.0), (100.0, 0.0, 0.0), (100.0, 10.0, 0.0), (10.0, 10.0, 0.0)])]); let whole: [Vertex; 4] = [(0.0, 0.0, 0.0), (100.0, 0.0, 0.0), (100.0, 10.0, 0.0), (0.0, 10.0, 0.0)]; - let (s, p) = trimmed(rect(), true, edge, true, [0.0, 4e-7]); + let (s, p, made) = attempt(rect(), true, edge.as_slice(), true, [0.0, 4e-7]); + assert!(!made); expect(&s, p, &[(true, true, &whole)]); // The rectangle alone, picked on its start vertex, and 4e-7 from it on its last segment. for pick in [[0.0, 0.0], [0.0, 4e-7]] {