mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 15:55:14 +00:00
linesweeper: short chords at huge x and subnormal line x stay finite and exact (#1258)
This commit is contained in:
1 parent
c7bb86a7f5
commit
5724da0f2e
5 files changed
+68
-16
No files matched your search
@@ -254,11 +254,10 @@ applications retain their own document-font selection and licensing policy.
|
||||
linesweeper
|
||||
|
||||
linesweeper 0.5.0 (https://crates.io/crates/linesweeper/0.5.0), MIT OR Apache-2.0, is stored in
|
||||
vendor/linesweeper with its upstream license files and local patches, to src/sweep/sweep_line.rs and
|
||||
docs/sweep.typ. Its manifest names Joe Neeman as author, and its LICENSE-MIT reads "Copyright (c) 2018
|
||||
Raph Levien". See vendor/linesweeper/README.craft.md and vendor/linesweeper/SOURCE.json for
|
||||
provenance, the local patches
|
||||
and the files left out.
|
||||
vendor/linesweeper with its upstream license files and local patches, to src/sweep/sweep_line.rs,
|
||||
src/curve/mod.rs, src/curve/line.rs and docs/sweep.typ. Its manifest names Joe Neeman as author, and
|
||||
its LICENSE-MIT reads "Copyright (c) 2018 Raph Levien". See vendor/linesweeper/README.craft.md and
|
||||
vendor/linesweeper/SOURCE.json for provenance, the local patches and the files left out.
|
||||
|
||||
Platform lifecycle source provenance
|
||||
|
||||
|
||||
Vendored
+2
@@ -17,6 +17,8 @@ holds `upstream_revision`. `local_patches` describes each local patch.
|
||||
| File | Change |
|
||||
|---|---|
|
||||
| `src/sweep/sweep_line.rs` | The doc comment of `SweepLine::line_segment` says it can return a segment that has exited, which `segment_range` leaves out. Code comments label the change `Craft patch (exited slots)`. |
|
||||
| `src/curve/mod.rs` | When the chord that `fit_quadratic` takes for a very short curve gives a constant term that is not finite, `fit_quadratic` takes the line through the curve's start point with the chord's slope instead. Before, a curve near x = 1e305 with heights near 1e4 and less than about 3e-12 tall gave a product of an x coordinate and a height above `f64::MAX`, and the chord's constant term was NaN. The `short_curve_chord_with_huge_x` test fits vertical curves one ulp tall at y = 1e4 and x = 1e305, -1e305 and 5e307. Code comments label the change `Craft patch (huge chord)`. |
|
||||
| `src/curve/line.rs` | `line_at_ys` multiplies the x coordinates by 2^600 before the weighted sum and the sum by 2^-600 after it when both x coordinates have a magnitude below 2^-500. Before, a vertical line at x = 5e-324 gave x = 0 halfway along its height. The `subnormal_x` test checks that line and a line from x = -1.5e-323 to 1.5e-323. Code comments label the change `Craft patch (subnormal x)`. |
|
||||
| `docs/sweep.typ` | The intersection scan to the left compares each segment with `alpha^(i+1)` instead of `alpha^i`. With `alpha^i`, its first pass compared `alpha^i` with itself, and the lemma after it is stated for `alpha^(i+1)`. |
|
||||
| `docs/sweep.typ` | Two typos are corrected. "condi0.2tions" reads "conditions", and `q(i+i)` reads `q(i+1)`, the first index for which `q` is false. |
|
||||
| `docs/sweep.typ` | The lemma for the intersection scan to the left asks for the ordering invariant on `(alpha^1, ..., alpha^(i+1))` and the crossing invariant on `(alpha^1, ..., alpha^i)`, which is how the bidirectional lemma uses it. The argument for the order invariant cites the crossing invariant instead of a "third invariant"; the document defines two. |
|
||||
|
||||
Vendored
+3
-2
@@ -9,6 +9,7 @@
|
||||
"upstream_directory": "",
|
||||
"local_patches": [
|
||||
"In `src/sweep/sweep_line.rs`, the doc comment of `SweepLine::line_segment` says it can return a segment that has exited, which `segment_range` leaves out.",
|
||||
"In `src/curve/mod.rs`, when the chord that `fit_quadratic` takes for a very short curve gives a constant term that is not finite, `fit_quadratic` takes the line through the curve's start point with the chord's slope, so a curve near x = 1e305 with heights near 1e4 no longer gives a NaN coefficient. In `src/curve/line.rs`, `line_at_ys` scales the x coordinates by 2^600 before the weighted sum and back after it when both have a magnitude below 2^-500, so a subnormal x coordinate no longer rounds to 0. Both files add tests for it.",
|
||||
"In `docs/sweep.typ`, the intersection scan to the left compares each segment with `alpha^(i+1)`, the segment the lemma after it is stated for, instead of `alpha^i`.",
|
||||
"In `docs/sweep.typ`, the typo \"condi0.2tions\" reads \"conditions\", and `q(i+i)` reads `q(i+1)`, the first index for which `q` is false.",
|
||||
"In `docs/sweep.typ`, the lemma for the intersection scan to the left asks for the ordering invariant on `(alpha^1, ..., alpha^(i+1))` and the crossing invariant on `(alpha^1, ..., alpha^i)`, as the bidirectional lemma uses it, and the argument for the order invariant cites the crossing invariant instead of a \"third invariant\"."
|
||||
@@ -36,8 +37,8 @@
|
||||
{ "path": "proptest-regressions/sweep/weak_ordering.txt", "source_sha256": "b5b183ab470399c15246457d8a1a35cafb49fee120d6777dfdd563bfdbadda52", "source_git_blob": null, "sha256": "b5b183ab470399c15246457d8a1a35cafb49fee120d6777dfdd563bfdbadda52" },
|
||||
{ "path": "proptest-regressions/topology.txt", "source_sha256": "49ff0b5b370f082984276d5312f03b1fc96ef0dd218f9efd7b084b11abbb9d19", "source_git_blob": null, "sha256": "49ff0b5b370f082984276d5312f03b1fc96ef0dd218f9efd7b084b11abbb9d19" },
|
||||
{ "path": "src/arbitrary.rs", "source_sha256": "9c3753a40cfea2528a546295d39e0513537db5a3411a931fccea063873d95750", "source_git_blob": null, "sha256": "9c3753a40cfea2528a546295d39e0513537db5a3411a931fccea063873d95750" },
|
||||
{ "path": "src/curve/line.rs", "source_sha256": "07aebead4b0050410a3f9f28b66062b6016c115df58e2b2669470263cd352f3e", "source_git_blob": null, "sha256": "07aebead4b0050410a3f9f28b66062b6016c115df58e2b2669470263cd352f3e" },
|
||||
{ "path": "src/curve/mod.rs", "source_sha256": "36a8dbe96ac1d3bec307dc63b504206ec7fdfc11d10799fbb1a8990c29b8c5f2", "source_git_blob": null, "sha256": "36a8dbe96ac1d3bec307dc63b504206ec7fdfc11d10799fbb1a8990c29b8c5f2" },
|
||||
{ "path": "src/curve/line.rs", "source_sha256": "07aebead4b0050410a3f9f28b66062b6016c115df58e2b2669470263cd352f3e", "source_git_blob": null, "sha256": "4cdcb0493a5772eaf64af3ee50b48ddf01e655e3ea0776c5c801cca146671a42" },
|
||||
{ "path": "src/curve/mod.rs", "source_sha256": "36a8dbe96ac1d3bec307dc63b504206ec7fdfc11d10799fbb1a8990c29b8c5f2", "source_git_blob": null, "sha256": "33b506d2d1d869ac384787825e19779fe40603effb33b40970e4ea6785ae2f84" },
|
||||
{ "path": "src/curve/split_quad.rs", "source_sha256": "b8aae4ce7c16fa623724f667e1058130a9c7eed693b5b01e8b9e16632b8ec45f", "source_git_blob": null, "sha256": "b8aae4ce7c16fa623724f667e1058130a9c7eed693b5b01e8b9e16632b8ec45f" },
|
||||
{ "path": "src/curve/transverse.rs", "source_sha256": "be9c695531f4cb0fe73af13e78040b51b9476c193d84562e701aadd07b3fd06e", "source_git_blob": null, "sha256": "be9c695531f4cb0fe73af13e78040b51b9476c193d84562e701aadd07b3fd06e" },
|
||||
{ "path": "src/generators.rs", "source_sha256": "cc9f2441546db9204219294c354449809dda77dffad06fec8a30f45df01e6fef", "source_git_blob": null, "sha256": "cc9f2441546db9204219294c354449809dda77dffad06fec8a30f45df01e6fef" },
|
||||
|
||||
Vendored
+30
-4
@@ -8,10 +8,25 @@ fn line_at_ys(line: Line, y0: f64, y1: f64) -> (f64, f64) {
|
||||
let dy_recip = (line.p1.y - line.p0.y).recip();
|
||||
let t0 = (y0 - line.p0.y) * dy_recip;
|
||||
let t1 = (y1 - line.p0.y) * dy_recip;
|
||||
(
|
||||
(1.0 - t0) * line.p0.x + t0 * line.p1.x,
|
||||
(1.0 - t1) * line.p0.x + t1 * line.p1.x,
|
||||
)
|
||||
(x_at_t(line, t0), x_at_t(line, t1))
|
||||
}
|
||||
|
||||
/// Returns `(1 - t) * line.p0.x + t * line.p1.x`.
|
||||
fn x_at_t(line: Line, t: f64) -> f64 {
|
||||
// Craft patch (subnormal x): a product of t and a subnormal x coordinate
|
||||
// loses bits, so x = 5e-324 times 0.5 rounds to 0. When both x coordinates
|
||||
// have a magnitude below 2^-500, take the weighted sum of the x coordinates
|
||||
// times 2^600 and scale the sum back. Multiplying by a power of two is
|
||||
// exact for normal numbers, and multiplying by 1 changes nothing.
|
||||
const SMALL: f64 = f64::from_bits((1023 - 500) << 52);
|
||||
const UP: f64 = f64::from_bits((1023 + 600) << 52);
|
||||
const DOWN: f64 = f64::from_bits((1023 - 600) << 52);
|
||||
let (up, down) = if line.p0.x.abs().max(line.p1.x.abs()) < SMALL {
|
||||
(UP, DOWN)
|
||||
} else {
|
||||
(1.0, 1.0)
|
||||
};
|
||||
((1.0 - t) * (line.p0.x * up) + t * (line.p1.x * up)) * down
|
||||
}
|
||||
|
||||
/// Checks the relative order of two line segments.
|
||||
@@ -156,4 +171,15 @@ mod tests {
|
||||
assert_eq!(order.cmps[1].order, Order::Ish);
|
||||
assert!((order.cmps[0].end - 1.0).abs() < 1e-5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn subnormal_x() {
|
||||
let x = f64::from_bits(1);
|
||||
let l = Line::new((x, 0.0), (x, 2.0));
|
||||
assert_eq!(line_at_ys(l, 0.5, 1.0), (x, x));
|
||||
|
||||
let l = Line::new((-3.0 * x, 0.0), (3.0 * x, 2.0));
|
||||
assert_eq!(line_at_ys(l, 0.0, 2.0), (-3.0 * x, 3.0 * x));
|
||||
assert_eq!(line_at_ys(l, 1.0, 1.0), (0.0, 0.0));
|
||||
}
|
||||
}
|
||||
Vendored
+29
-5
@@ -1142,11 +1142,16 @@ fn fit_quadratic(c: CubicBez) -> Quadratic {
|
||||
if p1.y - p0.y < 1e-12 || p2.y - p1.y < 1e-12 {
|
||||
// p0.x * (y - p3.y) / (p0.y - p3.y)
|
||||
// + p3.x * (y - p0.y) / (p3.y - p0.y)
|
||||
return Quadratic::new([
|
||||
(p0.x * p3.y - p3.x * p0.y) / (p3.y - p0.y),
|
||||
(p3.x - p0.x) / (p3.y - p0.y),
|
||||
0.0,
|
||||
]);
|
||||
let c1 = (p3.x - p0.x) / (p3.y - p0.y);
|
||||
let mut c0 = (p0.x * p3.y - p3.x * p0.y) / (p3.y - p0.y);
|
||||
if !c0.is_finite() {
|
||||
// Craft patch (huge chord): the product of an x coordinate and a
|
||||
// height overflows when the two together pass f64::MAX, for example
|
||||
// x near 1e305 with heights near 1e4, and the difference of the two
|
||||
// products is then NaN. Take the line through p0 with slope c1.
|
||||
c0 = p0.x - c1 * p0.y;
|
||||
}
|
||||
return Quadratic::new([c0, c1, 0.0]);
|
||||
}
|
||||
|
||||
let p = interpolate_quadratic(p0.y, p0.x, p1.y, p1.x, p2.y, p2.x);
|
||||
@@ -1771,6 +1776,25 @@ mod test {
|
||||
assert!(diff >= ep.dmin);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn short_curve_chord_with_huge_x() {
|
||||
// A vertical curve one ulp tall at y = 1e4, so `fit_quadratic` takes a
|
||||
// chord. The product of an x coordinate and a height is above f64::MAX.
|
||||
let y0 = 1e4f64;
|
||||
let y3 = f64::from_bits(y0.to_bits() + 1);
|
||||
for x in [1e305, -1e305, 5e307] {
|
||||
let c = CubicBez::new((x, y0), (x, y0), (x, y3), (x, y3));
|
||||
let q = fit_quadratic(c);
|
||||
assert!(q.coeffs().iter().all(|c| c.is_finite()), "x = {x:e}: {q:?}");
|
||||
assert_eq!(q.eval(y0), x, "x = {x:e}");
|
||||
assert_eq!(q.eval(y3), x, "x = {x:e}");
|
||||
|
||||
let approx = QuadraticApprox::from_cubic(c, None);
|
||||
assert_eq!(approx.dmin, 0.0, "x = {x:e}");
|
||||
assert_eq!(approx.dmax, 0.0, "x = {x:e}");
|
||||
}
|
||||
}
|
||||
|
||||
// At some point, this test case looped infinitely.
|
||||
#[test]
|
||||
fn test_loop() {
|
||||
|
||||
Reference in new issue
Block a user