mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-17 08:26:28 +00:00
Merge pull request #8100 from dotCooCoo/perf/brush-targets-dedup-set
Brushes: brush_targets drops repeated paths with a set instead of a linear search of its output
This commit is contained in:
@@ -5,15 +5,16 @@ use super::{
|
||||
|
||||
// ---------- brushes ----------
|
||||
|
||||
/// Brushable nodes (paths and compound paths) under `ids`.
|
||||
/// Brushable nodes (paths and compound paths) under `ids`, in walk order, each listed once where it is first reached.
|
||||
pub(crate) fn brush_targets(d: &Document, ids: &[NodeId]) -> Vec<NodeId> {
|
||||
let mut out = vec![];
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
for id in ids {
|
||||
let Some(n) = d.node(*id) else { continue };
|
||||
n.walk(&mut |c| {
|
||||
let own = matches!(c.kind, NodeKind::Path { guide: false, clipping: false, .. } | NodeKind::Compound { .. });
|
||||
let in_compound = d.parent_of(c.id).and_then(|p| d.node(p)).is_some_and(|p| matches!(p.kind, NodeKind::Compound { .. }));
|
||||
if own && !in_compound && !out.contains(&c.id) {
|
||||
if own && !in_compound && seen.insert(c.id) {
|
||||
out.push(c.id);
|
||||
}
|
||||
});
|
||||
@@ -748,3 +749,92 @@ pub(super) fn brush_freehand(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let checked = crate::schemas::check(spec, p, s.replaying())?;
|
||||
crate::cmd::draw2::freehand_brushed(s, checked.as_ref().unwrap_or(p), name.as_deref())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use vectorcraft_doc::Appearance;
|
||||
use vectorcraft_geom::PathData;
|
||||
|
||||
/// The collector with a linear search of its output, kept to compare results with.
|
||||
fn listed_targets(d: &Document, ids: &[NodeId]) -> Vec<NodeId> {
|
||||
let mut out = vec![];
|
||||
for id in ids {
|
||||
let Some(n) = d.node(*id) else { continue };
|
||||
n.walk(&mut |c| {
|
||||
let own = matches!(c.kind, NodeKind::Path { guide: false, clipping: false, .. } | NodeKind::Compound { .. });
|
||||
let in_compound = d.parent_of(c.id).and_then(|p| d.node(p)).is_some_and(|p| matches!(p.kind, NodeKind::Compound { .. }));
|
||||
if own && !in_compound && !out.contains(&c.id) {
|
||||
out.push(c.id);
|
||||
}
|
||||
});
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn path(d: &mut Document) -> Node {
|
||||
Node::path(d.alloc_id(), PathData::default(), Appearance::default())
|
||||
}
|
||||
|
||||
fn compound(d: &mut Document, children: Vec<Node>) -> Node {
|
||||
Node::new(d.alloc_id(), NodeKind::Compound { children: children.into_iter().map(Arc::new).collect(), rule: Default::default() })
|
||||
}
|
||||
|
||||
fn add(d: &mut Document, node: Node) -> NodeId {
|
||||
let layer = d.layers.first().map(|l| l.id);
|
||||
d.insert(layer, usize::MAX, node).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn brush_targets_list_each_target_once_in_first_reached_order() {
|
||||
let mut d = Document::new(400.0, 300.0);
|
||||
let a = path(&mut d);
|
||||
let a = add(&mut d, a);
|
||||
let mut guide = path(&mut d);
|
||||
if let NodeKind::Path { guide: g, .. } = &mut guide.kind {
|
||||
*g = true;
|
||||
}
|
||||
let guide = add(&mut d, guide);
|
||||
let mut clip = path(&mut d);
|
||||
if let NodeKind::Path { clipping, .. } = &mut clip.kind {
|
||||
*clipping = true;
|
||||
}
|
||||
let clip = add(&mut d, clip);
|
||||
// A group holding a path and a compound path of two members.
|
||||
let p1 = path(&mut d);
|
||||
let p1_id = p1.id;
|
||||
let (m1, m2) = (path(&mut d), path(&mut d));
|
||||
let m1_id = m1.id;
|
||||
let cp1 = compound(&mut d, vec![m1, m2]);
|
||||
let cp1_id = cp1.id;
|
||||
let group = Node::group(d.alloc_id(), vec![Arc::new(p1), Arc::new(cp1)]);
|
||||
let group = add(&mut d, group);
|
||||
// A compound path holding a compound path.
|
||||
let m3 = path(&mut d);
|
||||
let cp3 = compound(&mut d, vec![m3]);
|
||||
let cp3_id = cp3.id;
|
||||
let cp2 = compound(&mut d, vec![cp3]);
|
||||
let cp2 = add(&mut d, cp2);
|
||||
|
||||
let ids = [cp1_id, a, group, p1_id, a, guide, clip, m1_id, cp2, cp3_id, NodeId(u64::MAX)];
|
||||
let got = brush_targets(&d, &ids);
|
||||
assert_eq!(got, [cp1_id, a, p1_id, cp2], "repeats keep their first place; guides, clipping paths and compound members are left out");
|
||||
assert_eq!(got, listed_targets(&d, &ids));
|
||||
assert!(brush_targets(&d, &[]).is_empty());
|
||||
|
||||
// Many paths in groups, each group listed with some of its members.
|
||||
let mut ids = vec![];
|
||||
for _ in 0..50 {
|
||||
let members: Vec<Node> = (0..20).map(|_| path(&mut d)).collect();
|
||||
let member_ids: Vec<NodeId> = members.iter().map(|m| m.id).collect();
|
||||
let g = Node::group(d.alloc_id(), members.into_iter().map(Arc::new).collect());
|
||||
let g = add(&mut d, g);
|
||||
ids.push(member_ids[7]);
|
||||
ids.push(g);
|
||||
ids.push(member_ids[3]);
|
||||
}
|
||||
let got = brush_targets(&d, &ids);
|
||||
assert_eq!(got.len(), 50 * 20);
|
||||
assert_eq!(got, listed_targets(&d, &ids));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user