diff --git a/Cargo.lock b/Cargo.lock index 165407081..f5b87bf7e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2293,6 +2293,7 @@ dependencies = [ "effectcraft-raster", "kurbo", "linesweeper", + "log", "tiny-skia 0.12.0", ] diff --git a/crates/path/Cargo.toml b/crates/path/Cargo.toml index 2e9bc85d9..122c37fe0 100644 --- a/crates/path/Cargo.toml +++ b/crates/path/Cargo.toml @@ -14,5 +14,6 @@ effectcraft-geom = { workspace = true } effectcraft-keyframe = { workspace = true } effectcraft-raster = { workspace = true } kurbo = { workspace = true } +log = { workspace = true } tiny-skia = { workspace = true } linesweeper = "0.5" diff --git a/crates/path/src/ops.rs b/crates/path/src/ops.rs index ba5349312..5c89ce6bc 100644 --- a/crates/path/src/ops.rs +++ b/crates/path/src/ops.rs @@ -351,14 +351,28 @@ fn round_sub(sub: &Sub, r: f64, out: &mut BezPath) { /// join. `copies` copies are made, copy *i* (0-based) offset by `amount · (i + 1 + copy_offset)`, /// all returned in the same compound path. pub fn offset(paths: &[BezPath], amount: f64, join: Join, miter_limit: f64, copies: f64, copy_offset: f64) -> Vec { - let n = copies.round().clamp(1.0, 1000.0) as usize; + offset_within(paths, amount, join, miter_limit, copies, copy_offset, MAX_OPERATOR_SEGMENTS) +} + +/// [`offset`] with each path's result held to `max` elements (its first copy aside). +fn offset_within(paths: &[BezPath], amount: f64, join: Join, miter_limit: f64, copies: f64, copy_offset: f64, max: usize) -> Vec { + let n = pieces_within_budget(paths, copies.round().clamp(1.0, 1000.0) as usize); paths .iter() .map(|p| { let mut out = BezPath::new(); for i in 0..n { let d = amount * (i as f64 + 1.0 + copy_offset); - out.extend(crate::offset::offset_path(p, d, join, miter_limit).elements().iter().copied()); + let copy = crate::offset::offset_path(p, d, join, miter_limit); + // The budget is spent on what `offset_path` returns (an open subpath comes back as + // a closed outline of many segments; a zero offset as the path itself): the first + // copy is always kept, and a copy that would pass the budget is not made, so the + // work stays within a small multiple of it. + if i > 0 && out.elements().len().saturating_add(copy.elements().len()) > max { + log::warn!("path: Offset Paths made {i} of {n} copies; more would pass {max} segments"); + break; + } + out.extend(copy.elements().iter().copied()); } out }) @@ -387,6 +401,14 @@ pub fn twist(paths: &[BezPath], angle_deg: f64, center: [f64; 2]) -> Vec Vec usize { + if sub.closed { 2 } else { 1 } +} + +/// `want` pieces (or copies) per input segment, reduced so the whole output (segments times +/// pieces, plus per-subpath overhead) stays within [`MAX_OPERATOR_SEGMENTS`] (never below 1, so the input itself always passes through). +fn pieces_within_budget(paths: &[BezPath], want: usize) -> usize { + // Count what is subdivided (drawn segments) and keep room for each subpath's own MoveTo/Close. + let (mut segs, mut overhead) = (0usize, 0usize); + for sub in paths.iter().flat_map(subs) { + segs = segs.saturating_add(sub.segs.len()); + overhead = overhead.saturating_add(overhead_of(&sub)); + } + want.min(MAX_OPERATOR_SEGMENTS.saturating_sub(overhead) / segs.max(1)).max(1) +} + /// Split every segment into `m` pieces of equal arc length. Original vertices get the average of /// their incoming and outgoing tangents (so a corner's normal is its bisector). fn resample(sub: &Sub, m: usize) -> Vec { @@ -503,7 +547,7 @@ fn emit(samples: &[Sample], disp: &[Vec2], closed: bool, smooth: bool, handle: f /// `smooth` makes rounded waves instead of sharp zig zags. A closed path with `n` segments ends up /// with `n · (ridges + 1)` vertices (open: one more). pub fn zigzag(paths: &[BezPath], size: f64, ridges: f64, smooth: bool) -> Vec { - let m = ridges.round().clamp(0.0, 1000.0) as usize + 1; + let m = pieces_within_budget(paths, ridges.round().clamp(0.0, 1000.0) as usize + 1); paths .iter() .map(|p| { @@ -586,7 +630,7 @@ pub fn wiggle(paths: &[BezPath], w: &WiggleParams, time_s: f64) -> Vec if w.size == 0.0 { return paths.to_vec(); } - let m = w.detail.round().clamp(0.0, 1000.0) as usize + 1; + let m = pieces_within_budget(paths, w.detail.round().clamp(0.0, 1000.0) as usize + 1); let seed = w.seed.round() as i64 as u64; let t = time_s * w.speed + w.phase_deg / 360.0; let corr = (w.correlation / 100.0).clamp(0.0, 1.0); diff --git a/crates/path/src/ops/tests.rs b/crates/path/src/ops/tests.rs index 1ebdcea5b..e64a55b47 100644 --- a/crates/path/src/ops/tests.rs +++ b/crates/path/src/ops/tests.rs @@ -182,6 +182,25 @@ fn offset_copies() { assert!(close(o[0].bounding_box().width(), 140.0, 1e-3)); } +#[test] +fn offset_copies_are_budgeted_by_the_output_they_return() { + // An open one-segment subpath comes back as a closed outline of several elements, so the copies + // stop when the returned elements pass the budget, not when the input segments would. + let line = || { + let mut p = BezPath::new(); + p.move_to((0.0, 0.0)); + p.line_to((50.0, 0.0)); + p + }; + let one = offset_within(&[line()], 5.0, Join::Round, 4.0, 1.0, 0.0, usize::MAX)[0].elements().len(); + assert!(one > 4, "{one}"); + let o = offset_within(&[line()], 5.0, Join::Round, 4.0, 50.0, 0.0, one * 3); + let moves = o[0].elements().iter().filter(|e| matches!(e, PathEl::MoveTo(_))).count(); + assert!((1..50).contains(&moves) && o[0].elements().len() <= one * 4, "{moves} contours, {} elements", o[0].elements().len()); + // The first copy always comes back, however small the budget. + assert!(!offset_within(&[line()], 5.0, Join::Round, 4.0, 5.0, 0.0, 0)[0].elements().is_empty()); +} + #[test] fn offset_open_path_outlines() { let mut l = BezPath::new(); @@ -246,6 +265,22 @@ fn twist_of_centred_circle_keeps_it_round() { assert_eq!(id[0], sq(0.0, 0.0, 5.0)); } +#[test] +fn twist_keeps_requested_detail_when_the_whole_request_fits_the_budget() { + // One long segment asks for 64 pieces; 20,000 tiny ones ask for one each. Together that is + // far under the budget, so the long one is not cut to what a uniform 64 per segment would allow. + let mut long = BezPath::new(); + long.move_to((0.0, 0.0)); + long.line_to((5000.0, 0.0)); + let mut tiny = BezPath::new(); + tiny.move_to((0.0, 1.0)); + for i in 1..=20_000 { + tiny.line_to((i as f64 * 1e-3, 1.0)); + } + let t = twist(&[long, tiny], 90.0, [0.0, 0.0]); + assert_eq!(t[0].elements().len(), 1 + 64); +} + // ---------------------------------------------------------------- zig zag #[test] @@ -417,3 +452,48 @@ fn normalize_and_filled_area() { let n = crate::boolean::normalize(&p, FillRule::EvenOdd); assert!(close(n.area().abs(), 10000.0, 1e-6)); } + +// ---------------------------------------------------------------- operator budget + +#[test] +fn stacked_expanding_operators_stay_within_the_segment_budget() { + // Three Zig Zags at 1000 ridges would turn a square into 4 · 1001³ segments; each output + // is held to the budget instead, and the last operators still pass the geometry through. + let mut p = vec![sq(0.0, 0.0, 100.0)]; + for _ in 0..3 { + p = zigzag(&p, 1.0, 1000.0, false); + let n: usize = p.iter().map(|q| q.elements().len()).sum(); + assert!(n <= 2 * MAX_OPERATOR_SEGMENTS, "{n}"); + } + let w = wiggle(&p, &WiggleParams { detail: 1000.0, ..Default::default() }, 0.0); + let n: usize = w.iter().map(|q| q.elements().len()).sum(); + assert!(n <= 2 * MAX_OPERATOR_SEGMENTS, "{n}"); + let o = offset(&[sq(0.0, 0.0, 100.0)], 1.0, Join::Miter, 4.0, 1000.0, 0.0); + assert!(!o[0].elements().is_empty()); + // Within the budget nothing changes: 4 segments × 11 pieces. + assert_eq!(vertex_count(&zigzag(&[sq(0.0, 0.0, 100.0)], 1.0, 10.0, false)[0]), 44); +} + +#[test] +fn many_short_open_paths_keep_their_requested_detail_within_the_budget() { + // 1000 open lines at 1000 ridges: 1,001,000 segments fit the budget, so nothing is reduced. + let lines: Vec = (0..1000) + .map(|i| { + let mut p = BezPath::new(); + p.move_to((0.0, i as f64)); + p.line_to((100.0, i as f64)); + p + }) + .collect(); + let z = zigzag(&lines, 1.0, 1000.0, false); + assert!(z.iter().all(|q| q.elements().len() == 1 + 1001)); +} + +#[test] +fn open_subpaths_reserve_one_element_of_overhead() { + // 349,525 open lines: the move-to is the only extra element, so 2 copies fit the budget + // (a closed subpath's extra close-path would leave room for 1). + let line = BezPath::from_vec(vec![PathEl::MoveTo((0.0, 0.0).into()), PathEl::LineTo((1.0, 0.0).into())]); + let lines = vec![line; 349_525]; + assert_eq!(pieces_within_budget(&lines, 3), 2); +}