diff --git a/crates/ui-egui/src/dialogs/wide_angle_ui.rs b/crates/ui-egui/src/dialogs/wide_angle_ui.rs index b979f71cf4..1b44cd7add 100644 --- a/crates/ui-egui/src/dialogs/wide_angle_ui.rs +++ b/crates/ui-egui/src/dialogs/wide_angle_ui.rs @@ -11,7 +11,7 @@ use egui::{Align2, Color32, FontId, Pos2, Rect as ERect, Sense, Stroke, TextureHandle, pos2, vec2}; use photocraft_algo::transform::Interp; -use photocraft_algo::wideangle::{self, Camera, Constraint, Orientation, WideAngle, WideModel}; +use photocraft_algo::wideangle::{self, Camera, Constraint, Orientation, WideAngle, WideMesh, WideModel}; use photocraft_doc::LayerId; use photocraft_geom::Rect; use photocraft_raster::Surface; @@ -38,6 +38,9 @@ pub struct WideAngleDialog { ph: usize, src_tex: Option, out_tex: Option, + /// The proxy warp used by both the rendered preview and its constraint overlays. It is + /// replaced only by `render`, so every edit to `params` must set `dirty`. + mesh: Option, dirty: bool, pub preview: bool, drag: Option<([f64; 2], [f64; 2])>, @@ -71,8 +74,13 @@ impl WideAngleDialog { p } + /// The proxy rectangle shared by the preview and overlays. + fn proxy_rect(&self) -> Rect { + Rect::new(0, 0, self.pw as i32, self.ph as i32) + } + fn preview_surface(&mut self) -> Result { - let pf = Rect::new(0, 0, self.pw as i32, self.ph as i32); + let pf = self.proxy_rect(); let p = self.proxy_params(); let mesh = wideangle::solve(&p, pf); self.residual = mesh.residual / self.scale; @@ -82,6 +90,7 @@ impl WideAngleDialog { { photocraft_engine::color_channels::restrict_surface_in(&self.proxy, &mut out, colors, pf).map_err(|e| e.to_string())?; } + self.mesh = Some(mesh); Ok(out) } @@ -153,6 +162,7 @@ pub fn open(app: &mut PhotocraftApp, ctx: &egui::Context) -> Result<(), String> ph, src_tex: None, out_tex: None, + mesh: None, dirty: true, preview: true, drag: None, @@ -343,10 +353,10 @@ pub fn show(app: &mut PhotocraftApp, ctx: &egui::Context) { Orientation::Horizontal => t.warning, Orientation::Vertical => t.danger, }; - let pf = Rect::new(0, 0, d.pw as i32, d.ph as i32); - let mesh = (!show_src && !d.params.constraints.is_empty()).then(|| wideangle::solve(&d.proxy_params(), pf)); + let pf = d.proxy_rect(); + let mesh = if show_src { None } else { d.mesh.as_ref() }; for c in &d.params.constraints { - let pts: Vec = match &mesh { + let pts: Vec = match mesh { Some(m) => cam .arc(c.a, c.b, 24) .iter() @@ -457,6 +467,10 @@ pub fn show(app: &mut PhotocraftApp, ctx: &egui::Context) { action = Some(if role == widgets::ButtonRole::Default { "ok" } else { "cancel" }); } fu.label(egui::RichText::new(format!("{:.0} ms ยท straightness {:.1} px", d.render_ms, d.residual)).color(t.text_faint)); + // Panel edits mark the warp dirty after the overlays were drawn: redraw so they catch up. + if d.dirty { + ctx.request_repaint(); + } }); if ctx.input(|i| i.key_pressed(egui::Key::Escape)) { action = Some("cancel"); @@ -546,6 +560,75 @@ mod tests { menu(app, ctx, "filter.adaptiveWideAngle", &json!({"ui": {"add": {"a": [20, 50], "b": [140, 50]}}})).unwrap().unwrap(); } + #[test] + fn constraint_overlay_reuses_the_preview_mesh_on_redraw() { + fn constraint_line(shape: &egui::Shape) -> Option<&[Pos2]> { + match shape { + egui::Shape::Path(p) if p.points.len() == 25 => Some(&p.points), + egui::Shape::Vec(shapes) => shapes.iter().find_map(constraint_line), + _ => None, + } + } + let mut app = grey_layer(); + let ctx = egui::Context::default(); + PhotocraftApp::setup_context(&ctx, Default::default()); + open_with_a_constraint(&mut app, &ctx); + let input = || egui::RawInput { screen_rect: Some(ERect::from_min_size(Pos2::ZERO, vec2(1000.0, 600.0))), ..Default::default() }; + // Baseline: the source-space line, from which the screen scale and centre are measured. + app.wide_angle.as_mut().unwrap().preview = false; + // egui panics if a frame's texture uploads are dropped unhandled, hence `clear`. + ctx.run_ui(input(), |ui| show(&mut app, ui.ctx())).textures_delta.clear(); + let mut output = ctx.run_ui(input(), |ui| show(&mut app, ui.ctx())); + output.textures_delta.clear(); + let line = output.shapes.iter().find_map(|s| constraint_line(&s.shape)).expect("baseline constraint line"); + let (first, last) = (line.first().unwrap().x, line.last().unwrap().x); + let base_x = (first + last) * 0.5; + // The constraint spans x = 20..140 of the 160 px layer; the proxy is 1:1 with it. + let px = (last - first) / 120.0; + // Move only the cached warp. A fresh solve would draw the original line instead. + let d = app.wide_angle.as_mut().unwrap(); + for (out, _) in &mut d.mesh.as_mut().unwrap().verts { + out[0] += 10.0; + } + let verts = d.mesh.as_ref().unwrap().verts.clone(); + let render_ms = d.render_ms; + for (preview, dragging, shifted) in [(true, false, true), (false, false, false), (true, true, false), (true, false, true)] { + let d = app.wide_angle.as_mut().unwrap(); + d.preview = preview; + d.drag = dragging.then_some(([20.0, 30.0], [140.0, 30.0])); + let mut output = ctx.run_ui(input(), |ui| show(&mut app, ui.ctx())); + output.textures_delta.clear(); + let line = output.shapes.iter().find_map(|s| constraint_line(&s.shape)).expect("painted constraint line"); + // The source line is centred on the image; the altered cached warp moves its + // centre ten proxy pixels to the right. Measure against the unshifted baseline. + let centre_x = (line.first().unwrap().x + line.last().unwrap().x) * 0.5; + let expected_x = base_x + if shifted { 10.0 * px } else { 0.0 }; + assert!((centre_x - expected_x).abs() < 0.1, "preview={preview}, dragging={dragging}: centre={centre_x}, expected={expected_x}"); + let d = app.wide_angle.as_ref().unwrap(); + assert_eq!(d.mesh.as_ref().unwrap().verts, verts, "redraw retains the cached warp"); + assert_eq!(d.render_ms, render_ms, "redraw does not render the preview again"); + } + } + + #[test] + fn preview_mesh_refreshes_after_edits_and_history_steps() { + let mut app = grey_layer(); + let ctx = egui::Context::default(); + open_with_a_constraint(&mut app, &ctx); + let original = app.wide_angle.as_ref().unwrap().mesh.as_ref().unwrap().verts.clone(); + menu(&mut app, &ctx, "filter.adaptiveWideAngle", &json!({"ui": {"set": {"scale": 120.0}}})).unwrap().unwrap(); + let edited = app.wide_angle.as_ref().unwrap().mesh.as_ref().unwrap().verts.clone(); + assert_ne!(edited, original); + for (step, expected) in [(Step::Undo, &original), (Step::Redo, &edited)] { + let d = app.wide_angle.as_mut().unwrap(); + d.step(step); + assert!(d.dirty); + d.render(&ctx); + assert!(!d.dirty); + assert_eq!(&d.mesh.as_ref().unwrap().verts, expected); + } + } + /// `orient` sets a constraint's orientation, as the right-click menu does; a bad index is /// an error that changes nothing. #[test]