From aaa044d68e20e4783433acc86f3351331726bcd7 Mon Sep 17 00:00:00 2001 From: hnoerenberg Date: Wed, 2 Dec 2026 12:02:12 +0100 Subject: [PATCH 1/2] fix(paint): start Diagonal symmetry on the canvas diagonal The Diagonal preset kept a fixed 45 degree line, so on a non-square canvas the mirror axis cut across the picture at the wrong angle. Its axis is the diagonal of the centred box, which follows the canvas aspect (45 degrees only on a square canvas). The command now folds that canvas angle into the preset's stored turn, so rotation, transform and canvas edits keep working. --- crates/engine-core/src/cmd/symmetry_cmds.rs | 8 +- .../src/cmd/symmetry_cmds/preset_tests.rs | 6 +- crates/paint/src/lib.rs | 3 + crates/paint/src/symmetry.rs | 24 +++++ .../src/symmetry_diagonal_angle_tests.rs | 100 ++++++++++++++++++ 5 files changed, 138 insertions(+), 3 deletions(-) create mode 100644 crates/paint/src/symmetry_diagonal_angle_tests.rs diff --git a/crates/engine-core/src/cmd/symmetry_cmds.rs b/crates/engine-core/src/cmd/symmetry_cmds.rs index 0668f80dea1..30261c566c6 100644 --- a/crates/engine-core/src/cmd/symmetry_cmds.rs +++ b/crates/engine-core/src/cmd/symmetry_cmds.rs @@ -113,7 +113,7 @@ fn set_preset(s: &mut Session, p: &Value) -> Result { None => 0.0, Some(v) => v.as_f64().ok_or_else(fail)?, }; - let preset = PresetSymmetry::new(mode, center, rotation).ok_or_else(fail)?; + let preset = PresetSymmetry::for_canvas(mode, center, rotation, [st.doc.size.width, st.doc.size.height]).ok_or_else(fail)?; let symmetry = PaintingSymmetry::Preset(preset); let result = symmetry.inspect(); s.active_mut().ok_or(EngineError::NoDocument)?.symmetry = Some(symmetry); @@ -301,6 +301,7 @@ pub fn specs() -> Vec { mod tests { use super::*; use photocraft_doc::vector::Subpath; + use photocraft_paint::symmetry::diagonal_start_angle; #[test] fn curved_axis_reflects_near_local_segment() { @@ -338,7 +339,10 @@ mod tests { s.execute("image.imageRotation.flipCanvasHorizontal", json!({})).unwrap(); let p = preset(&s); assert_eq!(p.center, [90.0, 40.0], "edge coordinates mirror about the canvas width, not the last pixel"); - assert!((p.rotation - 90.0).abs() < 1e-9, "the 45° axis mirrors to 135°: {}", p.rotation); + // The 120×200 canvas' own diagonal (59.04°) mirrors to 120.96° on a horizontal flip; the + // stored turn is that angle over the 45° line `directions` still starts from. + let mirrored = (diagonal_start_angle([120, 200]).unwrap() - 45.0).rem_euclid(360.0) + (180.0 - 2.0 * diagonal_start_angle([120, 200]).unwrap()); + assert!((p.rotation - mirrored).abs() < 1e-9, "the canvas-diagonal axis mirrors to its flipped angle: {} vs {mirrored}", p.rotation); // A turned dual preset under a non-uniform Image Size: its axes (135° and 45°) map to // 153.4° and 26.6°, no longer perpendicular; the preset splits the difference. s.execute("paint.setSymmetry", json!({"mode": "dual", "center": [30, 40], "rotation": 45})).unwrap(); diff --git a/crates/engine-core/src/cmd/symmetry_cmds/preset_tests.rs b/crates/engine-core/src/cmd/symmetry_cmds/preset_tests.rs index 5e13ca71da1..ad5291c1dc7 100644 --- a/crates/engine-core/src/cmd/symmetry_cmds/preset_tests.rs +++ b/crates/engine-core/src/cmd/symmetry_cmds/preset_tests.rs @@ -1,4 +1,5 @@ use super::*; +use photocraft_paint::symmetry::diagonal_start_angle; fn session(depth: u8, mode: &str) -> Session { let mut s = crate::session(); @@ -16,7 +17,10 @@ fn presets_validate_atomically_and_leave_document_paths_and_history_untouched() s.execute("paint.setSymmetry", json!({"mode":mode.id()})).unwrap(); let p = s.active().unwrap().symmetry.as_ref().unwrap().preset().unwrap(); assert_eq!(p.center, [40.5, 30.5]); - assert_eq!(p.rotation, 0.0); + // Diagonal starts on the 81×61 canvas' own diagonal, so its stored turn is the difference + // to the 45° line `directions` starts from; the other modes start at their own angle. + let start = if mode == SymmetryMode::Diagonal { (diagonal_start_angle([81, 61]).unwrap() - 45.0).rem_euclid(360.0) } else { 0.0 }; + assert!((p.rotation - start).abs() < 1.0e-9, "{mode:?} starts where its mode starts: {}", p.rotation); assert_eq!(crate::inspect::document(s.active().unwrap())["symmetry"]["mode"], mode.id()); assert_eq!(s.active().unwrap().doc, before); assert_eq!((s.active().unwrap().history.past_len(), s.active().unwrap().revision), (history, revision)); diff --git a/crates/paint/src/lib.rs b/crates/paint/src/lib.rs index da3744833f1..b37e0bc77d8 100644 --- a/crates/paint/src/lib.rs +++ b/crates/paint/src/lib.rs @@ -519,3 +519,6 @@ mod brush_noise_strength_tests; #[cfg(test)] mod brush_size_fade_end_tests; + +#[cfg(test)] +mod symmetry_diagonal_angle_tests; diff --git a/crates/paint/src/symmetry.rs b/crates/paint/src/symmetry.rs index 54c9c5f3b9e..34c48cf9925 100644 --- a/crates/paint/src/symmetry.rs +++ b/crates/paint/src/symmetry.rs @@ -5,6 +5,19 @@ use serde::{Deserialize, Serialize}; pub const MAX_COORDINATE: f64 = 1_000_000.0; +/// The Diagonal preset's starting direction, in clockwise degrees over +x (document Y points +/// down). The centred 75 % box has the canvas' own aspect, so its diagonal runs at +/// `atan2(height, width)`: 45° only on a square canvas. `None` when a dimension is zero, which +/// leaves no diagonal to follow. +#[must_use = "the Diagonal preset needs the canvas angle to start on the canvas diagonal"] +pub fn diagonal_start_angle(canvas: [u32; 2]) -> Option { + let (width, height) = (f64::from(canvas[0]), f64::from(canvas[1])); + if width <= 0.0 || height <= 0.0 { + return None; + } + Some(height.atan2(width).to_degrees()) +} + #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, craft_ui_macros::PlainData)] #[serde(rename_all = "lowercase")] pub enum SymmetryMode { @@ -44,6 +57,17 @@ impl PresetSymmetry { } Some(Self { mode, center, rotation: rotation.rem_euclid(360.0) }) } + /// The preset for a canvas of `canvas` pixels. Diagonal starts along that canvas' diagonal + /// instead of the fixed 45° line [`Self::directions`] gives it, so the canvas angle is folded + /// into `rotation`, which still means "turned away from the mode's start". Every other mode, + /// and every `canvas` other than a degenerate one, is [`Self::new`]. + pub fn for_canvas(mode: SymmetryMode, center: [f64; 2], rotation: f64, canvas: [u32; 2]) -> Option { + let start = match mode { + SymmetryMode::Diagonal => diagonal_start_angle(canvas)? - 45.0, + _ => 0.0, + }; + Self::new(mode, center, rotation + start) + } pub fn directions(self) -> Vec<[f64; 2]> { let initial: f64 = match self.mode { SymmetryMode::Horizontal => 0.0, diff --git a/crates/paint/src/symmetry_diagonal_angle_tests.rs b/crates/paint/src/symmetry_diagonal_angle_tests.rs new file mode 100644 index 00000000000..94cfb68b7f1 --- /dev/null +++ b/crates/paint/src/symmetry_diagonal_angle_tests.rs @@ -0,0 +1,100 @@ +//! #17377: the Diagonal symmetry preset follows the canvas, not a fixed 45° line. +//! The measured rows come from the issue's specification of the reference application; the +//! geometry itself (the diagonal of the centred 75 % box) is ours. + +use crate::{ + StrokePoint, + symmetry::{PresetSymmetry, SymmetryMode, diagonal_start_angle}, +}; + +const TOL: f64 = 1.0e-9; + +fn near(a: [f64; 2], b: [f64; 2], tol: f64) -> bool { + (a[0] - b[0]).abs() < tol && (a[1] - b[1]).abs() < tol +} + +fn mirrored(p: PresetSymmetry, x: f64, y: f64) -> [f64; 2] { + let r = p.reflected_passes(&[StrokePoint::new(x, y, 1.0)])[0][0]; + [r.x, r.y] +} + +#[test] +fn on_800x600_the_diagonal_axis_is_the_canvas_diagonal() { + let s = PresetSymmetry::for_canvas(SymmetryMode::Diagonal, [400.0, 300.0], 0.0, [800, 600]).unwrap(); + let d = s.directions()[0]; + // The spec's axis runs (100,75)-(700,525): slope 450/600, so its direction is (0.8, 0.6). + assert!(near(d, [0.8, 0.6], 1.0e-9), "expected the (100,75)-(700,525) direction, got {d:?}"); + let angle = diagonal_start_angle([800, 600]).unwrap(); + assert!((angle - 36.87).abs() < 0.01, "the axis sits at 36.87 degrees, not 45: {angle}"); + // The drawn axis stays inside the canvas and keeps that slope end to end. + let [[a, b]] = s.segments([800.0, 600.0])[..].try_into().expect("one diagonal segment"); + assert!(near(a, [0.0, 0.0], 1.0e-9) && near(b, [800.0, 600.0], 1.0e-9), "clipped to the canvas diagonal: {a:?} {b:?}"); +} + +#[test] +fn on_800x600_a_dot_at_170_142_mirrors_to_about_184_123() { + let s = PresetSymmetry::for_canvas(SymmetryMode::Diagonal, [400.0, 300.0], 0.0, [800, 600]).unwrap(); + let got = mirrored(s, 170.0, 142.0); + assert!(near(got, [184.0, 123.0], 0.5), "the spec mirrors (170,142) to about (184,123), got {got:?}"); +} + +#[test] +fn the_axis_slope_follows_the_canvas_aspect() { + for canvas in [[1000u32, 400u32], [300, 900], [640, 480], [800, 600]] { + let (w, h) = (f64::from(canvas[0]), f64::from(canvas[1])); + let s = PresetSymmetry::for_canvas(SymmetryMode::Diagonal, [w / 2.0, h / 2.0], 0.0, canvas).unwrap(); + let [dx, dy] = s.directions()[0]; + assert!((dy / dx - h / w).abs() < 1.0e-9, "{canvas:?}: the axis follows the canvas aspect, got slope {}", dy / dx); + } +} + +#[test] +fn a_square_canvas_keeps_the_45_degree_axis() { + let s = PresetSymmetry::for_canvas(SymmetryMode::Diagonal, [400.0, 400.0], 0.0, [800, 800]).unwrap(); + let [dx, dy] = s.directions()[0]; + assert!((dx - dy).abs() < TOL, "a square canvas stays at 45 degrees: {dx} {dy}"); + assert!((diagonal_start_angle([800, 800]).unwrap() - 45.0).abs() < TOL, "45 degrees on a square canvas"); + // A 45° axis through the centre swaps the point's offsets, which is today's result. + assert!(near(mirrored(s, 170.0, 142.0), [142.0, 170.0], 1.0e-9), "the square mirror is unchanged"); +} + +#[test] +fn transform_symmetry_can_still_rotate_the_axis() { + let s = PresetSymmetry::for_canvas(SymmetryMode::Diagonal, [400.0, 300.0], 90.0, [800, 600]).unwrap(); + let d = s.directions()[0]; + let dot = d[0] * 0.8 + d[1] * 0.6; + assert!(dot.abs() < 1.0e-9, "a 90 degree turn is perpendicular to the canvas diagonal: {d:?} dot {dot}"); + let s45 = PresetSymmetry::for_canvas(SymmetryMode::Diagonal, [400.0, 300.0], 45.0, [800, 600]).unwrap(); + let turned = s45.directions()[0]; + assert!( + (turned[1] / turned[0] - (diagonal_start_angle([800, 600]).unwrap() + 45.0).to_radians().tan()).abs() < 1.0e-9, + "the turn adds to the canvas angle: {turned:?}" + ); +} + +#[test] +fn vertical_horizontal_and_dual_are_unchanged_on_a_non_square_canvas() { + for mode in [SymmetryMode::Vertical, SymmetryMode::Horizontal, SymmetryMode::Dual] { + let plain = PresetSymmetry::new(mode, [400.0, 300.0], 0.0).unwrap(); + let on_canvas = PresetSymmetry::for_canvas(mode, [400.0, 300.0], 0.0, [800, 600]).unwrap(); + assert_eq!(plain.directions(), on_canvas.directions(), "{mode:?} keeps its own start angle"); + assert_eq!(plain.mirror_count(), on_canvas.mirror_count(), "{mode:?} keeps its pass count"); + for (a, b) in + plain.reflected_passes(&[StrokePoint::new(170.0, 142.0, 1.0)]).iter().zip(on_canvas.reflected_passes(&[StrokePoint::new(170.0, 142.0, 1.0)])) + { + assert!(near([a[0].x, a[0].y], [b[0].x, b[0].y], TOL), "{mode:?} mirrors the same way"); + } + } + let dual = PresetSymmetry::for_canvas(SymmetryMode::Dual, [400.0, 300.0], 0.0, [800, 600]).unwrap(); + let axes = dual.directions(); + assert_eq!(axes.len(), 2, "dual keeps two axes"); + assert!((axes[0][0] * axes[1][0] + axes[0][1] * axes[1][1]).abs() < TOL, "the dual axes stay perpendicular"); +} + +#[test] +fn a_canvas_without_area_has_no_diagonal_angle() { + assert!(diagonal_start_angle([0, 600]).is_none(), "a zero width has no diagonal"); + assert!(diagonal_start_angle([800, 0]).is_none(), "a zero height has no diagonal"); + assert!(PresetSymmetry::for_canvas(SymmetryMode::Diagonal, [400.0, 300.0], 0.0, [0, 600]).is_none(), "and so no diagonal preset"); + assert!(PresetSymmetry::for_canvas(SymmetryMode::Vertical, [400.0, 300.0], 0.0, [0, 600]).is_some(), "the other modes need no angle"); +} From 77843cc40168cd30622580b58a69566ebfd61ee7 Mon Sep 17 00:00:00 2001 From: hnoerenberg Date: Wed, 2 Dec 2026 12:07:33 +0100 Subject: [PATCH 2/2] fix(engine): keep a named symmetry turn out of the canvas fold paint.setSymmetry now starts an unturned Diagonal on the canvas diagonal. A caller that names a turn keeps the turn it was given: Transform Symmetry reads the stored turn back and sends it in again, so folding the canvas angle into that value would move the axis on every commit. --- crates/engine-core/src/cmd/symmetry_cmds.rs | 9 ++++++++- .../src/cmd/symmetry_cmds/preset_tests.rs | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/crates/engine-core/src/cmd/symmetry_cmds.rs b/crates/engine-core/src/cmd/symmetry_cmds.rs index 30261c566c6..fac4c02b21d 100644 --- a/crates/engine-core/src/cmd/symmetry_cmds.rs +++ b/crates/engine-core/src/cmd/symmetry_cmds.rs @@ -109,11 +109,18 @@ fn set_preset(s: &mut Session, p: &Value) -> Result { [values.first().and_then(Value::as_f64).ok_or_else(fail)?, values.get(1).and_then(Value::as_f64).ok_or_else(fail)?] } }; + let rotation_given = p.get("rotation").is_some(); let rotation = match p.get("rotation") { None => 0.0, Some(v) => v.as_f64().ok_or_else(fail)?, }; - let preset = PresetSymmetry::for_canvas(mode, center, rotation, [st.doc.size.width, st.doc.size.height]).ok_or_else(fail)?; + // Only an unturned preset starts on the canvas diagonal: that is the start the issue measured. + // A caller that names a turn — Transform Symmetry sends its preview back through here — keeps + // the turn it was given, so the canvas angle is never added a second time. + let preset = match rotation_given { + true => PresetSymmetry::new(mode, center, rotation).ok_or_else(fail)?, + false => PresetSymmetry::for_canvas(mode, center, 0.0, [st.doc.size.width, st.doc.size.height]).ok_or_else(fail)?, + }; let symmetry = PaintingSymmetry::Preset(preset); let result = symmetry.inspect(); s.active_mut().ok_or(EngineError::NoDocument)?.symmetry = Some(symmetry); diff --git a/crates/engine-core/src/cmd/symmetry_cmds/preset_tests.rs b/crates/engine-core/src/cmd/symmetry_cmds/preset_tests.rs index ad5291c1dc7..917d50399ea 100644 --- a/crates/engine-core/src/cmd/symmetry_cmds/preset_tests.rs +++ b/crates/engine-core/src/cmd/symmetry_cmds/preset_tests.rs @@ -60,3 +60,17 @@ fn presets_validate_atomically_and_leave_document_paths_and_history_untouched() assert!(!empty.is_enabled("paint.setSymmetry")); assert!(empty.execute("paint.setSymmetry", json!({"mode":"vertical"})).is_err()); } + +#[test] +fn a_turned_diagonal_keeps_the_turn_it_was_given() { + // Transform Symmetry reads the stored turn back out and sends it in again; re-folding the + // canvas angle on that round trip would drift the axis every time the panel commits. + let mut s = session(8, "rgb"); + let turn = |s: &Session| s.active().unwrap().symmetry.as_ref().unwrap().preset().unwrap().rotation; + s.execute("paint.setSymmetry", json!({"mode":"diagonal"})).unwrap(); + let started = turn(&s); + s.execute("paint.setSymmetry", json!({"mode":"diagonal","rotation":started})).unwrap(); + assert!((turn(&s) - started).abs() < 1.0e-9, "sending the stored turn back keeps the axis put: {started} -> {}", turn(&s)); + s.execute("paint.setSymmetry", json!({"mode":"diagonal","rotation":30.0})).unwrap(); + assert!((turn(&s) - 30.0).abs() < 1.0e-9, "a named turn means what it meant before: {}", turn(&s)); +}