From 3e7c19829b75e703312a05de0430a8471a16f6aa Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 15 Nov 2026 11:36:38 -0800 Subject: [PATCH] Eraser: each step of the drag's area outline takes the tip halfway along it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A step took the tip halfway between the tips at its ends, turned the shorter way round. The release reads the pen's pressure and tilt halfway along the step, and its bearing and rotation halfway the shorter way round, so a step from no pressure to full pressure with the angle on Pressure by 90° erases with a level tip there while the outline showed it upright. Each step now takes the tip of the pen's readings halfway along it. --- crates/tools/src/draw2/gesture.rs | 47 ++++++++++++++++----- crates/tools/src/draw2/gesture/area.rs | 22 ++++------ crates/tools/src/draw2/gesture/tests.rs | 55 +++++++++++++++++++------ 3 files changed, 87 insertions(+), 37 deletions(-) diff --git a/crates/tools/src/draw2/gesture.rs b/crates/tools/src/draw2/gesture.rs index 5e8b4a9344..c8925addd5 100644 --- a/crates/tools/src/draw2/gesture.rs +++ b/crates/tools/src/draw2/gesture.rs @@ -1157,19 +1157,41 @@ impl GestureTool { self.poses.len() == self.points.len() } - /// The tip at point `i` of the stroke, as `path.eraseRegion` reads the pen there: the pressure - /// once the pen has pressed less than fully, the tilt and rotation of the point, and the - /// bearing of the last point up to it that the pen leaned 1° or more at, or before the first - /// such point the bearing of that first one ([`Self::tip_for`]). What the release does not - /// read ([`Self::reads_pressures`], [`Self::reads_poses`]) gives the values themselves. + /// The tip at point `i` of the stroke, as `path.eraseRegion` reads the pen there + /// ([`Self::controls_of_point`]). fn tip_of_point(&self, i: usize) -> [f64; 3] { + self.tip_of_controls(self.controls_of_point(i)) + } + + /// The tip halfway along the step from point `j` to point `j + 1`, as `path.eraseRegion` reads + /// the pen there: the pressure and tilt halfway between those at the two points, and the + /// bearing and rotation halfway between them the shorter way round. A tip whose angle follows + /// the pressure from 0 at one point to 1 at the next lies level there, not turned as at + /// either end. + fn tip_between(&self, j: usize) -> [f64; 3] { + let (a, b) = (self.controls_of_point(j), self.controls_of_point(j + 1)); + // Bearing and rotation read ½ plus the angle over 360° ([`super::pen_controls`]). + let turn = |a: f64, b: f64| { + let (a, b) = ((a - 0.5) * 360.0, (b - 0.5) * 360.0); + 0.5 + vectorcraft_geom::normalize_deg(a + vectorcraft_geom::normalize_deg(b - a) / 2.0) / 360.0 + }; + self.tip_of_controls([(a[0] + b[0]) / 2.0, (a[1] + b[1]) / 2.0, turn(a[2], b[2]), turn(a[3], b[3])]) + } + + /// What each of the variation modes Pressure, Tilt, Bearing and Rotation reads at point `i` of + /// the stroke, as `path.eraseRegion` reads the pen there ([`super::pen_controls`]): the + /// pressure once the pen has pressed less than fully, the tilt and rotation of the point, and + /// the bearing of the last point up to it that the pen leaned 1° or more at, or before the + /// first such point the bearing of that first one. What the release does not read + /// ([`Self::reads_pressures`], [`Self::reads_poses`]) reads ½, the values themselves. + fn controls_of_point(&self, i: usize) -> [f64; 4] { let pressure = if self.reads_pressures() { self.pressures.get(i).map_or(0.5, |p| f64::from(*p)) } else { 0.5 }; let (pose, bearing) = if self.reads_poses() { (self.poses.get(i).copied().flatten(), self.bearings.get(i).copied().flatten().or(self.first_bearing)) } else { (None, None) }; - self.tip_for(pressure, pose, bearing) + super::pen_controls(pressure, pose, bearing) } /// How the tip's angle, roundness and size vary: the Eraser's three modes, the Blob Brush's @@ -1207,8 +1229,13 @@ impl GestureTool { /// to the value plus it ([`super::pen_controls`]); for Random, Fixed and what the pen does not /// give, the values themselves. fn tip_for(&self, pressure: f64, pose: Option, bearing: Option) -> [f64; 3] { + self.tip_of_controls(super::pen_controls(pressure, pose, bearing)) + } + + /// The tip's angle, roundness and size where the modes Pressure, Tilt, Bearing and Rotation + /// read `controls` (0..1 each, [`super::pen_controls`]). + fn tip_of_controls(&self, controls: [f64; 4]) -> [f64; 3] { let base = [self.angle, self.roundness, self.size]; - let controls = super::pen_controls(pressure, pose, bearing); let variations = [self.angle_variation, self.roundness_variation, self.size_variation]; let mut out = base; for (i, (v, mode)) in out.iter_mut().zip(self.tip_modes()).enumerate() { @@ -1740,10 +1767,8 @@ impl Tool for GestureTool { variations: [self.angle_variation, self.roundness_variation, self.size_variation], }; let mut area = self.area.take().filter(|a| a.fits(key, &self.points)).unwrap_or_else(|| AreaPreview::new(key, self.points[0])); - area.step(&self.points, &|i| { - let [angle, roundness, size] = self.tip_of_point(i); - [angle, roundness, if size_varies { size } else { self.size }] - }); + let sized = |[angle, roundness, size]: [f64; 3]| [angle, roundness, if size_varies { size } else { self.size }]; + area.step(&self.points, &|i| sized(self.tip_of_point(i)), &|j| sized(self.tip_between(j))); let paths = area.paths(&self.points); o.extend(paths.settled.into_iter().map(|path| Overlay::SharedPath { path, color, width: 1.0 })); o.extend(paths.live.into_iter().map(|path| Overlay::Path { path, color, width: 1.0, dashed: false })); diff --git a/crates/tools/src/draw2/gesture/area.rs b/crates/tools/src/draw2/gesture/area.rs index 3b92c0291f..5a33e6e77f 100644 --- a/crates/tools/src/draw2/gesture/area.rs +++ b/crates/tools/src/draw2/gesture/area.rs @@ -241,9 +241,9 @@ impl AreaPreview { /// Take the points of the drag `pts` added since the last call and check outlines, within this /// call's limits. When the tip varies along the drag, `tip(i)` gives the tip at point `i`, and - /// each segment takes the tip halfway between those at its ends. A last point whose tip changed - /// since it was taken (a pen turned or tilted in place) gives its segment the new tip. - pub(super) fn step(&mut self, pts: &[Point], tip: &dyn Fn(usize) -> Tip) { + /// segment `j` takes `middle(j)`, the tip halfway along it. A last point whose tip changed since + /// it was taken (a pen turned or tilted in place) gives its segment the new tip. + pub(super) fn step(&mut self, pts: &[Point], tip: &dyn Fn(usize) -> Tip, middle: &dyn Fn(usize) -> Tip) { self.work = AreaWork::default(); let mut changed = BTreeSet::new(); if self.key.varies @@ -252,7 +252,7 @@ impl AreaPreview { { let k = self.piece_of(j); let old = self.radius[j]; - self.set_tip(j, tip(j), tip(j + 1)); + self.set_tip(j, middle(j)); self.last_tip = Some(tip(j + 1)); changed.insert(k); // The pieces near the old tip or the new one are checked again. @@ -272,7 +272,7 @@ impl AreaPreview { let Some(j) = i.checked_sub(1) else { continue }; if self.key.varies { self.radius.push(0.0); - self.set_tip(j, tip(j), tip(i)); + self.set_tip(j, middle(j)); } else { self.radius.push(self.key.size / 2.0); } @@ -290,17 +290,11 @@ impl AreaPreview { self.check_some(); } - /// Give segment `j` the tip halfway between `from` and `to`, the tips at its ends: its radius, - /// and with `shaped` the map to the space where that tip is round. The angles meet the shorter - /// way round, a tip turned by 180° being the same tip. - fn set_tip(&mut self, j: usize, from: Tip, to: Tip) { - let size = (from[2] + to[2]) / 2.0; + /// Give segment `j` the tip `[angle, roundness, size]`, the tip halfway along it: its radius, + /// and with `shaped` the map to the space where that tip is round. + fn set_tip(&mut self, j: usize, [angle, roundness, size]: Tip) { self.radius[j] = size.max(MIN_BRUSH_SIZE) / 2.0; if self.key.shaped { - let turn = (to[0] - from[0]).rem_euclid(180.0); - let turn = if turn > 90.0 { turn - 180.0 } else { turn }; - let angle = from[0] + turn / 2.0; - let roundness = (from[1] + to[1]) / 2.0; let round = eraser_tip(angle, roundness).inverse(); match self.to_round.get_mut(j) { Some(m) => *m = round, diff --git a/crates/tools/src/draw2/gesture/tests.rs b/crates/tools/src/draw2/gesture/tests.rs index a9cb4947e1..7aba70d4b6 100644 --- a/crates/tools/src/draw2/gesture/tests.rs +++ b/crates/tools/src/draw2/gesture/tests.rs @@ -3080,9 +3080,10 @@ fn the_eraser_drag_area_matches_the_release_when_pressure_or_a_mode_starts_durin press(&mut t, leaning, 1.0, PointerKind::Drag, 150.0); near(bounds(&t), (40.0, 147.5, 160.0, 152.5)); // The pen lifts to no pressure: the release reads full pressure at the first three points, the - // tip turned 90°, and none at the last, turned -90°. Every step stands upright. + // tip turned 90°, and none at the last, turned -90°. The first two steps stand upright, and + // the last, halfway between full pressure and none, lies level. press(&mut t, leaning, 0.0, PointerKind::Drag, 200.0); - near(bounds(&t), (47.5, 140.0, 202.5, 160.0)); + near(bounds(&t), (47.5, 140.0, 210.0, 160.0)); press(&mut t, leaning, 0.0, PointerKind::Up, 200.0); // The same tip with its angle on Pressure by up to 90°, pressed halfway all along, and @@ -3167,10 +3168,42 @@ fn the_eraser_drag_area_gives_the_first_lean_to_the_points_before_it() { same((drag([[0.0, 0.0]; 5], Some([0.0, 45.0])), drag([[0.0, 45.0]; 5], Some([0.0, 45.0])))); } +/// Each step of an Eraser drag's outline takes the tip the release erases with halfway along it: +/// with the angle on Pressure by up to 90°, a step from no pressure (the tip turned -90°) to full +/// pressure (turned 90°) shows the tip lying level, as the release turns it through level there, +/// though the tips at its ends are the same upright tip. +#[test] +fn the_eraser_drag_area_takes_the_tip_halfway_along_each_step() { + let (d, _) = doc_with_rect(); + let s = Selection::default(); + let p = paint(); + let cx = ToolContext { precise_cursors: true, ..cx(&d, &s, &p) }; + let mut t = GestureTool::new("eraser"); + for (k, v) in [("size", json!(20)), ("roundness", json!(25)), ("angleMode", json!("pressure")), ("angleVariation", json!(90))] { + t.set_option(k, &v); + } + t.pointer(&cx, &PointerEvent { pressure: 0.0, ..PointerEvent::new(PointerKind::Down, 50.0, 150.0) }); + t.pointer(&cx, &PointerEvent { pressure: 1.0, ..PointerEvent::new(PointerKind::Drag, 150.0, 150.0) }); + drain_area(&t, &cx); + let o = t.overlays(&cx); + let b = o + .iter() + .filter_map(|o| match o { + Overlay::Path { path, .. } => Some(vectorcraft_geom::Shape::bounding_box(path)), + Overlay::SharedPath { path, .. } => Some(vectorcraft_geom::Shape::bounding_box(&**path)), + _ => None, + }) + .reduce(|a, b| a.union(b)) + .unwrap_or_else(|| panic!("{o:?}")); + // The level tip 20 by 5 pt swept from x 50 to 150. + let want = (40.0, 147.5, 160.0, 152.5); + assert!([b.x0 - want.0, b.y0 - want.1, b.x1 - want.2, b.y1 - want.3].iter().all(|e| e.abs() < 0.01), "{b:?}"); + t.pointer(&cx, &PointerEvent { pressure: 1.0, ..PointerEvent::new(PointerKind::Up, 150.0, 150.0) }); +} + /// The outline of a long Eraser drag whose tip turns and changes shape with the pen is built a /// bounded amount per frame, and once finished it shows nothing inside the area: no point of it -/// lies well inside the area of any step, each step with the tip halfway between those at its -/// ends. +/// lies well inside the area of any step, each step with the tip halfway along it. #[test] fn the_outline_of_a_long_varied_eraser_drag_is_bounded_and_shows_nothing_inside() { let (d, _) = doc_with_rect(); @@ -3194,21 +3227,19 @@ fn the_outline_of_a_long_varied_eraser_drag_is_bounded_and_shows_nothing_inside( drain_area(&t, &cx); let shown = shown_points(&t.overlays(&cx)); assert!(!shown.is_empty()); - let tips: Vec<[f64; 3]> = (0..t.points.len()).map(|i| t.tip_of_point(i)).collect(); let steps: Vec<(Affine, Point, Point, f64)> = (0..t.points.len() - 1) .map(|j| { - let (a, b) = (tips[j], tips[j + 1]); - let turn = (b[0] - a[0]).rem_euclid(180.0); - let turn = if turn > 90.0 { turn - 180.0 } else { turn }; - let angle = a[0] + turn / 2.0; - let round = eraser_tip(angle, (a[1] + b[1]) / 2.0).inverse(); - (round, round * t.points[j], round * t.points[j + 1], (a[2] + b[2]) / 4.0) + let [angle, roundness, size] = t.tip_between(j); + let round = eraser_tip(angle, roundness).inverse(); + (round, round * t.points[j], round * t.points[j + 1], size / 2.0) }) .collect(); for &(q, _) in &shown { + // A curve of an outline is checked at the middles of its pieces, so where it crosses the + // end of a narrow tip's area, the end of a piece can lie a little way inside it. for &(round, a, b, r) in &steps { let d = distance_to_line(round * q, (a, b)); - assert!(d > r * 0.9, "a point of the outline {d} from a step, inside its area of radius {r}, at {q:?}"); + assert!(d > r * 0.85, "a point of the outline {d} from a step, inside its area of radius {r}, at {q:?}"); } } }