mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-05 13:32:45 -05:00
DXF/DWG import: dash phases keep their remainders; long dashes keep their ends
A pattern line's dash start is now taken from each term reduced to within half a period before the terms are added: the base and the boundary's center along the line, and each whole count of offsets, with the rounding error of the count times the offset reduced on its own. Offset counts that cancel (2^30 offsets each way, 2^30 along) no longer drop the center's 50, and a base 1e19 along keeps its remainder of 4 in a period of 6, so the dashes start at x 4 instead of x 0. The elements that end the period before a span now take their ends from the distances summed back from that period's end. A dash 1e16 long before a gap no longer ends at its rounded start plus its length, which drew a stroke into the gap.
This commit is contained in:
1 parent
28776f1903
commit
323aa1d56f
1 file changed
+81
-17
@@ -156,21 +156,21 @@ pub(crate) fn pattern(lines: &[PatternLine], boundary: &BezPath, budget: &mut Bu
|
||||
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.
|
||||
// however long the dash period is. Each term is reduced to within half a period before
|
||||
// the terms are added, so a distant base or a large offset count keeps its remainder.
|
||||
let od = offset.dot(d);
|
||||
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))
|
||||
let (bd, cd) = (near_rem(base.to_vec2().dot(d), period), near_rem(center.to_vec2().dot(d), period));
|
||||
let counts = near_rem(product_rem(to_origin, od, period) + product_rem(to_center + last, od, period), period);
|
||||
near_rem((bd - cd) + counts, period)
|
||||
};
|
||||
if !(across.is_finite() && along.is_finite()) {
|
||||
continue;
|
||||
}
|
||||
// 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 phase = |k: f64| near_rem(along + product_rem(k, od, period), period);
|
||||
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);
|
||||
@@ -213,10 +213,11 @@ pub(crate) fn pattern(lines: &[PatternLine], boundary: &BezPath, budget: &mut Bu
|
||||
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> {
|
||||
// An element from `s` to `e`: a dash when `v` is positive, a dot when it is zero.
|
||||
let mut element = |s: f64, e: 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));
|
||||
let (s0, s1) = (s.max(ta), e.min(tb));
|
||||
if s1 > s0 {
|
||||
emit(s0, s1, budget)?;
|
||||
}
|
||||
@@ -228,16 +229,17 @@ pub(crate) fn pattern(lines: &[PatternLine], boundary: &BezPath, budget: &mut Bu
|
||||
};
|
||||
// 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.
|
||||
// long period does not move them. Each element's start and end are the distances
|
||||
// summed back from that end, so a long element does not move its known end.
|
||||
if at >= ta {
|
||||
let (mut rest, mut first) = (0.0, l.dashes.len());
|
||||
while first > 0 && at - rest >= ta {
|
||||
let (mut back, mut first) = (vec![0.0], l.dashes.len());
|
||||
while first > 0 && at - back[back.len() - 1] >= ta {
|
||||
first -= 1;
|
||||
rest += l.dashes[first].abs();
|
||||
back.push(back[back.len() - 1] + l.dashes[first].abs());
|
||||
}
|
||||
for v in &l.dashes[first..] {
|
||||
element(at - rest, *v, budget)?;
|
||||
rest -= v.abs();
|
||||
for (i, v) in l.dashes[first..].iter().enumerate() {
|
||||
let rest = back.len() - 1 - i;
|
||||
element(at - back[rest], at - back[rest - 1], *v, budget)?;
|
||||
}
|
||||
}
|
||||
'span: loop {
|
||||
@@ -245,7 +247,7 @@ pub(crate) fn pattern(lines: &[PatternLine], boundary: &BezPath, budget: &mut Bu
|
||||
if at > tb {
|
||||
break 'span;
|
||||
}
|
||||
element(at, *v, budget)?;
|
||||
element(at, at + v.abs(), *v, budget)?;
|
||||
at += v.abs();
|
||||
}
|
||||
}
|
||||
@@ -258,6 +260,26 @@ pub(crate) fn pattern(lines: &[PatternLine], boundary: &BezPath, budget: &mut Bu
|
||||
Ok((out, dots))
|
||||
}
|
||||
|
||||
/// `x` less the whole number of periods nearest it, within half a period of zero. Both steps
|
||||
/// are exact: the remainder of a division, then one period added or taken away.
|
||||
fn near_rem(x: f64, period: f64) -> f64 {
|
||||
let r = x % period;
|
||||
if r > period / 2.0 {
|
||||
r - period
|
||||
} else if r < -period / 2.0 {
|
||||
r + period
|
||||
} else {
|
||||
r
|
||||
}
|
||||
}
|
||||
|
||||
/// `a * b` reduced as [`near_rem`] does. The rounded product and its rounding error are each
|
||||
/// reduced before they are added, so a large product keeps its remainder.
|
||||
fn product_rem(a: f64, b: f64, period: f64) -> f64 {
|
||||
let p = a * b;
|
||||
near_rem(near_rem(p, period) + near_rem(a.mul_add(b, -p), period), period)
|
||||
}
|
||||
|
||||
/// The boundary's edges as straight segments, each loop closed and its curves flattened to
|
||||
/// within [`FLATNESS`] of the loop's own size. Loops of no size are left out.
|
||||
fn edges(boundary: &BezPath) -> Result<Vec<(Point, Point)>, NoPattern> {
|
||||
@@ -639,6 +661,48 @@ mod tests {
|
||||
assert!(pieces(&bp).is_empty(), "{:?}", pieces(&bp));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn large_offset_counts_that_cancel_keep_the_dash_start() {
|
||||
// A 100 square 2^40 up, its family based on the row through its middle with offsets
|
||||
// 2^30 along: the row's dashes start at x 0, so it draws 0 to 30.
|
||||
let b = 2f64.powi(40);
|
||||
let mut boundary = square(100.0);
|
||||
boundary.apply_affine(Affine::translate((0.0, b)));
|
||||
let mut l = line(0.0, [2f64.powi(30), 1024.0], &[30.0, -70.0]);
|
||||
l.base = [0.0, b + 50.0];
|
||||
let p = pieces(&pattern(&[l], &boundary).unwrap().0);
|
||||
let row: Vec<_> = p.iter().filter(|(a, _)| a.y == b + 50.0).map(|(a, e)| (a.x, e.x)).collect();
|
||||
assert_eq!(row.len(), 1, "{p:?}");
|
||||
assert!((row[0].0 - 0.0).abs() < 1e-9 && (row[0].1 - 30.0).abs() < 1e-9, "{p:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_long_dash_before_the_boundary_ends_where_its_gap_starts() {
|
||||
// A gap 1e16 long from x 0, after a dash 1e16 long that ends at x 0: the unit square
|
||||
// lies in the gap, so nothing is drawn.
|
||||
let mut l = line(0.0, [0.0, 10.0], &[-1e16, 1e16]);
|
||||
l.base = [0.0, 0.5];
|
||||
let (bp, _) = pattern(&[l], &square(1.0)).unwrap();
|
||||
assert!(pieces(&bp).is_empty(), "{:?}", pieces(&bp));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn distant_bases_keep_their_dash_remainder() {
|
||||
// Dash 1, gap 5 from x 1e19, which is 4 more than a whole number of periods of 6: the
|
||||
// dashes run 4 to 5, 10 to 11 … 94 to 95 in squares centered on a whole and a half x.
|
||||
for side in [96.0, 98.0] {
|
||||
let mut l = line(0.0, [0.0, 10.0], &[1.0, -5.0]);
|
||||
l.base = [1e19, 0.0];
|
||||
let p = pieces(&pattern(&[l], &square(side)).unwrap().0);
|
||||
let row: Vec<_> = p.iter().filter(|(a, _)| a.y == 50.0).map(|(a, e)| (a.x, e.x)).collect();
|
||||
assert_eq!(row.len(), 16, "side {side}: {row:?}");
|
||||
assert!(
|
||||
row.iter().enumerate().all(|(i, (a, e))| (a - (4.0 + 6.0 * i as f64)).abs() < 1e-9 && (e - a - 1.0).abs() < 1e-9),
|
||||
"side {side}: {row:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[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.
|
||||
|
||||
Reference in new issue
Block a user