diff --git a/crates/cad/src/import/hatch.rs b/crates/cad/src/import/hatch.rs index 3457fa4527..1bc3e323f4 100644 --- a/crates/cad/src/import/hatch.rs +++ b/crates/cad/src/import/hatch.rs @@ -146,18 +146,33 @@ pub(crate) fn pattern(lines: &[PatternLine], boundary: &BezPath, budget: &mut Bu if !l.dashes.is_empty() && period <= tiny { return Err(NoPattern::TooDense); } - // The same family from the line nearest the boundary's center: a whole number of offsets - // along, so each line keeps its dashes' phase, and then a whole number of dash periods - // along the line. - let (across, along) = ((base - center).dot(n), (base - center).dot(d)); - let shift = -(across / step).round(); - let across = across + shift * step; - let along = if l.dashes.is_empty() { 0.0 } else { (along + shift * offset.dot(d)).rem_euclid(period) }; + // The same family from the line nearest the boundary's center, a whole number of offsets + // away. The base and the center are each measured from the line through the origin + // first, so a base far away keeps the fraction of the center's position. + let (bn, cn) = (base.to_vec2().dot(n), center.to_vec2().dot(n)); + let (to_origin, to_center) = (-(bn / step).round(), (cn / step).round()); + let across = (bn + to_origin * step) + (to_center * step - cn); + let last = -(across / step).round(); + let across = across + last * step; + // Where the dashes start on that line, from its point nearest the center. Each line is + // measured from its own point nearest the center, so its spans stay near the boundary + // however long the dash period is. + let along = if l.dashes.is_empty() { + 0.0 + } else { + let bd = base.to_vec2().dot(d) + to_origin * offset.dot(d); + bd + ((to_center + last) * offset.dot(d) - center.to_vec2().dot(d)) + }; if !(across.is_finite() && along.is_finite()) { continue; } - let base = center + n * across + d * along; - // The lines crossing the boundary's box: base + k * offset for whole k. + // The dash start nearest a line's point nearest the center, as a signed distance. + let phase = |k: f64| { + let s = along + k * offset.dot(d); + s - period * (s / period).round() + }; + let base = center + n * across; + // The lines crossing the boundary's box: base + k * step along the normal for whole k. let h = corners.map(|c| (c - base).dot(n) / step); let lo = h.iter().copied().fold(f64::INFINITY, f64::min).ceil(); let hi = h.iter().copied().fold(f64::NEG_INFINITY, f64::max).floor(); @@ -171,7 +186,7 @@ pub(crate) fn pattern(lines: &[PatternLine], boundary: &BezPath, budget: &mut Bu let mut t = Vec::new(); for k in (lo as i64)..=(hi as i64) { budget.work(&mut work, edges.len())?; - let q = base + offset * k as f64; + let q = base + n * (step * k as f64); crossings(&edges, q, d, n, &mut t); for &[ta, tb] in t.as_chunks::<2>().0 { // Spans of no length at the precision of their own coordinates are dropped. @@ -188,36 +203,50 @@ pub(crate) fn pattern(lines: &[PatternLine], boundary: &BezPath, budget: &mut Bu emit(ta, tb, budget)?; continue; } - // The dashes run from the line's base point, element by element, from the period - // before the one the span starts in, so a dot that ends that period (on the span's - // start) is kept. - let mut at = (ta / period).floor() * period; - if at >= ta { - at -= period; - } + // The dashes run from the line's dash start, element by element, from the period + // the span starts in. + let start = phase(k as f64); + let mut at = start + ((ta - start) / period).floor() * period; // Periods too short to move along the span, or more dashes than one hatch may // look at, are refused before any of them is looked at. - let steps = ((tb - at) / period + 1.0) * l.dashes.len() as f64; + let steps = ((tb - at) / period + 2.0) * l.dashes.len() as f64; if period <= LOCAL_TINY * at.abs().max(tb.abs()) || steps + work as f64 > MAX_WORK as f64 { return Err(NoPattern::TooDense); } + let mut element = |s: f64, v: f64, budget: &mut Budget| -> Result<(), NoPattern> { + budget.work(&mut work, 1)?; + if v > 0.0 { + let (s0, s1) = (s.max(ta), (s + v).min(tb)); + if s1 > s0 { + emit(s0, s1, budget)?; + } + } else if v == 0.0 && s >= ta { + emit(s, s, budget)?; + dots = true; + } + Ok(()) + }; + // When that period starts on the span's start, or past it, the elements that end + // the period before are measured back from its end, so a dot there is kept and a + // long period does not move them. + if at >= ta { + let (mut rest, mut first) = (0.0, l.dashes.len()); + while first > 0 && at - rest >= ta { + first -= 1; + rest += l.dashes[first].abs(); + } + for v in &l.dashes[first..] { + element(at - rest, *v, budget)?; + rest -= v.abs(); + } + } 'span: loop { for v in &l.dashes { if at > tb { break 'span; } - budget.work(&mut work, 1)?; - let len = v.abs(); - if *v > 0.0 { - let (s0, s1) = (at.max(ta), (at + len).min(tb)); - if s1 > s0 { - emit(s0, s1, budget)?; - } - } else if *v == 0.0 && at >= ta { - emit(at, at, budget)?; - dots = true; - } - at += len; + element(at, *v, budget)?; + at += v.abs(); } } } @@ -578,6 +607,38 @@ mod tests { assert!(a.iter().zip(&b).all(|(x, y)| x.0.distance(y.0) < 1e-9 && x.1.distance(y.1) < 1e-9), "{a:?} {b:?}"); } + #[test] + fn families_based_far_away_keep_lines_off_a_fractional_center() { + // A 3 square centered on y 1.5: lines one apart from a base 1e19 up run at whole y, not + // through the center. + let mut far = line(0.0, [0.0, 1.0], &[]); + far.base = [0.0, 1e19]; + let (bp, _) = pattern(&[far], &square(3.0)).unwrap(); + let p = pieces(&bp); + assert!(p.len() >= 2, "{p:?}"); + assert!(p.iter().all(|(a, b)| a.y == b.y && a.y.fract() == 0.0), "{p:?}"); + } + + #[test] + fn dash_periods_far_longer_than_the_boundary_still_draw() { + // Dashes 1e16 long from x 0, and from x -7.5e15: each row of the 100 square lies in a + // dash, so every row is drawn across the square. + for x in [0.0, -7.5e15] { + let mut l = line(0.0, [0.0, 10.0], &[1e16, -1e16]); + l.base = [x, 0.0]; + let (bp, _) = pattern(&[l], &square(100.0)).unwrap(); + let p = pieces(&bp); + let rows: Vec<_> = p.iter().filter(|(a, _)| a.y > 0.0 && a.y < 100.0).collect(); + assert_eq!(rows.len(), 9, "base x {x}: {p:?}"); + assert!(rows.iter().all(|(a, b)| a.x.abs() < 1e-9 && (b.x - 100.0).abs() < 1e-9), "base x {x}: {p:?}"); + } + // From x 5e15 the square lies in a gap: nothing is drawn. + let mut l = line(0.0, [0.0, 10.0], &[1e16, -1e16]); + l.base = [5e15, 0.0]; + let (bp, _) = pattern(&[l], &square(100.0)).unwrap(); + assert!(pieces(&bp).is_empty(), "{:?}", pieces(&bp)); + } + #[test] fn a_dot_on_the_start_of_a_span_is_kept() { // A gap of 5 then a dot, from x 0: dots at 0, 5 and 10 on the line along the bottom.