mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-20 01:14:23 +01:00
Merge pull request #6672 from dotCooCoo/fix/simplify-flat-loop-split-limit
Simplify: a flat closed loop keeps its outline when splitting reaches its limit
This commit is contained in:
2 files changed
+130
-9
No files matched your search
@@ -88,7 +88,10 @@ pub fn simplify_with(path: &PathData, opts: &SimplifyOptions) -> PathData {
|
||||
/// whose refit is one segment, or lines that enclose no area, is refitted in two parts, cut at the
|
||||
/// point farthest from its start, or in three when two still give that. When three parts still give
|
||||
/// no area, straight lines follow the subpath within the tolerance, and without straight lines it
|
||||
/// stays as it is, as does one of a single segment; one that is all in line keeps the refit.
|
||||
/// stays as it is, as does one of a single segment. When no lines within the limits on pieces enclose
|
||||
/// an area, it stays as it is, or with straight lines follows the lines between samples of each
|
||||
/// curved segment when the limits leave room for lines within the tolerance; one that is all in line
|
||||
/// keeps the refit.
|
||||
fn simplify_subpath(sp: &SubPath, opts: &SimplifyOptions) -> Option<SubPath> {
|
||||
let mut segs = subpath_segs(sp);
|
||||
let n = segs.len();
|
||||
@@ -139,33 +142,82 @@ fn simplify_subpath(sp: &SubPath, opts: &SimplifyOptions) -> Option<SubPath> {
|
||||
}
|
||||
let ctrl: Vec<Point> = segs.iter().flat_map(|s| [s.c.p0, s.c.p1, s.c.p2]).collect();
|
||||
if flat(&result) && !in_line(&ctrl) {
|
||||
// Each segment split in 2, 4, 8 and so on, up to 1,024 pieces and 65,536 in all, until every
|
||||
// piece is within the tolerance of the line between its ends and those lines enclose an
|
||||
// area: with straight lines they are the result, and without them the loop stays as it is.
|
||||
// A loop whose points are all in line keeps the lines of the refit.
|
||||
// Each curved segment split in 2, 4, 8 and so on, up to 1,024 pieces and 65,536 pieces in all
|
||||
// with the line segments, until every piece is within the tolerance of the line between its
|
||||
// ends and those lines enclose an area: with straight lines they are the result, and without
|
||||
// them the loop stays as it is. When no split within those limits encloses an area, the loop
|
||||
// stays as it is, or with straight lines each curved segment becomes the lines between its
|
||||
// samples, up to `MAX_RUN_RESULT` lines in all with the line segments; when those do not keep
|
||||
// some curved segment within the tolerance, the loop stays as it is. A loop whose points are
|
||||
// all in line keeps the lines of the refit.
|
||||
let near = |c: &CubicBez| [c.p1, c.p2].iter().all(|&p| Line::new(c.p0, c.p3).nearest(p, 1e-9).distance_sq.sqrt() <= opts.tolerance);
|
||||
let curved = segs.iter().filter(|s| !s.line).count();
|
||||
for k in 1..=10u32 {
|
||||
let parts = 1u32 << k;
|
||||
if segs.len().saturating_mul(parts as usize) > 1 << 16 {
|
||||
if curved.saturating_mul(parts as usize).saturating_add(n - curved) > 1 << 16 {
|
||||
break;
|
||||
}
|
||||
let at = |i: u32| f64::from(i) / f64::from(parts);
|
||||
let pieces: Vec<CubicBez> = segs.iter().flat_map(|s| (0..parts).map(move |i| s.c.subsegment(at(i)..at(i + 1)))).collect();
|
||||
let pieces: Vec<CubicBez> = segs
|
||||
.iter()
|
||||
.flat_map(|s| {
|
||||
let count = if s.line { 1 } else { parts };
|
||||
(0..count).map(move |i| if s.line { s.c } else { s.c.subsegment(at(i)..at(i + 1)) })
|
||||
})
|
||||
.collect();
|
||||
let lines: Vec<Seg> = pieces.iter().map(|c| Seg::line(c.p0, c.p3)).collect();
|
||||
if pieces.iter().all(near) && !flat(&lines) {
|
||||
if !opts.straight_lines {
|
||||
return Some(sp.clone());
|
||||
}
|
||||
result = lines;
|
||||
break;
|
||||
return segs_to_subpath(&lines, true);
|
||||
}
|
||||
}
|
||||
if !opts.straight_lines {
|
||||
return Some(sp.clone());
|
||||
}
|
||||
let Some(room) = sample_room(curved, n - curved) else {
|
||||
return Some(sp.clone());
|
||||
};
|
||||
let mut lines: Vec<Seg> = Vec::new();
|
||||
for s in &segs {
|
||||
if s.line {
|
||||
lines.push(Seg::line(s.c.p0, s.c.p3));
|
||||
continue;
|
||||
}
|
||||
let Some(steps) = steps_within(&s.c, opts.tolerance.max(1e-6), room) else {
|
||||
return Some(sp.clone());
|
||||
};
|
||||
let mut pts = Vec::new();
|
||||
sample(&s.c, steps, &mut pts, true);
|
||||
lines.extend(pts.windows(2).map(|p| Seg::line(p[0], p[1])));
|
||||
}
|
||||
return segs_to_subpath(&lines, true);
|
||||
}
|
||||
}
|
||||
let s = segs_to_subpath(&result, sp.closed)?;
|
||||
Some(if opts.straight_lines { s } else { no_worse(sp, s) })
|
||||
}
|
||||
|
||||
/// Most lines between samples of each of `curved` segments, so that they and the `kept` line segments
|
||||
/// make at most [`MAX_RUN_RESULT`] lines, and each has at most [`MAX_SEGMENT_SAMPLES`]. `None` when
|
||||
/// that leaves no line for some curved segment.
|
||||
fn sample_room(curved: usize, kept: usize) -> Option<usize> {
|
||||
let left = MAX_RUN_RESULT.checked_sub(kept)?;
|
||||
if curved > left {
|
||||
return None;
|
||||
}
|
||||
Some((left / curved.max(1)).min(MAX_SEGMENT_SAMPLES))
|
||||
}
|
||||
|
||||
/// How many evenly spaced lines along `c`, at most `room`, follow it: [`sample_steps`] when that
|
||||
/// fits, or fewer, but never fewer than keep `c` within `tol` of them. `None` when those do not fit
|
||||
/// in `room`.
|
||||
fn steps_within(c: &CubicBez, tol: f64, room: usize) -> Option<usize> {
|
||||
let need = line_steps(c, tol).max(1);
|
||||
(need <= room).then(|| sample_steps(c, tol).min(room).max(need))
|
||||
}
|
||||
|
||||
/// How far a segment may be from the lines between its samples, as a share of the tolerance.
|
||||
const SAMPLE_GAP: f64 = 0.005;
|
||||
|
||||
@@ -1130,6 +1182,39 @@ mod tests {
|
||||
assert!(work <= 400 * pts.len(), "{work} steps for {} samples", pts.len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_samples_of_a_flat_loop_and_its_line_segments_stay_within_the_result_limit() {
|
||||
for (curved, kept) in [(1, 0), (32, 1), (40, 1_000_000), (1_000, MAX_RUN_RESULT - 1_000), (3, MAX_RUN_RESULT - 7)] {
|
||||
let room = sample_room(curved, kept).unwrap_or_else(|| panic!("{curved} curved, {kept} kept: no room"));
|
||||
assert!((1..=MAX_SEGMENT_SAMPLES).contains(&room), "{curved} curved, {kept} kept: room {room}");
|
||||
assert!(curved * room + kept <= MAX_RUN_RESULT, "{curved} curved, {kept} kept: room {room}");
|
||||
}
|
||||
assert_eq!(sample_room(32, 0), Some(MAX_SEGMENT_SAMPLES));
|
||||
assert_eq!(sample_room(2, MAX_RUN_RESULT - 1), None);
|
||||
assert_eq!(sample_room(1, MAX_RUN_RESULT + 5), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lines_with_too_little_room_for_the_tolerance_are_not_used() {
|
||||
let c = CubicBez::new((0.0, 0.0), (0.0, 100.0), (100.0, 100.0), (100.0, 0.0));
|
||||
let tol = 0.01;
|
||||
let need = line_steps(&c, tol);
|
||||
assert!(need > 4, "{need}");
|
||||
assert_eq!(steps_within(&c, tol, need - 1), None);
|
||||
for room in [need, need + 3, MAX_SEGMENT_SAMPLES] {
|
||||
let steps = steps_within(&c, tol, room).unwrap_or_else(|| panic!("room {room}: no steps"));
|
||||
assert!((need..=room).contains(&steps), "room {room}: {steps} steps");
|
||||
let mut pts = Vec::new();
|
||||
sample(&c, steps, &mut pts, true);
|
||||
let lines: Vec<Line> = pts.windows(2).map(|p| Line::new(p[0], p[1])).collect();
|
||||
for i in 0..=1000 {
|
||||
let p = c.eval(f64::from(i) / 1000.0);
|
||||
let gap = lines.iter().map(|l| l.nearest(p, 1e-9).distance_sq.sqrt()).fold(f64::INFINITY, f64::min);
|
||||
assert!(gap <= tol + 1e-9, "room {room}, {steps} steps: {gap} at {i}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn smoothing_points_whose_arc_length_overflows_returns_every_point() {
|
||||
let pts: Vec<Point> = (0..40).map(|i| Point::new(if i % 2 == 0 { -1e308 } else { 1e308 }, f64::from(i))).collect();
|
||||
|
||||
@@ -479,6 +479,42 @@ fn simplify_keeps_a_closed_subpath_that_refits_to_one_curve() {
|
||||
assert!(max_deviation(&long, &s) <= 0.1 + 1e-6, "{s:?}");
|
||||
}
|
||||
|
||||
/// An S curve closed by a straight edge of 20,000 anchors, whose curve shows only when it is split in
|
||||
/// four, keeps its outline within the tolerance. With corner anchors on the edge, only the curve is
|
||||
/// split. With smooth anchors whose handles lie along the edge, four pieces of every segment pass the
|
||||
/// limit on pieces: the loop stays as it is, or with straight lines follows lines between samples.
|
||||
#[test]
|
||||
fn simplify_keeps_a_flat_loop_with_many_anchors_on_its_edge() {
|
||||
let n = 20_000;
|
||||
let step = 100.0 / f64::from(n);
|
||||
let curve_anchors = vec![
|
||||
Anchor::with_handles(Point::new(0.0, 0.0), Point::new(0.0, 0.0), Point::new(0.0, 3.0)),
|
||||
Anchor::with_handles(Point::new(100.0, 0.0), Point::new(0.0, -3.0), Point::new(100.0, 0.0)),
|
||||
];
|
||||
let curve = PathData::single(SubPath::new(curve_anchors.clone(), false));
|
||||
for smooth_edge in [false, true] {
|
||||
let mut anchors = curve_anchors.clone();
|
||||
anchors.extend((1..n).map(|i| {
|
||||
let p = Point::new(100.0 - f64::from(i) * step, 0.0);
|
||||
if smooth_edge {
|
||||
Anchor::with_handles(p, Point::new(p.x + step / 3.0, 0.0), Point::new(p.x - step / 3.0, 0.0))
|
||||
} else {
|
||||
Anchor::corner(p)
|
||||
}
|
||||
}));
|
||||
let path = PathData::single(SubPath::new(anchors, true));
|
||||
for straight_lines in [false, true] {
|
||||
let s = simplify_with(&path, &SimplifyOptions { tolerance: 4.0, straight_lines, ..Default::default() });
|
||||
let at = format!("smooth edge {smooth_edge}, straight lines {straight_lines}");
|
||||
assert_eq!(s.subpaths.len(), 1, "{at}");
|
||||
let sp = &s.subpaths[0];
|
||||
assert!(sp.closed && sp.area().abs() > 1.0, "{at}: area {}", sp.area());
|
||||
assert!(max_deviation(&curve, &s) <= 4.0 + 1e-6, "{at}: {}", max_deviation(&curve, &s));
|
||||
assert!(!straight_lines || (0..sp.segment_count()).all(|i| sp.segment_is_line(i)), "{at}: a curve is left");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The Simplify slider's levels: 1 pt at 50, doubled for every 10 levels down and halved for every
|
||||
/// 10 up; Auto-Simplify takes the lowest level whose tolerance is at most 1/400 of the size, and
|
||||
/// level 100 for a size under 12.5 pt.
|
||||
|
||||
Reference in new issue
Block a user