diff --git a/crates/pipeline/src/mask_space.rs b/crates/pipeline/src/mask_space.rs index cb8853d795..36b998ab5e 100644 --- a/crates/pipeline/src/mask_space.rs +++ b/crates/pipeline/src/mask_space.rs @@ -420,27 +420,30 @@ mod tests { } /// Where a zoomed-in pass's new box reaches past what it covered (a turned box's corners), it - /// takes the whole-photo selection, not a copy of its own edge (review of #3550). + /// takes the whole-photo selection: neither a copy of its own edge (a negative whole-photo + /// selection) nor a fixed "unselected" (a positive one) (review of #3550). #[test] fn a_moved_detail_pass_takes_the_whole_selection_past_its_own_area() { const SIDE: usize = 16; let mut s = DevelopSettings::default(); s.geometry.rotate = 20.0; let frame = Frame::new(300, 200, &s, false); - let detail = SegMask::from_logits_in(SIDE, &[8.0; SIDE * SIDE], [0.4, 0.4, 0.6, 0.6]); - let coarse = SegMask::from_logits(SIDE, &[8.0; SIDE * SIDE]); - let mut m = - mask(MaskShape::Object { hint: vec![Point::new(0.5, 0.5)], exclude: Vec::new(), seg: Some(coarse), detail: vec![detail], edge: 0.0 }); - m.on_photo = false; - attach(&mut m, &frame); - let MaskShape::Object { detail, .. } = &m.components[0].shape else { panic!() }; - let g = SegGrid::new(&detail[0]).unwrap(); - let b = detail[0].bounds().unwrap(); - // a corner of the new box is outside the turned patch: it takes the whole-photo selection - // (selected here), not a fixed "unselected" - let inset = |t: f64| Point::new(b[0] + t * (b[2] - b[0]), b[1] + t * (b[3] - b[1])); - assert!(g.logit(inset(0.03)) > 0.0, "{}", g.logit(inset(0.03))); - assert!(g.logit(inset(0.5)) > 0.0); + for whole in [8.0f32, -8.0] { + let detail = SegMask::from_logits_in(SIDE, &[8.0; SIDE * SIDE], [0.4, 0.4, 0.6, 0.6]); + let coarse = SegMask::from_logits(SIDE, &[whole; SIDE * SIDE]); + let mut m = + mask(MaskShape::Object { hint: vec![Point::new(0.5, 0.5)], exclude: Vec::new(), seg: Some(coarse), detail: vec![detail], edge: 0.0 }); + m.on_photo = false; + attach(&mut m, &frame); + let MaskShape::Object { detail, .. } = &m.components[0].shape else { panic!() }; + let g = SegGrid::new(&detail[0]).unwrap(); + let b = detail[0].bounds().unwrap(); + // a corner of the new box is outside the turned patch: the whole-photo selection's sign + let inset = |t: f64| Point::new(b[0] + t * (b[2] - b[0]), b[1] + t * (b[3] - b[1])); + let corner = g.logit(inset(0.03)); + assert!((corner > 0.0) == (whole > 0.0), "whole {whole}: corner {corner}"); + assert!(g.logit(inset(0.5)) > 0.0, "whole {whole}: the patch itself stays selected"); + } } /// The box around a mapped rectangle holds the whole bent edge, also off the centre lines diff --git a/crates/ui-egui/src/panels/detail/geometry.rs b/crates/ui-egui/src/panels/detail/geometry.rs index bfee31813d..b02c5eb9c1 100644 --- a/crates/ui-egui/src/panels/detail/geometry.rs +++ b/crates/ui-egui/src/panels/detail/geometry.rs @@ -492,5 +492,27 @@ mod mask_map_tests { let runs = super::super::masks::runs([Some(a), Some(b), None, Some(c), None, None, Some(a), Some(b)]); assert_eq!(runs, vec![vec![a, b], vec![a, b]], "single points are dropped, the rest split"); assert!(super::super::masks::runs([None, Some(a), None]).is_empty()); + // a chord far longer than the usual step (across a fold) cuts the run too + let line: Vec = (0..10).map(|i| pos2(i as f32, 0.0)).chain((0..10).map(|i| pos2(200.0 + i as f32, 0.0))).collect(); + let cut = super::super::masks::split_jumps(vec![line]); + assert_eq!(cut.len(), 2, "{cut:?}"); + assert!(cut.iter().all(|r| r.len() == 10)); + } + + /// A guide through an end far off the photo still crosses the canvas (review of #3550). + #[test] + fn a_guide_through_an_end_off_the_photo_still_crosses_it() { + let mut d = DevelopSettings::default(); + d.geometry.vertical = 35.0; + d.optics.distortion = 50.0; + let frame = Frame::new(400, 300, &d, true); + let m = CanvasMap::new(&frame, Rect::from_min_size(pos2(100.0, 50.0), vec2(400.0, 300.0))).for_mask(true); + // a gradient drawn off the photo's right side (start and end both there): its guides are + // horizontal and cross the photo from 2.5 photo widths away + let (start, end) = (Point::new(3.5, 0.4), Point::new(3.5, 0.6)); + for at in [start, end] { + let guide: Vec = super::super::masks::linear_guide(&m, start, end, at).concat(); + assert!(guide.iter().filter(|g| m.rect.expand(-2.0).contains(**g)).count() > 2, "{at:?}: the guide crosses the canvas"); + } } } diff --git a/crates/ui-egui/src/panels/detail/masks.rs b/crates/ui-egui/src/panels/detail/masks.rs index 49c8692688..2150d513b8 100644 --- a/crates/ui-egui/src/panels/detail/masks.rs +++ b/crates/ui-egui/src/panels/detail/masks.rs @@ -455,11 +455,48 @@ pub(super) fn linear_guide(map: &CanvasMap, start: Point, end: Point, at: Point) return Vec::new(); } let perp = (-d.1 / n * l.0, d.0 / n * l.1); - const STEPS: usize = 48; - runs((0..=STEPS).map(|i| { - let t = -1.5 + 3.0 * i as f64 / STEPS as f64; + // from where the line enters the photo to where it leaves it (with a margin), measured from + // `at`, so a guide whose end lies off the photo still reaches it + let pp = perp.0 * perp.0 + perp.1 * perp.1; + let ts = [(0.0, 0.0), (1.0, 0.0), (0.0, 1.0), (1.0, 1.0)].map(|(x, y)| ((x - at.x) * perp.0 + (y - at.y) * perp.1) / pp); + let (lo, hi) = ts.iter().fold((f64::INFINITY, f64::NEG_INFINITY), |(lo, hi), t| (lo.min(*t), hi.max(*t))); + let pad = 0.1 * (hi - lo); + let (lo, hi) = (lo - pad, hi + pad); + if !(lo.is_finite() && hi.is_finite() && hi > lo) { + return Vec::new(); + } + // 48 steps across about a photo's width, at most 256 + let steps = ((hi - lo) * 16.0).ceil().clamp(48.0, 256.0) as usize; + split_jumps(runs((0..=steps).map(|i| { + let t = lo + (hi - lo) * i as f64 / steps as f64; map.try_screen(Point::new(at.x + perp.0 * t, at.y + perp.1 * t)) - })) + }))) +} + +/// `runs` cut again where a step is far longer than the run's usual one: a strong warp can fold +/// the line, and a chord across the fold isn't part of it. +pub(super) fn split_jumps(runs: Vec>) -> Vec> { + let mut out = Vec::new(); + for run in runs { + let mut steps: Vec = run.windows(2).filter_map(|w| Some(w.first()?.distance(*w.get(1)?))).collect(); + steps.sort_by(f32::total_cmp); + let usual = steps.get(steps.len() / 2).copied().unwrap_or(0.0); + let limit = (8.0 * usual).max(2.0); + let mut cur: Vec = Vec::new(); + for q in run { + if cur.last().is_some_and(|p: &Pos2| p.distance(q) > limit) { + if cur.len() >= 2 { + out.push(std::mem::take(&mut cur)); + } + cur.clear(); + } + cur.push(q); + } + if cur.len() >= 2 { + out.push(cur); + } + } + out } /// The unbroken runs (two points or more) of a sampled line whose missing samples (`None`) cut it.