DXF/DWG import: dash phases keep small terms when large ones cancel

A pattern line's dash start is the sum of the base and the boundary's
center along the line and the whole counts of offsets, each reduced to
within half a period. Those reduced terms were added in plain floating
point, so a small term was rounded away before larger ones cancelled: a
family based and offset 2^54 along, with a dash 16 long in a period of
2^57, drew the line along the bottom of a unit square from x 0.5 instead
of x 0. The terms are now added with their rounding errors kept apart and
added back after the sum is reduced, for the dash start and for each
line's phase.
This commit is contained in:
dotCooCoo committed 2026-12-05 02:20:05 -08:00
1 parent 323aa1d56f
commit f651cf6d1b
1 file changed
+36 -8
+36 -8
View File
@@ -162,15 +162,17 @@ pub(crate) fn pattern(lines: &[PatternLine], boundary: &BezPath, budget: &mut Bu
let along = if l.dashes.is_empty() {
0.0
} else {
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)
let ([o, oe], [c, ce]) = (product_parts(to_origin, od), product_parts(to_center + last, od));
sum_rem(&[base.to_vec2().dot(d), -center.to_vec2().dot(d), o, oe, c, ce], 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| near_rem(along + product_rem(k, od, period), period);
let phase = |k: f64| {
let [p, e] = product_parts(k, od);
sum_rem(&[along, p, e], 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);
@@ -273,11 +275,25 @@ fn near_rem(x: f64, period: f64) -> f64 {
}
}
/// `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 {
/// `a * b` as the rounded product and its rounding error, which add up to it exactly.
fn product_parts(a: f64, b: f64) -> [f64; 2] {
let p = a * b;
near_rem(near_rem(p, period) + near_rem(a.mul_add(b, -p), period), period)
[p, a.mul_add(b, -p)]
}
/// The sum of `terms` reduced as [`near_rem`] does. Each term is reduced first, so a large term
/// keeps its remainder, and the reduced terms are added with their rounding errors kept apart, so
/// a small term is kept when larger ones cancel.
fn sum_rem(terms: &[f64], period: f64) -> f64 {
let (mut hi, mut lo) = (0.0, 0.0);
for &t in terms {
let t = near_rem(t, period);
let s = hi + t;
let v = s - hi;
lo += (hi - (s - v)) + (t - v);
hi = s;
}
near_rem(near_rem(hi, period) + near_rem(lo, period), period)
}
/// The boundary's edges as straight segments, each loop closed and its curves flattened to
@@ -676,6 +692,18 @@ mod tests {
assert!((row[0].0 - 0.0).abs() < 1e-9 && (row[0].1 - 30.0).abs() < 1e-9, "{p:?}");
}
#[test]
fn large_phase_terms_that_cancel_keep_a_small_one() {
// Based and offset by 2^54 along and 10 up: the line one offset back runs along the
// bottom of the unit square with its dash 16 long from x 0, so it covers 0 to 1.
let big = 2f64.powi(54);
let mut l = line(0.0, [big, 10.0], &[16.0, -(2f64.powi(57) - 16.0)]);
l.base = [big, 10.0];
let p = pieces(&pattern(&[l], &square(1.0)).unwrap().0);
let row: Vec<_> = p.iter().filter(|(a, _)| a.y == 0.0).map(|(a, e)| (a.x, e.x)).collect();
assert_eq!(row, [(0.0, 1.0)], "{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