From 30ff8bef31b45acbcbb9987b385ed1666bfe9389 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 2 Dec 2026 13:08:30 -0800 Subject: [PATCH] Marquee: skip paths whose control box lies apart from the marquee hit::intersects_rect measured every path's exact bounds before deciding it missed the marquee. Since the anchors became chunked (#7814), that walk costs about three times as much, and a marquee over many paths became about 1.8x slower than in 0.24.1. It now first compares the path's cached control box with the rect and returns false when the two lie strictly apart. A control box that only touches the rect, or has no area, still goes to the exact checks. On a 50,000-path document a 400x300 marquee takes 8.2 to 10.6 ms instead of 46 to 64 ms, and selects the same 3,675 paths. --- crates/geom/src/hit.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/crates/geom/src/hit.rs b/crates/geom/src/hit.rs index 0358acac90..fea760b548 100644 --- a/crates/geom/src/hit.rs +++ b/crates/geom/src/hit.rs @@ -118,6 +118,14 @@ pub fn stroke_contains(path: &BezPath, width: f64, tol: f64, p: Point) -> bool { /// filled shape doesn't touch it, so a marquee drawn over a background picks only what is on it /// (#1103). pub fn intersects_rect(path: &PathData, r: Rect) -> bool { + // The control box holds every anchor and segment, and the path keeps it measured, so a path + // whose control box lies wholly apart from the rect misses it without measuring its segments. + // The test is strict: a control box that only touches the rect, or has no area, goes on to the + // exact checks below. + let (Some(c), n) = (path.control_bounds(), r.abs()) else { return false }; + if c.x1 < n.x0 || n.x1 < c.x0 || c.y1 < n.y0 || n.y1 < c.y0 { + return false; + } let Some(b) = path.bounds() else { return false }; let degenerate = |x: Rect| x.width() == 0.0 || x.height() == 0.0; if b.intersect(r).area() <= 0.0 && !degenerate(b) && !degenerate(r) { @@ -238,6 +246,30 @@ mod tests { // zero-height marquee missing it assert!(!intersects_rect(&p, Rect::new(20.0, 5.0, 30.0, 5.0))); } + + /// A marquee that lies apart from a path's control box misses it without measuring the path's + /// segments. One that reaches the control box but not the curve is still judged on the curve. + #[test] + fn marquee_apart_from_the_control_box_measures_no_segment() { + let p = shapes::ellipse(Rect::new(0.0, 0.0, 10.0, 10.0)); + let measured = crate::path::measured_segments; + let before = measured(); + assert!(!intersects_rect(&p, Rect::new(20.0, 20.0, 30.0, 30.0))); + assert!(!intersects_rect(&p, Rect::new(-30.0, 4.0, -20.0, 6.0))); + assert_eq!(measured(), before, "a marquee apart from the control box measures nothing"); + // A curve whose handle reaches into the marquee while the curve stays below it. + use crate::path::{Anchor, SubPath}; + let arch = PathData::new(vec![SubPath::new( + vec![ + Anchor::with_handles(Point::new(0.0, 0.0), Point::new(0.0, 0.0), Point::new(0.0, 40.0)), + Anchor::with_handles(Point::new(10.0, 0.0), Point::new(10.0, 40.0), Point::new(10.0, 0.0)), + ], + false, + )]); + assert!(!intersects_rect(&arch, Rect::new(-1.0, 35.0, 11.0, 39.0)), "the curve peaks at y 30"); + assert!(measured() > before, "a marquee inside the control box measures the curve"); + assert!(intersects_rect(&arch, Rect::new(4.0, 25.0, 6.0, 35.0)), "a marquee across the peak"); + } } #[cfg(test)]