Twist (#7069): the derivative's step grows with the coordinates so it doesn't round away

This commit is contained in:
Don Omondi
2026-11-13 03:25:35 -05:00
parent e9b926de34
commit 128ba86be3
2 changed files with 30 additions and 2 deletions
+24
View File
@@ -90,6 +90,30 @@ mod tests {
}
}
/// The smooth map takes each handle along the map's derivative, also far from the origin,
/// where a fixed sampling step would round away (#7069): the identity map keeps a curve's
/// handles there.
#[test]
fn map_smooth_within_keeps_handles_far_from_the_origin() {
for at in [0.0, 1e13] {
let o = Point::new(at, at);
let curve = PathData::single(vectorcraft_geom::SubPath::new(
vec![
vectorcraft_geom::Anchor::with_handles(o, o, o + vectorcraft_geom::Vec2::new(0.2, 0.0)),
vectorcraft_geom::Anchor::with_handles(
o + vectorcraft_geom::Vec2::new(1.0, 0.5),
o + vectorcraft_geom::Vec2::new(0.8, 0.5),
o + vectorcraft_geom::Vec2::new(1.0, 0.5),
),
],
false,
));
let out = map_smooth_within(&curve, 10.0, usize::MAX, |p| p);
let (a, b) = (&curve.subpaths[0].anchors, &out.subpaths[0].anchors);
assert!(a[0].h_out.distance(b[0].h_out) < 0.01 && a[1].h_in.distance(b[1].h_in) < 0.01, "at {at}: {:?} {:?}", b[0].h_out, b[1].h_in);
}
}
/// #3578: a non-linear map within an anchor budget splits its segments into longer pieces,
/// down to one a segment, and matches the plain map where the budget leaves room.
#[test]
+6 -2
View File
@@ -517,8 +517,12 @@ fn mapped_handle(f: &impl Fn(Point) -> Point, p: Point, h: Point) -> Point {
if len.is_nan() || len <= 0.0 || len.is_infinite() {
return f(p);
}
const STEP: f64 = 1e-3;
let d = (f(p + v * STEP) - f(p - v * STEP)) / (2.0 * STEP);
// A thousandth of the handle, or more where the coordinates are so large that so short a
// step would round away (#7069): the samples stay a billionth of the coordinates apart, and
// at most at the handle's own end.
let size = p.x.abs().max(p.y.abs()).max(1.0);
let step = (1e-3f64).max(size * 1e-9 / len).min(1.0);
let d = (f(p + v * step) - f(p - v * step)) / (2.0 * step);
f(p) + d
}