mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-24 23:50:13 +00:00
Fill Area: a left-out curve cuts an area only where it reaches it
A segment's box can meet an area while the segment does not, as with a diagonal line or a curve that arcs around the area. The finder now halves such a segment until a piece has an end in the area, its box misses it, or it is smaller than 0.01 pt.
This commit is contained in:
@@ -316,30 +316,37 @@ fn fill_area_does_not_fill_across_a_huge_path_it_left_out() {
|
||||
}
|
||||
}
|
||||
|
||||
/// A path with too many segments whose subpaths lie far apart on either side of the square has a box
|
||||
/// that holds the square, but no segment of it comes near: it is left out and the square is filled.
|
||||
/// A path with too many segments whose box holds the square, but no segment of it comes near: its
|
||||
/// subpaths lie far apart on either side of the square, or one curve arcs around the square and its
|
||||
/// box holds the square. It is left out and the square is filled.
|
||||
#[test]
|
||||
fn fill_area_fills_an_area_that_a_left_out_paths_box_holds_but_its_segments_miss() {
|
||||
let mut s = session();
|
||||
line(&mut s, (-20.0, 0.0), (420.0, 0.0));
|
||||
line(&mut s, (-20.0, 400.0), (420.0, 400.0));
|
||||
line(&mut s, (0.0, -20.0), (0.0, 420.0));
|
||||
line(&mut s, (400.0, -20.0), (400.0, 420.0));
|
||||
let n = vectorcraft_pathops::SHAPE_BUILDER_MAX_SEGMENTS;
|
||||
let pts: Vec<Point> = (0..=n).map(|k| Point::new(-2000.0 + 400.0 * k as f64 / n as f64, -1000.0 + (k % 2) as f64)).collect();
|
||||
let far = [Point::new(2000.0, 1500.0), Point::new(2100.0, 1500.0)];
|
||||
let path = PathData::new(vec![vectorcraft_geom::SubPath::polyline(&pts, false), vectorcraft_geom::SubPath::polyline(&far, false)]);
|
||||
let look = vectorcraft_doc::Appearance::basic(Paint::None, Paint::solid(Color::BLACK), 1.0);
|
||||
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();
|
||||
deselect(&mut s);
|
||||
let r = s.execute("shapeBuilder.copyRegion", &json!({"point": [200, 200]})).unwrap();
|
||||
assert!((area(&s, id_of(&r)) - 160_000.0).abs() < 1.0, "{}", area(&s, id_of(&r)));
|
||||
// A cubic from (-1000, 1500) up over the square to (1400, 1500); at its top it is at y -750.
|
||||
let mut arc = vectorcraft_geom::SubPath::polyline(&[Point::new(-1000.0, 1500.0), Point::new(1400.0, 1500.0)], false);
|
||||
arc.anchors[0].h_out = Point::new(-1000.0, -1500.0);
|
||||
arc.anchors[1].h_in = Point::new(1400.0, -1500.0);
|
||||
let far = vectorcraft_geom::SubPath::polyline(&[Point::new(2000.0, 1500.0), Point::new(2100.0, 1500.0)], false);
|
||||
for second in [far, arc] {
|
||||
let mut s = session();
|
||||
line(&mut s, (-20.0, 0.0), (420.0, 0.0));
|
||||
line(&mut s, (-20.0, 400.0), (420.0, 400.0));
|
||||
line(&mut s, (0.0, -20.0), (0.0, 420.0));
|
||||
line(&mut s, (400.0, -20.0), (400.0, 420.0));
|
||||
let n = vectorcraft_pathops::SHAPE_BUILDER_MAX_SEGMENTS;
|
||||
let pts: Vec<Point> = (0..=n).map(|k| Point::new(-2000.0 + 400.0 * k as f64 / n as f64, -1000.0 + (k % 2) as f64)).collect();
|
||||
let path = PathData::new(vec![vectorcraft_geom::SubPath::polyline(&pts, false), second.clone()]);
|
||||
let look = vectorcraft_doc::Appearance::basic(Paint::None, Paint::solid(Color::BLACK), 1.0);
|
||||
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();
|
||||
deselect(&mut s);
|
||||
let r = s.execute("shapeBuilder.copyRegion", &json!({"point": [200, 200]})).unwrap();
|
||||
assert!((area(&s, id_of(&r)) - 160_000.0).abs() < 1.0, "{second:?}: {}", area(&s, id_of(&r)));
|
||||
}
|
||||
}
|
||||
|
||||
/// With Gap Detection on, a square on a higher layer within the gap of the area's corners does not
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
//! paths around the area stay as they are, and nothing needs to be selected.
|
||||
|
||||
use vectorcraft_geom::path::segment_bounds;
|
||||
use vectorcraft_geom::{ParamCurve, PathSeg};
|
||||
|
||||
use super::lookup::path_contains;
|
||||
use super::*;
|
||||
@@ -66,10 +67,31 @@ fn meet(a: &Rect, b: &Rect) -> bool {
|
||||
}
|
||||
|
||||
/// 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 does.
|
||||
/// 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| meet(&segment_bounds(seg), zone))
|
||||
vectorcraft_geom::kurbo::segments(bez.elements().iter().copied()).any(|seg| segment_reaches(seg, zone, 0))
|
||||
}
|
||||
|
||||
/// 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.
|
||||
const REACH_SPLITS: u32 = 24;
|
||||
|
||||
/// The side (pt) below which a piece of a segment whose box meets the zone counts as meeting 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 {
|
||||
let b = segment_bounds(seg);
|
||||
if !meet(&b, zone) {
|
||||
return false;
|
||||
}
|
||||
if holds(zone, seg.start()) || holds(zone, seg.end()) || depth >= REACH_SPLITS || b.width().max(b.height()) < REACH_TOLERANCE {
|
||||
return true;
|
||||
}
|
||||
segment_reaches(seg.subsegment(0.0..0.5), zone, depth + 1) || segment_reaches(seg.subsegment(0.5..1.0), zone, depth + 1)
|
||||
}
|
||||
|
||||
/// Does `outer` hold all of `inner`?
|
||||
|
||||
Reference in New Issue
Block a user