From 5a2eae86a7fb9bae02a417dfb81bc4215bef5199 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 20 Nov 2026 23:43:00 -0800 Subject: [PATCH 1/3] Smooth: a drag over a long path measures only the anchors and segments near it A Smooth drag measured every anchor of each path against every stretch of the drag, and every curved segment's box against every stretch twice, so a release on a path of 100,000 anchors made tens of millions of measures however short the drag was. - The drag's stretches are listed once per release in a grid over the drag's bounds grown past the reach. An anchor outside those bounds is out of reach without measuring, and a point or box is measured only against the stretches listed in the cells it covers. Every query gives at least the stretches the whole-drag measure finds in reach, so the marks, the cuts and the smoothed path are the same as before. - The refitted runs of a path are put back from its end without moving the rest of the anchor list for each run. On a curved path of 100,000 anchors, a drag of 200 points over 200 pt of it makes 7,533 measures to find the curved segments in its reach, where it made 19,899,801. --- crates/engine/src/cmd/draw2/smooth.rs | 344 ++++++++++++++++++++++++-- docs/target-app-parity.md | 1 + 2 files changed, 325 insertions(+), 20 deletions(-) diff --git a/crates/engine/src/cmd/draw2/smooth.rs b/crates/engine/src/cmd/draw2/smooth.rs index d0f480d132..5667922907 100644 --- a/crates/engine/src/cmd/draw2/smooth.rs +++ b/crates/engine/src/cmd/draw2/smooth.rs @@ -1,3 +1,5 @@ +use std::collections::HashMap; + use vectorcraft_pathops::addons::pathtools as pt; use super::common::{dist_to_polyline, ids_json, path_mut, path_of, points_param, poly_bounds, subpath_len, target_paths}; @@ -421,6 +423,8 @@ fn drag_stretches(pts: &[Point]) -> Vec { /// The stretches of the drag `pts` among `of` whose bounds come within `radius` of `r` on both /// axes: every stretch within `radius` of a point in `r`, and perhaps a few more. fn stretches_near(pts: &[Point], of: &[usize], r: Rect, radius: f64) -> Vec { + #[cfg(test)] + STRETCH_TESTS.with(|c| c.set(c.get() + of.len())); let r = r.inflate(radius, radius); of.iter() .copied() @@ -432,8 +436,115 @@ fn stretches_near(pts: &[Point], of: &[usize], r: Rect, radius: f64) -> Vec = const { std::cell::Cell::new(0) }; +} + +/// Most grid cells [`DragReach`] lists one stretch of the drag in. A stretch whose reach covers more +/// is measured against every point and box instead. +const MAX_STRETCH_CELLS: usize = 64; + +/// Most grid cells across the drag's bounds on each axis in [`DragReach`]. +const MAX_DRAG_CELLS: f64 = 1024.0; + +/// The stretches of a Smooth drag listed by the cells of a grid that their reach covers, so that a +/// point or a piece of path is measured only against the stretches whose reach it is in. Each query +/// gives at least every stretch that measuring against the whole drag finds in reach, so the results +/// are the same as measuring against the whole drag. +struct DragReach<'a> { + pts: &'a [Point], + radius: f64, + /// Every stretch of the drag, in order. + all: Vec, + /// The drag's bounds grown by more than `radius`, with the cell size; `None` when the drag or + /// the reach is not a finite number, and then every query gives every stretch. + grid: Option<(Rect, f64)>, + cells: HashMap<(i64, i64), Vec>, + /// The stretches whose reach covers more than [`MAX_STRETCH_CELLS`] cells. + wide: Vec, +} + +impl<'a> DragReach<'a> { + fn new(pts: &'a [Point], radius: f64) -> Self { + let all = drag_stretches(pts); + let mut reach = DragReach { pts, radius, all, grid: None, cells: HashMap::new(), wide: vec![] }; + if pts.is_empty() || !radius.is_finite() || pts.iter().any(|p| !p.x.is_finite() || !p.y.is_finite()) { + return reach; + } + // Enough more than the reach to cover the rounding of each distance and box measured. + let size = pts.iter().fold(0.0_f64, |m, p| m.max(p.x.abs()).max(p.y.abs())); + let pad = radius + 1e-6 * radius.max(1.0) + 1e-9 * size; + let area = poly_bounds(pts, pad); + let cell = pad.max(area.width().max(area.height()) / MAX_DRAG_CELLS); + if !(cell.is_finite() && cell > 0.0 && area.width().is_finite() && area.height().is_finite()) { + return reach; + } + reach.grid = Some((area, cell)); + for &k in &reach.all { + let Some(&a) = pts.get(k) else { continue }; + let b = pts.get(k + 1).copied().unwrap_or(a); + let r = Rect::from_points(a, b).inflate(pad, pad); + let ((x0, y0), (x1, y1)) = (reach.cell_of(Point::new(r.x0, r.y0)), reach.cell_of(Point::new(r.x1, r.y1))); + if (x1 - x0 + 1).saturating_mul(y1 - y0 + 1) > MAX_STRETCH_CELLS as i64 { + reach.wide.push(k); + continue; + } + for x in x0..=x1 { + for y in y0..=y1 { + reach.cells.entry((x, y)).or_default().push(k); + } + } + } + reach + } + + /// The cell `q` is in, counted from the corner of the grid. + fn cell_of(&self, q: Point) -> (i64, i64) { + let Some((area, cell)) = self.grid else { return (0, 0) }; + (((q.x - area.x0) / cell).floor() as i64, ((q.y - area.y0) / cell).floor() as i64) + } + + /// Whether `q` is within the reach of the drag, and `slack` more. + fn in_reach(&self, q: Point, slack: f64) -> bool { + let limit = self.radius + slack; + let Some((area, _)) = self.grid else { return dist_to_stretches(self.pts, &self.all, q) <= limit }; + if !(q.x >= area.x0 && q.x <= area.x1 && q.y >= area.y0 && q.y <= area.y1) { + return false; + } + let listed = self.cells.get(&self.cell_of(q)).map_or(&[][..], Vec::as_slice); + dist_to_stretches(self.pts, listed, q).min(dist_to_stretches(self.pts, &self.wide, q)) <= limit + } + + /// The stretches, in order, that may come within the reach of `r`: at least every one that + /// [`stretches_near`] gives for `r` from the whole drag. + fn near_rect(&self, r: Rect) -> Vec { + let Some((area, _)) = self.grid else { return self.all.clone() }; + let (x0, x1, y0, y1) = (r.x0.max(area.x0), r.x1.min(area.x1), r.y0.max(area.y0), r.y1.min(area.y1)); + if !(x0 <= x1 && y0 <= y1) { + return vec![]; + } + let ((cx0, cy0), (cx1, cy1)) = (self.cell_of(Point::new(x0, y0)), self.cell_of(Point::new(x1, y1))); + if (cx1 - cx0 + 1).saturating_mul(cy1 - cy0 + 1) > self.all.len() as i64 { + return self.all.clone(); + } + let mut out = self.wide.clone(); + for x in cx0..=cx1 { + for y in cy0..=cy1 { + out.extend(self.cells.get(&(x, y)).into_iter().flatten()); + } + } + out.sort_unstable(); + out.dedup(); + out + } +} + /// The distance from `q` to the nearest of the stretches `of` of the drag `pts`. fn dist_to_stretches(pts: &[Point], of: &[usize], q: Point) -> f64 { + #[cfg(test)] + STRETCH_TESTS.with(|c| c.set(c.get() + of.len())); of.iter() .filter_map(|&k| { let a = *pts.get(k)?; @@ -468,15 +579,18 @@ fn nearest_to_stretches(s: CubicBez, pts: &[Point], of: &[usize]) -> (f64, f64) best } -/// How many curved segments of `pd` may come within `radius` of the drag `pts`. -fn segments_in_reach(pd: &PathData, pts: &[Point], radius: f64) -> usize { - let all = drag_stretches(pts); +/// How many curved segments of `pd` may come within the reach of the drag. +fn segments_in_reach(pd: &PathData, reach: &DragReach) -> usize { pd.subpaths .iter() .map(|sp| { (0..sp.segment_count()) .filter(|&i| { - !vectorcraft_tools::draw2::segment_is_straight(sp, i) && !stretches_near(pts, &all, control_box(sp.segment(i)), radius).is_empty() + if vectorcraft_tools::draw2::segment_is_straight(sp, i) { + return false; + } + let b = control_box(sp.segment(i)); + !stretches_near(reach.pts, &reach.near_rect(b), b, reach.radius).is_empty() }) .count() }) @@ -516,26 +630,26 @@ fn reach_samples(c: CubicBez, (t0, t1): (f64, f64), (pts, of): (&[Point], &[usiz reach_samples(c, (mid, t1), (pts, &near), radius, budget, out); } -/// `sp` cut where the reach `radius` of the drag `pts` crosses its curved segments, and whether each +/// `sp` cut where the reach of the drag crosses its curved segments, and whether each /// anchor of the result is in reach. An anchor in reach is added in the middle of each stretch of a /// segment in reach that does not run on from one of the segment's anchors in reach, and an anchor /// out of reach in the middle of each stretch out of reach between two stretches in reach, so that a /// stretch in reach is smoothed even when the segment's anchors are not, and the stretches between /// stay as they are. A segment is searched no more than half the reach apart where it comes within /// the reach of the drag ([`reach_samples`]), measuring up to `share` pieces of each segment. -fn mark_reached(sp: &SubPath, pts: &[Point], radius: f64, share: usize) -> (SubPath, Vec) { - let near = |q: Point| dist_to_polyline(pts, q) <= radius; +fn mark_reached(sp: &SubPath, reach: &DragReach, share: usize) -> (SubPath, Vec) { + let (pts, radius) = (reach.pts, reach.radius); let n = sp.anchors.len(); - let marked: Vec = sp.anchors.iter().map(|a| near(a.p)).collect(); + let marked: Vec = sp.anchors.iter().map(|a| reach.in_reach(a.p, 0.0)).collect(); let mut places = vec![]; - let all = drag_stretches(pts); for i in 0..sp.segment_count() { if vectorcraft_tools::draw2::segment_is_straight(sp, i) { continue; } let c = sp.segment(i); let (mut samples, mut budget) = (vec![], share); - reach_samples(c, (0.0, 1.0), (pts, &all), radius, &mut budget, &mut samples); + let near = reach.near_rect(control_box(c.subsegment(0.0..1.0))); + reach_samples(c, (0.0, 1.0), (pts, &near), radius, &mut budget, &mut samples); // Stretches of samples alike, in order: (in reach, first, last). let mut runs: Vec<(bool, usize, usize)> = vec![]; for (k, s) in samples.iter().enumerate() { @@ -573,7 +687,7 @@ fn mark_reached(sp: &SubPath, pts: &[Point], radius: f64, share: usize) -> (SubP .anchors .iter() .enumerate() - .map(|(j, a)| if added.contains(&j) { dist_to_polyline(pts, a.p) <= radius + 1e-9 * radius.max(1.0) } else { kept.next().unwrap_or(false) }) + .map(|(j, a)| if added.contains(&j) { reach.in_reach(a.p, 1e-9 * radius.max(1.0)) } else { kept.next().unwrap_or(false) }) .collect(); (out, marked) } @@ -581,13 +695,14 @@ fn mark_reached(sp: &SubPath, pts: &[Point], radius: f64, share: usize) -> (SubP /// `sp` with the anchors in the drag's reach smoothed and refitted, `None` when none are in reach. /// A closed path whose first anchor is in reach starts at the point of the refitted path nearest /// that anchor's place (VectorCraft's own rule). -fn smooth_subpath(sp: &SubPath, pts: &[Point], radius: f64, share: usize, tol: f64, sigma: f64) -> Option { +fn smooth_subpath(sp: &SubPath, reach: &DragReach, share: usize, tol: f64, sigma: f64) -> Option { if sp.anchors.len() < 2 { return None; } - let near = &|q: Point| dist_to_polyline(pts, q) <= radius; + let radius = reach.radius; + let near = &|q: Point| reach.in_reach(q, 0.0); // A drag along a segment smooths it even when its anchors are out of reach. - let (sp, marked) = mark_reached(sp, pts, radius, share); + let (sp, marked) = mark_reached(sp, reach, share); let sp = &sp; if !marked.iter().any(|m| *m) { return None; @@ -693,14 +808,15 @@ fn smooth_open(anchors: &[Anchor], marked: &[bool], near: &dyn Fn(Point) -> bool } i += 1; } - let mut out = anchors.to_vec(); + let mut out = EditsFromEnd::new(anchors.to_vec()); for (a, b) in runs.into_iter().rev() { let lo = a.saturating_sub(1); let hi = (b + 1).min(n - 1); if hi == lo { continue; } - let mut piece = out[lo..=hi].to_vec(); + let Some(piece) = out.get(lo, hi) else { continue }; + let mut piece = piece.to_vec(); // The anchors outside the reach, kept with the part of their segment that is outside. let (mut head, mut tail) = (None, None); if hi > b @@ -725,9 +841,49 @@ fn smooth_open(anchors: &[Anchor], marked: &[bool], near: &dyn Fn(Point) -> bool if same_anchors(&new, &piece) { continue; } - out.splice(lo..=hi, head.into_iter().chain(new).chain(tail)); + out.replace(lo, hi, head.into_iter().chain(new).chain(tail)); + } + out.finish() +} + +/// A list edited by replacing stretches of it, each one ending no later than where the one before +/// started, in time proportional to the items replaced and moved rather than to the list's length +/// for each edit. +struct EditsFromEnd { + /// The start of the list. + head: Vec, + /// The rest of the list, last item first. + rest: Vec, +} + +impl EditsFromEnd { + fn new(items: Vec) -> Self { + EditsFromEnd { head: items, rest: vec![] } + } + + /// The items `lo` to `hi` of the list, `None` when it is shorter. + fn get(&mut self, lo: usize, hi: usize) -> Option<&[T]> { + while self.head.len() <= hi { + self.head.push(self.rest.pop()?); + } + self.head.get(lo..=hi) + } + + /// The list with the items `lo` to `hi` replaced by `with`, as [`Vec::splice`] gives it. Leaves + /// the list as it is when it is shorter. + fn replace(&mut self, lo: usize, hi: usize, with: impl IntoIterator) { + if lo > hi || self.get(lo, hi).is_none() { + return; + } + self.rest.extend(self.head.drain(hi + 1..).rev()); + self.head.truncate(lo); + self.head.extend(with); + } + + fn finish(mut self) -> Vec { + self.head.extend(self.rest.into_iter().rev()); + self.head } - out } pub(super) fn smooth_region(s: &mut Session, p: &Value) -> Result { @@ -743,15 +899,16 @@ pub(super) fn smooth_region(s: &mut Session, p: &Value) -> Result { for id in target_paths(&st.doc, &st.selection, poly_bounds(&pts, radius)) { paths.push((id, path_of(&st.doc, id)?.0)); } + let reach = DragReach::new(&pts, radius); // Each curved segment in reach gets the same share of the search, whatever order they come in. - let in_reach: usize = paths.iter().map(|(_, pd)| segments_in_reach(pd, &pts, radius)).sum(); + let in_reach: usize = paths.iter().map(|(_, pd)| segments_in_reach(pd, &reach)).sum(); let share = (MAX_REACH_PIECES / in_reach.max(1)).max(1); for (id, pd) in paths { let mut changed = false; let subs: Vec = pd .subpaths .iter() - .map(|sp| match smooth_subpath(sp, &pts, radius, share, tol, sigma) { + .map(|sp| match smooth_subpath(sp, &reach, share, tol, sigma) { Some(n) if n != *sp && n.anchors.len() >= 2 => { changed = true; n @@ -1127,6 +1284,153 @@ mod tests { pts.iter().map(|&(x, y)| Anchor::corner(Point::new(x, y))).collect() } + /// A path of `n` smooth anchors 1 pt apart, along x and up and down by 3 pt, every segment + /// curved; closed, the anchors go round a circle `n` pt long instead. + fn wave(n: usize, closed: bool) -> SubPath { + let anchors = (0..n) + .map(|i| { + let f = i as f64; + if closed { + let r = n as f64 / std::f64::consts::TAU + f64::from(u8::from(i % 2 == 1)) * 3.0; + let a = f / n as f64 * std::f64::consts::TAU; + let (p, t) = (Point::new(r * a.cos(), r * a.sin()), kurbo::Vec2::new(-a.sin(), a.cos()) * 0.4); + Anchor::with_handles(p, p - t, p + t) + } else { + let p = Point::new(f, f64::from(u8::from(i % 2 == 1)) * 3.0); + Anchor::with_handles(p, p - kurbo::Vec2::new(0.4, 0.0), p + kurbo::Vec2::new(0.4, 0.0)) + } + }) + .collect(); + SubPath::new(anchors, closed) + } + + /// The same drag measured against all of its stretches for every point and box. + fn linear<'a>(pts: &'a [Point], radius: f64) -> DragReach<'a> { + DragReach { grid: None, ..DragReach::new(pts, radius) } + } + + /// How many stretch measures `f` made on this thread. + fn stretch_tests(f: impl FnOnce()) -> usize { + STRETCH_TESTS.with(|c| c.set(0)); + f(); + STRETCH_TESTS.with(std::cell::Cell::get) + } + + /// A drag of 200 points over 200 pt of a curved path of 100,000 anchors measures the anchors + /// and segments near the drag against the stretches near them, not every anchor and segment + /// against every stretch (about 40 million measures), and smooths the path as measuring + /// everything does. + #[test] + fn a_drag_over_a_long_path_measures_only_what_is_near_it() { + let sp = wave(100_000, false); + let pts: Vec = (0..200).map(|i| Point::new(50_000.0 + f64::from(i), 1.5)).collect(); + let reach = DragReach::new(&pts, 12.0); + let pd = PathData::single(sp.clone()); + let mut in_reach = 0; + let boxes = stretch_tests(|| in_reach = segments_in_reach(&pd, &reach)); + assert!(in_reach > 0 && boxes < 100_000, "{in_reach} segments in reach, {boxes} measures"); + let share = (MAX_REACH_PIECES / in_reach).max(1); + let mut got = None; + let marks = stretch_tests(|| got = Some(mark_reached(&sp, &reach, share))); + assert!(marks < 100_000, "{marks} measures"); + let smoothed = smooth_subpath(&sp, &reach, share, 2.5, 4.0); + assert!(smoothed.is_some()); + let all = linear(&pts, 12.0); + assert_eq!(segments_in_reach(&pd, &all), in_reach); + assert_eq!(Some(mark_reached(&sp, &all, share)), got); + assert_eq!(smooth_subpath(&sp, &all, share, 2.5, 4.0), smoothed); + } + + /// Paths and drags of many shapes, a drag of one point, one far away, one with a stretch longer + /// than the grid lists by cell, one with a point that is not a number, and reaches of 0 to 50 pt: + /// the grid gives the same segments in reach, the same marks and the same smoothed path as + /// measuring against the whole drag. + #[test] + fn the_drag_grid_gives_the_results_of_measuring_the_whole_drag() { + let far = Point::new(1e7, -1e7); + let paths = [ + ring(), + wave(1500, false), + wave(1500, true), + SubPath::polyline(&(0..500).map(|i| Point::new(f64::from(i) * 2.0, f64::from(i % 3) * 4.0)).collect::>(), false), + SubPath::new( + vec![Anchor::with_handles(Point::new(0.0, 0.0), Point::new(0.0, 0.0), Point::new(3e5, 4e5)), Anchor::corner(Point::new(1e6, 0.0))], + false, + ), + SubPath::new( + wave(200, true) + .anchors + .iter() + .map(|a| Anchor::with_handles(a.p + far.to_vec2(), a.h_in + far.to_vec2(), a.h_out + far.to_vec2())) + .collect(), + true, + ), + ]; + let line = |from: (f64, f64), to: (f64, f64), n: u32| -> Vec { + (0..=n).map(|i| Point::new(from.0, from.1).lerp(Point::new(to.0, to.1), f64::from(i) / f64::from(n))).collect() + }; + let mut loop_drag: Vec = + (0..400).map(|i| Point::new(100.0 + 120.0 * (f64::from(i) * 0.05).cos(), 120.0 * (f64::from(i) * 0.05).sin())).collect(); + loop_drag.extend(line((0.0, 0.0), (1500.0, 2.0), 50)); + let drags = [ + vec![Point::new(150.0, 100.0)], + line((40.0, 90.0), (160.0, 110.0), 30), + line((-100.0, 1.0), (1600.0, 2.0), 1500), + loop_drag, + line((5e6, 5e6), (5e6 + 10.0, 5e6), 5), + vec![Point::new(0.0, 0.0), Point::new(1500.0, 3.0), Point::new(1500.0, 3.2), Point::new(10.0, 0.5)], + vec![Point::new(10.0, 1.0), Point::new(f64::NAN, 0.0), Point::new(12.0, 1.0)], + (0..1000).map(|i| Point::new(f64::from(i) * 0.01, 1.0)).collect(), + line((far.x, far.y), (far.x + 300.0, far.y + 40.0), 80), + ]; + for pts in &drags { + for radius in [0.0, 0.5, 12.0, 50.0] { + let (grid, all) = (DragReach::new(pts, radius), linear(pts, radius)); + for (k, sp) in paths.iter().enumerate() { + let pd = PathData::single(sp.clone()); + let in_reach = segments_in_reach(&pd, &all); + assert_eq!(segments_in_reach(&pd, &grid), in_reach, "path {k}, reach {radius}, drag {:?}", pts.first()); + let share = (MAX_REACH_PIECES / in_reach.max(1)).max(1); + assert_eq!(mark_reached(sp, &grid, share), mark_reached(sp, &all, share), "path {k}, reach {radius}, drag {:?}", pts.first()); + let sigma = radius * SMOOTH_SPREAD; + let want = smooth_subpath(sp, &all, share, 2.5, sigma); + assert_eq!(smooth_subpath(sp, &grid, share, 2.5, sigma), want, "path {k}, reach {radius}, drag {:?}", pts.first()); + } + } + } + } + + /// Replacing stretches of a list from its end, each one ending no later than where the one + /// before started (sometimes on that item, sometimes with nothing or with more items), gives the + /// list that [`Vec::splice`] gives. + #[test] + fn edits_from_the_end_match_splicing() { + let mut seed = 0x2545_f491_u64; + let mut next = |m: usize| { + seed = seed.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1_442_695_040_888_963_407); + (seed >> 33) as usize % m.max(1) + }; + for round in 0..200 { + let items: Vec = (0..next(60) + 1).collect(); + let (mut want, mut got) = (items.clone(), EditsFromEnd::new(items.clone())); + let mut end = items.len() - 1; + loop { + let hi = end.saturating_sub(next(3)); + let lo = hi.saturating_sub(next(6)); + let with: Vec = (0..next(5)).map(|j| 1000 * (round + 1) + j).collect(); + let Some(stretch) = want.get(lo..=hi).map(<[usize]>::to_vec) else { break }; + assert_eq!(got.get(lo, hi).map(<[usize]>::to_vec), Some(stretch), "round {round}"); + want.splice(lo..=hi, with.clone()); + got.replace(lo, hi, with); + if lo == 0 { + break; + } + end = lo; + } + assert_eq!(got.finish(), want, "round {round}"); + } + } + /// Anchors on one point, anchors that are not numbers or are too far out to measure, and a /// zigzag of 100,001 anchors 1 pt high give no corner and do not fail; a square's four anchors /// are its corners, and an open path's ends never are. diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index f8a3dc84a0..bfa156bd1c 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1552,6 +1552,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur When a Smooth tool drag reaches the first or last anchor of an open path, that anchor stays where it is and the curve there leaves or arrives along the smoothed path within the reach, so a jagged first or last segment no longer sets the end's direction; an end where the smoothed path turns back by 90° or more keeps its segment's direction, and where the refitted stretch ends inside the path the curve keeps the direction of the part outside the drag. This rule is VectorCraft's own (`tests/draw2.rs`, `smoothing_an_open_path_turns_its_jagged_ends_with_the_path` and `a_smooth_drag_turns_only_the_end_it_reaches`). A Smooth tool drag keeps a corner where the path turns by more than 57.5° (an angle inside the corner under 122.5°) and rounds a wider one. An anchor is kept as a corner when the path turns by that much at the anchor and also over the drag's reach and over twice the reach on either side of it, so a jag or a bump smaller than the reach is still smoothed; [the help page](https://helpx.adobe.com/illustrator/desktop/draw-shapes-and-paths/modify-paths/refine-paths.html) describes the tool as reducing irregularities in a path. The stretches on either side of a kept corner are smoothed and refitted on their own, and the corner keeps its place and the directions the path arrives and leaves along, on every drag, on open and closed paths, the anchor a closed path starts at included. A drag that changes no anchor adds no undo step. The threshold and the two distances are VectorCraft's own values, the same on every drag and at any Fidelity (`tests/draw2.rs`, `the_smooth_tool_keeps_corners_sharper_than_its_threshold`, `smoothing_a_closed_path_keeps_its_corners` and `a_smooth_drag_that_only_meets_corners_adds_no_undo_step`). A Smooth drag along a curved segment smooths the stretch of it in the drag's reach even when neither of the segment's anchors is in reach, as [the help page](https://helpx.adobe.com/illustrator/desktop/draw-shapes-and-paths/modify-paths/refine-paths.html) describes dragging along the path segment; the segment is cut where the drag enters and leaves the reach, and the parts outside keep their shape (`path.smoothRegion`). Paths of two anchors are smoothed too, and Alt with the Paintbrush smooths a selected two-anchor curve; a two-anchor straight line still gets the closed stroke. How the reach is found on a segment (samples no more than half the reach apart) is VectorCraft's own, and a straight segment is left as it is. + A Smooth drag measures only the anchors and curved segments near the drag, and each against only the stretches of the drag near it, so the work of a release on a long path follows the part of the path under the drag rather than the whole path's length. On a curved path of 100,000 anchors, a drag of 200 points over 200 pt of it makes 7,533 measures to find the curved segments in its reach, where it made 19,899,801 (99,999 segments times 199 stretches). The smoothed path is the same as before (engine tests `a_drag_over_a_long_path_measures_only_what_is_near_it` and `the_drag_grid_gives_the_results_of_measuring_the_whole_drag` in `crates/engine/src/cmd/draw2/smooth.rs`). Before a press that draws a new path, the Pencil cursor shows a small star, as [Draw freeform paths with the Pencil tool](https://web.archive.org/web/20260926095638/https://helpx.adobe.com/illustrator/desktop/draw-shapes-and-paths/draw-shapes/draw-freeform-paths-with-the-pencil-tool.html) describes; over a selected path within Edit selected paths' distance, which the press continues from an end or redraws, the star is not shown ([Reshape paths with the Pencil tool](https://web.archive.org/web/20260106004407/https://helpx.adobe.com/illustrator/desktop/draw-shapes-and-paths/draw-shapes/reshape-paths-with-the-pencil-tool.html)). Keep selected in the Pencil and Paintbrush Tool Options is on by default: while it is on, a stroke selects the path it draws or continues and replaces the earlier selection. While it is off, a new stroke leaves nothing selected, the earlier selection included, as the reference application starts the stroke by dropping it (#5752); a continued or redrawn path is deselected while the rest of the selection stays (`path.freehand` `keepSelected`). With two open paths selected, a Pencil stroke from an end of one that ends within Edit selected paths' distance of an end of the other connects them into one path with the first path's appearance, in one undo step, as [Connect two paths with the Pencil tool](https://helpx.adobe.com/illustrator/desktop/draw-shapes-and-paths/draw-shapes/connect-two-paths-with-the-pencil-tool.html) describes (`path.freehand` `connect`). From 4367a9ce68b9b1cba848dd06c1a573369096ac8a Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 21 Nov 2026 00:22:58 -0800 Subject: [PATCH 2/3] Smooth: the progress line says every anchor and segment is still looked up A release still looks up every anchor and curved segment of a path in the drag's grid, so that work grows with the path's length. The line now says the grid reduces the stretch measures, and that the 7,533 and 19,899,801 figures count stretch measures. --- docs/target-app-parity.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index bfa156bd1c..bce51bbfb4 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -1552,7 +1552,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur When a Smooth tool drag reaches the first or last anchor of an open path, that anchor stays where it is and the curve there leaves or arrives along the smoothed path within the reach, so a jagged first or last segment no longer sets the end's direction; an end where the smoothed path turns back by 90° or more keeps its segment's direction, and where the refitted stretch ends inside the path the curve keeps the direction of the part outside the drag. This rule is VectorCraft's own (`tests/draw2.rs`, `smoothing_an_open_path_turns_its_jagged_ends_with_the_path` and `a_smooth_drag_turns_only_the_end_it_reaches`). A Smooth tool drag keeps a corner where the path turns by more than 57.5° (an angle inside the corner under 122.5°) and rounds a wider one. An anchor is kept as a corner when the path turns by that much at the anchor and also over the drag's reach and over twice the reach on either side of it, so a jag or a bump smaller than the reach is still smoothed; [the help page](https://helpx.adobe.com/illustrator/desktop/draw-shapes-and-paths/modify-paths/refine-paths.html) describes the tool as reducing irregularities in a path. The stretches on either side of a kept corner are smoothed and refitted on their own, and the corner keeps its place and the directions the path arrives and leaves along, on every drag, on open and closed paths, the anchor a closed path starts at included. A drag that changes no anchor adds no undo step. The threshold and the two distances are VectorCraft's own values, the same on every drag and at any Fidelity (`tests/draw2.rs`, `the_smooth_tool_keeps_corners_sharper_than_its_threshold`, `smoothing_a_closed_path_keeps_its_corners` and `a_smooth_drag_that_only_meets_corners_adds_no_undo_step`). A Smooth drag along a curved segment smooths the stretch of it in the drag's reach even when neither of the segment's anchors is in reach, as [the help page](https://helpx.adobe.com/illustrator/desktop/draw-shapes-and-paths/modify-paths/refine-paths.html) describes dragging along the path segment; the segment is cut where the drag enters and leaves the reach, and the parts outside keep their shape (`path.smoothRegion`). Paths of two anchors are smoothed too, and Alt with the Paintbrush smooths a selected two-anchor curve; a two-anchor straight line still gets the closed stroke. How the reach is found on a segment (samples no more than half the reach apart) is VectorCraft's own, and a straight segment is left as it is. - A Smooth drag measures only the anchors and curved segments near the drag, and each against only the stretches of the drag near it, so the work of a release on a long path follows the part of the path under the drag rather than the whole path's length. On a curved path of 100,000 anchors, a drag of 200 points over 200 pt of it makes 7,533 measures to find the curved segments in its reach, where it made 19,899,801 (99,999 segments times 199 stretches). The smoothed path is the same as before (engine tests `a_drag_over_a_long_path_measures_only_what_is_near_it` and `the_drag_grid_gives_the_results_of_measuring_the_whole_drag` in `crates/engine/src/cmd/draw2/smooth.rs`). + A Smooth drag measures only the anchors and curved segments near the drag, and each against only the stretches of the drag near it. A release still looks up every anchor and curved segment of a path in the drag's grid, so the lookups still grow with the path's length. The grid reduces the number of stretch measures. On a curved path of 100,000 anchors, a drag of 200 points over 200 pt of it makes 7,533 stretch measures to find the curved segments in its reach, where it made 19,899,801 (99,999 segments times 199 stretches). The smoothed path is the same as before (engine tests `a_drag_over_a_long_path_measures_only_what_is_near_it` and `the_drag_grid_gives_the_results_of_measuring_the_whole_drag` in `crates/engine/src/cmd/draw2/smooth.rs`). Before a press that draws a new path, the Pencil cursor shows a small star, as [Draw freeform paths with the Pencil tool](https://web.archive.org/web/20260926095638/https://helpx.adobe.com/illustrator/desktop/draw-shapes-and-paths/draw-shapes/draw-freeform-paths-with-the-pencil-tool.html) describes; over a selected path within Edit selected paths' distance, which the press continues from an end or redraws, the star is not shown ([Reshape paths with the Pencil tool](https://web.archive.org/web/20260106004407/https://helpx.adobe.com/illustrator/desktop/draw-shapes-and-paths/draw-shapes/reshape-paths-with-the-pencil-tool.html)). Keep selected in the Pencil and Paintbrush Tool Options is on by default: while it is on, a stroke selects the path it draws or continues and replaces the earlier selection. While it is off, a new stroke leaves nothing selected, the earlier selection included, as the reference application starts the stroke by dropping it (#5752); a continued or redrawn path is deselected while the rest of the selection stays (`path.freehand` `keepSelected`). With two open paths selected, a Pencil stroke from an end of one that ends within Edit selected paths' distance of an end of the other connects them into one path with the first path's appearance, in one undo step, as [Connect two paths with the Pencil tool](https://helpx.adobe.com/illustrator/desktop/draw-shapes-and-paths/draw-shapes/connect-two-paths-with-the-pencil-tool.html) describes (`path.freehand` `connect`). From 26200f0599051ee336aeea9c76c7d70be4e6fc9a Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 21 Nov 2026 21:44:17 -0800 Subject: [PATCH 3/3] Smooth: read the anchors around a run through the edited list The merge of main brought in code that indexes the anchor list while smooth_open now keeps it in an EditsFromEnd, which has no indexing, so the engine did not build. The anchors at both ends of a run and of its neighboring segments are now read from the span that EditsFromEnd::get returns. --- crates/engine/src/cmd/draw2/smooth.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/cmd/draw2/smooth.rs b/crates/engine/src/cmd/draw2/smooth.rs index 8cf41929d9..65eac0afd2 100644 --- a/crates/engine/src/cmd/draw2/smooth.rs +++ b/crates/engine/src/cmd/draw2/smooth.rs @@ -755,8 +755,10 @@ fn smooth_open(anchors: &[Anchor], marked: &[bool], near: &dyn Fn(Point) -> bool // How much of the segments leading out of the run is in reach. A segment that is in reach // only at the run's anchor stays out of the piece, so the piece ends at that anchor rather // than in a sliver of a segment, which would leave the refit no direction to end along. - let after = if hi > b { split_at_reach(CubicBez::new(out[hi].p, out[hi].h_in, out[b].h_out, out[b].p), near, radius) } else { Reach::Whole }; - let before = if lo < a { split_at_reach(CubicBez::new(out[lo].p, out[lo].h_out, out[a].h_in, out[a].p), near, radius) } else { Reach::Whole }; + let Some(span) = out.get(lo, hi) else { continue }; + let (first, start, end, last) = (span[0], span[a - lo], span[b - lo], span[hi - lo]); + let after = if hi > b { split_at_reach(CubicBez::new(last.p, last.h_in, end.h_out, end.p), near, radius) } else { Reach::Whole }; + let before = if lo < a { split_at_reach(CubicBez::new(first.p, first.h_out, start.h_in, start.p), near, radius) } else { Reach::Whole }; if matches!(after, Reach::End) { hi = b; }