Merge pull request #8467 from dotCooCoo/perf/direct-blend-spine-guard

Direct Selection: the selected blend spine check returns at once when no blend is selected
This commit is contained in:
BFlat
2026-11-18 13:14:15 +07:00
committed by GitHub
5 changed files with 153 additions and 5 deletions
+14 -2
View File
@@ -384,6 +384,18 @@ pub(crate) fn spines(cx: &ToolContext) -> Vec<(NodeId, PathData)> {
v
}
/// [`spines`] of the blends that are themselves selected, topmost first. When no selected object
/// is a blend, it returns an empty list without walking the document or building any spine.
pub(crate) fn selected_spines(cx: &ToolContext) -> Vec<(NodeId, PathData)> {
let is_blend = |id: &NodeId| cx.doc.node(*id).is_some_and(|n| matches!(n.kind, NodeKind::Blend { .. }));
if !cx.selection.objects.iter().any(is_blend) {
return vec![];
}
let mut v = spines(cx);
v.retain(|(id, _)| cx.selection.contains(*id));
v
}
/// The spine point under `p`: (blend, anchor, where it is).
fn hit_spine_point(cx: &ToolContext, p: Point, tol: f64) -> Option<(NodeId, usize, Point)> {
spines(cx).into_iter().find_map(|(id, path)| {
@@ -395,7 +407,7 @@ fn hit_spine_point(cx: &ToolContext, p: Point, tol: f64) -> Option<(NodeId, usiz
/// The segment of a selected blend's spine passing within `tol` of `p`: (blend, segment, t, whether
/// it is straight).
pub(crate) fn hit_spine_segment(cx: &ToolContext, p: Point, tol: f64) -> Option<(NodeId, usize, f64, bool)> {
spines(cx).into_iter().filter(|(id, _)| cx.selection.contains(*id)).find_map(|(id, path)| {
selected_spines(cx).into_iter().find_map(|(id, path)| {
let (si, seg, t, _, dist) = path.nearest(p)?;
(dist <= tol).then(|| (id, seg, t, path.subpaths.get(si).is_some_and(|sp| sp.segment_is_line(seg))))
})
@@ -1722,7 +1734,7 @@ impl DirectSelectionTool {
// marquee that holds none of them drops the points picked before.
let mut spined: Option<NodeId> = None;
if !self.group {
let found = spines(cx).into_iter().filter(|(id, _)| cx.selection.contains(*id)).find_map(|(id, path)| {
let found = selected_spines(cx).into_iter().find_map(|(id, path)| {
let inside: BTreeSet<usize> = path.anchors().filter(|(s, _, a)| *s == 0 && r.contains(a.p)).map(|(_, i, _)| i).collect();
(!inside.is_empty()).then_some((id, inside))
});
+135
View File
@@ -416,6 +416,141 @@ fn spine_segments_bend_with_the_anchor_point_tool_and_direct_selection() {
assert_eq!(t.pointer(&curved, &PointerEvent::new(PointerKind::Up, 160.0, 345.0)), vec![Action::Commit]);
}
/// [`selected_spines`] written as a filter of every spine, for comparing results.
fn every_spine_then_selected(cx: &ToolContext) -> Vec<(NodeId, PathData)> {
spines(cx).into_iter().filter(|(id, _)| cx.selection.contains(*id)).collect()
}
/// [`hit_spine_segment`] written over [`every_spine_then_selected`], for comparing results.
fn hit_spine_segment_of_every_spine(cx: &ToolContext, p: Point, tol: f64) -> Option<(NodeId, usize, f64, bool)> {
every_spine_then_selected(cx).into_iter().find_map(|(id, path)| {
let (si, seg, t, _, dist) = path.nearest(p)?;
(dist <= tol).then(|| (id, seg, t, path.subpaths.get(si).is_some_and(|sp| sp.segment_is_line(seg))))
})
}
/// [`blend_doc`] with a stored spine the keys are spread along (not pinned to anchors), whose
/// layout builds a [`vectorcraft_doc::blend::Spine`], and the id of the plain rectangle beside it.
fn blend_doc_with_spread_spine() -> (vectorcraft_doc::Document, NodeId, NodeId, NodeId) {
let (mut d, g, k1) = blend_doc();
let Some(rect) = d.layers[0].children().into_iter().flatten().map(|n| n.id).find(|id| *id != g) else { panic!("rectangle") };
let mut sp = vectorcraft_geom::SubPath::polyline(&[Point::new(110.0, 310.0), Point::new(210.0, 310.0)], false);
sp.bend_segment(0, 0.5, vectorcraft_geom::Vec2::new(0.0, 20.0));
let Some(NodeKind::Blend { spec, .. }) = d.node_mut(g).map(|n| &mut n.kind) else { panic!("blend") };
(spec.spine, spec.key_anchors) = (Some(PathData::single(sp)), vec![]);
(d, g, k1, rect)
}
/// With no blend among the selected objects, the selected-spine check builds no blend spine; with
/// the blend selected it still finds the spine.
#[test]
fn the_selected_spine_check_builds_no_spine_without_a_selected_blend() {
let (d, g, _, rect) = blend_doc_with_spread_spine();
let p = paint();
let mut s = Selection::default();
s.set([rect]);
let c = cx(&d, &s, &p);
let before = vectorcraft_doc::blend::spines_built();
assert_eq!(hit_spine_segment(&c, Point::new(160.0, 330.0), 4.0), None);
assert!(selected_spines(&c).is_empty());
assert_eq!(vectorcraft_doc::blend::spines_built() - before, 0, "no spine built");
s.set([rect, g]);
let c = cx(&d, &s, &p);
assert!(hit_spine_segment(&c, Point::new(160.0, 330.0), 4.0).is_some_and(|h| h.0 == g));
}
/// The selected-spine check gives what a filter of every spine gives, for selections with and
/// without the blend, its key, a stale id, and the blend locked, hidden or without a spine.
#[test]
fn the_selected_spine_check_matches_a_filter_of_every_spine() {
let (d, g, k1, rect) = blend_doc_with_spread_spine();
let p = paint();
let mut docs = vec![d.clone()];
for change in 0..3 {
let mut d = d.clone();
if let Some(n) = d.node_mut(g) {
match change {
0 => n.locked = true,
1 => n.visible = false,
_ => {
if let NodeKind::Blend { spec, .. } = &mut n.kind {
spec.no_spine = true;
}
}
}
}
docs.push(d);
}
let selections: [&[NodeId]; 7] = [&[], &[rect], &[g], &[rect, g], &[g, rect], &[k1], &[NodeId(9_999)]];
for (di, d) in docs.iter().enumerate() {
for ids in selections {
let mut s = Selection::default();
s.set(ids.iter().copied());
let c = cx(d, &s, &p);
assert_eq!(selected_spines(&c), every_spine_then_selected(&c), "doc {di}, {ids:?}");
for x in (90..=230).step_by(5) {
for y in (295..=340).step_by(5) {
let at = Point::new(f64::from(x), f64::from(y));
assert_eq!(hit_spine_segment(&c, at, 4.0), hit_spine_segment_of_every_spine(&c, at, 4.0), "doc {di}, {ids:?}, {at:?}");
}
}
}
}
}
/// Times the selected-spine check on 20,000 paths and 200 blends with curved spines, against the
/// same check written as a filter of every spine, in one process: 500 points, the median of 7
/// runs. Case A selects one plain path, case B one blend.
#[test]
#[ignore = "manual large-document timing"]
fn selected_spine_check_timing() {
let mut d = vectorcraft_doc::Document::new(4000.0, 4000.0);
let l = d.layers[0].id;
for i in 0..20_000_u32 {
let (x, y) = (f64::from(i % 200) * 20.0, f64::from(i / 200) * 20.0);
let id = d.alloc_id();
let r = vectorcraft_geom::shapes::rectangle(Rect::new(x, y, x + 10.0, y + 10.0));
d.insert(Some(l), 0, vectorcraft_doc::Node::path(id, r, vectorcraft_doc::Appearance::default_art())).unwrap();
}
let first_path = d.layers[0].children().into_iter().flatten().map(|n| n.id).next();
let mut blends = vec![];
for i in 0..200_u32 {
let (x, y) = (f64::from(i % 20) * 200.0, 2050.0 + f64::from(i / 20) * 190.0);
let key = |id: NodeId, x: f64| {
let r = vectorcraft_geom::shapes::rectangle(Rect::new(x, y, x + 20.0, y + 20.0));
std::sync::Arc::new(vectorcraft_doc::Node::path(id, r, vectorcraft_doc::Appearance::default_art()))
};
let (g, k1, k2) = (d.alloc_id(), d.alloc_id(), d.alloc_id());
let mut sp = vectorcraft_geom::SubPath::polyline(&[Point::new(x + 10.0, y + 10.0), Point::new(x + 110.0, y + 10.0)], false);
sp.bend_segment(0, 0.5, vectorcraft_geom::Vec2::new(0.0, 20.0));
let spec = vectorcraft_doc::BlendSpec { spine: Some(PathData::single(sp)), ..Default::default() };
let b = vectorcraft_doc::Node::new(g, NodeKind::Blend { children: vec![key(k1, x), key(k2, x + 100.0)], spec: Box::new(spec) });
d.insert(Some(l), 0, b).unwrap();
blends.push(g);
}
let points: Vec<Point> = (0..500_u32).map(|i| Point::new(f64::from(i * 37 % 4000), f64::from(i * 53 % 4000))).collect();
let p = paint();
let (Some(plain), Some(&blend)) = (first_path, blends.first()) else { panic!("fixture") };
for (case, id) in [("A: one plain path selected", plain), ("B: one blend selected", blend)] {
let mut s = Selection::default();
s.set([id]);
let c = cx(&d, &s, &p);
let (mut old, mut new) = (vec![], vec![]);
for _ in 0..7 {
let t = std::time::Instant::now();
let a: Vec<_> = points.iter().map(|q| hit_spine_segment_of_every_spine(&c, *q, 4.0)).collect();
old.push(t.elapsed());
let t = std::time::Instant::now();
let b: Vec<_> = points.iter().map(|q| hit_spine_segment(&c, *q, 4.0)).collect();
new.push(t.elapsed());
assert_eq!(a, b, "{case}");
}
old.sort();
new.sort();
eprintln!("{case}: 500 checks, median of 7: every spine then filter {:?}, selected spines {:?}", old[3], new[3]);
}
}
/// Shift held while a curved segment is dragged sends `perpendicular`. The test drags a path's
/// segment with Direct Selection, and a blend spine's segment with Direct Selection and with the
/// Anchor Point tool.
+2 -2
View File
@@ -168,7 +168,7 @@ impl AnchorTool {
| State::SpineHandle { id, .. }
| State::Reshape { id, .. } => Some((cx.selection.contains(id), cx.doc.index_path(id))),
};
for (id, path) in crate::direct::spines(cx).into_iter().filter(|(id, _)| cx.selection.contains(*id)) {
for (id, path) in crate::direct::selected_spines(cx) {
let candidate = if let Some((ai, out)) = Self::spine_handle(cx, &path, p, point) {
State::SpineHandle { id, ai, out, start: p, began: false }
} else if let Some(ai) = Self::spine_point(&path, p, point) {
@@ -192,7 +192,7 @@ impl AnchorTool {
/// tool switch. Show the handles on selected blends so they stay editable.
fn spine_overlays(cx: &ToolContext) -> Vec<Overlay> {
let mut out = vec![];
for (id, path) in crate::direct::spines(cx).into_iter().filter(|(id, _)| cx.selection.contains(*id)) {
for (id, path) in crate::direct::selected_spines(cx) {
let anchors: Vec<_> = path.anchors().filter(|(si, _, _)| *si == 0).collect();
if !cx.handles_multiple && anchors.len() > 1 {
continue;
+1 -1
View File
@@ -708,7 +708,7 @@ fn auto_add_delete(cx: &ToolContext, p: Point, m: Mods, tol: f64) -> Option<Acti
/// A click on a selected blend's spine: delete the point under `p` when no key object sits on it
/// (a key's point does nothing), else add one where the spine passes within `tol`.
fn spine_click(cx: &ToolContext, p: Point, tol: f64) -> Option<Vec<Action>> {
for (id, path) in crate::direct::spines(cx).into_iter().filter(|(id, _)| cx.selection.contains(*id)) {
for (id, path) in crate::direct::selected_spines(cx) {
let Some(NodeKind::Blend { children, spec }) = cx.doc.node(id).map(|n| &n.kind) else { continue };
let keys = vectorcraft_doc::live::blend_spine(children, spec).and_then(|(_, a)| a).unwrap_or_default();
if let Some((_, ai, _)) = path.anchors().find(|(_, _, a)| a.p.distance(p) <= tol) {
+1
View File
@@ -1359,6 +1359,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur
View › Hide Edges hides the Live Corners widgets, the bounding box and the type widget, and the selection tools then do not answer a press on them or change the cursor over them.
- **Blend spine Anchor Point tool (#2544):** on a selected blend, dragging a spine corner makes symmetric Smooth handles; the tool displays and picks existing spine handles when switched to, drags one independently or retracts it on click, and clicking a point with handles retracts both. The spine is edited live without moving its key objects. Windows native canvas checks and screenshots reviewed: symmetric and independent handles, corner/retraction, Shift constraints and perpendicular reshaping, tool switching, key preservation, Undo/Redo, save/reopen, hidden handles and overlapping Path/Blend priority (PR #2643). Behaviour reference: [public path-editing documentation](https://helpx.adobe.com/illustrator/using/adjust-path-segments.html).
- **Blend spine points by marquee:** with Direct Selection, a marquee picks the spine points inside it on a selected blend, the topmost one when several are selected, and the picked points drag together as Shift-clicked ones do; Shift-marquee adds points or takes picked ones away, a plain marquee around none of them drops the picks, and Escape during the marquee keeps them. Unselected, locked and hidden blends are skipped. The spine's points are anchor points edited with Direct Selection ([the help page](https://helpx.adobe.com/illustrator/desktop/manage-colors/apply-transparency-and-blending/modify-spine-of-blended-objects.html)), and a boundary drag with the tool selects anchor points ([the help page](https://helpx.adobe.com/illustrator/desktop/draw-shapes-and-paths/modify-paths/select-anchor-points-in-paths.html)); the rules for selected, topmost and Shift are VectorCraft's own (`crates/tools/src/direct/tests.rs`, `marquee_picks_selected_blend_spine_points_which_drag_together`, `shift_marquee_toggles_selected_blend_spine_points`, `marquee_skips_unselected_locked_and_hidden_blend_spines`, `escape_cancels_a_marquee_and_keeps_the_picked_spine_points`).
When no selected object is a blend, the selected-spine checks of Direct Selection's hover, press and marquee, the Anchor Point tool and the Pen return at once instead of building every blend's spine first; on 20,000 paths and 200 blends with one path selected, 500 checks took 24 µs instead of 336 ms, and the results are the same (`the_selected_spine_check_builds_no_spine_without_a_selected_blend`, `the_selected_spine_check_matches_a_filter_of_every_spine`; `selected_spine_check_timing` times it).
- **Blend steps of brushed strokes:** the steps interpolate the pen pressure, tilt, bearing and rotation recorded with the Paintbrush, so a Calligraphic, Scatter or Art brush that varies with the pen changes gradually from one key to the next. A key without a recording counts as half pressure, half tilt and 0°, as brushes read it, and angles turn the shorter way round. When the blend starts a key's path at another anchor (an open path run from its end, a closed one turned to another start), the key's recordings, width profile, arrowheads, dashes and a gradient along or across the stroke turn with the path. Different brushes still switch halfway (VectorCraft's own rule; strokes blend their options, as [Blended objects overview](https://helpx.adobe.com/illustrator/desktop/manage-colors/apply-transparency-and-blending/blended-objects-overview.html) describes; `crates/doc/src/blend.rs`, `pen_pressure_tilt_and_angles_interpolate_for_brushes`, `stroke_profiles_turn_with_a_key_path_started_elsewhere`, `stroke_gradients_and_dashes_turn_with_a_key_path_started_elsewhere`).
- **Blend steps of spot gradients with process plates:** when the matching stops of two gradients, or of a gradient and a solid, are tints of one spot swatch, each step's stop stays linked to it at a tint between the two and keeps the process inks printed with it, each of C, M, Y and K between the two stops' amounts; a stop or solid without process inks counts as none of each. Gradients with different stop counts keep the inks at every resampled stop. PDF export writes such a step as one DeviceN shading of the spot and process inks. Tints of one spot color blend into tints of it, as [the help page](https://helpx.adobe.com/illustrator/desktop/manage-colors/apply-transparency-and-blending/blended-objects-overview.html) describes; the process-ink interpolation is VectorCraft's own rule (`crates/doc/src/blend.rs`, `blend_gradient_spot_process_plates_interpolate`, `resampled_gradient_spot_process_plates_interpolate`, `blend_gradient_missing_plates_mean_zero`, `blend_solid_to_spot_gradient_does_not_copy_plates`; `crates/pdf/src/tests/output.rs`, `blend_steps_between_spot_gradients_with_process_plates_print_both_inks`).
- **Path editing (#494):** the handles shown are the handles that drag: Direct Selection drags those of a path selected as a whole (as the Pen leaves it), the Anchor Point tool and Pen+Alt no longer grab handles that Show handles when multiple anchors are selected hides, a handle shorter than the tolerance leaves its anchor to be picked, and the anchor tools pick within Selection & Anchor Display › Tolerance. A dragged segment keeps a smooth anchor at either end smooth. With Direct Selection, the Pen, the Curvature and the anchor tools, a path selected as a whole shows all its anchors selected, and the Control bar and the Properties panel offer Convert (corner, smooth) for them too; Remove and Cut wait for anchors direct-selected (#504). A handle drag gathers its snap targets once per drag (it took 21 ms per pointer move on a 20,000-path document, now 1 ms; `vectorcraft-cli perf` measures handle and segment drags on a 5,000-anchor path). Join merges the selected anchor points that lie on one another, as after Average, into one anchor (#1582, fixes #1533), and removes the merged anchors in one pass over the path, so a long run of them no longer moves the anchors after it once per merge (`cmd::path::tests::a_long_run_before_many_other_anchors_merges_in_one_pass`); Object › Path › Average & Join… (`path.averageJoin`, Cmd+Alt+Shift+J, the "Create corner or smooth join" shortcut of [Default keyboard shortcuts](https://helpx.adobe.com/illustrator/using/default-keyboard-shortcuts.html)) moves two open path ends to their midpoint and joins them there, a corner or smooth point as its dialog's Points choice asks, in one undo step.