mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-04 13:02:50 -05:00
Skip distant curves when testing outline hits (#10627)
Co-authored-by: FredApps <[email protected]>
This commit is contained in:
1 parent
406dac5645
commit
e83947b9af
1 file changed
+89
-4
+89
-4
@@ -2,7 +2,7 @@
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
use kurbo::{BezPath, ParamCurveNearest, PathEl, Point, Rect, Shape};
|
||||
use kurbo::{BezPath, ParamCurveNearest, PathEl, PathSeg, Point, Rect, Shape};
|
||||
|
||||
use crate::path::{FillRule, PathData};
|
||||
|
||||
@@ -104,13 +104,51 @@ fn closed(path: &BezPath) -> Cow<'_, BezPath> {
|
||||
|
||||
/// Distance from `p` to the nearest point on the path outline.
|
||||
pub fn distance_to_outline(path: &BezPath, p: Point) -> f64 {
|
||||
path.segments().map(|s| s.nearest(p, 1e-9).distance_sq).fold(f64::INFINITY, f64::min).sqrt()
|
||||
path.segments().map(|s| nearest_distance_sq(s, p)).fold(f64::INFINITY, f64::min).sqrt()
|
||||
}
|
||||
|
||||
/// Is `p` within `tol` of the path's stroke of width `width`?
|
||||
pub fn stroke_contains(path: &BezPath, width: f64, tol: f64, p: Point) -> bool {
|
||||
let quick = path.bounding_box().inflate(width / 2.0 + tol, width / 2.0 + tol);
|
||||
quick.contains(p) && distance_to_outline(path, p) <= width / 2.0 + tol
|
||||
let radius = width / 2.0 + tol;
|
||||
if !path.bounding_box().inflate(radius, radius).contains(p) {
|
||||
return false;
|
||||
}
|
||||
if !radius.is_finite() {
|
||||
return distance_to_outline(path, p) <= radius;
|
||||
}
|
||||
|
||||
// Keep candidates whose squared distance could underflow near a zero radius.
|
||||
let search_radius = radius.max(f64::MIN_POSITIVE.sqrt());
|
||||
path.segments().any(|segment| !curve_outside_reach(segment, p, search_radius) && nearest_distance_sq(segment, p).sqrt() <= radius)
|
||||
}
|
||||
|
||||
// A Bézier stays inside the bounds of its control points. A line projection is
|
||||
// already cheap, so only check the bounds before running a curve solver.
|
||||
fn curve_outside_reach(segment: PathSeg, p: Point, radius: f64) -> bool {
|
||||
let bounds = match segment {
|
||||
PathSeg::Line(_) => return false,
|
||||
PathSeg::Quad(q) => Rect::from_points(q.p0, q.p1).union_pt(q.p2),
|
||||
PathSeg::Cubic(c) => Rect::from_points(c.p0, c.p1).union_pt(c.p2).union_pt(c.p3),
|
||||
};
|
||||
outside_axis(p.x, radius, bounds.x0, bounds.x1) || outside_axis(p.y, radius, bounds.y0, bounds.y1)
|
||||
}
|
||||
|
||||
fn outside_axis(p: f64, radius: f64, low: f64, high: f64) -> bool {
|
||||
// Leave room for rounding in the curve evaluation and the distance comparison.
|
||||
let scale = p.abs().max(low.abs()).max(high.abs()).max(radius);
|
||||
let padded = radius + 8.0 * f64::EPSILON * scale;
|
||||
p < low - padded || p > high + padded
|
||||
}
|
||||
|
||||
fn nearest_distance_sq(segment: PathSeg, p: Point) -> f64 {
|
||||
#[cfg(test)]
|
||||
OUTLINE_NEAREST_CALLS.with(|count| count.set(count.get() + 1));
|
||||
segment.nearest(p, 1e-9).distance_sq
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
thread_local! {
|
||||
static OUTLINE_NEAREST_CALLS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
|
||||
}
|
||||
|
||||
/// Does the path touch the rect: an anchor inside it, or a segment crossing its edges? Used for
|
||||
@@ -185,6 +223,53 @@ mod tests {
|
||||
assert!(!stroke_contains(&p, 2.0, 0.0, Point::new(12.0, 5.0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stroke_hit_skips_distant_curves_without_changing_results() {
|
||||
let mut path = BezPath::new();
|
||||
for i in 0..96 {
|
||||
let x = f64::from(i) * 16.0;
|
||||
path.move_to((x, 0.0));
|
||||
path.curve_to((x + 1.0, 4.0), (x + 2.0, -4.0), (x + 3.0, 0.0));
|
||||
}
|
||||
path.move_to((512.0, 0.0));
|
||||
path.quad_to((513.0, 4.0), (514.0, 0.0));
|
||||
|
||||
for (point, expected_hit, expected_calls) in [(Point::new(512.0, 0.0), true, 1), (Point::new(520.0, 0.0), false, 0)] {
|
||||
assert_eq!(distance_to_outline(&path, point) <= 0.5, expected_hit);
|
||||
assert!(path.bounding_box().contains(point));
|
||||
OUTLINE_NEAREST_CALLS.with(|count| count.set(0));
|
||||
assert_eq!(stroke_contains(&path, 0.0, 0.5, point), expected_hit);
|
||||
assert_eq!(OUTLINE_NEAREST_CALLS.with(std::cell::Cell::get), expected_calls);
|
||||
}
|
||||
|
||||
assert_stroke_hits_at_cutoff();
|
||||
}
|
||||
|
||||
fn assert_stroke_hits_at_cutoff() {
|
||||
let mut path = BezPath::new();
|
||||
path.move_to((0.0, 0.0));
|
||||
path.line_to((0.0, 0.0));
|
||||
path.quad_to((4.0, 8.0), (8.0, 0.0));
|
||||
path.curve_to((12.0, 8.0), (16.0, -4.0), (20.0, 0.0));
|
||||
path.close_path();
|
||||
path.move_to((2.0, -3.0));
|
||||
path.line_to((2.0, -3.0));
|
||||
|
||||
for transform in [kurbo::Affine::IDENTITY, kurbo::Affine::new([-2.0, 1.0, 0.0, 1.0, 10.0, 1e8])] {
|
||||
let transformed = transform * path.clone();
|
||||
for x in -2..23 {
|
||||
for y in -5..9 {
|
||||
let point = transform * Point::new(f64::from(x), f64::from(y));
|
||||
let distance = distance_to_outline(&transformed, point);
|
||||
for radius in [distance.next_down(), distance, distance.next_up(), 0.0, 1.5] {
|
||||
let inside = transformed.bounding_box().inflate(radius, radius).contains(point);
|
||||
assert_eq!(stroke_contains(&transformed, 0.0, radius, point), inside && distance <= radius, "{point:?}, {radius}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn evenodd_hole() {
|
||||
let mut outer = shapes::rectangle(Rect::new(0.0, 0.0, 10.0, 10.0));
|
||||
|
||||
Reference in new issue
Block a user