mirror of
https://github.com/storytold/cadcraft.git
synced 2026-12-01 10:13:53 -06:00
EXTRIM: delete only pieces with no length at all, leave the too-short stretch out of the preview, rebuild the preview when the frozen layers change (CodeRabbit on #3526)
This commit is contained in:
@@ -465,12 +465,17 @@ fn midpoint(pts: &[Vec2]) -> Option<Vec2> {
|
||||
pts.last().copied()
|
||||
}
|
||||
|
||||
/// The length of an object's drawn outline.
|
||||
/// Whether `e` has no length to speak of: shorter than f64 can tell apart where it is.
|
||||
/// Whether `e` has no length at all: every point of its outline the same f64 point.
|
||||
fn degenerate(d: &Drawing, e: &Entity, tol: f64) -> bool {
|
||||
let b = entity_bounds(d, e, 0);
|
||||
let size = [b.min.len(), b.max.len()].into_iter().filter(|v| v.is_finite()).fold(0.0, f64::max);
|
||||
length(d, e, tol) <= 64.0 * f64::EPSILON * (1.0 + size)
|
||||
length(d, e, tol) <= 0.0
|
||||
}
|
||||
|
||||
/// The length of an object's drawn outline.
|
||||
/// Whether TRIM would pass over a cut at `st`'s end (within ON_CUT of its pick) and run on to the
|
||||
/// next one, taking more than `st`: such a stretch is left, by the trim and by its preview.
|
||||
fn too_short(st: &Stretch) -> bool {
|
||||
let ends = st.parts.first().and_then(|p| p.first()).into_iter().chain(st.parts.last().and_then(|p| p.last()));
|
||||
ends.into_iter().any(|e| e.dist(st.pick) <= 2.0 * ON_CUT)
|
||||
}
|
||||
|
||||
fn length(d: &Drawing, e: &Entity, tol: f64) -> f64 {
|
||||
@@ -515,8 +520,7 @@ fn trim_side(s: &mut Session, edge: &Edge, side: Side) -> Result<Value> {
|
||||
for st in stretches {
|
||||
// TRIM passes over a cut within ON_CUT of the pick and runs on to the next one, which
|
||||
// would take more than this stretch: a stretch that short is left, and said so.
|
||||
let ends = st.parts.first().and_then(|p| p.first()).into_iter().chain(st.parts.last().and_then(|p| p.last()));
|
||||
if ends.into_iter().any(|e| e.dist(st.pick) <= 2.0 * ON_CUT) {
|
||||
if too_short(&st) {
|
||||
if !uncut.contains(&h) {
|
||||
uncut.push(h);
|
||||
}
|
||||
@@ -633,7 +637,7 @@ fn preview(d: &Drawing, space: &cadcraft_doc::Space, frozen: &[String], edge: &E
|
||||
let mut out = Vec::new();
|
||||
for (h, stretches) in plan.work {
|
||||
let Some(e) = d.entity(h) else { continue };
|
||||
for part in stretches.into_iter().flat_map(|st| st.parts).filter(|p| p.len() >= 2) {
|
||||
for part in stretches.into_iter().filter(|st| !too_short(st)).flat_map(|st| st.parts).filter(|p| p.len() >= 2) {
|
||||
let mut ent = Entity::new(Handle(0), lwpoly(part.into_iter().map(cadcraft_geom::PolyVertex::new).collect(), false));
|
||||
ent.common.color = Color::Index(1);
|
||||
ent.common.layer = e.common.layer.clone();
|
||||
@@ -647,8 +651,9 @@ fn preview(d: &Drawing, space: &cadcraft_doc::Space, frozen: &[String], edge: &E
|
||||
/// with the parts that will go shown red as the cursor moves.
|
||||
pub(super) struct ExtrimM {
|
||||
edge: Option<Edge>,
|
||||
/// The preview for each side (left, right), worked out once.
|
||||
previews: RefCell<[Option<Vec<Entity>>; 2]>,
|
||||
/// The preview for each side (left, right), worked out once for the layers the active
|
||||
/// viewport freezes then: a VPLAYER or MSPACE run meanwhile changes what the trim leaves.
|
||||
previews: RefCell<[Option<(Vec<String>, Vec<Entity>)>; 2]>,
|
||||
}
|
||||
|
||||
impl ExtrimM {
|
||||
@@ -725,14 +730,15 @@ impl Interactive for ExtrimM {
|
||||
let Some(edge) = &self.edge else { return Vec::new() };
|
||||
let Some(side) = edge.side(cursor) else { return Vec::new() };
|
||||
let slot = usize::from(side == Side::Right);
|
||||
let cached = self.previews.borrow().get(slot).cloned().flatten();
|
||||
let frozen = s.viewport_frozen();
|
||||
let cached = self.previews.borrow().get(slot).cloned().flatten().filter(|(f, _)| f.as_slice() == frozen).map(|(_, v)| v);
|
||||
let mut v = match cached {
|
||||
Some(v) => v,
|
||||
None => {
|
||||
let Ok(d) = s.doc() else { return Vec::new() };
|
||||
let v = preview(d, &s.space(), s.viewport_frozen(), edge, side);
|
||||
let v = preview(d, &s.space(), frozen, edge, side);
|
||||
if let Some(c) = self.previews.borrow_mut().get_mut(slot) {
|
||||
*c = Some(v.clone());
|
||||
*c = Some((frozen.to_vec(), v.clone()));
|
||||
}
|
||||
v
|
||||
}
|
||||
|
||||
@@ -568,3 +568,47 @@ fn the_preview_cap_counts_drawn_parts_not_objects() {
|
||||
assert!(top_but(&s, &edge) <= 1e-6, "{k} teeth: {}", top_but(&s, &edge));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_short_piece_far_from_the_origin_stays_too() {
|
||||
// #3526 review: the deletion test is no tolerance that grows with the distance from the
|
||||
// origin. Near x = 1e9 a 1e-5 piece is a real piece.
|
||||
let mut s = Session::new();
|
||||
let edge = line(&mut s, [1e9, 0.0], [1e9 + 10.0, 0.0]);
|
||||
line(&mut s, [1e9 + 5.0, -1e-5], [1e9 + 5.0, 5.0]);
|
||||
let v = extrim(&mut s, &edge, [1e9 + 5.0, 3.0]);
|
||||
assert_eq!(v["trimmed"], 1, "{v}");
|
||||
let d = s.doc().unwrap();
|
||||
let kept: Vec<f64> = d
|
||||
.model
|
||||
.iter()
|
||||
.filter_map(|e| match &e.kind {
|
||||
EntityKind::Line(l) if (l.a.x - (1e9 + 5.0)).abs() < 1e-6 => Some(l.a.xy().dist(l.b.xy())),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(kept.len(), 1, "{kept:?}");
|
||||
assert!((kept[0] - 1e-5).abs() < 1e-9, "{kept:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_preview_leaves_out_a_stretch_the_trim_leaves() {
|
||||
// #3526 review: the poke too short for TRIM to cut alone is not previewed as going.
|
||||
let mut s = Session::new();
|
||||
let edge = line(&mut s, [0.0, 0.0], [0.01, 0.0]);
|
||||
let verts = json!([[0.003, -5.0], [0.003, 4e-7], [0.0031, -5.0], [0.0031, 5.0]]);
|
||||
s.execute("pline", &json!({ "vertices": verts })).unwrap();
|
||||
s.set_selection(Vec::new());
|
||||
s.start("extrim").unwrap();
|
||||
s.input(Input::Pick(vec![Handle::parse_hex(&edge).unwrap()])).unwrap();
|
||||
let shown = s.preview(Vec2::new(0.005, 3.0));
|
||||
let xs: Vec<f64> = shown
|
||||
.iter()
|
||||
.flat_map(|e| match &e.kind {
|
||||
EntityKind::LwPolyline(p) => p.vertices.iter().map(|v| v.p.x).collect(),
|
||||
_ => Vec::new(),
|
||||
})
|
||||
.collect();
|
||||
assert!(!xs.is_empty(), "the rise is previewed");
|
||||
assert!(xs.iter().all(|x| *x > 0.00305), "the poke is not: {xs:?}");
|
||||
}
|
||||
|
||||
@@ -795,3 +795,19 @@ fn extrim_through_a_viewport_leaves_layers_frozen_there() {
|
||||
assert_eq!(edited(&s, hidden), hidden_trimmed, "frozen in this viewport: {frozen_here}");
|
||||
}
|
||||
}
|
||||
|
||||
/// #3526 review: EXTRIM's preview follows a change of the frozen layers while it runs.
|
||||
#[test]
|
||||
fn extrims_preview_follows_the_frozen_layers_while_it_runs() {
|
||||
let Sheet { mut s, b, .. } = sheet();
|
||||
enter(&mut s, &b);
|
||||
let edge = line(&mut s, [5.0, -5.0], [5.0, 5.0]);
|
||||
s.set_selection(Vec::new());
|
||||
s.start("extrim").unwrap();
|
||||
s.input(cadcraft_engine::Input::Pick(vec![edge])).unwrap();
|
||||
let side = Vec2::new(8.0, 1.0);
|
||||
assert_eq!(s.preview(side).len(), 2, "both lines lose their right half");
|
||||
s.execute("vplayer", &json!({ "handles": [b], "freeze": ["Hidden"] })).unwrap();
|
||||
assert!(s.running.is_some(), "EXTRIM is still running");
|
||||
assert_eq!(s.preview(side).len(), 1, "the line now frozen here is left, and not previewed");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user