mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-10-25 10:17:02 -06:00
117 lines
4.4 KiB
Rust
117 lines
4.4 KiB
Rust
//! Hit testing primitives.
|
|
|
|
use kurbo::{BezPath, ParamCurveNearest, Point, Rect, Shape};
|
|
|
|
use crate::path::{FillRule, PathData};
|
|
|
|
/// Is `p` inside the filled area of `path` under `rule`? Open subpaths are implicitly closed (as when filled).
|
|
pub fn fill_contains(path: &BezPath, rule: FillRule, p: Point) -> bool {
|
|
let w = path.winding(p);
|
|
match rule {
|
|
FillRule::NonZero => w != 0,
|
|
FillRule::EvenOdd => w % 2 != 0,
|
|
}
|
|
}
|
|
|
|
/// 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()
|
|
}
|
|
|
|
/// 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
|
|
}
|
|
|
|
/// Does the path intersect (or lie inside) the rect? Used for marquee selection.
|
|
pub fn intersects_rect(path: &PathData, r: Rect) -> bool {
|
|
let Some(b) = path.bounds() else { return false };
|
|
if b.intersect(r).area() <= 0.0 && !(b.width() == 0.0 || b.height() == 0.0) {
|
|
return false;
|
|
}
|
|
// Any anchor inside?
|
|
if path.anchors().any(|(_, _, a)| r.contains(a.p)) {
|
|
return true;
|
|
}
|
|
// Any segment crossing the rect edges?
|
|
let edges = [
|
|
kurbo::Line::new((r.x0, r.y0), (r.x1, r.y0)),
|
|
kurbo::Line::new((r.x1, r.y0), (r.x1, r.y1)),
|
|
kurbo::Line::new((r.x1, r.y1), (r.x0, r.y1)),
|
|
kurbo::Line::new((r.x0, r.y1), (r.x0, r.y0)),
|
|
];
|
|
let bp = path.to_bezpath();
|
|
for seg in bp.segments() {
|
|
for e in &edges {
|
|
if !seg.intersect_line(*e).is_empty() {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
// Rect entirely inside a closed shape counts as touching it.
|
|
path.is_closed() && fill_contains(&bp, FillRule::NonZero, r.center())
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::shapes;
|
|
|
|
#[test]
|
|
fn fill_and_stroke() {
|
|
let p = shapes::rectangle(Rect::new(0.0, 0.0, 10.0, 10.0)).to_bezpath();
|
|
assert!(fill_contains(&p, FillRule::NonZero, Point::new(5.0, 5.0)));
|
|
assert!(!fill_contains(&p, FillRule::NonZero, Point::new(15.0, 5.0)));
|
|
assert!(stroke_contains(&p, 2.0, 0.0, Point::new(10.5, 5.0)));
|
|
assert!(!stroke_contains(&p, 2.0, 0.0, Point::new(12.0, 5.0)));
|
|
}
|
|
|
|
#[test]
|
|
fn evenodd_hole() {
|
|
let mut outer = shapes::rectangle(Rect::new(0.0, 0.0, 10.0, 10.0));
|
|
outer.subpaths.extend(shapes::rectangle(Rect::new(3.0, 3.0, 7.0, 7.0)).subpaths);
|
|
let bp = outer.to_bezpath();
|
|
assert!(!fill_contains(&bp, FillRule::EvenOdd, Point::new(5.0, 5.0)));
|
|
assert!(fill_contains(&bp, FillRule::NonZero, Point::new(5.0, 5.0)));
|
|
assert!(fill_contains(&bp, FillRule::EvenOdd, Point::new(1.0, 5.0)));
|
|
}
|
|
|
|
#[test]
|
|
fn marquee() {
|
|
let p = shapes::ellipse(Rect::new(0.0, 0.0, 10.0, 10.0));
|
|
assert!(intersects_rect(&p, Rect::new(-1.0, 4.0, 1.0, 6.0)));
|
|
assert!(!intersects_rect(&p, Rect::new(20.0, 20.0, 30.0, 30.0)));
|
|
// corner region of the bbox that misses the ellipse outline
|
|
assert!(!intersects_rect(&p, Rect::new(0.0, 0.0, 0.5, 0.5)));
|
|
// fully inside
|
|
assert!(intersects_rect(&p, Rect::new(4.0, 4.0, 6.0, 6.0)));
|
|
// crossing a line
|
|
let l = shapes::line(Point::new(0.0, 0.0), Point::new(10.0, 10.0));
|
|
assert!(intersects_rect(&l, Rect::new(4.0, 0.0, 6.0, 10.0)));
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod regress_tests {
|
|
use super::*;
|
|
use kurbo::{CubicBez, ParamCurve};
|
|
|
|
/// A cubic whose handles sit on its anchors on one side (cusp-like): points on the curve are
|
|
/// at distance ~0 (proptest regression).
|
|
#[test]
|
|
fn nearest_on_degenerate_cubic() {
|
|
let c = CubicBez::new(
|
|
(131.69052599341336, 172.64518298885875),
|
|
(104.00879084385166, 172.64518298885875),
|
|
(145.71215277397286, 141.82358989609716),
|
|
(131.4188160942415, 162.38678549385997),
|
|
);
|
|
let mut bp = BezPath::new();
|
|
bp.move_to(c.p0);
|
|
bp.curve_to(c.p1, c.p2, c.p3);
|
|
let worst = (0..=200).map(|i| distance_to_outline(&bp, c.eval(i as f64 / 200.0))).fold(0.0, f64::max);
|
|
assert!(worst < 1e-6, "{worst}");
|
|
}
|
|
}
|