Merge pull request #8213 from dotCooCoo/perf/trim-selection-one-pass

Trim at Crossings: the selection is updated in one pass
This commit is contained in:
BFlat
2026-11-17 09:46:42 +07:00
committed by GitHub
3 changed files with 154 additions and 9 deletions
+17 -8
View File
@@ -4,6 +4,8 @@
//! be edited, selected or not. What it removes is worked out by
//! [`vectorcraft_tools::addons::trim::plan`], which the tool also uses to show it beforehand.
use std::collections::HashSet;
use serde_json::{Value, json};
use vectorcraft_doc::{NodeId, NodeKind};
use vectorcraft_geom::{Point, Rect};
@@ -100,20 +102,21 @@ fn trim(s: &mut Session, p: &Value) -> Result<Value> {
compounds.dedup();
let mut beside = Beside::replacing(d, &trimmed);
let mut all = vec![];
// The selection is read through a set and changed once at the end, so a trim of many
// paths does not go through the selected objects once per path and per piece.
let mut selected_ids: HashSet<NodeId> = sel.objects.iter().copied().collect();
let mut joining: Vec<NodeId> = vec![];
let mut gone: Vec<NodeId> = vec![];
for (id, pieces) in plan {
let selected = sel.contains(id);
let selected = selected_ids.contains(&id);
sel.anchors.remove(&id);
sel.segments.remove(&id);
let made = replace_with_pieces(d, &mut beside, id, pieces)?;
// The pieces of a selected path are selected; what was not selected stays so.
if made.is_empty() {
sel.remove(id);
gone.push(id);
} else if selected {
for n in &made {
if !sel.contains(*n) {
sel.add(*n);
}
}
joining.extend(made.iter().copied().filter(|n| selected_ids.insert(*n)));
}
all.extend(made);
}
@@ -121,9 +124,15 @@ fn trim(s: &mut Session, p: &Value) -> Result<Value> {
for c in compounds {
if d.node(c).is_some_and(|n| n.children().is_some_and(|ch| ch.is_empty())) {
d.remove(c)?;
sel.remove(c);
gone.push(c);
}
}
sel.remove_all(gone);
if let Some((first, rest)) = joining.split_first() {
// `add` clears what any change to the object selection clears; the rest are new ids.
sel.add(*first);
sel.objects.extend_from_slice(rest);
}
Ok(all)
})?;
Ok(json!({ "ids": ids.iter().map(|i| i.0).collect::<Vec<_>>(), "removed": removed }))
@@ -2,8 +2,9 @@
//! one undo step, and what takes no part.
use serde_json::{Value, json};
use vectorcraft_doc::{NodeId, NodeKind};
use vectorcraft_doc::{NodeId, NodeKind, Selection};
use vectorcraft_geom::{PathData, Point};
use vectorcraft_tools::addons::trim::plan::{self, Limits, Stretches};
use crate::Session;
@@ -420,6 +421,139 @@ fn the_selection_stays_as_it_was_and_the_pieces_of_a_selected_path_are_selected(
assert!(s.doc().unwrap().selection.is_empty(), "a removed path leaves the selection");
}
/// Run the command with `p`, and check that it leaves the selection exactly as changing it one
/// id at a time does: for each trimmed path in the plan's order, its anchors and segments let go,
/// a path with no pieces left removed, and each piece of a selected path added; then each emptied
/// compound path removed. Returns the selection.
fn same_selection_as_one_id_at_a_time(s: &mut Session, p: Value) -> Selection {
let st = s.doc().unwrap();
let pick = super::pick(&p).unwrap();
let root = crate::cmd::ordinary_isolation(st);
let plan: Vec<(NodeId, usize)> =
plan::plan(&st.doc, root, &pick, Limits::COMMAND, &mut Stretches::default()).unwrap().into_iter().map(|t| (t.id, t.pieces.len())).collect();
let mut compounds: Vec<NodeId> = plan
.iter()
.filter_map(|(id, _)| st.doc.parent_of(*id))
.filter(|c| matches!(st.doc.node(*c).map(|n| &n.kind), Some(NodeKind::Compound { .. })))
.collect();
compounds.dedup();
let mut want = st.selection.clone();
let made = ids(&s.execute(TRIM, &p).unwrap());
assert_eq!(made.len(), plan.iter().map(|(_, n)| n).sum::<usize>());
let mut made = made.into_iter();
for (id, n) in plan {
let selected = want.contains(id);
want.anchors.remove(&id);
want.segments.remove(&id);
let pieces: Vec<NodeId> = made.by_ref().take(n).collect();
if pieces.is_empty() {
want.remove(id);
} else if selected {
for piece in pieces {
if !want.contains(piece) {
want.add(piece);
}
}
}
}
let st = s.doc().unwrap();
for c in compounds {
if st.doc.node(c).is_none() {
want.remove(c);
}
}
want.prune(&st.doc);
assert_eq!(st.selection, want);
st.selection.clone()
}
/// Two lines across split by a rectangle, one of them selected; a selected line nothing crosses
/// and a selected compound path of two such lines, which the rectangle removes whole; and a
/// selected line elsewhere with an anchor and a segment direct-selected. Returns (the selected
/// line across, the line removed whole, the compound path, the line elsewhere).
fn trim_scene(s: &mut Session) -> (NodeId, NodeId, NodeId, NodeId) {
let (h, ..) = across_and_uprights(s);
line(s, &[(0.0, 120.0), (300.0, 120.0)], false);
let alone = line(s, &[(145.0, 200.0), (155.0, 200.0)], false);
let c1 = line(s, &[(145.0, 170.0), (155.0, 170.0)], false);
let c2 = line(s, &[(145.0, 180.0), (155.0, 180.0)], false);
s.execute("select.set", &json!({"ids": [c1.0, c2.0]})).unwrap();
let compound = NodeId(s.execute("object.compoundPath.make", &json!({})).unwrap()["id"].as_u64().unwrap());
let elsewhere = line(s, &[(0.0, 250.0), (10.0, 250.0), (20.0, 250.0)], false);
s.execute("select.set", &json!({"ids": [elsewhere.0, h.0, alone.0, compound.0]})).unwrap();
s.edit("direct", |_, sel| {
sel.anchors.insert(elsewhere, [(0, 1)].into());
sel.anchors.insert(h, [(0, 0)].into());
sel.segments.insert(elsewhere, [(0, 0)].into());
Ok(())
})
.unwrap();
(h, alone, compound, elsewhere)
}
#[test]
fn the_selection_after_a_trim_is_the_one_changing_it_one_id_at_a_time_gives() {
// A removed key object stops being the key.
let mut s = session();
let (h, alone, compound, elsewhere) = trim_scene(&mut s);
s.edit("key", |_, sel| {
sel.key = Some(alone);
Ok(())
})
.unwrap();
let sel = same_selection_as_one_id_at_a_time(&mut s, json!({"rect": [140, 90, 20, 120]}));
assert_eq!(sel.key, None);
assert!(!sel.contains(alone) && !sel.contains(compound));
assert_eq!(&sel.objects[..2], [elsewhere, h]);
assert_eq!(sel.objects.len(), 3, "{:?}", sel.objects);
assert!(sel.anchors.contains_key(&elsewhere) && sel.segments.contains_key(&elsewhere) && !sel.anchors.contains_key(&h));
// A key object that keeps its first piece stays the key.
let mut s = session();
let (h, alone, compound, _) = trim_scene(&mut s);
s.edit("key", |_, sel| {
sel.key = Some(h);
Ok(())
})
.unwrap();
let sel = same_selection_as_one_id_at_a_time(&mut s, json!({"rect": [140, 90, 20, 120]}));
assert_eq!(sel.key, Some(h));
assert!(!sel.contains(alone) && !sel.contains(compound));
// Trimming only paths that are not selected leaves a targeted object targeted.
let mut s = session();
let (h, _, _, elsewhere) = trim_scene(&mut s);
s.execute("select.set", &json!({"ids": [elsewhere.0]})).unwrap();
s.edit("target", |_, sel| {
sel.target = Some(elsewhere);
Ok(())
})
.unwrap();
let sel = same_selection_as_one_id_at_a_time(&mut s, json!({"rect": [140, 90, 20, 40]}));
assert_eq!((sel.objects.as_slice(), sel.target), ([elsewhere].as_slice(), Some(elsewhere)));
assert!(exists(&s, h));
// A selected path that gains a piece changes the object selection, which lets go of the target.
let mut s = session();
let (h, _, _, elsewhere) = trim_scene(&mut s);
s.execute("select.set", &json!({"ids": [elsewhere.0, h.0]})).unwrap();
s.edit("target", |_, sel| {
sel.target = Some(elsewhere);
Ok(())
})
.unwrap();
let sel = same_selection_as_one_id_at_a_time(&mut s, json!({"rect": [140, 90, 20, 40]}));
assert_eq!((sel.objects.len(), sel.target), (3, None));
// So does a selected path that is removed whole.
let mut s = session();
let (_, alone, _, elsewhere) = trim_scene(&mut s);
s.execute("select.set", &json!({"ids": [elsewhere.0, alone.0]})).unwrap();
s.edit("target", |_, sel| {
sel.target = Some(elsewhere);
Ok(())
})
.unwrap();
let sel = same_selection_as_one_id_at_a_time(&mut s, json!({"rect": [140, 190, 20, 20]}));
assert_eq!((sel.objects.as_slice(), sel.target), ([elsewhere].as_slice(), None));
}
#[test]
fn many_paths_far_from_the_trimmed_one_cost_no_crossing_search() {
let mut s = session();