From 64db46f5abab516ed01ccd2efff009049274e450 Mon Sep 17 00:00:00 2001 From: kayrockscreenprinting Date: Thu, 8 Oct 2026 15:28:36 -0700 Subject: [PATCH] Puppet Warp: an expansion that empties the mesh is refused, not a panic (#951) (#1410) With at least one pin, a density or expansion that remeshed to no vertices panicked in PuppetSolver::new: `locate` falls back to vertex 0 when nothing is closer, and the union-find over an empty mesh indexed it ("index out of bounds: the len is 0 but the index is 0"). The UI reached it through the control channel and the options bar's Expansion field (its -50 minimum empties a 20 px tall bar), and edit.puppetWarp reached it through puppet_warp, where the dispatch guard caught the panic. - algo: PuppetSolver::new binds no pins when the mesh has no vertices, and puppet_warp returns the source when there are no triangles. With only the first change the command erased the layer, since rendering keeps only pixels inside the mesh. - ui-egui: a remesh that leaves no triangles is refused, as `begin` refuses a layer without opaque pixels. The previous density, expansion and mesh come back. The control channel returns the error; the options bar's fields snap back. Tests in algo, engine and ui-egui follow the issue's fixture and failed with that panic before the fix. Co-authored-by: Claude Opus 5.5 --- crates/algo/src/puppet.rs | 29 +++++++++++++- crates/engine/src/distort_cmds/tests.rs | 10 +++++ crates/ui-egui/src/puppet_ui.rs | 50 ++++++++++++++++++++++++- 3 files changed, 86 insertions(+), 3 deletions(-) diff --git a/crates/algo/src/puppet.rs b/crates/algo/src/puppet.rs index 275c0ac8de..abb9b56b85 100644 --- a/crates/algo/src/puppet.rs +++ b/crates/algo/src/puppet.rs @@ -470,7 +470,9 @@ impl PuppetSolver { [0.5 * cot(a, b, c), 0.5 * cot(b, c, a), 0.5 * cot(c, a, b)] }) .collect(); - let bound: Vec = pin_src.iter().map(|p| locate(&mesh, *p)).collect(); + // Without vertices there is nothing to bind a pin to (`locate` would fall back to a vertex 0 + // that doesn't exist, #951); every pin loop zips `bound`, so the pins are then ignored. + let bound: Vec = if nv == 0 { Vec::new() } else { pin_src.iter().map(|p| locate(&mesh, *p)).collect() }; // Connected components (union-find over triangle edges). let mut parent: Vec = (0..nv).collect(); fn find(p: &mut [usize], mut i: usize) -> usize { @@ -767,6 +769,11 @@ pub fn render(src: &Surface, bounds: Rect, mesh: &PuppetMesh, deformed: &[[f64; /// Applies a puppet warp to `src` (content inside `bounds`). pub fn puppet_warp(src: &Surface, bounds: Rect, w: &PuppetWarp, interp: Interp) -> Surface { let (solver, v, order) = deform(src, bounds, w, ITERATIONS); + // No triangles (an expansion that shrinks every pixel away): nothing to warp. Rendering the + // empty mesh would drop every pixel of the layer. + if solver.mesh.tris.is_empty() { + return src.clone(); + } render(src, bounds, &solver.mesh, &v, &order, interp) } @@ -832,6 +839,26 @@ mod tests { } } + #[test] + fn an_expansion_that_empties_the_mesh_warps_nothing() { + // #951: with pins and no vertices, `locate` fell back to vertex 0 and the solver indexed + // its empty union-find. + let solver = PuppetSolver::new(PuppetMesh::default(), &[[10.0, 10.0]], &[true]); + assert!(solver.mesh.verts.is_empty()); + let s = blob(SampleType::U8); + let b = s.content_bounds(); + let w = PuppetWarp { + pins: vec![PuppetPin { src: [28.0, 30.0], dst: [38.0, 25.0], rotate: None, depth: 0 }], + mode: PuppetMode::Normal, + density: PuppetDensity::Normal, + expansion: -200.0, + }; + assert!(build_mesh(&s, b, w.density, w.expansion).tris.is_empty()); + // Nothing to warp: the pixels stay, rather than being dropped outside an empty mesh. + let out = puppet_warp(&s, b, &w, Interp::Bilinear); + assert_eq!(out.read_region(b), s.read_region(b)); + } + #[test] fn a_single_pin_translates_rigidly() { let s = blob(SampleType::U8); diff --git a/crates/engine/src/distort_cmds/tests.rs b/crates/engine/src/distort_cmds/tests.rs index 96fe10187e..bdf7863903 100644 --- a/crates/engine/src/distort_cmds/tests.rs +++ b/crates/engine/src/distort_cmds/tests.rs @@ -124,6 +124,16 @@ fn puppet_identity_translation_and_undo() { } } +#[test] +fn puppet_with_an_expansion_that_empties_the_mesh_keeps_the_layer() { + // #951: a pinned warp over an empty mesh panicked in the solver; it must not erase the layer + // either (the rasterizer keeps only pixels inside the mesh). + let mut s = session(8); + let before = active_surface(&s); + s.execute(PUPPET, json!({"pins": [{"src": [30, 25], "dst": [36, 29]}], "expansion": -200})).unwrap(); + assert_eq!(active_surface(&s).read_region(Rect::new(0, 0, 80, 60)), before.read_region(Rect::new(0, 0, 80, 60))); +} + #[test] fn puppet_two_pins_bend_the_block() { let mut s = session(8); diff --git a/crates/ui-egui/src/puppet_ui.rs b/crates/ui-egui/src/puppet_ui.rs index 24f7439838..42791715d6 100644 --- a/crates/ui-egui/src/puppet_ui.rs +++ b/crates/ui-egui/src/puppet_ui.rs @@ -58,6 +58,20 @@ impl PuppetSession { self.rebind(); } + /// Remeshes for a new density or expansion. One that leaves no pixels is refused, as `begin` + /// refuses a layer without any: the previous `density` and `expansion` and their mesh come + /// back (#951). + fn remesh_or_restore(&mut self, density: PuppetDensity, expansion: f64) -> Result<(), String> { + self.rebuild_mesh(); + if self.mesh.tris.is_empty() { + self.warp.density = density; + self.warp.expansion = expansion; + self.rebuild_mesh(); + return Err("Could not use Puppet Warp: no opaque pixels at this density and expansion".into()); + } + Ok(()) + } + /// New solver for the current pin set (pins added/removed, rotation fixed or freed). fn rebind(&mut self) { let src: Vec<[f64; 2]> = self.warp.pins.iter().map(|p| p.src).collect(); @@ -179,6 +193,7 @@ pub fn control(app: &mut PhotocraftApp, ui: &Value) -> Result { } let s = app.distort.puppet.as_mut().ok_or(tl!("Puppet Warp is not active"))?; let mut remesh = false; + let (density, expansion) = (s.warp.density, s.warp.expansion); if let Some(m) = ui.get("mode").and_then(Value::as_str) { s.warp.mode = PuppetMode::parse(m).ok_or("mode: rigid|normal|distort")?; } @@ -194,7 +209,7 @@ pub fn control(app: &mut PhotocraftApp, ui: &Value) -> Result { s.show_mesh = v; } if remesh { - s.rebuild_mesh(); + s.remesh_or_restore(density, expansion)?; } else { s.solve(true); } @@ -314,6 +329,7 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) { ); remesh |= density != s.warp.density; ui.label(tl!("Expansion:")); + let expansion = s.warp.expansion; let mut e = s.warp.expansion as f32; if crate::widgets::value_field(ui, &mut e, -50.0..=100.0, "px", 56.0).changed() { s.warp.expansion = f64::from(e); @@ -354,7 +370,8 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) { rebind = true; } if remesh { - s.rebuild_mesh(); + // A refused value snaps the fields back to the previous settings. + let _ = s.remesh_or_restore(density, expansion); } else if rebind { s.rebind(); } else if resolve { @@ -418,4 +435,33 @@ mod tests { let b = st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap().content_bounds(); assert!(b.y0 < 25, "the right end went up: {b:?}"); } + + #[test] + fn an_expansion_that_empties_the_mesh_is_refused() { + // #951: remeshing to no vertices rebound the pins to a vertex that doesn't exist and + // panicked. -50 is the options bar's minimum and also empties this 20 px tall bar. + let ctx = egui::Context::default(); + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.session.execute("file.new", json!({"width": 100, "height": 80, "depth": 8})).unwrap(); + app.session.execute("layer.new.layer", json!({})).unwrap(); + app.session + .edit("paint", |doc, active| { + doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap().fill_rect(Rect::new(20, 30, 80, 50), &[1.0, 0.0, 0.0, 1.0]); + Ok(()) + }) + .unwrap(); + app.sync_views(); + crate::distort_ui::menu(&mut app, &ctx, "edit.puppetWarp", &json!({})).unwrap().unwrap(); + crate::distort_ui::pointer(&mut app, ToolEvent::Down { x: 50.0, y: 40.0, pressure: 1.0 }, egui::Modifiers::NONE); + crate::distort_ui::pointer(&mut app, ToolEvent::Up { x: 50.0, y: 40.0 }, egui::Modifiers::NONE); + let before = app.distort.puppet.as_ref().unwrap().describe(); + for e in [-200, -50] { + assert!(control(&mut app, &json!({"expansion": e})).is_err(), "{e}"); + assert_eq!(app.distort.puppet.as_ref().unwrap().describe(), before, "{e}: the previous settings and mesh stay"); + } + // A smaller shrink still remeshes. + let v = control(&mut app, &json!({"expansion": -2})).unwrap(); + assert_eq!(v["expansion"], -2.0); + assert!(v["triangles"].as_u64().unwrap() > 0); + } }