diff --git a/crates/nodes/src/patterns.rs b/crates/nodes/src/patterns.rs index e62c319..78506e7 100644 --- a/crates/nodes/src/patterns.rs +++ b/crates/nodes/src/patterns.rs @@ -321,9 +321,13 @@ fn scratches(ctx: &Ctx) -> CookResult { for (k, (p, d, _)) in segs.iter().enumerate() { let (x0, x1) = (p[0].min(p[0] + d[0]) - width, p[0].max(p[0] + d[0]) + width); let (y0, y1) = (p[1].min(p[1] + d[1]) - width, p[1].max(p[1] + d[1]) + width); - for gy in (y0 * G as f32).floor() as i32..=(y1 * G as f32).floor() as i32 { - for gx in (x0 * G as f32).floor() as i32..=(x1 * G as f32).floor() as i32 { - if let Some(b) = bins.get_mut(gy.rem_euclid(G as i32) as usize * G + gx.rem_euclid(G as i32) as usize) { + // At most one full turn of bins per axis, so no segment lands twice in a bin. + let (gy0, gx0) = ((y0 * G as f32).floor() as i64, (x0 * G as f32).floor() as i64); + let gy1 = ((y1 * G as f32).floor() as i64).min(gy0.saturating_add(G as i64 - 1)); + let gx1 = ((x1 * G as f32).floor() as i64).min(gx0.saturating_add(G as i64 - 1)); + for gy in gy0..=gy1 { + for gx in gx0..=gx1 { + if let Some(b) = bins.get_mut(gy.rem_euclid(G as i64) as usize * G + gx.rem_euclid(G as i64) as usize) { b.push(k); } } @@ -358,7 +362,7 @@ fn shape_node(ctx: &Ctx) -> CookResult { } fn splatter(ctx: &Ctx) -> CookResult { - let count = ctx.i("count").clamp(1, 20000) as u32; + let count = ctx.i("count").clamp(1, 2000) as u32; let (k, size, sr, lr, s) = (ctx.i("pattern"), ctx.f("size"), ctx.f("size_random"), ctx.f("lum_random"), ctx.seed); let items: Vec<([f32; 2], f32, f32, f32)> = (0..count) .map(|i| ([rand1(i * 5, s), rand1(i * 5 + 1, s)], size * (1.0 - sr * rand1(i * 5 + 2, s)), 1.0 - lr * rand1(i * 5 + 3, s), rand1(i * 5 + 4, s))) @@ -379,11 +383,13 @@ fn stamp( let mut bins: Vec> = vec![Vec::new(); G * G]; for (i, (c, r, _, _)) in items.iter().enumerate() { let r = r * 1.42; - for gy in ((c[1] - r) * G as f32).floor() as i32..=((c[1] + r) * G as f32).floor() as i32 { - for gx in ((c[0] - r) * G as f32).floor() as i32..=((c[0] + r) * G as f32).floor() as i32 { - if let Some(b) = bins.get_mut(gy.rem_euclid(G as i32) as usize * G + gx.rem_euclid(G as i32) as usize) - && !b.contains(&i) - { + // At most one full turn of bins per axis, so no item lands twice in a bin. + let (gy0, gx0) = (((c[1] - r) * G as f32).floor() as i64, ((c[0] - r) * G as f32).floor() as i64); + let gy1 = (((c[1] + r) * G as f32).floor() as i64).min(gy0.saturating_add(G as i64 - 1)); + let gx1 = (((c[0] + r) * G as f32).floor() as i64).min(gx0.saturating_add(G as i64 - 1)); + for gy in gy0..=gy1 { + for gx in gx0..=gx1 { + if let Some(b) = bins.get_mut(gy.rem_euclid(G as i64) as usize * G + gx.rem_euclid(G as i64) as usize) { b.push(i); } } @@ -546,3 +552,7 @@ fn waveform(ctx: &Ctx) -> CookResult { ((x + w) * TAU * n).sin() * 0.5 + 0.5 })]) } + +#[cfg(test)] +#[path = "patterns.test.rs"] +mod tests; diff --git a/crates/nodes/src/patterns.test.rs b/crates/nodes/src/patterns.test.rs new file mode 100644 index 0000000..33290c1 --- /dev/null +++ b/crates/nodes/src/patterns.test.rs @@ -0,0 +1,36 @@ +//! Hostile-parameter tests for the binned pattern generators in `patterns.rs`. + +use std::collections::BTreeMap; + +use materialcraft_core::Value; +use materialcraft_image::Image; + +use crate::{Ctx, find, resolve_params}; + +fn cook(id: &str, over: &[(&str, Value)]) -> Image { + let def = find(id).unwrap(); + let over: BTreeMap = over.iter().map(|(k, v)| (k.to_string(), v.clone())).collect(); + let params = resolve_params(def, &over); + let no_res = |_: &str| None; + let vars = BTreeMap::new(); + let inputs = vec![None; def.inputs.len()]; + let ctx = Ctx { w: 32, h: 32, seed: 3, tile: true, inputs: &inputs, params: ¶ms, resource: &no_res, vars: &vars }; + (def.cook)(&ctx).unwrap().remove(0) +} + +fn assert_quick(id: &str, over: &[(&str, Value)]) { + let t = std::time::Instant::now(); + let img = cook(id, over); + assert!(img.data.iter().all(|v| v.is_finite()), "{id} produced non-finite values"); + assert!(t.elapsed().as_secs() < 20, "{id} took {:?}", t.elapsed()); +} + +#[test] +fn splatter_huge_size_is_bounded() { + assert_quick("splatter", &[("size", Value::Float(1e6)), ("count", Value::Int(2000))]); +} + +#[test] +fn scratches_huge_length_is_bounded() { + assert_quick("scratches", &[("length", Value::Float(1e6)), ("width", Value::Float(1e6)), ("count", Value::Int(5000))]); +}