mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Batch fix-up: Symmetry Fill dilations are the same for any selection on the canvas (CodeRabbit on #18551)
The range of dilation steps came from the painted rect, so a small selection dropped enlarged copies that still cover it (a translucent tile's centre pixel changed), and type 6 numbered its copies from the largest step, so the same copy drifted to another colour when the range changed. The range now comes from the canvas (with the area), and dilated copies take their own step as the number for the colour drift; the drawing order stays as it was. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
f84e580ebb
commit
289fe5cc14
3 files changed
+68
-17
No files matched your search
@@ -508,6 +508,7 @@ pub fn render(script: &str, value: &dyn Fn(&str) -> f64, seed: u64, tile: &Tile,
|
||||
scale: value("patternScale"),
|
||||
shift: (value("translationAlongWidth"), value("translationAlongHeight")),
|
||||
centre,
|
||||
bounds: canvas,
|
||||
jitter,
|
||||
};
|
||||
Some(fill.render(tile, width, height, rect))
|
||||
|
||||
@@ -74,6 +74,8 @@ pub struct SymmetryFill {
|
||||
pub shift: (f64, f64),
|
||||
/// The point the symmetry turns about, in document pixels.
|
||||
pub centre: (f64, f64),
|
||||
/// The region the layout is made for (the canvas): the copies are the same for every painted area within it.
|
||||
pub bounds: Rect,
|
||||
pub jitter: Jitter,
|
||||
}
|
||||
|
||||
@@ -129,12 +131,17 @@ impl Map {
|
||||
|
||||
/// Where the copies go.
|
||||
enum Layout {
|
||||
/// A fixed list of copies, the last on top.
|
||||
Copies(Vec<Map>),
|
||||
/// A fixed list of copies, the last on top, each with its own number for the per-copy randomness.
|
||||
Copies(Vec<(i64, Map)>),
|
||||
/// The copies `reps` repeated over the lattice of `e1` and `e2` (a band along `e1` without `e2`).
|
||||
Lattice { e1: (f64, f64), e2: Option<(f64, f64)>, reps: Vec<Map> },
|
||||
}
|
||||
|
||||
/// The copies `maps` numbered in order.
|
||||
fn numbered(maps: Vec<Map>) -> Layout {
|
||||
Layout::Copies((0..).zip(maps).collect())
|
||||
}
|
||||
|
||||
fn turns(n: u32) -> Vec<Map> {
|
||||
(0..n).map(|i| Map::turn(f64::from(i) * 360.0 / f64::from(n))).collect()
|
||||
}
|
||||
@@ -143,11 +150,12 @@ fn mirrors(degrees: &[f64]) -> Vec<Map> {
|
||||
degrees.iter().map(|d| Map::mirror(*d)).collect()
|
||||
}
|
||||
|
||||
/// Copies scaled by `k` and turned by `deg` per step about the centre, for the steps `steps`, the largest first.
|
||||
/// Copies scaled by `k` and turned by `deg` per step about the centre, for the steps `steps`, the largest first; each
|
||||
/// copy is numbered by its step.
|
||||
fn dilations(k: f64, deg: f64, steps: std::ops::RangeInclusive<i64>) -> Layout {
|
||||
let (lo, hi) = (*steps.start(), *steps.end());
|
||||
let lo = lo.max(hi - MAX_DILATION_STEPS + 1);
|
||||
Layout::Copies((lo..=hi).rev().map(|n| Map::turn(n as f64 * deg).scaled(k.powf(n as f64))).collect())
|
||||
Layout::Copies((lo..=hi).rev().map(|n| (n, Map::turn(n as f64 * deg).scaled(k.powf(n as f64)))).collect())
|
||||
}
|
||||
|
||||
/// The layout of `kind` for lattice size `a`; `steps(k)` is the range of dilation steps that can show for ratio `k`.
|
||||
@@ -159,9 +167,9 @@ fn layout(kind: usize, a: f64, steps: impl Fn(f64) -> std::ops::RangeInclusive<i
|
||||
let grid = |e2: (f64, f64), reps: Vec<Map>| Layout::Lattice { e1: (a, 0.0), e2: Some(e2), reps };
|
||||
let (oblique, square, centred, hex) = ((0.25 * a, a), (0.0, a), (half, 0.75 * a), (half, a * 3f64.sqrt() / 2.0));
|
||||
match kind {
|
||||
1 => Layout::Copies(vec![id, mx]),
|
||||
2 => Layout::Copies(vec![id, r180]),
|
||||
3 => Layout::Copies(turns(4)),
|
||||
1 => numbered(vec![id, mx]),
|
||||
2 => numbered(vec![id, r180]),
|
||||
3 => numbered(turns(4)),
|
||||
4 => Layout::Lattice { e1: (0.0, a), e2: None, reps: vec![id] },
|
||||
5 => Layout::Lattice { e1: (0.0, a), e2: None, reps: vec![id, mx.shifted(0.0, half)] },
|
||||
6 => dilations(1.5, 0.0, steps(1.5)),
|
||||
@@ -195,11 +203,12 @@ fn layout(kind: usize, a: f64, steps: impl Fn(f64) -> std::ops::RangeInclusive<i
|
||||
}
|
||||
}
|
||||
|
||||
/// One copy that may cover a pixel: its place in the drawing order (higher on top), the map from the pixel back to the
|
||||
/// motif, and how much it enlarges the motif.
|
||||
/// One copy that may cover a pixel: its place in the drawing order (higher on top), its number for the per-copy
|
||||
/// randomness, the map from the pixel back to the motif, and how much it enlarges the motif.
|
||||
#[derive(Clone, Copy)]
|
||||
struct Candidate {
|
||||
order: (i64, i64, usize),
|
||||
id: (i64, i64),
|
||||
back: Map,
|
||||
stretch: f64,
|
||||
}
|
||||
@@ -230,8 +239,10 @@ impl SymmetryFill {
|
||||
let motif = (corner.0 + hw, corner.1 + hh);
|
||||
let radius = hw.hypot(hh);
|
||||
let a = (1.5 * shift.0.hypot(shift.1)).max(2.0 * radius).max(1.0);
|
||||
// How far the area reaches from the centre: the dilations stop once a copy is four times that size.
|
||||
let reach = [(rect.x0, rect.y0), (rect.x1, rect.y0), (rect.x0, rect.y1), (rect.x1, rect.y1)]
|
||||
// How far the region the layout is made for (and the area) reaches from the centre: the dilations stop once a
|
||||
// copy is four times that size, whatever part of it is painted.
|
||||
let region = rect.union(&self.bounds);
|
||||
let reach = [(region.x0, region.y0), (region.x1, region.y0), (region.x0, region.y1), (region.x1, region.y1)]
|
||||
.iter()
|
||||
.map(|&(x, y)| (f64::from(x) - centre.0).hypot(f64::from(y) - centre.1))
|
||||
.fold(0.0, f64::max);
|
||||
@@ -244,8 +255,8 @@ impl SymmetryFill {
|
||||
};
|
||||
let layout = layout(self.kind(), a, steps);
|
||||
// The fixed copies with their inverse, centre and reach; the lattice representatives with their inverse.
|
||||
let fixed: Vec<(Map, (f64, f64), f64)> = match &layout {
|
||||
Layout::Copies(maps) => maps.iter().filter_map(|m| Some((m.inverse()?, m.apply(motif), radius * m.stretch()))).collect(),
|
||||
let fixed: Vec<(i64, Map, (f64, f64), f64)> = match &layout {
|
||||
Layout::Copies(maps) => maps.iter().filter_map(|(n, m)| Some((*n, m.inverse()?, m.apply(motif), radius * m.stretch()))).collect(),
|
||||
Layout::Lattice { .. } => Vec::new(),
|
||||
};
|
||||
let reps: Vec<(Map, (f64, f64))> = match &layout {
|
||||
@@ -260,9 +271,9 @@ impl SymmetryFill {
|
||||
found.clear();
|
||||
match &layout {
|
||||
Layout::Copies(_) => {
|
||||
for (k, (back, c, r)) in fixed.iter().enumerate() {
|
||||
for (k, (n, back, c, r)) in fixed.iter().enumerate() {
|
||||
if (p.0 - c.0).powi(2) + (p.1 - c.1).powi(2) <= r * r {
|
||||
found.push(Candidate { order: (0, 0, k), back: *back, stretch: 1.0 / back.stretch() });
|
||||
found.push(Candidate { order: (0, 0, k), id: (0, *n), back: *back, stretch: 1.0 / back.stretch() });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -272,7 +283,8 @@ impl SymmetryFill {
|
||||
if found.len() < MAX_LAYERS {
|
||||
// Back from the shifted copy: first undo the lattice step, then the representative.
|
||||
let back = Map { t: back.apply((-off.0, -off.1)), ..*back };
|
||||
found.push(Candidate { order: (row, col, k), back, stretch: 1.0 });
|
||||
let id = (row, col.wrapping_mul(64).wrapping_add(k as i64));
|
||||
found.push(Candidate { order: (row, col, k), id, back, stretch: 1.0 });
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -317,7 +329,7 @@ impl SymmetryFill {
|
||||
continue;
|
||||
}
|
||||
let mut q = [q[0] / q[3], q[1] / q[3], q[2] / q[3], q[3] / (taps * taps) as f32];
|
||||
let d = self.jitter.offsets(c.order.0, c.order.1.wrapping_mul(64).wrapping_add(c.order.2 as i64));
|
||||
let d = self.jitter.offsets(c.id.0, c.id.1);
|
||||
// Without a drift a copy keeps its exact colour, also a float one past 1.
|
||||
for ch in 0..3 {
|
||||
if d[ch] != 0.0 {
|
||||
|
||||
@@ -206,3 +206,41 @@ fn colour_randomness_varies_the_copies_only_above_zero() {
|
||||
s.execute("edit.fill", symmetry(json!({"symmetryType": 1, "colorRandomness": 1, "seed": 3}))).unwrap();
|
||||
assert_ne!(rgb8(pixel(&s, 315, 210)), rgb8(pixel(&s, 285, 210)), "each copy drifts on its own");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_small_selection_keeps_the_dilations_that_cover_it() {
|
||||
// A faint 10 % tile on the centre: every enlarged copy covers the centre and shows through the ones above it, so the
|
||||
// centre pixel is the same whether the whole canvas or only a small square around it is filled.
|
||||
let mut s = crate::session();
|
||||
s.execute("file.new", json!({"width": 40, "height": 30, "background": "transparent"})).unwrap();
|
||||
s.execute("edit.fill", json!({"contents": "color", "color": "#ff0000", "opacity": 10})).unwrap();
|
||||
s.execute("edit.definePattern", json!({"name": "probe", "rect": [0, 0, 40, 30]})).unwrap();
|
||||
s.execute("file.new", json!({"width": 600, "height": 400, "background": "white"})).unwrap();
|
||||
s.execute("layer.new.layer", json!({})).unwrap();
|
||||
let p = symmetry(json!({"symmetryType": 6, "translationAlongWidth": 0, "translationAlongHeight": 0}));
|
||||
s.execute("edit.fill", p.clone()).unwrap();
|
||||
let whole = pixel(&s, 300, 200);
|
||||
s.execute("edit.undo", json!({})).unwrap();
|
||||
s.execute("select.rect", json!({"x": 290, "y": 190, "width": 20, "height": 20})).unwrap();
|
||||
s.execute("edit.fill", p).unwrap();
|
||||
let small = pixel(&s, 300, 200);
|
||||
assert!(whole[3] > 0.1, "several copies over the centre: {whole:?}");
|
||||
assert!((small[3] - whole[3]).abs() < 0.01, "{small:?} as {whole:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_dilated_copy_keeps_its_colour_drift_on_a_larger_canvas() {
|
||||
// The copies of 6: dilatation symmetry drift by their own step, not by how many larger copies the canvas needs: the
|
||||
// same fill about the same centre on a larger canvas gives the copies near the centre the same colours.
|
||||
let p = symmetry(json!({"symmetryType": 6, "colorRandomness": 1, "seed": 5, "translationAlongWidth": 0, "translationAlongHeight": 0}));
|
||||
let mut s = session();
|
||||
s.execute("edit.fill", p.clone()).unwrap();
|
||||
let small: Vec<[u8; 3]> = (290..310).map(|x| rgb8(pixel(&s, x, 205))).collect();
|
||||
s.execute("file.new", json!({"width": 1000, "height": 800, "background": "white"})).unwrap();
|
||||
s.execute("layer.new.layer", json!({})).unwrap();
|
||||
s.execute("select.rect", json!({"x": 200, "y": 200, "width": 600, "height": 400})).unwrap();
|
||||
s.execute("edit.fill", p).unwrap();
|
||||
let large: Vec<[u8; 3]> = (490..510).map(|x| rgb8(pixel(&s, x, 405))).collect();
|
||||
assert!(small.iter().any(|c| *c != [0, 0, 0]), "{small:?}");
|
||||
assert_eq!(large, small);
|
||||
}
|
||||
Reference in new issue
Block a user