PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Brush smoothing: the lagging brush keeps the pressure of where it is (#5713)
Higgs32584b768970d4dcf
diff --git a/crates/paint/src/brush_tests.rs b/crates/paint/src/brush_tests.rsindex d0d7de411..7a5e4dec4 100644--- a/crates/paint/src/brush_tests.rs+++ b/crates/paint/src/brush_tests.rs@@ -874,6 +874,38 @@ fn smoothing_without_catch_up_stops_while_the_pointer_pauses() {assert_eq!(out.len(), n, "held still: the brush doesn't move");}+#[test]+fn a_smoothed_brush_keeps_the_pressure_of_where_it_is_when_the_pen_lifts() {+ // #5713: a fast stroke at full pressure, lifted in the last sample. The brush trails far+ // behind the pointer; it used to take the lift's pressure there, and the end catch-up drew+ // a hairline from the trailing brush to the pointer.+ let pts: Vec<StrokePoint> =+ (0..=20).map(|i| StrokePoint { time: f64::from(i) * 4.0, ..StrokePoint::new(f64::from(i) * 10.0, 0.0, if i < 20 { 1.0 } else { 0.05 }) }).collect();+ let out = smooth_points(&Smoothing { amount: 0.8, ..Default::default() }, 1.0, &pts);+ let (last, rest) = out.split_last().unwrap();+ assert_eq!((last.x, last.pressure), (200.0, 0.05), "the catch-up still ends at the pen's last sample");+ let lifted = rest.last().unwrap();+ assert!(lifted.x < 190.0, "the brush lags the pointer: {}", lifted.x);+ for p in rest {+ assert!(p.pressure > 0.5, "the trailing brush at x {} has pressure {}", p.x, p.pressure);+ }+}++#[test]+fn a_pulled_string_brush_takes_the_pose_along_the_string() {+ let ps = Smoothing { amount: 0.2, pulled_string: true, catch_up_on_end: false, ..Default::default() };+ let a = StrokePoint { rotation: 350.0, ..StrokePoint::new(0.0, 0.0, 1.0) };+ let b = StrokePoint { rotation: 10.0, tilt_x: 40.0, ..StrokePoint::new(100.0, 0.0, 0.0) };+ let out = smooth_points(&ps, 1.0, &[a, b]);+ // The 20 px string leaves the brush 80 % of the way to the pointer.+ let p = out.last().unwrap();+ assert!((p.x - 80.0).abs() < 1e-4, "{}", p.x);+ assert!((p.pressure - 0.2).abs() < 1e-4, "{}", p.pressure);+ assert!((p.tilt_x - 32.0).abs() < 1e-3, "{}", p.tilt_x);+ // Rotation the short way round: 350° + 80 % of 20°.+ assert!((p.rotation - 6.0).abs() < 1e-3, "{}", p.rotation);+}+#[test]fn pose_overrides_stylus_values() {let b = BrushSettings {diff --git a/crates/paint/src/dynamics.rs b/crates/paint/src/dynamics.rsindex ab846f195..c0cad0946 100644--- a/crates/paint/src/dynamics.rs+++ b/crates/paint/src/dynamics.rs@@ -46,6 +46,27 @@ fn lerp_pt(a: &StrokePoint, b: &StrokePoint, f: f64) -> StrokePoint {// Smoothing// ---------------------------------------------------------------------------+/// The smoothed brush moved `f` of the way from `brush` towards the pointer `p`. Its pose+/// (pressure, tilt, rotation, wheel) moves with it, so the lagging brush paints with the pose the+/// pen had along the way, not the pointer's newest one. Taking the pointer's pressure made a fast+/// stroke's lift thin the brush while it still trailed far behind, and the end catch-up a hairline+/// (#5713). Time is the pointer's.+fn pull(brush: &StrokePoint, p: &StrokePoint, f: f64) -> StrokePoint {+ let ff = f as f32;+ // Barrel rotation takes the shorter way round.+ let dr = (p.rotation - brush.rotation + 540.0).rem_euclid(360.0) - 180.0;+ StrokePoint {+ x: brush.x + (p.x - brush.x) * f,+ y: brush.y + (p.y - brush.y) * f,+ pressure: brush.pressure + (p.pressure - brush.pressure) * ff,+ tilt_x: brush.tilt_x + (p.tilt_x - brush.tilt_x) * ff,+ tilt_y: brush.tilt_y + (p.tilt_y - brush.tilt_y) * ff,+ rotation: (brush.rotation + dr * ff).rem_euclid(360.0),+ wheel: brush.wheel + (p.wheel - brush.wheel) * ff,+ time: p.time,+ }+}+/// Incremental stroke smoothing: exponential (with optional time-based catch-up) or pulled string.#[derive(Clone, Debug)]pub struct Smoother {@@ -87,7 +108,7 @@ impl Smoother {let d = (dx * dx + dy * dy).sqrt();if d > len {let k = (d - len) / d;- let np = StrokePoint { x: pos.x + dx * k, y: pos.y + dy * k, ..p };+ let np = pull(&pos, &p, k);self.pos = Some(np);out.push(np);}@@ -113,7 +134,7 @@ impl Smoother {};let mut cur = pos;for _ in 0..iters {- cur = StrokePoint { x: cur.x + (p.x - cur.x) * step, y: cur.y + (p.y - cur.y) * step, ..p };+ cur = pull(&cur, &p, step);out.push(cur);}self.pos = Some(cur);