diff --git a/crates/engine/src/tests/fill_area.rs b/crates/engine/src/tests/fill_area.rs index 7ba4bcc26f..a11f8d2975 100644 --- a/crates/engine/src/tests/fill_area.rs +++ b/crates/engine/src/tests/fill_area.rs @@ -480,6 +480,76 @@ fn fill_area_fills_an_area_that_a_left_out_paths_box_holds_but_its_segments_miss } } +/// Puts `path` with `look` in front on the current layer. +fn insert_path(s: &mut Session, path: PathData, look: vectorcraft_doc::Appearance) -> NodeId { + s.edit("t", |d, _| { + let id = d.alloc_id(); + let top = d.children(d.default_layer()).map_or(0, |c| c.len()); + d.insert(d.default_layer(), top, Node::path(id, path, look))?; + Ok(id) + }) + .unwrap() +} + +/// A path with too many segments in the empty corner of a filled L, well clear of both arms, is +/// left out and does not stop the L from being filled, though the L's box holds it. +#[test] +fn fill_area_fills_an_l_whose_empty_corner_holds_a_left_out_path() { + let mut s = session(); + let l = [(0.0, 0.0), (400.0, 0.0), (400.0, 100.0), (100.0, 100.0), (100.0, 400.0), (0.0, 400.0)].map(|(x, y)| Point::new(x, y)); + let solid = vectorcraft_doc::Appearance::basic(Paint::solid(Color::BLACK), Paint::None, 0.0); + insert_path(&mut s, PathData::single(vectorcraft_geom::SubPath::polyline(&l, true)), solid); + let n = vectorcraft_pathops::SHAPE_BUILDER_MAX_SEGMENTS; + let pts: Vec = (0..=n).map(|k| Point::new(250.0 + 100.0 * k as f64 / n as f64, 250.0 + (k % 2) as f64)).collect(); + let look = vectorcraft_doc::Appearance::basic(Paint::None, Paint::solid(Color::BLACK), 1.0); + insert_path(&mut s, PathData::single(vectorcraft_geom::SubPath::polyline(&pts, false)), look); + deselect(&mut s); + let r = s.execute("shapeBuilder.copyRegion", &json!({"point": [50, 50]})).unwrap(); + assert!((area(&s, id_of(&r)) - 70_000.0).abs() < 1.0, "{}", area(&s, id_of(&r))); +} + +/// A filled contour with too many segments on a higher layer surrounds a small square, its outline +/// far from it: it is left out of the map, and the new path still goes above it, since it covers +/// the area. +#[test] +fn fill_area_stacks_above_a_left_out_path_that_covers_the_area() { + let mut s = session(); + let layer = s.doc().unwrap().doc.default_layer(); + rect(&mut s, 0.0, 0.0, 100.0, 100.0); + let over = id_of(&s.execute("layer.new", &json!({"name": "Over"})).unwrap()); + let n = vectorcraft_pathops::SHAPE_BUILDER_MAX_SEGMENTS + 1; + let pts: Vec = (0..n) + .map(|k| { + let a = k as f64 * std::f64::consts::TAU / n as f64; + Point::new(50.0 + 3000.0 * a.cos(), 50.0 + 3000.0 * a.sin()) + }) + .collect(); + let solid = vectorcraft_doc::Appearance::basic(Paint::solid(Color::BLACK), Paint::None, 0.0); + let contour = insert_path(&mut s, PathData::single(vectorcraft_geom::SubPath::polyline(&pts, true)), solid); + deselect(&mut s); + let r = s.execute("shapeBuilder.copyRegion", &json!({"point": [50, 50]})).unwrap(); + assert!((area(&s, id_of(&r)) - 10_000.0).abs() < 1.0, "{}", area(&s, id_of(&r))); + assert_eq!(r["above"], json!(contour.0)); + let d = &s.doc().unwrap().doc; + assert_eq!(d.parent_of(id_of(&r)), Some(over)); + assert_ne!(d.parent_of(id_of(&r)), layer); +} + +/// With Gap Detection off, a long line on a higher layer 0.75 pt from a square's edge neither +/// touches nor covers the square, so the new path stays above the square. +#[test] +fn fill_area_does_not_stack_above_a_line_that_does_not_touch_the_area() { + let mut s = session(); + let layer = s.doc().unwrap().doc.default_layer(); + let square = rect(&mut s, 0.0, 0.0, 100.0, 100.0); + id_of(&s.execute("layer.new", &json!({"name": "Other"})).unwrap()); + line(&mut s, (-5000.0, -0.75), (5000.0, -0.75)); + deselect(&mut s); + let r = s.execute("shapeBuilder.copyRegion", &json!({"point": [50, 50]})).unwrap(); + assert_eq!(r["above"], json!(square.0)); + assert_eq!(s.doc().unwrap().doc.parent_of(id_of(&r)), layer); +} + /// With Gap Detection on, a square on a higher layer within the gap of the area's corners does not /// bound the area, so the new path stays above the square that does. #[test] diff --git a/crates/tools/src/builder/fill_area.rs b/crates/tools/src/builder/fill_area.rs index 663068f1b7..bd41eeaf24 100644 --- a/crates/tools/src/builder/fill_area.rs +++ b/crates/tools/src/builder/fill_area.rs @@ -38,7 +38,8 @@ const NEAR_ROUNDS: usize = 4; /// have no area under the point, or the area does not fit after [`WINDOW_ROUNDS`] widenings, the /// finder maps every path. When a window's paths have more segments than one Shape Builder map /// takes, the paths with the most segments are left out until the rest fit, and an area is taken -/// only when no path left out reaches it. When every path together has more segments than that, +/// only when no path left out crosses it or comes within the gap of it; a path left out that +/// covers it still counts for where the new path goes. When every path together has more segments than that, /// an area no window finds is not found. The map of every path, once a search has needed it, is /// kept for the next search. pub struct AreaFinder { @@ -67,32 +68,77 @@ fn meet(a: &Rect, b: &Rect) -> bool { a.x0 <= b.x1 && b.x0 <= a.x1 && a.y0 <= b.y1 && b.y0 <= a.y1 } -/// Does a segment of `path` (closing segments included) meet `zone`? A path whose box meets the zone -/// may still lie wholly outside it, as a path of subpaths far apart, a diagonal line or a curve -/// that arcs around the zone does. -fn reaches(path: &PathData, zone: &Rect) -> bool { - let bez = path.to_bezpath(); - vectorcraft_geom::kurbo::segments(bez.elements().iter().copied()).any(|seg| segment_reaches(seg, zone, 0)) +/// The area a path left out of a window's map may cut: its outline, the same as a closed path +/// (`bez`), and the box (`zone`) its outline reaches with the gap. +struct Reach<'a> { + face: &'a PathData, + bez: &'a vectorcraft_geom::kurbo::BezPath, + zone: Rect, + /// How close (pt) to the outline a path may come and still change the area: the gap and 1 pt. + reach: f64, } -/// How many times [`segment_reaches`] halves a segment: a piece of 1/2^24 of a segment counts as -/// meeting the zone when its box does. +/// Does a segment of `path` (closing segments included) lie inside the area or come within its +/// reach of the area's outline? A path in the box of the area may still lie wholly outside the +/// area, in the empty corner of an L or around it, and such a path does not change it. `budget` +/// counts the work (pieces checked times the outline's segments); when it runs out, the path +/// counts as cutting the area. +fn cuts(path: &PathData, area: &Reach, budget: &mut usize) -> bool { + let bez = path.to_bezpath(); + let cost = area.face.subpaths.iter().map(|sp| sp.segment_count()).sum::().max(1); + vectorcraft_geom::kurbo::segments(bez.elements().iter().copied()).any(|seg| segment_cuts(seg, area, cost, budget, 0)) +} + +/// How many times [`segment_cuts`] halves a segment: a piece of 1/2^24 of a segment near the area +/// counts as cutting it. const REACH_SPLITS: u32 = 24; -/// The side (pt) below which a piece of a segment whose box meets the zone counts as meeting it. +/// The side (pt) below which a piece of a segment that may come within reach of the area counts as +/// cutting it. const REACH_TOLERANCE: f64 = 0.01; -/// Does `seg` meet `zone`? Its halves are checked in turn while their boxes meet the zone and -/// neither end is in it; a piece smaller than [`REACH_TOLERANCE`] whose box meets the zone counts. -fn segment_reaches(seg: PathSeg, zone: &Rect, depth: u32) -> bool { +/// How much work [`cuts`] does for one area before it takes a path as cutting it: pieces checked +/// times the segments of the area's outline. +const CUT_BUDGET: usize = 1 << 24; + +/// Does `seg` lie inside the area or come within its reach of the area's outline? A piece whose +/// box misses the area's zone does not; a piece whose middle is inside the area or within reach of +/// its outline does; a piece farther from the outline than its box is wide does not, since every +/// point of it is then out of reach and on the same side. The rest is halved. +fn segment_cuts(seg: PathSeg, area: &Reach, cost: usize, budget: &mut usize, depth: u32) -> bool { let b = segment_bounds(seg); - if !meet(&b, zone) { + if !meet(&b, &area.zone) { return false; } - if holds(zone, seg.start()) || holds(zone, seg.end()) || depth >= REACH_SPLITS || b.width().max(b.height()) < REACH_TOLERANCE { + let Some(left) = budget.checked_sub(cost) else { return true }; + *budget = left; + let mid = seg.eval(0.5); + if area.bez.winding(mid) != 0 { return true; } - segment_reaches(seg.subsegment(0.0..0.5), zone, depth + 1) || segment_reaches(seg.subsegment(0.5..1.0), zone, depth + 1) + let Some((.., d)) = area.face.nearest(mid) else { return true }; + if d <= area.reach { + return true; + } + let wide = b.width().hypot(b.height()); + if d - area.reach > wide { + return false; + } + if depth >= REACH_SPLITS || wide < REACH_TOLERANCE { + return true; + } + segment_cuts(seg.subsegment(0.0..0.5), area, cost, budget, depth + 1) || segment_cuts(seg.subsegment(0.5..1.0), area, cost, budget, depth + 1) +} + +/// How far (pt) an anchor of an area's outline may lie from a path's outline, with Gap Detection +/// off, and still count as on it: the least snap distance [`outline_snap`] gives, five times the +/// 0.01 pt precision the Shape Builder's map works to. +const CONTACT: f64 = 0.05; + +/// Does the filled area of the closed subpaths of `s` hold `p`? +fn covers(s: &po::Shape, p: Point) -> bool { + let closed = PathData::new(s.path.subpaths.iter().filter(|sp| sp.closed).cloned().collect()); + path_contains(&closed, s.rule, p) } /// Does `outer` hold all of `inner`? @@ -201,7 +247,7 @@ impl AreaFinder { /// `p` is in no enclosed area. pub fn find(&mut self, p: Point) -> Option { if let Some((map, picked)) = &self.full { - return self.area_in(map, picked, p); + return self.area_in(map, picked, &[], p); } let total = self.total?; // A gap bridge reaches at most `gap` past a path, so a window takes in every path within @@ -245,22 +291,27 @@ impl AreaFinder { let every = picked.len() == self.inputs.len(); let shapes: Vec = picked.iter().filter_map(|&i| self.inputs.get(i).map(|(s, ..)| s.clone())).collect(); let map = BuilderMap::of_shapes(shapes, false, self.gap); - let bounds = map.region_at(p).and_then(|k| map.regions.get(k)).map(|(.., rb)| *rb); + let face = map.region_at(p).and_then(|k| map.regions.get(k)); + let bounds = face.map(|(.., rb)| *rb); if every { - let area = self.area_in(&map, &picked, p); + let area = self.area_in(&map, &picked, &[], p); self.full = Some((map, picked)); return area; } let before = window; match bounds { - // A path left out that reaches the area could cut it, and only the map of every - // path holds both. + // A path left out that crosses the area or comes within its reach could cut it, and + // only the map of every path holds both. Some(rb) if within(&rb, &window) => { let reached = rb.inflate(reach, reach); + let mut budget = CUT_BUDGET; let cut = any_left_out - && self.inputs.iter().zip(&left_out).any(|((s, b, _), out)| *out && meet(b, &reached) && reaches(&s.path, &reached)); + && face.is_none_or(|(region, bez, _)| { + let area = Reach { face: ®ion.path, bez, zone: reached, reach }; + self.inputs.iter().zip(&left_out).any(|((s, b, _), out)| *out && meet(b, &reached) && cuts(&s.path, &area, &mut budget)) + }); if !cut { - return self.area_in(&map, &picked, p); + return self.area_in(&map, &picked, &left_out, p); } every_path = true; } @@ -301,8 +352,11 @@ impl AreaFinder { out } - /// The area under `p` in `map`, made of the inputs `picked` (in its order). - fn area_in(&self, map: &BuilderMap, picked: &[usize], p: Point) -> Option { + /// The area under `p` in `map`, made of the inputs `picked` (in its order). `left_out` marks + /// the inputs left out of the map, by index, none of which comes near the area: the front-most + /// of them that covers the area decides where the new path goes when it is in front of the + /// shape the map gives. + fn area_in(&self, map: &BuilderMap, picked: &[usize], left_out: &[bool], p: Point) -> Option { let (region, ..) = map.region_at(p).and_then(|k| map.regions.get(k))?; let path = po::FaceMerger::with_gap(&map.shapes, self.gap).merge(&[region]); if path.is_empty() { @@ -312,9 +366,10 @@ impl AreaFinder { // The front-most shape that covers the area or that an anchor of its outline lies on. A // shape within the gap of an anchor counts only when no shape is along the area itself, // so a path near the area that bounds none of it does not decide where the new path goes. + // Without a gap, an anchor is on a shape's outline within [`CONTACT`]. let along = |gap: f64| { map.shapes.iter().enumerate().rev().find(|(i, s)| { - let tol = outline_snap(&s.path) + gap; + let tol = if gap > 0.0 { outline_snap(&s.path) + gap } else { CONTACT }; region.sources.contains(i) || s.path.bounds().is_some_and(|b| { let b = b.inflate(tol, tol); @@ -323,7 +378,12 @@ impl AreaFinder { }) }; let k = along(0.0).or_else(|| along(self.gap)).map_or(map.shapes.len().checked_sub(1)?, |(i, _)| i); - let above = picked.get(k).and_then(|&i| self.inputs.get(i)).map(|(.., id)| *id)?; + let mapped = *picked.get(k)?; + // A path left out comes near no part of the area, so it covers all of it or none of it. + let covering = + left_out.iter().enumerate().rev().find(|(i, out)| **out && *i > mapped && self.inputs.get(*i).is_some_and(|(s, ..)| covers(s, p))); + let front = covering.map_or(mapped, |(i, _)| i); + let above = self.inputs.get(front).map(|(.., id)| *id)?; Some(EnclosedArea { path, above }) } } @@ -371,9 +431,22 @@ struct HoverJob { done: std::sync::mpsc::Receiver<(AreaFinder, Option)>, } +#[cfg(not(target_arch = "wasm32"))] +thread_local! { + /// The hover searches this thread has started on other threads ([`hover_searches_started`]). + static HOVER_SEARCHES: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +/// How many hover searches this thread has started on other threads, for tests that count them. +#[cfg(all(test, not(target_arch = "wasm32")))] +pub(super) fn hover_searches_started() -> usize { + HOVER_SEARCHES.with(std::cell::Cell::get) +} + /// Runs `finder.find(p)` on another thread; gives the finder back when no thread starts. #[cfg(not(target_arch = "wasm32"))] fn spawn_find(finder: AreaFinder, p: Point) -> Result)>, Box> { + HOVER_SEARCHES.with(|n| n.set(n.get() + 1)); let (tx, done) = std::sync::mpsc::channel(); let (back_tx, back) = std::sync::mpsc::channel(); let spawned = std::thread::Builder::new().name("vectorcraft-fill-area".into()).spawn(move || { @@ -437,14 +510,13 @@ impl FillAreaTool { } /// The area under `p`, found here and now (a click). A search running on another thread is - /// waited for first, so its finder is not made again. + /// waited for first, so its finder is not made again; no search starts for where the pointer + /// moved meanwhile. fn area_at(&mut self, cx: &ToolContext, p: Point) -> Option { if let Some(a) = self.hover_holding(cx, p) { return Some(a.clone()); } - while self.job.is_some() { - self.settle(cx, true); - } + self.settle(cx, true, false); let key = self.finder_key(cx); let mut finder = self.take_finder(cx); let area = finder.find(p); @@ -485,8 +557,8 @@ impl FillAreaTool { } /// Takes the result of the hover search on another thread when it is done (`wait`: waits for - /// it), and starts the search for where the pointer moved meanwhile. - fn settle(&mut self, cx: &ToolContext, wait: bool) { + /// it), and with `follow` starts the search for where the pointer moved meanwhile. + fn settle(&mut self, cx: &ToolContext, wait: bool, follow: bool) { let Some(job) = self.job.take() else { return }; let got = if wait { job.done.recv().ok() @@ -514,7 +586,8 @@ impl FillAreaTool { // The area found serves wherever the pointer moved meanwhile, as long as it holds it. let at = self.at; self.hover = area.filter(|a| current && at.is_some_and(|p| path_contains(&a.path, FillRule::NonZero, p))).map(|a| (job.key, a)); - if let Some(p) = at + if follow + && let Some(p) = at && self.hover.is_none() && (p != job.p || !current) { @@ -591,9 +664,9 @@ impl Tool for FillAreaTool { } fn poll(&mut self, cx: &ToolContext, wait: bool) -> Vec { - self.settle(cx, wait); + self.settle(cx, wait, true); while wait && self.job.is_some() { - self.settle(cx, true); + self.settle(cx, true, true); } vec![] } diff --git a/crates/tools/src/builder/tests.rs b/crates/tools/src/builder/tests.rs index dc31c1ba79..0cdaad3434 100644 --- a/crates/tools/src/builder/tests.rs +++ b/crates/tools/src/builder/tests.rs @@ -3767,6 +3767,35 @@ fn live_paint_selection_options_set_the_highlight() { assert_eq!(t.options()["width"], json!(LP_SELECT_WIDTH_RANGE.0)); } +/// A press while a Fill Area hover search runs on another thread waits for that search only: no +/// search starts for where the pointer moved meanwhile, and the press finds its own area. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn fill_area_press_does_not_search_a_hover_position_it_passed() { + let mut d = Document::new(800.0, 600.0); + let l = d.layers[0].id; + let pts: Vec = (0..200) + .map(|k| { + let a = k as f64 * std::f64::consts::TAU / 200.0; + Point::new(300.0 + 100.0 * a.cos(), 300.0 + 100.0 * a.sin()) + }) + .collect(); + let poly = d.alloc_id(); + d.insert(Some(l), 0, Node::path(poly, PathData::single(SubPath::polyline(&pts, true)), Appearance::default_art())).unwrap(); + let s = Selection::default(); + let p = paint(); + let cx = ToolContext { revision: (1, 1), ..cx(&d, &s, &p) }; + let mut t = FillAreaTool::new(); + let started = super::fill_area::hover_searches_started(); + t.pointer(&cx, &ev(PointerKind::Move, 500.0, 500.0)); + assert!(t.waiting()); + t.pointer(&cx, &ev(PointerKind::Move, 510.0, 500.0)); + let acts = t.pointer(&cx, &ev(PointerKind::Down, 300.0, 300.0)); + assert!(matches!(acts.as_slice(), [Action::Begin(_), Action::Preview(id, _)] if id == "shapeBuilder.copyRegion"), "{acts:?}"); + assert!(!t.waiting()); + assert_eq!(super::fill_area::hover_searches_started() - started, 1, "only the search that was running"); +} + #[test] fn create_builds_the_three_tools() { for id in ["shapeBuilder", "livePaintBucket", "livePaintSelection"] {