diff --git a/crates/effects/src/distort2.rs b/crates/effects/src/distort2.rs index a01d09595..b2b4e1683 100644 --- a/crates/effects/src/distort2.rs +++ b/crates/effects/src/distort2.rs @@ -596,18 +596,19 @@ fn bend_it(ctx: &EffectCtx, mut b: Buf) -> Buf { map_xy(&mut b.img, |x, y, _| { let (vx, vy) = (x as f64 + 0.5 - s.0, y as f64 + 0.5 - s.1); let (lx, ly) = (vx * a.0 + vy * a.1, vx * n.0 + vy * n.1); + // The rigid end covers the arc where bends beyond half a turn overlap. let mut cands: [Option<(f64, f64)>; 3] = [None; 3]; // Arc. let (dx, dy) = (lx, ly - rho); let th = dx.atan2(-dy); if (0.0..=beta).contains(&th) { - cands[0] = Some((rho * th, rho - (dx * dx + dy * dy).sqrt())); + cands[1] = Some((rho * th, rho - (dx * dx + dy * dy).sqrt())); } // Rigid continuation beyond the end. let (ex, ey) = (lx - pe.0, ly - pe.1); let u2 = ex * t.0 + ey * t.1; if u2 >= 0.0 { - cands[1] = Some((l + u2, ex * nn.0 + ey * nn.1)); + cands[0] = Some((l + u2, ex * nn.0 + ey * nn.1)); } if lx < 0.0 && static_pre { cands[2] = Some((lx, ly)); @@ -997,6 +998,54 @@ mod tests { assert!(max_diff(&run("ec.distort.opticscompensation", &[("fieldOfView", num(90.0))], img.clone()).img, &img) > 1e-3); } + #[test] + fn bend_it_keeps_the_rigid_end_intact_when_it_overlaps_the_arc() { + let mut img = test_img(128, 128); + for y in 0..128 { + for x in 0..128 { + let a = if (x / 8 + y / 8) % 2 == 0 { 1.0 } else { 0.5 }; + let p = img.get(x, y); + img.set(x as u32, y as u32, [p[0] * a, p[1] * a, p[2] * a, a]); + } + } + for bend in [120.0_f64, 180.0, 181.0, 225.0, 270.0, 359.0, 360.0, 450.0, -120.0, -180.0, -181.0, -225.0, -270.0, -359.0, -360.0, -450.0] { + let beta = bend.abs().to_radians(); + let (sin, cos) = beta.sin_cos(); + let sign = bend.signum(); + let radius = 32.0 / beta; + for prestart in 0..2 { + let out = run( + "ec.distort.ccbendit", + &[("bend", num(bend)), ("start", pt(64.0, 48.0)), ("end", pt(64.0, 80.0)), ("renderPrestart", Value::Enum(prestart))], + img.clone(), + ); + let mut checked = 0; + for y in 0..128 { + for x in 0..128 { + // Undo only the rigid end's rotation and translation. A later + // turn may overlap the arc, but must not acquire its distortion. + let ex = y as f64 + 0.5 - 48.0 - radius * sin; + let ey = sign * (64.0 - x as f64 - 0.5) - radius * (1.0 - cos); + let u = 32.0 + ex * cos + ey * sin; + let w = -ex * sin + ey * cos; + let sx = 64.0 - sign * w; + let sy = 48.0 + u; + if u <= 34.0 || !(2.0..126.0).contains(&sx) || sy >= 126.0 { + continue; + } + let expected = img.sample_bilinear(sx, sy); + let actual = out.img.get(x, y); + for i in 0..4 { + assert!((actual[i] - expected[i]).abs() < 0.0001, "bend {bend}, prestart {prestart}, ({x}, {y}): {actual:?} != rigid {expected:?}"); + } + checked += 1; + } + } + assert!(checked > 50, "bend {bend}: the rigid segment is visible"); + } + } + } + #[test] fn bend_it_zero_is_identity() { let img = test_img(32, 24); diff --git a/crates/gpu/src/shaders/fx_warp.wgsl b/crates/gpu/src/shaders/fx_warp.wgsl index 9ab44d14d..f017d388d 100644 --- a/crates/gpu/src/shaders/fx_warp.wgsl +++ b/crates/gpu/src/shaders/fx_warp.wgsl @@ -186,11 +186,12 @@ fn fxw_bendit(@builtin(global_invocation_id) gid: vec3) { let th = atan2(lx, -dy); let dd = lx * lx + ly * ly - 2.0 * ly * rho; let w_arc = -dd / (rho + sqrt(max(rho * rho + dd, 0.0))); + // The rigid end covers the arc where bends beyond half a turn overlap. var cand: array, 3>; - cand[0] = vec3(rho * th, w_arc, select(0.0, 1.0, th >= 0.0 && th <= beta)); + cand[1] = vec3(rho * th, w_arc, select(0.0, 1.0, th >= 0.0 && th <= beta)); let e = vec2(lx, ly) - pe; let u2 = dot(e, t); - cand[1] = vec3(l + u2, dot(e, nn), select(0.0, 1.0, u2 >= 0.0)); + cand[0] = vec3(l + u2, dot(e, nn), select(0.0, 1.0, u2 >= 0.0)); cand[2] = vec3(lx, ly, select(0.0, 1.0, lx < 0.0 && P.u[0].y != 0u)); for (var i = 0; i < 3; i++) { let c = cand[i]; diff --git a/crates/gpu/src/tests/fx_warp.rs b/crates/gpu/src/tests/fx_warp.rs index c85f900a1..708e5b106 100644 --- a/crates/gpu/src/tests/fx_warp.rs +++ b/crates/gpu/src/tests/fx_warp.rs @@ -243,3 +243,53 @@ fn smear_and_reshape() { composite_with_masks("ec.distort.reshape", vals); } } + +#[test] +fn bend_it_keeps_the_overlapping_rigid_end_intact() { + let Some(g) = gpu() else { return }; + let spec = effectcraft_effects::find("ec.distort.ccbendit").unwrap(); + let mut img = effectcraft_raster::Image::new(128, 128); + // Include translucent checks so a wrong branch cannot hide behind matching alpha. + for y in 0..128 { + for x in 0..128 { + let a = if (x / 8 + y / 8) % 2 == 0 { 1.0 } else { 0.5 }; + img.set(x, y, [x as f32 / 128.0 * a, y as f32 / 128.0 * a, 0.25 * a, a]); + } + } + let buf = effectcraft_effects::Buf { img, offset: [0.0, 0.0], scale: 1.0 }; + for bend in [120.0_f64, 225.0, 270.0, 359.0, 450.0, -120.0, -225.0, -270.0, -359.0, -450.0] { + let beta = bend.abs().to_radians(); + let (sin, cos) = beta.sin_cos(); + let radius = 32.0 / beta; + let mut params = effectcraft_effects::Params { values: spec.params.iter().map(|p| (p.id.to_owned(), p.default.clone())).collect() }; + for (key, value) in [("bend", n(bend)), ("start", pt(64.0, 48.0)), ("end", pt(64.0, 80.0))] { + params.values.insert(key.into(), value); + } + let ctx = + || effectcraft_effects::EffectCtx { params: ¶ms, time: 0.0, layer_size: [128.0, 128.0], seed: 0, adjustment: false, env: Default::default() }; + let cpu = (spec.render)(&ctx(), buf.clone()); + let out = effectcraft_render::Accelerator::effects(g, &[effectcraft_render::FxStep { spec, ctx: ctx() }], &buf, None).expect("GPU Two-Point Bend"); + let mut checked = 0; + for y in 0..128 { + for x in 0..128 { + let ex = y as f64 + 0.5 - 48.0 - radius * sin; + let ey = bend.signum() * (64.0 - x as f64 - 0.5) - radius * (1.0 - cos); + let u = 32.0 + ex * cos + ey * sin; + let w = -ex * sin + ey * cos; + let sx = 64.0 - bend.signum() * w; + let sy = 48.0 + u; + if u <= 34.0 || !(2.0..126.0).contains(&sx) || sy >= 126.0 { + continue; + } + let expected = buf.img.sample_bilinear(sx, sy); + let actual = out.img.get(x, y); + for i in 0..4 { + assert!((actual[i] - expected[i]).abs() < 0.001, "bend {bend}, ({x}, {y}): {actual:?} != rigid {expected:?}"); + } + checked += 1; + } + } + assert!(checked > 50, "bend {bend}: the rigid segment is visible"); + check("Two-Point Bend overlapping end", Some(diff(&cpu.img, &out.img, 1.0 / 255.0)), 0.001); + } +}