mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-21 09:54:09 +01:00
Retype: a contour reaching outside a concave one is no counter of it, and a rejected split gives way to another
A contour counts as lying inside another only when up to eight of its anchor points do. When the paint check rejects the split with the longest runs, the split with the shortest runs is tried before the shape stays an outline.
This commit is contained in:
1 parent
8158107e82
commit
fe7d225d6f
3 files changed
+110
-31
No files matched your search
@@ -1210,39 +1210,51 @@ fn split_parts<M>(groups: &[BezPath], find: &dyn Fn(&BezPath) -> Vec<M>, togethe
|
||||
return vec![];
|
||||
}
|
||||
let g = groups.len();
|
||||
// From the last group back: the run that starts at each group, the longest that matches and
|
||||
// leaves a rest that can all be matched, with its candidates. Runs past such a start are
|
||||
// never tried for one whose rest cannot be matched.
|
||||
let mut chosen: Vec<Option<(usize, Vec<M>)>> = (0..g).map(|_| None).collect();
|
||||
for i in (0..g).rev() {
|
||||
for n in (1..=MAX_RUN.min(g - i)).rev() {
|
||||
let rest_ok = i + n == g || chosen.get(i + n).is_some_and(Option::is_some);
|
||||
if !rest_ok {
|
||||
continue;
|
||||
// The split with the longest runs first; when `together` rejects it, the one with the shortest
|
||||
// runs, which is the other end of the complete splits.
|
||||
for longest in [true, false] {
|
||||
// From the last group back: the run that starts at each group, the longest (or shortest)
|
||||
// that matches and leaves a rest that can all be matched, with its candidates. Runs past
|
||||
// such a start are never tried for one whose rest cannot be matched.
|
||||
let mut chosen: Vec<Option<(usize, Vec<M>)>> = (0..g).map(|_| None).collect();
|
||||
for i in (0..g).rev() {
|
||||
let mut lens: Vec<usize> = (1..=MAX_RUN.min(g - i)).collect();
|
||||
if longest {
|
||||
lens.reverse();
|
||||
}
|
||||
let Some(run) = groups.get(i..i + n) else { continue };
|
||||
let cands = find(&joined(run));
|
||||
if !cands.is_empty() {
|
||||
if let Some(c) = chosen.get_mut(i) {
|
||||
*c = Some((n, cands));
|
||||
for n in lens {
|
||||
let rest_ok = i + n == g || chosen.get(i + n).is_some_and(Option::is_some);
|
||||
if !rest_ok {
|
||||
continue;
|
||||
}
|
||||
let Some(run) = groups.get(i..i + n) else { continue };
|
||||
let cands = find(&joined(run));
|
||||
if !cands.is_empty() {
|
||||
if let Some(c) = chosen.get_mut(i) {
|
||||
*c = Some((n, cands));
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
let mut out = vec![];
|
||||
let mut runs = vec![];
|
||||
let mut i = 0;
|
||||
while i < g {
|
||||
let Some(Some((n, cands))) = chosen.get_mut(i).map(Option::take) else { return vec![] };
|
||||
out.push(cands);
|
||||
runs.push(groups.get(i..i + n).map(joined).unwrap_or_default());
|
||||
i += n;
|
||||
}
|
||||
if out.len() == 1 || together(&runs) {
|
||||
return out;
|
||||
}
|
||||
// Every run a single group: the other order gives the same split.
|
||||
if out.len() == g {
|
||||
break;
|
||||
}
|
||||
}
|
||||
let mut out = vec![];
|
||||
let mut runs = vec![];
|
||||
let mut i = 0;
|
||||
while i < g {
|
||||
let Some(Some((n, cands))) = chosen.get_mut(i).map(Option::take) else { return vec![] };
|
||||
out.push(cands);
|
||||
runs.push(groups.get(i..i + n).map(joined).unwrap_or_default());
|
||||
i += n;
|
||||
}
|
||||
if out.len() > 1 && !together(&runs) {
|
||||
return vec![];
|
||||
}
|
||||
out
|
||||
vec![]
|
||||
}
|
||||
|
||||
/// Contour groups joined into one path.
|
||||
|
||||
@@ -1312,6 +1312,31 @@ fn a_split_takes_a_shorter_run_when_the_longest_leaves_a_group_unmatched() {
|
||||
assert!(super::parts(&p, &groups, &none, &|_, _| true).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_split_the_paint_check_rejects_gives_way_to_another_complete_split() {
|
||||
use kurbo::Shape;
|
||||
let mut p = kurbo::BezPath::new();
|
||||
for x in [0.0, 100.0, 200.0] {
|
||||
p.extend(kurbo::Rect::new(x, 0.0, x + 40.0, 40.0).path_elements(0.1));
|
||||
}
|
||||
let find = |path: &kurbo::BezPath, part: bool, _near: bool| -> Vec<&'static str> {
|
||||
let b = path.bounding_box();
|
||||
match (part, b.x0.round() as i64, b.x1.round() as i64) {
|
||||
(true, 0, 140) => vec!["AB"],
|
||||
(true, 0, 40) => vec!["A"],
|
||||
(true, 100, 140) => vec!["B"],
|
||||
(true, 200, 240) => vec!["C"],
|
||||
_ => vec![],
|
||||
}
|
||||
};
|
||||
let groups = all_groups(&p);
|
||||
// The longest runs first (AB, C) are rejected; the three single glyphs are kept.
|
||||
let singles = |runs: &[kurbo::BezPath], _near: bool| runs.len() == 3;
|
||||
assert_eq!(super::parts(&p, &groups, &find, &singles), vec![vec!["A"], vec!["B"], vec!["C"]]);
|
||||
// When no complete split is accepted, nothing.
|
||||
assert!(super::parts(&p, &groups, &find, &|_, _| false).is_empty());
|
||||
}
|
||||
|
||||
/// `path`'s contour groups, with no limit on the contours grouped.
|
||||
fn all_groups(path: &kurbo::BezPath) -> Vec<kurbo::BezPath> {
|
||||
let mut budget = usize::MAX;
|
||||
|
||||
@@ -829,6 +829,9 @@ fn fits_within(results: Vec<((f64, f64), f64)>, limit: f64) -> Vec<((f64, f64),
|
||||
/// Rounds of pairing and fitting [`near_fit`] gives each turn, at most.
|
||||
const REFITS: usize = 4;
|
||||
|
||||
/// Anchor points of a contour [`clusters`] tests against a candidate container, at most.
|
||||
const MAX_CONTAINMENT_PROBES: usize = 8;
|
||||
|
||||
/// `path`'s contours in groups that can each be one glyph: a contour with the contours that lie
|
||||
/// inside it (a letter's counters, and what lies inside those), in the order the outer contours
|
||||
/// come. A path of several glyphs (a word outlined as one compound path) gives one group per glyph
|
||||
@@ -853,16 +856,33 @@ pub fn clusters(path: &BezPath) -> Vec<BezPath> {
|
||||
}
|
||||
}
|
||||
let boxes: Vec<kurbo::Rect> = subs.iter().map(Shape::bounding_box).collect();
|
||||
let starts: Vec<Option<Point>> = subs.iter().map(|s| s.elements().first().and_then(|e| e.end_point())).collect();
|
||||
// Up to MAX_CONTAINMENT_PROBES of each contour's anchor points, evenly spread: a contour lies in
|
||||
// another only when every one of them does.
|
||||
let probes: Vec<Vec<Point>> = subs
|
||||
.iter()
|
||||
.map(|s| {
|
||||
let anchors: Vec<Point> = s.elements().iter().filter_map(|e| e.end_point()).collect();
|
||||
let step = anchors.len().div_ceil(MAX_CONTAINMENT_PROBES).max(1);
|
||||
anchors.into_iter().step_by(step).collect()
|
||||
})
|
||||
.collect();
|
||||
// Each contour's container: the smallest contour around it.
|
||||
let parent: Vec<Option<usize>> = (0..subs.len())
|
||||
.map(|i| {
|
||||
let (Some(bi), Some(Some(p))) = (boxes.get(i), starts.get(i)) else { return None };
|
||||
let (Some(bi), Some(pi)) = (boxes.get(i), probes.get(i)) else { return None };
|
||||
if pi.is_empty() {
|
||||
return None;
|
||||
}
|
||||
(0..subs.len())
|
||||
.filter(|&j| j != i)
|
||||
.filter_map(|j| Some((j, boxes.get(j)?, subs.get(j)?)))
|
||||
.filter(|(_, bj, sj)| {
|
||||
bj.area() > bi.area() && bj.x0 <= bi.x0 && bj.y0 <= bi.y0 && bj.x1 >= bi.x1 && bj.y1 >= bi.y1 && sj.winding(*p) != 0
|
||||
bj.area() > bi.area()
|
||||
&& bj.x0 <= bi.x0
|
||||
&& bj.y0 <= bi.y0
|
||||
&& bj.x1 >= bi.x1
|
||||
&& bj.y1 >= bi.y1
|
||||
&& pi.iter().all(|p| sj.winding(*p) != 0)
|
||||
})
|
||||
.min_by(|a, b| a.1.area().total_cmp(&b.1.area()))
|
||||
.map(|(j, _, _)| j)
|
||||
@@ -2067,6 +2087,28 @@ mod tests {
|
||||
assert!(!narrow_contour(&wide, 3.0 * tol));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_contour_reaching_outside_a_concave_one_is_no_counter_of_it() {
|
||||
// An L, and a triangle that starts inside the L and inside its box but reaches outside it.
|
||||
let mut l = BezPath::new();
|
||||
for (i, (x, y)) in [(0.0, 0.0), (100.0, 0.0), (100.0, 20.0), (20.0, 20.0), (20.0, 100.0), (0.0, 100.0)].into_iter().enumerate() {
|
||||
if i == 0 { l.move_to((x, y)) } else { l.line_to((x, y)) }
|
||||
}
|
||||
l.close_path();
|
||||
let mut tri = BezPath::new();
|
||||
tri.move_to((5.0, 5.0));
|
||||
tri.line_to((90.0, 90.0));
|
||||
tri.line_to((5.0, 50.0));
|
||||
tri.close_path();
|
||||
let mut both = l.clone();
|
||||
both.extend(tri.iter());
|
||||
assert_eq!(clusters(&both).len(), 2);
|
||||
// A contour wholly inside stays with its container.
|
||||
let mut inside = l.clone();
|
||||
inside.extend(kurbo::Rect::new(5.0, 5.0, 15.0, 15.0).to_path(0.1).iter());
|
||||
assert_eq!(clusters(&inside).len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_hull_width_is_the_least_width_in_any_direction() {
|
||||
let strip = kurbo::Rect::new(-30.0, -5.0, 30.0, 5.0).to_path(0.1);
|
||||
|
||||
Reference in new issue
Block a user