diff --git a/crates/compose/src/scripted_pattern.rs b/crates/compose/src/scripted_pattern.rs index af0ed9d00d..c9e652e863 100644 --- a/crates/compose/src/scripted_pattern.rs +++ b/crates/compose/src/scripted_pattern.rs @@ -434,8 +434,31 @@ pub struct Spiral { /// Most tile positions worked out once per fill; a layout with more works each one out where a pixel needs it. const MAX_TABLE: usize = 1 << 20; -/// Most tiles tried for one pixel: bounds the work of layouts packed far tighter than a tile. -const MAX_CANDIDATES: usize = 4096; +/// Most tiles tried for one pixel, covering it or not: bounds the work of layouts packed far tighter than a tile. Each +/// turn offers only the tiles within reach of the pixel, so a covering tile is found long before this in any layout +/// the controls allow for patterns up to a few thousand pixels. +const MAX_TRIES: usize = 1 << 16; + +/// The angle either side of the pixel's direction within which the curve at a radius in `near..=far` can lie within +/// `reach` of a pixel at radius `r`; `None` when no point of that band is in reach. By the law of cosines a point at +/// radius ρ and angle δ from the pixel is in reach when cos δ ≥ (r² + ρ² − reach²) / (2 r ρ); this takes the least +/// bound over the band (at its ends or at ρ = √(r² − reach²), where it is smallest). +fn turn_window(r: f64, reach: f64, near: f64, far: f64) -> Option { + let near = near.max(1e-9); + if r <= 0.0 || far < near { + return Some(PI); + } + let bound = |rho: f64| (r * r + rho * rho - reach * reach) / (2.0 * r * rho); + let low = (r * r - reach * reach).max(0.0).sqrt().clamp(near, far); + let least = bound(near).min(bound(far)).min(bound(low)); + if least.is_nan() || least <= -1.0 { + Some(PI) + } else if least > 1.0 { + None + } else { + Some(least.acos()) + } +} /// The spiral r = `b` θ, θ from 0 at the centre. #[derive(Clone, Copy)] @@ -508,7 +531,36 @@ impl Spiral { let (hx, hy) = (-tw / 2.0, -th / 2.0); Placement::anchored((x + hx * c + hy * s, y - hx * s + hy * c), true, (0.0, 0.0), scale as f32, angle as f32) }; - let mut spans: Vec<(i64, i64)> = Vec::new(); + // A tile whose centre lies within half its shorter side of a pixel covers it, however it is turned: the topmost + // such tile, searched like the others but with that smaller reach, which finds it after a few tries. + let inner = tw.min(th) / 2.0; + let sure_cover = |px: f64, py: f64, r: f64, phi: f64| -> Option { + let (lo, hi) = (((r - inner) / curve.b).max(0.0), (r + inner) / curve.b); + let first = ((lo - PI - phi) / TAU).ceil() as i64; + let turns = ((hi + PI - phi) / TAU).floor() as i64; + let mut tried = 0; + for k in (first..=turns).rev() { + let t = phi + TAU * k as f64; + let Some(half) = turn_window(r, inner, curve.b * (t - PI), curve.b * (t + PI)) else { continue }; + let (a, b) = ((t - half).max(lo), (t + half).min(hi)); + if a > b { + continue; + } + let (n0, n1) = ((curve.arc(a) / pitch).floor().max(0.0) as i64, (curve.arc(b) / pitch).ceil() as i64); + for n in (n0..=n1).rev() { + tried += 1; + if tried > MAX_TRIES { + return None; + } + let t = angle_of(n); + let (sin, cos) = t.sin_cos(); + if (cx + curve.b * t * cos - px).hypot(cy - curve.b * t * sin - py) <= inner { + return Some(n); + } + } + } + None + }; let mut i = 0; for y in rect.y0..rect.y1 { let py = f64::from(y) + 0.5; @@ -517,38 +569,27 @@ impl Spiral { let (dx, dy) = (px - cx, cy - py); let r = dx.hypot(dy); let phi = dy.atan2(dx).rem_euclid(TAU); - // The curve within reach lies between these angles, and on each turn only within `half` of the pixel's - // own direction: a point at angle δ from it is at least 2 √(r ρ) sin(δ/2) away. + // The curve within reach lies between these angles. Each turn, the outermost (topmost) first, offers the + // tiles within reach of the pixel's own direction; a turn's tiles all lie after the next turn in. let (lo, hi) = (((r - reach) / curve.b).max(0.0), (r + reach) / curve.b); - let q = reach / (2.0 * (r * (r - reach).max(0.0)).sqrt()); - let half = if q.is_finite() && q < 1.0 { 2.0 * q.asin() } else { PI }; - // The tiles near the pixel on each turn, the outermost (topmost) turn first, until enough to try. - spans.clear(); - let first = ((lo - half - phi) / TAU).ceil() as i64; - let turns = ((hi + half - phi) / TAU).floor() as i64; - let mut count = 0; - for k in (first..=turns).rev() { + let first = ((lo - PI - phi) / TAU).ceil() as i64; + let turns = ((hi + PI - phi) / TAU).floor() as i64; + let mut acc = [0.0f32; 4]; + let (mut layers, mut visits, mut tried) = (0, 0, 0); + let mut below = i64::MAX; + 'tiles: for k in (first..=turns).rev() { let t = phi + TAU * k as f64; + let Some(half) = turn_window(r, reach, curve.b * (t - PI), curve.b * (t + PI)) else { continue }; let (a, b) = ((t - half).max(lo), (t + half).min(hi)); if a > b { continue; } - let (n0, n1) = ((curve.arc(a) / pitch).floor().max(0.0) as i64, (curve.arc(b) / pitch).ceil() as i64); - match spans.last_mut() { - Some(last) if n1 + 1 >= last.0 => last.0 = last.0.min(n0), - _ => spans.push((n0, n1)), - } - count += n1 - n0 + 1; - if count > MAX_CANDIDATES as i64 { - break; - } - } - let mut acc = [0.0f32; 4]; - let (mut layers, mut visits, mut tried) = (0, 0, 0); - 'tiles: for &(n0, n1) in &spans { + let n0 = (curve.arc(a) / pitch).floor().max(0.0) as i64; + let n1 = ((curve.arc(b) / pitch).ceil() as i64).min(below.saturating_sub(1)); + below = below.min(n0); for n in (n0..=n1).rev() { tried += 1; - if layers >= MAX_LAYERS || visits >= MAX_VISITS || tried > MAX_CANDIDATES || acc[3] >= 1.0 { + if layers >= MAX_LAYERS || visits >= MAX_VISITS || tried > MAX_TRIES || acc[3] >= 1.0 { break 'tiles; } let place = placement(n); @@ -565,6 +606,17 @@ impl Spiral { blend_behind(&mut acc, q, self.jitter.offsets(n, 0)); } } + // Past the try limit the tiles that show may not have been reached: the topmost sure cover stands in for them. + if tried > MAX_TRIES + && acc[3] < 1.0 + && let Some(n) = sure_cover(px, py, r, phi) + { + let place = placement(n); + let q = tile.sample_area(&place, px, py); + if q[3] > 0.0 { + blend_behind(&mut acc, q, self.jitter.offsets(n, 0)); + } + } out[i] = finish(acc); i += 1; } diff --git a/crates/engine-paint/src/fill_script_spiral_tests.rs b/crates/engine-paint/src/fill_script_spiral_tests.rs index 2a4168028c..f81892676e 100644 --- a/crates/engine-paint/src/fill_script_spiral_tests.rs +++ b/crates/engine-paint/src/fill_script_spiral_tests.rs @@ -242,3 +242,24 @@ fn spiral_runs_in_every_mode_and_depth() { assert_eq!(undo_label(&s).as_deref(), Some("Fill"), "{mode}/{depth}"); } } + +#[test] +fn a_tile_larger_than_the_canvas_packed_tightest_still_covers_every_pixel() { + // A 1024 x 1024 opaque tile at the most overlap (-1022 px on both spacings): rings and tiles 2 px apart, so most + // tiles near a pixel's turns do not reach it. The first tile alone covers the whole 600 x 400 canvas, so no pixel + // may stay transparent, also at the canvas edge far from the centre. + let mut s = crate::session(); + s.execute("file.new", json!({"width": 1024, "height": 1024, "background": "white"})).unwrap(); + s.execute("edit.fill", json!({"contents": "color", "color": "#ff0000"})).unwrap(); + s.execute("edit.definePattern", json!({"name": "big", "rect": [0, 0, 1024, 1024]})).unwrap(); + for upright in [true, false] { + s.execute("file.new", json!({"width": 600, "height": 400, "background": "white"})).unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + s.execute("select.rect", json!({"x": 596, "y": 196, "width": 4, "height": 8})).unwrap(); + let p = json!({"pattern": "big", "ringSpacing": -1022, "patternSpacing": -1022, "keepUpright": upright}); + s.execute("edit.fill", spiral(p)).unwrap(); + for (x, y) in [(599, 199), (596, 196), (599, 203)] { + assert!(pixel(&s, x, y)[3] > 0.99, "upright {upright}: ({x}, {y}) {:?}", pixel(&s, x, y)); + } + } +}