PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Brush smoothing: catch-up compares the pose too, not only the position (#7747)
David Mannc4e8eb2b8632
diff --git a/crates/paint/src/brush_tests.rs b/crates/paint/src/brush_tests.rsindex 2365d5e58..c498d7a35 100644--- a/crates/paint/src/brush_tests.rs+++ b/crates/paint/src/brush_tests.rs@@ -939,6 +939,46 @@ fn a_pulled_string_brush_takes_the_pose_along_the_string() {assert!((p.rotation - 6.0).abs() < 1e-3, "{}", p.rotation);}+#[test]+fn a_smoothed_brush_catches_up_with_a_pen_lifted_in_place() {+ // #7747: only the pressure changes while the pointer stands still. The brush lags in pose,+ // so catch-up must keep running and the stroke end at the pen's last pressure.+ let cfg = Smoothing { amount: 0.8, ..Default::default() };+ let mut s = Smoother::new(&cfg, 1.0);+ let mut out = Vec::new();+ s.push(StrokePoint::new(50.0, 50.0, 1.0), &mut out);+ s.push(StrokePoint { time: 32.0, ..StrokePoint::new(50.0, 50.0, 0.0) }, &mut out);+ assert!(out.last().unwrap().pressure > 0.5, "the brush trails in pressure: {:?}", out.last());+ assert!(s.lagging(), "a pose behind the pointer is still catching up");+ // Held long enough, the brush arrives and stops asking for time.+ for i in 1..=200 {+ s.push(StrokePoint { time: 32.0 + f64::from(i) * 16.0, ..StrokePoint::new(50.0, 50.0, 0.0) }, &mut out);+ }+ assert!(!s.lagging(), "{:?}", out.last());+ s.finish(&mut out);+ assert_eq!(out.last().unwrap().pressure, 0.0);+}++#[test]+fn catch_up_on_stroke_end_finishes_the_pose_of_a_still_pen() {+ // #7747: tilt, rotation and wheel changed in place; the end catch-up lands on all of them.+ let cfg = Smoothing { amount: 0.8, ..Default::default() };+ let a = StrokePoint { rotation: 350.0, ..StrokePoint::new(50.0, 50.0, 1.0) };+ let b = StrokePoint { time: 16.0, tilt_x: 40.0, tilt_y: -20.0, rotation: 10.0, wheel: 0.5, ..StrokePoint::new(50.0, 50.0, 1.0) };+ let mut s = Smoother::new(&cfg, 1.0);+ let mut out = Vec::new();+ s.push(a, &mut out);+ s.push(b, &mut out);+ assert!(s.lagging());+ s.finish(&mut out);+ let p = out.last().unwrap();+ assert_eq!((p.tilt_x, p.tilt_y, p.rotation, p.wheel), (40.0, -20.0, 10.0, 0.5));+ // Nothing left to catch up: no extra point.+ let n = out.len();+ s.finish(&mut out);+ assert_eq!(out.len(), n);+}+#[test]fn pose_overrides_stylus_values() {let b = BrushSettings {diff --git a/crates/paint/src/dynamics.rs b/crates/paint/src/dynamics.rsindex 0c535cf89..e53b366fa 100644--- a/crates/paint/src/dynamics.rs+++ b/crates/paint/src/dynamics.rs@@ -76,6 +76,18 @@ fn pull(brush: &StrokePoint, p: &StrokePoint, f: f64) -> StrokePoint {}}+/// Does the brush's pose (pressure, tilt, rotation, wheel) differ from `p`'s by more than `eps`?+/// [`pull`] moves the pose with the position, so a pen that only changes pressure or tilt in place+/// leaves the brush behind too (#7747).+fn pose_differs(brush: &StrokePoint, p: &StrokePoint, eps: f32) -> bool {+ let dr = (p.rotation - brush.rotation + 540.0).rem_euclid(360.0) - 180.0;+ (p.pressure - brush.pressure).abs() > eps+ || (p.tilt_x - brush.tilt_x).abs() > eps+ || (p.tilt_y - brush.tilt_y).abs() > eps+ || dr.abs() > eps+ || (p.wheel - brush.wheel).abs() > eps+}+/// Incremental stroke smoothing: exponential (with optional time-based catch-up) or pulled string.#[derive(Clone, Debug)]pub struct Smoother {@@ -153,7 +165,7 @@ impl Smoother {/// stroke needs time to keep passing (repeats of the last point) while the pointer is held.pub fn lagging(&self) -> bool {match (self.active() && self.cfg.catch_up && !self.cfg.pulled_string, self.pos, self.last_input) {- (true, Some(pos), Some(last)) => (pos.x - last.x).abs() + (pos.y - last.y).abs() > 0.01,+ (true, Some(pos), Some(last)) => (pos.x - last.x).abs() + (pos.y - last.y).abs() > 0.01 || pose_differs(&pos, &last, 1e-3),_ => false,}}@@ -166,13 +178,14 @@ impl Smoother {(self.active() && (pos.x != last.x || pos.y != last.y)).then_some(([pos.x, pos.y], [last.x, last.y]))}- /// End of stroke: with Catch-Up On Stroke End the brush finishes at the last pointer position.+ /// End of stroke: with Catch-Up On Stroke End the brush finishes at the last pointer position+ /// and pose.pub fn finish(&mut self, out: &mut Vec<StrokePoint>) {if !self.active() || !self.cfg.catch_up_on_end {return;}if let (Some(pos), Some(last)) = (self.pos, self.last_input)- && (pos.x != last.x || pos.y != last.y)+ && (pos.x != last.x || pos.y != last.y || pose_differs(&pos, &last, 0.0)){self.pos = Some(last);out.push(last);