mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-05 13:32:45 -05:00
Scripting: a self-crossing clip path counts as enclosing an area
The clip-outline check behind `pathItems.removeAll()` measured each subpath by its signed area. The loops of a figure eight turn opposite ways and cancel, so the check treated the outline as enclosing nothing, although both fill rules fill it. A subpath whose signed area is within roundoff of zero is now measured by the area it fills under nonzero. A subpath whose points all lie on one line still encloses nothing.
This commit is contained in:
1 parent
971bc00f8d
commit
fc84b657d3
1 file changed
+55
-3
@@ -869,7 +869,9 @@ fn items_remove_all(h: &super::Host, a: &[Data]) -> Answer {
|
||||
|
||||
/// Whether a subpath of `outline` encloses an area. Each subpath's area is measured from its first
|
||||
/// point, so its position does not change the result, and an area within roundoff of its own
|
||||
/// bounding box's is counted as none.
|
||||
/// bounding box's is counted as none. A subpath that crosses itself, such as a figure eight whose
|
||||
/// loops turn opposite ways and cancel in its signed area, is measured by the area it fills under
|
||||
/// nonzero, which every loop counts toward.
|
||||
fn encloses(outline: &kurbo::BezPath) -> bool {
|
||||
let mut subpaths: Vec<kurbo::BezPath> = Vec::new();
|
||||
for el in outline.elements() {
|
||||
@@ -883,11 +885,35 @@ fn encloses(outline: &kurbo::BezPath) -> bool {
|
||||
subpaths.into_iter().any(|mut sub| {
|
||||
let Some(start) = sub.elements().iter().find_map(|e| e.end_point()) else { return false };
|
||||
sub.apply_affine(kurbo::Affine::translate(-start.to_vec2()));
|
||||
let area = kurbo::Shape::area(&sub).abs();
|
||||
area > 0.0 && area > 64.0 * f64::EPSILON * kurbo::Shape::bounding_box(&sub).area()
|
||||
let floor = 64.0 * f64::EPSILON * kurbo::Shape::bounding_box(&sub).area();
|
||||
let counts = |area: f64| area > 0.0 && area > floor;
|
||||
counts(kurbo::Shape::area(&sub).abs())
|
||||
|| (!collinear(&sub)
|
||||
&& vectorcraft_pathops::try_area(&vectorcraft_geom::PathData::from_bezpath(&sub), vectorcraft_geom::FillRule::NonZero)
|
||||
.is_ok_and(counts))
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether every point and control point of `sub` lies within roundoff of one straight line, as
|
||||
/// in a frame collapsed to a line, which encloses no area however the path operations snap it.
|
||||
fn collinear(sub: &kurbo::BezPath) -> bool {
|
||||
let points: Vec<kurbo::Point> = sub
|
||||
.elements()
|
||||
.iter()
|
||||
.flat_map(|e| match *e {
|
||||
kurbo::PathEl::MoveTo(p) | kurbo::PathEl::LineTo(p) => vec![p],
|
||||
kurbo::PathEl::QuadTo(a, p) => vec![a, p],
|
||||
kurbo::PathEl::CurveTo(a, b, p) => vec![a, b, p],
|
||||
kurbo::PathEl::ClosePath => vec![],
|
||||
})
|
||||
.collect();
|
||||
let Some(&first) = points.first() else { return true };
|
||||
let Some(&far) = points.iter().max_by(|a, b| a.distance(first).total_cmp(&b.distance(first))) else { return true };
|
||||
let axis = far - first;
|
||||
let length = axis.hypot();
|
||||
length == 0.0 || points.iter().all(|p| axis.cross(*p - first).abs() <= 64.0 * f64::EPSILON * length * length)
|
||||
}
|
||||
|
||||
/// Whether `outline` fills an area under `rule`: a subpath encloses an area ([`encloses`]) and the
|
||||
/// outline with its overlaps resolved, thin strips included, has an area. An outline the path
|
||||
/// operations refuse is judged by [`encloses`] alone.
|
||||
@@ -2536,6 +2562,32 @@ mod tests {
|
||||
assert_eq!(anchors, vec![(0, 1), (0, 3)], "the undo puts back the selection the write changed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_figure_eight_clip_outline_fills_its_loops() {
|
||||
let mut eight = kurbo::BezPath::new();
|
||||
eight.move_to((0.0, 0.0));
|
||||
eight.line_to((10.0, 10.0));
|
||||
eight.line_to((10.0, 0.0));
|
||||
eight.line_to((0.0, 10.0));
|
||||
eight.close_path();
|
||||
assert!(kurbo::Shape::area(&eight).abs() < 1e-9, "the loops cancel in the signed area");
|
||||
assert!(encloses(&eight), "each loop of the figure eight encloses an area");
|
||||
assert!(fills(&eight, vectorcraft_geom::FillRule::NonZero));
|
||||
assert!(fills(&eight, vectorcraft_geom::FillRule::EvenOdd));
|
||||
let mut line = kurbo::BezPath::new();
|
||||
line.move_to((0.0, 0.0));
|
||||
line.line_to((10.0, 10.0));
|
||||
line.close_path();
|
||||
assert!(!encloses(&line), "a closed line still encloses nothing");
|
||||
let mut flat = kurbo::BezPath::new();
|
||||
flat.move_to((0.0, 0.0));
|
||||
flat.line_to((1000.0, 0.001));
|
||||
flat.line_to((2000.0, 0.002));
|
||||
flat.line_to((1000.0, 0.001));
|
||||
flat.close_path();
|
||||
assert!(!encloses(&flat), "a frame collapsed onto a shallow diagonal encloses nothing");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_made_id_above_two_to_the_53_stays_exact() {
|
||||
let made = |v: Value| made_id(&(Data::Undefined, v));
|
||||
|
||||
Reference in new issue
Block a user