diff --git a/NOTICE b/NOTICE index bbd9c7b026..2ef4a30f22 100644 --- a/NOTICE +++ b/NOTICE @@ -319,6 +319,9 @@ vendor/linesweeper/README.craft.md and vendor/linesweeper/SOURCE.json for proven vendor/linesweeper with its upstream license files and one local patch. 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 +vendor/linesweeper with its upstream license files and one local patch. 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 change and the files left out. Platform lifecycle source provenance diff --git a/vendor/linesweeper/README.craft.md b/vendor/linesweeper/README.craft.md index c8c6367aab..3e20b3d45d 100644 --- a/vendor/linesweeper/README.craft.md +++ b/vendor/linesweeper/README.craft.md @@ -255,6 +255,10 @@ local patches listed below. `.cargo_vcs_info.json` names the commit it was packa `824b0791a750f7b27b8edcbb6c6244d0e629875e`. That commit is not in the GitHub repository the manifest names, which is archived; its README says development moved off GitHub. +local patches listed below. `.cargo_vcs_info.json` names the commit it was packaged from, +`824b0791a750f7b27b8edcbb6c6244d0e629875e`. That commit is not in the GitHub repository the manifest +names, which is archived; its README says development moved off GitHub. + Every file here except this one, `SOURCE.json` and the patched files below is byte for byte the file in the `.crate` archive. `SOURCE.json` records the hash of the `.crate` download (`source_archive_sha256`) and, for every file kept, the hash in the package (`source_sha256`) and the @@ -326,6 +330,10 @@ Every change here updates this table and `SOURCE.json` in the same commit. | `src/curve/line.rs` | `intersect_lines` compares the x coordinates and the tolerance times 1/16 when one of them has a magnitude above 2^1019, and works with half of each height for a line or height range more than `f64::MAX` tall. Before, two vertical lines at x = -1e308 and x = 1e308 came back `Right`, because the difference of their x values overflowed, and lines from y = -1e308 to 1e308 gave NaN positions. The `unchanging_with_huge_coordinates` test covers 1e300, 1e308 and `f64::MAX`, and `huge_coordinates_on_one_axis` covers lines near x = 1e308 that are 1e-307 tall, lines from y = -1e308 to 1e308 that stay apart or cross, and a line 1e-308 tall beside one of them. | | `src/geom.rs` | For a straight segment more than `f64::MAX` long on an axis, `Segment::at_y` works with half of each coordinate on that axis and `Segment::to_kurbo_cubic` takes weighted sums of the end points on that axis. Before, both returned NaN or infinite coordinates for such a segment. The `line_wider_and_taller_than_f64_max` test covers a segment from (-1e308, -1e308) to (1e308, 1e308), and `line_wider_than_f64_max_and_very_short` covers a segment from (-1e308, 0) to (1e308, 3 times the smallest positive `f64`). | | `src/lib.rs` | The `union_with_huge_coordinates` test checks the union of a circle and a rectangle at sizes 1e155, 1e200 and 1e300. | +| `src/lib.rs` | `binary_op_with_eps` returns the new `Error::InvalidEps` when `eps` is NaN, infinite, zero or negative, instead of returning a wrong region (NaN or negative `eps`) or hanging (`-inf`). The `binary_op_with_eps_rejects_invalid_eps`, `topology_raises_unusable_eps` and `binary_op_with_eps_raises_eps_below_coordinate_resolution` tests cover the changes in this file, `src/segments.rs`, `src/sweep/sweep_line.rs` and `src/topology.rs`. | +| `src/segments.rs`, `src/sweep/sweep_line.rs`, `src/topology.rs` | The `Topology` constructors and `Sweeper::new_with_correctness` replace an `eps` that is NaN, infinite, not positive, or below `64 * f64::EPSILON` times the largest coordinate magnitude (and at least 64 times the spacing of subnormal floats) with that value, the scale `binary_op` uses. Before, an infinite `eps` made a lens of two arcs vanish, and an `eps` below one ulp of the coordinates (0 included, or 1e-10 at 1.5e8) overflowed the stack and aborted the process. | +| `src/curve/mod.rs` | `intersect_cubics` and `intersect_cubics_with_precomputed_decomp` raise a positive `ish_tolerance` below `4 * f64::EPSILON` times the two curves' largest x coordinate magnitude (two to four ulps of it, and at least four times the spacing of subnormal floats) to that value, and a positive `sure_tolerance` below half of it to half of it. Below that, rounding made the comparison report the wrong order. `intersect_cubics_rec` returns close (`Ish`) for an interval whose midpoint rounds to one of its ends instead of splitting it again, which repeated the same call until the stack overflowed. The `ish_tolerance_below_float_spacing` test covers both. `raised_tolerance_keeps_far_curves_apart`, `raised_tolerance_ignores_large_y` and `raised_tolerance_keeps_subnormal_curves_apart` check that lines 1.0 apart at x = 1e15, lines 1.0 apart at y = 2e15, and lines at x = 1e-310 and 2e-310 stay ordered. | +| `src/position.rs` | `approximate` stops halving an interval whose midpoint rounds to one of its ends. Before, it could return the interval's start, and the loop in `compute_positions_correct` then made no progress. The `approx_stops_at_one_float` test covers it. | `omitted_files` lists the 43 files of the package left out here, with their hashes: diff --git a/vendor/linesweeper/SOURCE.json b/vendor/linesweeper/SOURCE.json index d31028d919..51118f33dc 100644 --- a/vendor/linesweeper/SOURCE.json +++ b/vendor/linesweeper/SOURCE.json @@ -67,6 +67,7 @@ "`F64Perturbation::apply` clamps a ulp step on a normal value to infinity when the bits reach the infinity encoding, instead of testing the sign bit, so `Ulp(2)` of `f64::MAX` gives infinity instead of NaN." "`ComparisonCache` in `src/order.rs` holds the comparisons of one `Segments` arena. `compare_segments` reads the arena's number, which `src/segments.rs` gives each arena and each clone, and drops the cached comparisons when the number differs from the one they were made with. Before, a cache given a second arena returned the first arena's order for the same pair of indices." "In `src/curve/mod.rs`, `fit_quadratic` multiplies the heights by the power of two that brings the largest to between 2 and 4 when one of them has a magnitude above 2^500 and scales the coefficients back, and `QuadraticApprox::from_cubic` multiplies by the `y^2` coefficient first when a product of two y coefficients is not finite, so curves taller than about 1.3e154 no longer give NaN coefficients or error bounds that make curve comparisons, and `binary_op`, return a wrong order without an error. The quadratic sign checks of curve comparisons report an unknown (or close) order for a quadratic with a NaN or infinite coefficient. In `src/curve/line.rs`, `intersect_lines` scales the x coordinates and the tolerance by 1/16 when one of them is above 2^1019 and works with half of each height for lines more than `f64::MAX` tall, so coordinates near `f64::MAX` no longer give a wrong order. In `src/geom.rs`, `Segment::at_y` and `Segment::to_kurbo_cubic` give finite results for a straight segment more than `f64::MAX` long on either axis. `src/curve/mod.rs`, `src/curve/line.rs`, `src/geom.rs` and `src/lib.rs` add tests for it." + "In `src/lib.rs`, `src/segments.rs`, `src/sweep/sweep_line.rs` and `src/topology.rs`, `binary_op_with_eps` returns the new `Error::InvalidEps` for an `eps` that is NaN, infinite, zero or negative, and the `Topology` constructors and `Sweeper::new_with_correctness` replace an `eps` that is NaN, infinite, not positive, or below `64 * f64::EPSILON` times the largest coordinate magnitude (and at least 64 times the spacing of subnormal floats) with that value, so a bad `eps` no longer gives a wrong region, a hang or a stack overflow. In `src/curve/mod.rs`, `intersect_cubics` and `intersect_cubics_with_precomputed_decomp` raise a positive `ish_tolerance` below `4 * f64::EPSILON` times the curves' largest x coordinate magnitude (and at least four times the spacing of subnormal floats) to that value and a positive `sure_tolerance` below half of it to half of it, and `intersect_cubics_rec` returns `Ish` for an interval whose midpoint rounds to one of its ends instead of recursing on it again. In `src/position.rs`, `approximate` stops halving such an interval, so `compute_positions_correct` always advances." ], "files": [ { "path": ".cargo_vcs_info.json", "source_sha256": "47fba6a9dbdf445a5c97f783c9be62093035f9d5e5cdc2cf3216ad8259bcd8fc", "source_git_blob": null, "sha256": "47fba6a9dbdf445a5c97f783c9be62093035f9d5e5cdc2cf3216ad8259bcd8fc" }, @@ -117,6 +118,7 @@ { "path": "src/curve/mod.rs", "source_sha256": "36a8dbe96ac1d3bec307dc63b504206ec7fdfc11d10799fbb1a8990c29b8c5f2", "source_git_blob": null, "sha256": "36ace9cc769ed06e6aaaf2060828e09394a15adc77fcec25378ff793be4135d8" }, { "path": "src/curve/mod.rs", "source_sha256": "36a8dbe96ac1d3bec307dc63b504206ec7fdfc11d10799fbb1a8990c29b8c5f2", "source_git_blob": null, "sha256": "f992312ad44b74336a18c992cbabff0bb8a42bd759bc4bee778586b44f2dfe1d" }, { "path": "src/curve/mod.rs", "source_sha256": "36a8dbe96ac1d3bec307dc63b504206ec7fdfc11d10799fbb1a8990c29b8c5f2", "source_git_blob": null, "sha256": "7bbc99e120d3af0af93df9b6f2fde4da7b5075fef7588f8bf3c4807c007e9d76" }, + { "path": "src/curve/mod.rs", "source_sha256": "36a8dbe96ac1d3bec307dc63b504206ec7fdfc11d10799fbb1a8990c29b8c5f2", "source_git_blob": null, "sha256": "cbc6f444153525e7da85473466434b07826c75d0500773a0aa3cf068c5225d09" }, { "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" }, @@ -139,6 +141,7 @@ { "path": "src/generators.rs", "source_sha256": "cc9f2441546db9204219294c354449809dda77dffad06fec8a30f45df01e6fef", "source_git_blob": null, "sha256": "cc9f2441546db9204219294c354449809dda77dffad06fec8a30f45df01e6fef" }, { "path": "src/geom.rs", "source_sha256": "9878ffa6d4a13fa2c9149741c137205ca01acb626a5b9f86a3db75cf91b63a08", "source_git_blob": null, "sha256": "edf80b2320ba371502cfef13e6fbe3e23ecfc67f254b9b7e5715776deeeffa8a" }, { "path": "src/lib.rs", "source_sha256": "761c153c3b5c449569ff85c571344b9252ea64f7f0b0424fb4819041b03d13c6", "source_git_blob": null, "sha256": "8a341eb0eafb16c68ddd1e51f1ad3121597825eb780b3624bd0428b5a3f87908" }, + { "path": "src/lib.rs", "source_sha256": "761c153c3b5c449569ff85c571344b9252ea64f7f0b0424fb4819041b03d13c6", "source_git_blob": null, "sha256": "5eab08a7e620f1eca3744826ceb2c575abc6b9154315feba35db486380e2edc5" }, { "path": "src/num.rs", "source_sha256": "39ff85da50c500ce91cf6dea81aedc0dcca1db5e7d0506696dde2da118e93a2a", "source_git_blob": null, "sha256": "39ff85da50c500ce91cf6dea81aedc0dcca1db5e7d0506696dde2da118e93a2a" }, { "path": "src/order.rs", "source_sha256": "7e5b04eae8af134abb032e995f1d3c2ac473822923dfee8d463d9ade6ffeb596", "source_git_blob": null, "sha256": "8d2393e43000543c4ac3d47c2615e16733106af78ac67708ebf6941a039b9cdc" }, { "path": "src/order.rs", "source_sha256": "7e5b04eae8af134abb032e995f1d3c2ac473822923dfee8d463d9ade6ffeb596", "source_git_blob": null, "sha256": "2cce12b9110a5812ce0da81088122d4d64e8852d41bad9139bd29ff9d4363a0f" }, @@ -159,6 +162,8 @@ { "path": "src/segments.rs", "source_sha256": "379f9455120492d237cb5a62b3fc06da018a72e2eb75bbfcd61b4cce501d9bbc", "source_git_blob": null, "sha256": "74478b647c0243a18bf0e34af28735544754b42445910cd4145aa1c31826d24d" }, { "path": "src/segments.rs", "source_sha256": "379f9455120492d237cb5a62b3fc06da018a72e2eb75bbfcd61b4cce501d9bbc", "source_git_blob": null, "sha256": "5b3ca2c01ac51b55d950372cad15a5ccb2dfb50a6e568ac94b855f611ae25eea" }, { "path": "src/segments.rs", "source_sha256": "379f9455120492d237cb5a62b3fc06da018a72e2eb75bbfcd61b4cce501d9bbc", "source_git_blob": null, "sha256": "e9cd649b07cd285d9abde07e82260c8a23031f1c5f530320f3853734ef0869de" }, + { "path": "src/position.rs", "source_sha256": "dfa70840805ecccf5b7eb6122921e46ca60c24cb7b3c17f3b55ffc2549368ab0", "source_git_blob": null, "sha256": "0934efacd54a7bbd55705bbbbea6bd7820df3c4d3f4e97673eb68fdfc9007bf0" }, + { "path": "src/segments.rs", "source_sha256": "379f9455120492d237cb5a62b3fc06da018a72e2eb75bbfcd61b4cce501d9bbc", "source_git_blob": null, "sha256": "16976779627959735493eb16177220148048cbb9fed0efb85112b50b0f78c2f4" }, { "path": "src/snapshots/linesweeper__tests__empty_horizontals.snap", "source_sha256": "e319881853e889badf6524f19af4cd6f112e8dd71bdc516826f4463134a82244", "source_git_blob": null, "sha256": "e319881853e889badf6524f19af4cd6f112e8dd71bdc516826f4463134a82244" }, { "path": "src/snapshots/linesweeper__tests__two_squares.snap", "source_sha256": "01961e0311a5af3b8f13eb52a2a7112560c47cf86771c04e40475dc6f15a0846", "source_git_blob": null, "sha256": "01961e0311a5af3b8f13eb52a2a7112560c47cf86771c04e40475dc6f15a0846" }, { "path": "src/snapshots/linesweeper__topology__tests__diamond.snap", "source_sha256": "cfaefd620a0a3d1376861bca09cd7c658ef00a32635b88f1b259a9b39cd15502", "source_git_blob": null, "sha256": "cfaefd620a0a3d1376861bca09cd7c658ef00a32635b88f1b259a9b39cd15502" }, @@ -210,6 +215,8 @@ { "path": "src/topology.rs", "source_sha256": "9c9b48bc3853884425127e2d64e8ed957ba0d27e1696e2c457e95282e52d6a5b", "source_git_blob": null, "sha256": "12a3a120b6bd89091387e12efbc1a408fdbaed786d98a7a115dcd4ab9f916081" }, { "path": "src/sweep/sweep_line.rs", "source_sha256": "822b261f682911ea0c0370eaf17d9201a1e639b315359dcb6a3b877a229816e3", "source_git_blob": null, "sha256": "822b261f682911ea0c0370eaf17d9201a1e639b315359dcb6a3b877a229816e3" }, { "path": "src/topology.rs", "source_sha256": "9c9b48bc3853884425127e2d64e8ed957ba0d27e1696e2c457e95282e52d6a5b", "source_git_blob": null, "sha256": "c520c286f7bb896546beb312ec114f7f8019a5af0094c1ab149fa36dc36f96b8" }, + { "path": "src/sweep/sweep_line.rs", "source_sha256": "822b261f682911ea0c0370eaf17d9201a1e639b315359dcb6a3b877a229816e3", "source_git_blob": null, "sha256": "8e901d5cb80f2b920db1c5be85fdd31d6a31ca4b71191aea930a17a90ede1a35" }, + { "path": "src/topology.rs", "source_sha256": "9c9b48bc3853884425127e2d64e8ed957ba0d27e1696e2c457e95282e52d6a5b", "source_git_blob": null, "sha256": "701a53d627f3ef2ccde552fb1d35fb445ef51dba356fcce0e9f9f091bf71f66f" }, { "path": "src/treevec.rs", "source_sha256": "d88b9fbf02fa9f318560e1d483d465555f2b696ae12c00f8e4f5b71ea6779445", "source_git_blob": null, "sha256": "d88b9fbf02fa9f318560e1d483d465555f2b696ae12c00f8e4f5b71ea6779445" }, { "path": "src/sweep/sweep_line.rs", "source_sha256": "822b261f682911ea0c0370eaf17d9201a1e639b315359dcb6a3b877a229816e3", "source_git_blob": null, "sha256": "5dfbff54f25438ddb800f4136af5c7ef1d6d2a3a626ea5311dacefd884c5c9e8" }, { "path": "src/topology.rs", "source_sha256": "9c9b48bc3853884425127e2d64e8ed957ba0d27e1696e2c457e95282e52d6a5b", "source_git_blob": null, "sha256": "9c9b48bc3853884425127e2d64e8ed957ba0d27e1696e2c457e95282e52d6a5b" }, diff --git a/vendor/linesweeper/src/curve/mod.rs b/vendor/linesweeper/src/curve/mod.rs index 77d02019db..03b4342383 100644 --- a/vendor/linesweeper/src/curve/mod.rs +++ b/vendor/linesweeper/src/curve/mod.rs @@ -1369,6 +1369,30 @@ fn max_distance(c0: CubicBez, c1: CubicBez) -> f64 { .max(l_infty_distance(c0.p3, c1.p3)) } +/// The largest absolute value of any control point's x coordinate. +fn max_abs_x(c: CubicBez) -> f64 { + [c.p0, c.p1, c.p2, c.p3] + .iter() + .fold(0.0, |m: f64, p| m.max(p.x.abs())) +} + +/// The spacing of the smallest floats, which is also the spacing of all +/// subnormal floats. +const MIN_SPACING: f64 = f64::from_bits(1); + +/// The smallest tolerance that tells apart coordinates of magnitude up to +/// `max_coord`: `64 * f64::EPSILON * max_coord`, the scale [`crate::binary_op`] +/// uses, and never below 64 times the spacing of subnormal floats. +pub(crate) fn resolution_floor(max_coord: f64) -> f64 { + (max_coord * (f64::EPSILON * 64.0)).max(MIN_SPACING * 64.0) +} + +/// `f64::EPSILON * max_coord`, between one and two ulps of `max_coord`, and +/// never below the spacing of subnormal floats. +fn float_spacing(max_coord: f64) -> f64 { + (max_coord * f64::EPSILON).max(MIN_SPACING) +} + #[derive(Clone, Debug)] struct CubicParams { c: CubicBez, @@ -1516,7 +1540,14 @@ fn intersect_cubics_rec( // "ish" interval that's just an artifact. In this case, new_y0 and new_y1 // will be very close, but we'll recurse one more time to get a better // quadratic approximation. + // + // When `mid` rounds to one of the ends, the interval is one float + // wide and cannot be split, so splitting it would repeat the same + // call until the stack overflows. That happens when `ish_tolerance` + // is below the spacing of floats at this height. if y1 - y0 <= ish_tolerance + || mid <= new_y0 + || mid >= new_y1 || (ep0.dmax + ep1.dmax <= 2.0 * ish_tolerance && ep0.dmin + ep1.dmin >= -2.0 * ish_tolerance) { @@ -1739,6 +1770,12 @@ impl AlmostHorizontalDecomposition { /// When the vertical ranges of the curves do not overlap, the returned order /// has no entries. /// +/// A positive `ish_tolerance` below `4 * f64::EPSILON` times the largest +/// x coordinate magnitude of the two curves (two to four ulps of it, and at +/// least four times the spacing of subnormal floats) is raised to that value, +/// and a positive `sure_tolerance` below half of it is raised to half of it. +/// Below that, rounding in the comparison can report the wrong order. +/// /// # Panics /// /// Panics if either curve is completely horizontal. @@ -1779,7 +1816,7 @@ fn increasing_in_y(c: CubicBez) -> CubicBez { /// /// `c0_div` and `c1_div` must be the decompositions of `c0` and `c1`, so each /// curve must have `p0.y < p3.y`. The tolerances follow the same rule as in -/// [intersect_cubics]. +/// [intersect_cubics] and are raised as in [intersect_cubics]. pub fn intersect_cubics_with_precomputed_decomp( c0: CubicBez, c0_div: &AlmostHorizontalDecomposition, @@ -1834,6 +1871,18 @@ fn intersect_decompositions( ish_tolerance: f64, approx_first: bool, ) -> CurveOrder { + let floor = 4.0 * float_spacing(max_abs_x(c0).max(max_abs_x(c1))); + let ish_tolerance = if ish_tolerance > 0.0 && ish_tolerance < floor { + floor + } else { + ish_tolerance + }; + let sure_tolerance = if sure_tolerance > 0.0 && sure_tolerance < floor / 2.0 { + floor / 2.0 + } else { + sure_tolerance + }; + let y0 = c0.p0.y.max(c1.p0.y); let y1 = c0.p3.y.min(c1.p3.y); @@ -2791,6 +2840,70 @@ mod test { assert!(cmp.iter().all(|(_, _, order)| order != Order::Right)); } + // At a height of 1.5e8 one ulp is about 3e-8, more than `ish_tolerance`. + // Splitting the close interval around a crossing used to repeat the same + // one-ulp interval until the stack overflowed. + #[test] + fn ish_tolerance_below_float_spacing() { + let offset = 1.5e8; + let mut segs = crate::Segments::default(); + segs.add_bez_path(&kurbo::Circle::new((offset, offset), 1.0).to_path(1e-9)) + .unwrap(); + segs.add_bez_path(&kurbo::Circle::new((offset + 1.0, offset), 1.0).to_path(1e-9)) + .unwrap(); + let cubics: Vec<_> = segs.segments().map(|s| s.to_kurbo_cubic()).collect(); + let mut compared = 0; + for c0 in &cubics { + for c1 in &cubics { + let y1 = c0.p3.y.min(c1.p3.y); + if c0.p0.y.max(c1.p0.y) >= y1 { + continue; + } + let order = intersect_cubics(*c0, *c1, 5e-11, 1e-10); + order.check_invariants(); + assert_eq!(order.iter().last().map(|(_, end, _)| end), Some(y1)); + compared += 1; + } + } + assert!(compared > 0); + } + + // Raising tiny tolerances must not merge curves that are a few ulps + // apart: at 1e15 one ulp is 0.125, and these lines are 1.0 apart. + #[test] + fn raised_tolerance_keeps_far_curves_apart() { + let x = 1e15; + let c0 = PathSeg::Line(Line::new((x, 0.0), (x, 1.0))).to_cubic(); + let c1 = PathSeg::Line(Line::new((x + 1.0, 0.0), (x + 1.0, 1.0))).to_cubic(); + let order = intersect_cubics(c0, c1, 1e-12, 1e-11); + let entries: Vec<_> = order.iter().collect(); + assert_eq!(entries, vec![(0.0, 1.0, Order::Left)]); + } + + // The floor depends on the x coordinates only: large y coordinates do + // not make lines 1.0 apart close. + #[test] + fn raised_tolerance_ignores_large_y() { + let y = 2e15; + let c0 = PathSeg::Line(Line::new((0.0, y), (0.0, y + 1.0))).to_cubic(); + let c1 = PathSeg::Line(Line::new((1.0, y), (1.0, y + 1.0))).to_cubic(); + let order = intersect_cubics(c0, c1, 1e-12, 1e-11); + let entries: Vec<_> = order.iter().collect(); + assert_eq!(entries, vec![(y, y + 1.0, Order::Left)]); + } + + // Subnormal coordinates keep their order: the floor goes down to the + // spacing of subnormal floats, not to `f64::MIN_POSITIVE`. + #[test] + fn raised_tolerance_keeps_subnormal_curves_apart() { + let x = 1e-310; + let c0 = PathSeg::Line(Line::new((x, 0.0), (x, 1.0))).to_cubic(); + let c1 = PathSeg::Line(Line::new((2.0 * x, 0.0), (2.0 * x, 1.0))).to_cubic(); + let order = intersect_cubics(c0, c1, 1e-320, 2e-320); + let entries: Vec<_> = order.iter().collect(); + assert_eq!(entries, vec![(0.0, 1.0, Order::Left)]); + } + #[test] fn quadratic_comparison() { let c0 = CubicBez::new((0.0, 0.0), (10.0, 10.0), (20.0, 20.0), (30.0, 30.0)); diff --git a/vendor/linesweeper/src/lib.rs b/vendor/linesweeper/src/lib.rs index c9ab24f59e..8b86810f2d 100644 --- a/vendor/linesweeper/src/lib.rs +++ b/vendor/linesweeper/src/lib.rs @@ -82,6 +82,8 @@ pub enum Error { /// The sweep did more work (segment comparisons, position checks and linear searches of the /// sweep line) than its work limit allows. WorkLimitExceeded, + /// The accuracy parameter was NaN, infinite, zero or negative. + InvalidEps, } /// The largest coordinate magnitude that [`Topology`] and [`binary_op`] accept. @@ -132,6 +134,9 @@ impl std::fmt::Display for Error { Error::WorkLimitExceeded => { write!(f, "the sweep did more work than its work limit") } + Error::InvalidEps => { + write!(f, "the accuracy parameter was not a positive finite number") + } } } } @@ -188,6 +193,10 @@ pub fn binary_op( /// /// The output paths will not exactly agree with the input paths in general. /// The accuracy parameter `eps` bounds how much they can differ. +/// +/// Returns [`Error::InvalidEps`] when `eps` is NaN, infinite, zero or +/// negative. An `eps` below `64 * f64::EPSILON` times the largest coordinate +/// magnitude is raised to that value. pub fn binary_op_with_eps( set_a: &kurbo::BezPath, set_b: &kurbo::BezPath, @@ -195,6 +204,9 @@ pub fn binary_op_with_eps( op: BinaryOp, eps: f64, ) -> Result { + if !(eps.is_finite() && eps > 0.0) { + return Err(Error::InvalidEps); + } let top = Topology::from_paths_binary(set_a, set_b, eps)?; #[cfg(feature = "debug-svg")] { @@ -481,6 +493,44 @@ mod tests { } } + /// Two unit circles whose centers are one unit apart, at `(offset, offset)` + /// and `(offset + 1, offset)`. + fn crossing_circles(offset: f64) -> (BezPath, BezPath) { + ( + kurbo::Circle::new((offset, offset), 1.0).to_path(1e-9), + kurbo::Circle::new((offset + 1.0, offset), 1.0).to_path(1e-9), + ) + } + + /// The single contour's area, measured after moving it back to the origin. + fn single_contour_area(contours: &topology::Contours, offset: f64) -> f64 { + let contours: Vec<_> = contours.contours().collect(); + assert_eq!(contours.len(), 1); + (kurbo::Affine::translate((-offset, -offset)) * contours[0].path.clone()) + .area() + .abs() + } + + const UNION_AREA: f64 = 5.054816; + + #[test] + fn binary_op_with_eps_rejects_invalid_eps() { + let (a, b) = crossing_circles(0.0); + // -inf goes last: before the check it made the sweep loop forever. + for eps in [ + f64::NAN, + -1.0, + -1e-6, + 0.0, + -0.0, + f64::INFINITY, + f64::NEG_INFINITY, + ] { + let result = binary_op_with_eps(&a, &b, FillRule::NonZero, BinaryOp::Union, eps); + assert_eq!(result.err(), Some(Error::InvalidEps), "eps = {eps}"); + } + } + #[test] fn union_with_huge_coordinates() { use kurbo::{Affine, Circle, Rect}; @@ -511,4 +561,47 @@ mod tests { ); } } + + #[test] + fn topology_raises_unusable_eps() { + let inside = |w: &BinaryWindingNumber| w.shape_a != 0 || w.shape_b != 0; + // At 1.5e8 one ulp is about 3e-8, so 1e-10 used to recurse in + // intersect_cubics_rec until the stack overflowed. + for (offset, eps) in [ + (0.0, f64::NAN), + (0.0, -1.0), + (0.0, 0.0), + (1.5e8, 1e-10), + (1.5e8, 0.0), + (0.0, f64::NEG_INFINITY), + (0.0, f64::INFINITY), + ] { + let (a, b) = crossing_circles(offset); + for correctness in [ + sweep::Correctness::GoodEnough, + sweep::Correctness::ReallyCorrect, + ] { + let top = Topology::from_paths_with_correctness( + [(&a, topology::OneOfTwo::A), (&b, topology::OneOfTwo::B)], + eps, + correctness, + ) + .unwrap(); + let area = single_contour_area(&top.contours(inside), offset); + assert!( + (area - UNION_AREA).abs() < 1e-3, + "offset {offset}, eps {eps}, {correctness:?}: area {area}" + ); + } + } + } + + #[test] + fn binary_op_with_eps_raises_eps_below_coordinate_resolution() { + let (a, b) = crossing_circles(1.5e8); + let contours = + binary_op_with_eps(&a, &b, FillRule::NonZero, BinaryOp::Union, 1e-10).unwrap(); + let area = single_contour_area(&contours, 1.5e8); + assert!((area - UNION_AREA).abs() < 1e-3, "area {area}"); + } } diff --git a/vendor/linesweeper/src/position.rs b/vendor/linesweeper/src/position.rs index 68c231693c..0b3a33150a 100644 --- a/vendor/linesweeper/src/position.rs +++ b/vendor/linesweeper/src/position.rs @@ -168,7 +168,10 @@ fn approximate( let factor = horizontal_error_weight(cubic); - let too_short = y1 <= y0 + accuracy; + // An interval whose midpoint rounds to one of its ends cannot be + // halved. Halving it anyway would leave `y1` where it is, or return + // `y0` and stop the caller's loop from advancing. + let too_short = y1 <= y0 + accuracy || y_mid <= y0 || y_mid >= y1; let accurate = approx.dmax.max(-approx.dmin) * factor < accuracy; if !too_short && !accurate { // TODO: can we be smarter about this? @@ -610,4 +613,26 @@ mod tests { assert!(below[2] > 0, "{below:?}"); assert_eq!(below, above); } + + // With an accuracy that no approximation meets, the interval is halved + // until it is one float wide. At 1.5e8 the midpoint of such an interval + // rounds to its start, and `approximate` used to return the start, which + // left the loop in `compute_positions_correct` without progress. + #[test] + fn approx_stops_at_one_float() { + let y0 = 1.5e8; + let mut p = BezPath::new(); + p.move_to((y0, y0)); + p.curve_to((y0 + 1.0, y0 + 0.3), (y0 - 1.0, y0 + 0.7), (y0, y0 + 1.0)); + p.close_path(); + + let mut segs = Segments::default(); + segs.add_bez_path(&p).unwrap(); + let s0 = segs.indices().next().unwrap(); + + let mut out = Vec::new(); + let next_y0 = super::approximate(y0, y0 + 1.0, &[s0], &segs, 0.0, &mut out); + assert!(next_y0 > y0, "next_y0 = {next_y0}"); + assert_eq!(out.len(), 1); + } } diff --git a/vendor/linesweeper/src/segments.rs b/vendor/linesweeper/src/segments.rs index 3b4383c0b1..0457b6cef0 100644 --- a/vendor/linesweeper/src/segments.rs +++ b/vendor/linesweeper/src/segments.rs @@ -538,6 +538,33 @@ impl Segments { debug_assert!(!self.segs[idx].is_horizontal()); &self.horizontal_decomps[idx] } + + /// The sweep tolerance to use for these segments when the caller asks for `eps`. + /// + /// Returns `eps` when it is at least `64 * f64::EPSILON` times the largest + /// coordinate magnitude (the scale [`crate::binary_op`] uses), and that + /// floor otherwise. The floor replaces an `eps` that is NaN, infinite, + /// negative, zero, or too small to tell coordinates of this size apart. It + /// is never below 64 times the spacing of subnormal floats. + pub(crate) fn usable_eps(&self, eps: f64) -> f64 { + let max_coord = self + .segs + .iter() + .map(|(_, seg)| { + seg.min_x() + .abs() + .max(seg.max_x().abs()) + .max(seg.start().y.abs()) + .max(seg.end().y.abs()) + }) + .fold(0.0, f64::max); + let floor = crate::curve::resolution_floor(max_coord); + if eps.is_finite() && eps >= floor { + eps + } else { + floor + } + } } impl std::ops::Index for Segments { diff --git a/vendor/linesweeper/src/sweep/sweep_line.rs b/vendor/linesweeper/src/sweep/sweep_line.rs index f8dfbe4c61..7d43771e51 100644 --- a/vendor/linesweeper/src/sweep/sweep_line.rs +++ b/vendor/linesweeper/src/sweep/sweep_line.rs @@ -409,11 +409,15 @@ impl<'segs> Sweeper<'segs> { /// The really correct mode doesn't actually change any of the /// output orders, but it does emit extra events whenever lines /// touch without crossing. + /// + /// An `eps` that is NaN, infinite, not positive, or below `64 * f64::EPSILON` + /// times the segments' largest coordinate magnitude is replaced by that value. pub fn new_with_correctness( segments: &'segs Segments, eps: f64, correctness: Correctness, ) -> Self { + let eps = segments.usable_eps(eps); let events = EventQueue::default(); Sweeper { eps, diff --git a/vendor/linesweeper/src/topology.rs b/vendor/linesweeper/src/topology.rs index 286e487951..467fb0429e 100644 --- a/vendor/linesweeper/src/topology.rs +++ b/vendor/linesweeper/src/topology.rs @@ -530,6 +530,10 @@ impl Topology { /// /// Returns an error if a path is not closed, or if a coordinate is not finite or has a /// magnitude above [`crate::MAX_COORDINATE`]. + /// + /// An `eps` that is NaN, infinite, not positive, or below `64 * f64::EPSILON` + /// times the paths' largest coordinate magnitude is replaced by that value. The + /// other constructors pass `eps` through here, or follow the same rule. pub fn from_paths_with_correctness<'a, Iter>(paths: Iter, eps: f64, correctness: Correctness) -> Result where Iter: IntoIterator, @@ -596,6 +600,7 @@ impl Topology { correctness: Correctness, work_limit: Option, ) -> Result { + let eps = segments.usable_eps(eps); let mut ret = Self { eps, correctness,