Files
cadcraft/crates/engine/tests/it/extrim.rs
T
okereke-dev c7e6b10f42 EXTRIM: find each stretch on the object itself, report one it can't find (review on #2808)
A large arc's flattened outline strays from the arc by up to a millionth of its radius, more
than the tolerance next to a short edge, so a stretch's pick could miss every piece and the
object was still counted as trimmed through. The pick is now brought onto the object's exact
lines and arcs and the pieces are measured exactly; a stretch still not found counts the object
as uncut. The menu entry moves to Additional Tools, and the es/zh rows go (feature PRs add
English only).
2026-11-24 22:38:00 -05:00

523 lines
21 KiB
Rust

//! EXTRIM (#2802): everything that crosses one cutting edge loses its part on the side picked.
use cadcraft_engine::doc::{EntityKind, Handle};
use cadcraft_engine::geom::Vec2;
use cadcraft_engine::{Input, Session};
use serde_json::{Value, json};
/// The handle a drawing command returns.
fn h(v: &Value) -> String {
v["handle"].as_str().or_else(|| v["handles"][0].as_str()).unwrap().to_string()
}
fn line(s: &mut Session, a: [f64; 2], b: [f64; 2]) -> String {
h(&s.execute("line", &json!({ "points": [a, b] })).unwrap())
}
fn exists(s: &Session, hex: &str) -> bool {
s.doc().unwrap().entity(Handle::parse_hex(hex).unwrap()).is_some()
}
/// The lines of the drawing, as their end points rounded to 1e-6.
fn lines(s: &Session) -> Vec<[[i64; 2]; 2]> {
let r = |x: f64| (x * 1e6).round() as i64;
let mut v: Vec<[[i64; 2]; 2]> = s
.doc()
.unwrap()
.model
.iter()
.filter_map(|e| match &e.kind {
EntityKind::Line(l) => {
let (a, b) = ([r(l.a.x), r(l.a.y)], [r(l.b.x), r(l.b.y)]);
Some(if a <= b { [a, b] } else { [b, a] })
}
_ => None,
})
.collect();
v.sort();
v
}
/// `[[x1, y1], [x2, y2]]` in the units of [`lines`].
fn seg(a: [f64; 2], b: [f64; 2]) -> [[i64; 2]; 2] {
let r = |x: f64| (x * 1e6).round() as i64;
let (a, b) = ([r(a[0]), r(a[1])], [r(b[0]), r(b[1])]);
if a <= b { [a, b] } else { [b, a] }
}
/// A two-point preview polyline as a [`seg`].
fn preview_segment(k: &EntityKind) -> [[i64; 2]; 2] {
let EntityKind::LwPolyline(p) = k else { panic!("{k:?}") };
let [a, b] = &p.vertices[..] else { panic!("{p:?}") };
seg([a.p.x, a.p.y], [b.p.x, b.p.y])
}
/// Every point of every object but `edge`, as drawn.
fn points_but(s: &Session, edge: &str) -> Vec<Vec2> {
let d = s.doc().unwrap();
let skip = Handle::parse_hex(edge).unwrap();
d.model.iter().filter(|e| e.handle != skip).flat_map(|e| cadcraft_engine::select::hit_polylines(d, e, 1e-4).into_iter().flatten()).collect()
}
/// The highest point of every object but `edge`.
fn top_but(s: &Session, edge: &str) -> f64 {
points_but(s, edge).iter().map(|p| p.y).fold(f64::NEG_INFINITY, f64::max)
}
fn extrim(s: &mut Session, edge: &str, side: [f64; 2]) -> Value {
s.execute("extrim", &json!({ "edge": edge, "side": side })).unwrap()
}
#[test]
fn trims_one_side_of_a_line() {
let mut s = Session::new();
let edge = line(&mut s, [0.0, 0.0], [10.0, 0.0]);
for x in [2.0, 5.0, 8.0] {
line(&mut s, [x, -5.0], [x, 5.0]);
}
let v = extrim(&mut s, &edge, [5.0, 3.0]);
assert_eq!(v["trimmed"], 3);
let mut want = vec![seg([0.0, 0.0], [10.0, 0.0])];
want.extend([2.0, 5.0, 8.0].map(|x| seg([x, -5.0], [x, 0.0])));
want.sort();
assert_eq!(lines(&s), want);
}
#[test]
fn trims_the_other_side_of_a_line() {
let mut s = Session::new();
let edge = line(&mut s, [0.0, 0.0], [10.0, 0.0]);
line(&mut s, [5.0, -5.0], [5.0, 5.0]);
extrim(&mut s, &edge, [5.0, -3.0]);
let mut want = vec![seg([0.0, 0.0], [10.0, 0.0]), seg([5.0, 0.0], [5.0, 5.0])];
want.sort();
assert_eq!(lines(&s), want);
}
#[test]
fn an_open_edge_trims_only_along_its_length() {
let mut s = Session::new();
let edge = line(&mut s, [0.0, 0.0], [10.0, 0.0]);
// Crosses the line the edge lies on, but past its end.
line(&mut s, [20.0, -5.0], [20.0, 5.0]);
let v = extrim(&mut s, &edge, [5.0, 3.0]);
assert_eq!(v["trimmed"], 0);
assert_eq!(lines(&s).len(), 2);
}
#[test]
fn inside_and_outside_a_circle() {
let mut s = Session::new();
let c = h(&s.execute("circle", &json!({ "center": [0, 0], "radius": 5 })).unwrap());
line(&mut s, [-10.0, 0.0], [10.0, 0.0]);
extrim(&mut s, &c, [0.0, 1.0]);
assert_eq!(lines(&s), vec![seg([-10.0, 0.0], [-5.0, 0.0]), seg([5.0, 0.0], [10.0, 0.0])]);
let mut s = Session::new();
let c = h(&s.execute("circle", &json!({ "center": [0, 0], "radius": 5 })).unwrap());
line(&mut s, [-10.0, 0.0], [10.0, 0.0]);
let v = extrim(&mut s, &c, [0.0, 8.0]);
assert_eq!(v["trimmed"], 1);
assert_eq!(lines(&s), vec![seg([-5.0, 0.0], [5.0, 0.0])]);
}
#[test]
fn inside_a_closed_polyline() {
let mut s = Session::new();
let r = h(&s.execute("pline", &json!({ "points": [[0, 0], [10, 0], [10, 6], [0, 6]], "closed": true })).unwrap());
line(&mut s, [-5.0, 3.0], [15.0, 3.0]);
line(&mut s, [5.0, -5.0], [5.0, 11.0]);
let v = extrim(&mut s, &r, [2.0, 2.0]);
assert_eq!(v["trimmed"], 2);
let mut want = vec![seg([-5.0, 3.0], [0.0, 3.0]), seg([10.0, 3.0], [15.0, 3.0]), seg([5.0, -5.0], [5.0, 0.0]), seg([5.0, 6.0], [5.0, 11.0])];
want.sort();
assert_eq!(lines(&s), want);
}
#[test]
fn inside_an_ellipse() {
let mut s = Session::new();
let e = h(&s.execute("ellipse", &json!({ "center": [0, 0], "major": [10, 0], "ratio": 0.5 })).unwrap());
line(&mut s, [-20.0, 0.0], [20.0, 0.0]);
extrim(&mut s, &e, [0.0, 1.0]);
// Cut where TRIM cuts at an ellipse: on its flattened outline, within 1e-4 of the axis.
let [[a, b], [c, d]] = lines(&s)[..] else { panic!("{:?}", lines(&s)) };
assert_eq!((a, d), ([-20_000_000, 0], [20_000_000, 0]), "the outer parts stay");
assert!((b[0] + 10_000_000).abs() <= 2_000 && (c[0] - 10_000_000).abs() <= 2_000, "{b:?} {c:?}");
}
#[test]
fn along_a_spline() {
let mut s = Session::new();
let sp = h(&s.execute("spline", &json!({ "control": [[0, 0], [3, 3], [6, -3], [9, 0]] })).unwrap());
// A vertical line through the spline's middle, where it crosses y = 0.
line(&mut s, [4.5, -5.0], [4.5, 5.0]);
let v = extrim(&mut s, &sp, [4.5, 4.0]);
assert_eq!(v["trimmed"], 1);
let [[a, b]] = lines(&s)[..] else { panic!("{:?}", lines(&s)) };
assert_eq!(a, [4_500_000, -5_000_000], "the lower part stays");
assert!(b[1].abs() < 10, "cut at the spline: {b:?}");
}
#[test]
fn an_object_crossing_twice_loses_the_stretch_between() {
let mut s = Session::new();
let edge = line(&mut s, [-10.0, 0.0], [10.0, 0.0]);
let u = h(&s.execute("pline", &json!({ "points": [[0, -2], [0, 2], [4, 2], [4, -2]] })).unwrap());
let v = extrim(&mut s, &edge, [2.0, 1.0]);
assert_eq!(v["trimmed"], 1);
// As TRIM leaves them: the first leg keeps the polyline's handle, the second is new.
assert!(exists(&s, &u));
let pieces = s.doc().unwrap().model.iter().filter(|e| matches!(e.kind, EntityKind::LwPolyline(_))).count();
assert_eq!(pieces, 2, "the two legs below the edge stay");
}
#[test]
fn leaves_the_edge_and_objects_that_do_not_cross_it() {
let mut s = Session::new();
let c = h(&s.execute("circle", &json!({ "center": [0, 0], "radius": 5 })).unwrap());
let inside = line(&mut s, [-1.0, 0.0], [1.0, 0.0]);
let v = extrim(&mut s, &c, [0.0, 2.0]);
assert_eq!(v["trimmed"], 0);
assert!(exists(&s, &c));
assert!(exists(&s, &inside), "an object wholly on the side but not crossing the edge stays");
}
#[test]
fn skips_locked_layers_blocks_and_hatches() {
let mut s = Session::new();
let edge = line(&mut s, [0.0, 0.0], [10.0, 0.0]);
s.execute("layer.new", &json!({ "name": "L1" })).unwrap();
let locked = line(&mut s, [2.0, -5.0], [2.0, 5.0]);
s.execute("chprop", &json!({ "handles": [locked.clone()], "layer": "L1" })).unwrap();
s.execute("layer.set", &json!({ "name": "L1", "locked": true })).unwrap();
let b = line(&mut s, [0.0, 0.0], [0.0, 10.0]);
s.execute("block", &json!({ "name": "B", "base": [0, 0], "handles": [b] })).unwrap();
s.execute("insert", &json!({ "name": "B", "at": [5, -5] })).unwrap();
let r = h(&s.execute("rectang", &json!({ "p1": [7, -2], "p2": [9, 2] })).unwrap());
s.execute("hatch", &json!({ "handles": [r] })).unwrap();
let v = extrim(&mut s, &edge, [5.0, 3.0]);
assert_eq!(v["skipped"]["locked"], 1);
assert!(v["skipped"]["other"].as_u64().unwrap() >= 2, "{v}");
assert!(exists(&s, &locked), "the locked line is not trimmed");
assert!(v["message"].as_str().unwrap().contains("locked"));
}
#[test]
fn one_undo_restores_everything() {
let mut s = Session::new();
let edge = line(&mut s, [0.0, 0.0], [10.0, 0.0]);
for x in [2.0, 5.0, 8.0] {
line(&mut s, [x, -5.0], [x, 5.0]);
}
let before = lines(&s);
extrim(&mut s, &edge, [5.0, 3.0]);
assert_ne!(lines(&s), before);
s.execute("undo", &json!({})).unwrap();
assert_eq!(lines(&s), before);
}
#[test]
fn never_leaves_a_piece_of_no_length() {
let mut s = Session::new();
let edge = line(&mut s, [0.0, 0.0], [10.0, 0.0]);
// Starts a hair below the edge: trimming above would leave a sliver.
line(&mut s, [5.0, -1e-8], [5.0, 5.0]);
extrim(&mut s, &edge, [5.0, 3.0]);
let d = s.doc().unwrap();
for e in d.model.iter() {
if let EntityKind::Line(l) = &e.kind {
assert!(l.a.xy().dist(l.b.xy()) > 1e-6, "sliver left: {l:?}");
}
}
}
#[test]
fn refuses_bad_input_and_changes_nothing() {
let mut s = Session::new();
let edge = line(&mut s, [0.0, 0.0], [10.0, 0.0]);
line(&mut s, [5.0, -5.0], [5.0, 5.0]);
let t = h(&s.execute("text", &json!({ "at": [0, 0], "text": "NOTE", "height": 0.25 })).unwrap());
let before = lines(&s);
for p in [
json!({ "side": [5, 3] }),
json!({ "edge": "zz", "side": [5, 3] }),
json!({ "edge": 5, "side": [5, 3] }),
json!({ "edge": "FFFFFF", "side": [5, 3] }),
json!({ "edge": t, "side": [5, 3] }),
json!({ "edge": edge, "side": [5, 0] }),
json!({ "edge": edge }),
json!({ "edge": edge, "side": ["a", 3] }),
] {
assert!(s.execute("extrim", &p).is_err(), "{p}");
}
assert_eq!(lines(&s), before);
}
#[test]
fn refuses_an_edge_in_another_space() {
let mut s = Session::new();
let edge = line(&mut s, [0.0, 0.0], [10.0, 0.0]);
s.execute("layout.set", &json!({ "name": "Layout1" })).unwrap();
assert!(s.execute("extrim", &json!({ "edge": edge, "side": [5, 3] })).is_err());
}
#[test]
fn prompts_for_the_edge_then_the_side() {
let mut s = Session::new();
let edge = line(&mut s, [0.0, 0.0], [10.0, 0.0]);
line(&mut s, [5.0, -5.0], [5.0, 5.0]);
s.set_selection(Vec::new());
s.start("extrim").unwrap();
assert!(s.prompt_text().contains("cutting edge"), "{}", s.prompt_text());
s.input(Input::Pick(vec![Handle::parse_hex(&edge).unwrap()])).unwrap();
assert!(s.prompt_text().contains("side"), "{}", s.prompt_text());
// The preview shows what goes: the upper half of the vertical line.
let shown = s.preview(Vec2::new(5.0, 3.0));
assert_eq!(shown.len(), 1);
s.input(Input::Point(Vec2::new(5.0, 3.0))).unwrap();
assert!(s.current_prompt().is_none());
let mut want = vec![seg([0.0, 0.0], [10.0, 0.0]), seg([5.0, -5.0], [5.0, 0.0])];
want.sort();
assert_eq!(lines(&s), want);
}
#[test]
fn uses_a_preselected_edge() {
let mut s = Session::new();
let edge = line(&mut s, [0.0, 0.0], [10.0, 0.0]);
line(&mut s, [5.0, -5.0], [5.0, 5.0]);
s.set_selection(vec![Handle::parse_hex(&edge).unwrap()]);
s.start("extrim").unwrap();
assert!(s.prompt_text().contains("side"), "{}", s.prompt_text());
s.input(Input::Point(Vec2::new(5.0, -3.0))).unwrap();
let mut want = vec![seg([0.0, 0.0], [10.0, 0.0]), seg([5.0, 0.0], [5.0, 5.0])];
want.sort();
assert_eq!(lines(&s), want);
}
#[test]
fn many_objects_in_one_call() {
let mut s = Session::new();
let c = h(&s.execute("circle", &json!({ "center": [0, 0], "radius": 50 })).unwrap());
for i in 0..500 {
let y = -45.0 + 90.0 * f64::from(i) / 500.0;
line(&mut s, [-100.0, y], [100.0, y]);
}
let v = extrim(&mut s, &c, [0.0, 0.5]);
assert_eq!(v["trimmed"], 500);
assert_eq!(lines(&s).len(), 1000, "two outer pieces of each line");
}
#[test]
fn refuses_more_objects_than_the_limit_and_changes_nothing() {
let mut s = Session::new();
let edge = line(&mut s, [0.0, 0.0], [10_002.0, 0.0]);
// One more crossing object than one call trims.
for i in 0..10_001 {
let x = 0.5 + f64::from(i);
line(&mut s, [x, -1.0], [x, 1.0]);
}
let before = lines(&s);
let e = s.execute("extrim", &json!({ "edge": edge, "side": [5, 0.5] })).unwrap_err();
assert!(e.to_string().contains("objects cross the cutting edge"), "{e}");
assert_eq!(lines(&s), before);
}
#[test]
fn refuses_an_edge_with_too_many_points_and_changes_nothing() {
let mut s = Session::new();
// A zigzag polyline whose corners alone are more points than the trace may have.
let pts: Vec<[f64; 2]> = (0..10_002).map(|i| [f64::from(i), if i % 2 == 0 { 0.0 } else { 1.0 }]).collect();
let edge = h(&s.execute("pline", &json!({ "points": pts })).unwrap());
line(&mut s, [5.5, -5.0], [5.5, 5.0]);
let before = lines(&s);
let e = s.execute("extrim", &json!({ "edge": edge, "side": [5.5, 3.0] })).unwrap_err();
assert!(e.to_string().contains("too complex"), "{e}");
assert_eq!(lines(&s), before);
}
#[test]
fn a_circle_crossed_once_is_reported_not_trimmed() {
let mut s = Session::new();
// The edge ends inside the circle, so it crosses it only once: no trim can open it.
let edge = line(&mut s, [0.0, 0.0], [10.0, 0.0]);
let c = h(&s.execute("circle", &json!({ "center": [10, 0], "radius": 2 })).unwrap());
let crossing = line(&mut s, [5.0, -5.0], [5.0, 5.0]);
s.set_selection(vec![Handle::parse_hex(&edge).unwrap()]);
s.start("extrim").unwrap();
// The preview shows only what will go: the upper half of the vertical line.
let shown = s.preview(Vec2::new(5.0, 3.0));
assert_eq!(shown.len(), 1);
assert_eq!(preview_segment(&shown[0].kind), seg([5.0, 0.0], [5.0, 5.0]));
s.cancel();
let v = extrim(&mut s, &edge, [5.0, 3.0]);
assert_eq!(v["trimmed"], 1);
assert_eq!(v["skipped"]["uncut"], 1, "{v}");
assert!(v["message"].as_str().unwrap().contains("could not be cut"), "{v}");
assert!(exists(&s, &c), "the circle stays whole");
// The vertical line keeps its handle and only its lower half.
let mut want = vec![seg([0.0, 0.0], [10.0, 0.0]), seg([5.0, -5.0], [5.0, 0.0])];
want.sort();
assert_eq!(lines(&s), want);
assert!(exists(&s, &crossing));
}
#[test]
fn an_open_polyline_drawn_back_to_its_start_loses_both_ends() {
let mut s = Session::new();
let edge = line(&mut s, [-5.0, 0.0], [10.0, 0.0]);
// Open, though its last point lands on its first.
h(&s.execute("pline", &json!({ "points": [[0, 2], [0, -2], [4, -2], [4, 2], [0, 2]] })).unwrap());
let v = extrim(&mut s, &edge, [2.0, 3.0]);
assert_eq!(v["trimmed"], 1);
assert!(top_but(&s, &edge) <= 1e-6, "nothing left above the edge: {:?}", points_but(&s, &edge));
}
#[test]
fn an_object_that_only_touches_the_edge_stays_and_is_not_previewed() {
let mut s = Session::new();
let edge = line(&mut s, [0.0, 0.0], [10.0, 0.0]);
let touching = line(&mut s, [5.0, 0.0], [5.0, 5.0]);
line(&mut s, [2.0, -5.0], [2.0, 5.0]);
s.set_selection(vec![Handle::parse_hex(&edge).unwrap()]);
s.start("extrim").unwrap();
let shown = s.preview(Vec2::new(5.0, 3.0));
assert_eq!(shown.len(), 1);
assert_eq!(preview_segment(&shown[0].kind), seg([2.0, 0.0], [2.0, 5.0]));
s.cancel();
let v = extrim(&mut s, &edge, [5.0, 3.0]);
assert_eq!(v["trimmed"], 1);
assert_eq!(v["skipped"]["uncut"], 0, "{v}");
assert!(exists(&s, &touching), "touching is not crossing");
}
#[test]
fn a_circle_crossed_at_its_seam_is_cut_in_half() {
let mut s = Session::new();
// The edge meets the circle at (5, 0), where its outline starts and ends.
let edge = line(&mut s, [-10.0, 0.0], [10.0, 0.0]);
s.execute("circle", &json!({ "center": [0, 0], "radius": 5 })).unwrap();
let v = extrim(&mut s, &edge, [0.0, 8.0]);
assert_eq!(v["trimmed"], 1);
assert!(top_but(&s, &edge) <= 1e-6);
assert!(s.doc().unwrap().model.iter().any(|e| matches!(e.kind, EntityKind::Arc(_))), "the lower half stays as an arc");
}
#[test]
fn trims_arcs_ellipses_splines_and_bulged_polylines() {
let mut s = Session::new();
let edge = line(&mut s, [-30.0, 0.0], [40.0, 0.0]);
s.execute("arc", &json!({ "center": [-20, -2], "radius": 5, "start": 0, "end": 180 })).unwrap();
s.execute("ellipse", &json!({ "center": [-5, 0], "major": [4, 0], "ratio": 0.5 })).unwrap();
s.execute("spline", &json!({ "control": [[4, -2], [7, 4], [10, -4], [13, 2]] })).unwrap();
s.execute("pline", &json!({ "vertices": [{ "p": [20, -1], "bulge": -1 }, [28, -1]] })).unwrap();
assert!(top_but(&s, &edge) > 1.0);
let v = extrim(&mut s, &edge, [0.0, 3.0]);
assert_eq!(v["trimmed"], 4, "{v}");
// Curves are cut where TRIM cuts them, on their flattened outlines.
assert!(top_but(&s, &edge) <= 1e-3, "{}", top_but(&s, &edge));
}
#[test]
fn an_object_with_two_stretches_on_the_side_loses_both() {
let mut s = Session::new();
let edge = line(&mut s, [-5.0, 0.0], [10.0, 0.0]);
s.execute("pline", &json!({ "points": [[0, -1], [1, 1], [2, -1], [3, 1], [4, -1]] })).unwrap();
let v = extrim(&mut s, &edge, [2.0, 3.0]);
assert_eq!(v["trimmed"], 1);
assert!(top_but(&s, &edge) <= 1e-6);
let pieces = s.doc().unwrap().model.iter().filter(|e| matches!(e.kind, EntityKind::LwPolyline(_))).count();
assert_eq!(pieces, 3, "the three dips below the edge stay");
}
#[test]
fn past_an_arcs_ends_the_side_is_inside_or_outside_its_circle() {
let mut s = Session::new();
let arc = h(&s.execute("arc", &json!({ "center": [0, 0], "radius": 5, "start": 0, "end": 90 })).unwrap());
line(&mut s, [1.0, 1.0], [6.0, 6.0]);
// Outside the circle, though left of the arc's direction at its nearer end.
extrim(&mut s, &arc, [4.0, -4.0]);
let r = 5.0 / 2f64.sqrt();
let [[a, b]] = lines(&s)[..] else { panic!("{:?}", lines(&s)) };
assert_eq!(a, [1_000_000, 1_000_000], "the inner part stays");
assert!((b[0] as f64 / 1e6 - r).abs() < 1e-3 && (b[1] as f64 / 1e6 - r).abs() < 1e-3, "{b:?}");
}
#[test]
fn refuses_more_stretches_than_the_limit_and_changes_nothing() {
let mut s = Session::new();
let edge = line(&mut s, [-1.0, 0.0], [10_003.0, 0.0]);
// One object dipping across the edge more times than one call trims.
let pts: Vec<[f64; 2]> = (0..20_004).map(|i| [f64::from(i) / 2.0, if i % 2 == 0 { -1.0 } else { 1.0 }]).collect();
let z = h(&s.execute("pline", &json!({ "points": pts })).unwrap());
let e = s.execute("extrim", &json!({ "edge": edge, "side": [5, 3] })).unwrap_err();
assert!(e.to_string().contains("stretches"), "{e}");
assert!(exists(&s, &z));
}
#[test]
fn a_long_polyline_edge_is_accepted() {
let mut s = Session::new();
// 6,000 straight segments: its corners fit the trace once its pieces are joined.
let pts: Vec<[f64; 2]> = (0..6_001).map(|i| [f64::from(i), if i % 2 == 0 { 0.0 } else { 0.5 }]).collect();
let edge = h(&s.execute("pline", &json!({ "points": pts })).unwrap());
line(&mut s, [10.2, -5.0], [10.2, 5.0]);
let v = extrim(&mut s, &edge, [10.2, 3.0]);
assert_eq!(v["trimmed"], 1);
}
#[test]
fn takes_exactly_one_cutting_edge() {
let mut s = Session::new();
let a = line(&mut s, [0.0, 0.0], [10.0, 0.0]);
let b = line(&mut s, [0.0, 2.0], [10.0, 2.0]);
line(&mut s, [5.0, -5.0], [5.0, 5.0]);
let before = lines(&s);
let e = s.execute("extrim", &json!({ "handles": [a.clone(), b.clone()], "side": [5, 3] })).unwrap_err();
assert!(e.to_string().contains("exactly one"), "{e}");
assert_eq!(lines(&s), before);
// At the prompt, a window of two objects is refused and the edge is asked for again.
s.set_selection(Vec::new());
s.start("extrim").unwrap();
s.input(Input::Pick(vec![Handle::parse_hex(&a).unwrap(), Handle::parse_hex(&b).unwrap()])).unwrap();
assert!(s.prompt_text().contains("cutting edge"), "{}", s.prompt_text());
s.input(Input::Pick(vec![Handle::parse_hex(&a).unwrap()])).unwrap();
assert!(s.prompt_text().contains("side"), "{}", s.prompt_text());
}
#[test]
fn large_radius_arcs_weaving_through_a_short_edge_lose_every_stretch_on_the_side() {
// #2808 review: a large arc's flattened outline strays from the arc by up to a millionth of its
// radius, far more than the tolerance next to a short edge, so the stretches must be found on
// the arc itself. Each arc weaves through the teeth of a short zigzag several times.
let mut s = Session::new();
let zig = |x: f64| {
let t = (x * 10.0).fract();
0.004 * if t < 0.5 { t * 2.0 } else { 2.0 - t * 2.0 }
};
let verts: Vec<[f64; 2]> = (0..=20).map(|i| [i as f64 * 0.05, zig(i as f64 * 0.05)]).collect();
let edge = h(&s.execute("pline", &json!({ "vertices": verts })).unwrap());
let radii = [50.0f64, 200.0, 1e3, 5e3, 2e4, 1e5];
for (k, r) in radii.iter().enumerate() {
// The arc's top a little above the middle of the teeth, its ends past the edge's.
let top = 0.002 + k as f64 * 1e-5;
let a = (0.6 / r).asin().to_degrees();
s.execute("arc", &json!({ "center": [0.5, top - r], "radius": r, "start": 90.0 - a, "end": 90.0 + a })).unwrap();
}
let v = extrim(&mut s, &edge, [0.5, 1.0]);
assert_eq!(v["trimmed"], radii.len(), "{v}");
assert_eq!(v["skipped"]["uncut"], 0, "{v}");
let d = s.doc().unwrap();
let skip = Handle::parse_hex(&edge).unwrap();
let above = d
.model
.iter()
.filter(|e| e.handle != skip)
.flat_map(|e| cadcraft_engine::select::hit_polylines(d, e, 1e-9).into_iter().flatten())
.filter(|p| p.x > 0.0 && p.x < 1.0)
.map(|p| p.y - zig(p.x))
.fold(f64::NEG_INFINITY, f64::max);
assert!(above <= 1e-6, "a stretch above the edge was left: {above:e}");
}