linesweeper: intersect_cubics no longer panics on reversed curves or bad tolerances

intersect_cubics panicked in clamp ("min > max") when either curve ran
from a larger y to a smaller one. It now compares such a curve as the
same curve with its control points reversed.

A negative or zero sure_tolerance, or a negative ish_tolerance, could
panic on an unwrap, overflow the stack on identical curves, or report
two parallel lines one unit apart as Right. With a sure_tolerance that
is not positive or is larger than ish_tolerance, or an ish_tolerance
that is not finite, the comparison now returns Ish over the common
range. Curves whose vertical ranges do not overlap give an empty order
instead of failing a debug assertion.

Topology::bounding_box returns Rect::ZERO when there are no segments,
as kurbo does for an empty path, instead of a rectangle from infinity
to negative infinity.

The docs of solve_t_for_y, y_subsegment and from_monotone_cubic state
their input rules and what happens outside them. README.craft.md,
SOURCE.json and NOTICE record the local patch.
This commit is contained in:
dotCooCoo committed 2026-11-07 12:00:47 -08:00
1 parent 904f55bb21
commit 0cbf8f472c
5 files changed
+148 -15

No files matched your search

+3 -3
View File
@@ -218,10 +218,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 no file changed. Its manifest names Joe Neeman
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 and the files
left out.
vendor/linesweeper/README.craft.md and vendor/linesweeper/SOURCE.json for provenance, the local
change and the files left out.
Platform lifecycle source provenance
+14 -7
View File
@@ -1,15 +1,22 @@
# Local linesweeper 0.5.0 copy
This is the crates.io linesweeper 0.5.0 package (MIT OR Apache-2.0) with some files left out and no
file changed. `.cargo_vcs_info.json` names the commit it was packaged from,
This is the crates.io linesweeper 0.5.0 package (MIT OR Apache-2.0) with some files left out and the
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 and `SOURCE.json` 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 hash here (`sha256`). `upstream_repository` is
null because no public repository this copy can name holds `upstream_revision`. `local_patches` is
empty.
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
hash here (`sha256`). `upstream_repository` is null because no public repository this copy can name
holds `upstream_revision`. `local_patches` describes each local patch.
## Local patches
| File | Change |
|---|---|
| `src/curve/mod.rs` | `intersect_cubics` compares a curve whose `p3.y` is below its `p0.y` as the same curve reversed, instead of panicking in `clamp`. `intersect_cubics_with_precomputed_decomp` returns close (`Ish`) over the common range when `sure_tolerance` is not positive, is larger than `ish_tolerance`, or `ish_tolerance` is not finite, instead of panicking, overflowing the stack or returning a wrong order; when the curves' vertical ranges do not overlap, it returns an empty order without a debug assertion failure. The docs of these functions, `from_monotone_cubic`, `solve_t_for_y` and `y_subsegment` state their input rules. The `curves_decreasing_in_y`, `invalid_tolerances` and `disjoint_vertical_ranges` tests cover the changes. |
| `src/topology.rs` | `Topology::bounding_box` returns `Rect::ZERO` for a topology with no segments, as kurbo does for an empty path, instead of a rectangle from infinity to negative infinity. The `empty_bounding_box` test covers it. |
`omitted_files` lists the 43 files of the package left out here, with their hashes:
+5 -3
View File
@@ -7,7 +7,9 @@
"upstream_repository": null,
"upstream_revision": "824b0791a750f7b27b8edcbb6c6244d0e629875e",
"upstream_directory": "",
"local_patches": [],
"local_patches": [
"In `src/curve/mod.rs`, `intersect_cubics` reverses a curve whose `p3.y` is below its `p0.y` before comparing it, and `intersect_cubics_with_precomputed_decomp` returns `Ish` over the common range when `sure_tolerance` is not positive or is larger than `ish_tolerance`, or `ish_tolerance` is not finite, and an empty order for curves whose vertical ranges do not overlap; the docs of these functions, `from_monotone_cubic`, `solve_t_for_y` and `y_subsegment` state their input rules. In `src/topology.rs`, `Topology::bounding_box` returns `Rect::ZERO` when there are no segments."
],
"files": [
{ "path": ".cargo_vcs_info.json", "source_sha256": "47fba6a9dbdf445a5c97f783c9be62093035f9d5e5cdc2cf3216ad8259bcd8fc", "source_git_blob": null, "sha256": "47fba6a9dbdf445a5c97f783c9be62093035f9d5e5cdc2cf3216ad8259bcd8fc" },
{ "path": ".gitignore", "source_sha256": "88b59092499ae05234330eb7d0b01e953e969caf05261530e3765eef02536cf0", "source_git_blob": null, "sha256": "88b59092499ae05234330eb7d0b01e953e969caf05261530e3765eef02536cf0" },
@@ -32,7 +34,7 @@
{ "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/mod.rs", "source_sha256": "36a8dbe96ac1d3bec307dc63b504206ec7fdfc11d10799fbb1a8990c29b8c5f2", "source_git_blob": null, "sha256": "52cb0916966f786b2ea3c33d5d60981930014a18c202577cce8b8cebeb634afe" },
{ "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" },
@@ -63,7 +65,7 @@
{ "path": "src/sweep/snapshots/linesweeper__sweep__sweep_line__tests__square_and_diamond.snap", "source_sha256": "3f01a76fb7ff4aef0fd55fd869602d4a2b69fff1bd65632f406b275b5d6af600", "source_git_blob": null, "sha256": "3f01a76fb7ff4aef0fd55fd869602d4a2b69fff1bd65632f406b275b5d6af600" },
{ "path": "src/sweep/snapshots/linesweeper__sweep__sweep_line__tests__two_squares.snap", "source_sha256": "79e3c95e563a66c0ce244125470a106a18dfe29f276b732defd305406c873c7e", "source_git_blob": null, "sha256": "79e3c95e563a66c0ce244125470a106a18dfe29f276b732defd305406c873c7e" },
{ "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": "9c9b48bc3853884425127e2d64e8ed957ba0d27e1696e2c457e95282e52d6a5b" },
{ "path": "src/topology.rs", "source_sha256": "9c9b48bc3853884425127e2d64e8ed957ba0d27e1696e2c457e95282e52d6a5b", "source_git_blob": null, "sha256": "d74f3ab448fba643daaac39c43a05c0578518053f68c74b218b1ba3ced7012e5" },
{ "path": "src/treevec.rs", "source_sha256": "d88b9fbf02fa9f318560e1d483d465555f2b696ae12c00f8e4f5b71ea6779445", "source_git_blob": null, "sha256": "d88b9fbf02fa9f318560e1d483d465555f2b696ae12c00f8e4f5b71ea6779445" },
{ "path": "src/typed_vec.rs", "source_sha256": "3e9a2675b8a40d8fc4df0575bd9d0d16b5604d18e9b78638dd5a612b28fc4c30", "source_git_blob": null, "sha256": "3e9a2675b8a40d8fc4df0575bd9d0d16b5604d18e9b78638dd5a612b28fc4c30" },
{ "path": "tests/fonts.rs", "source_sha256": "081604ebf560a19ff0427efd9a17eaaa0a3b46ad0bf58323fd6e21b6c0764400", "source_git_blob": null, "sha256": "081604ebf560a19ff0427efd9a17eaaa0a3b46ad0bf58323fd6e21b6c0764400" },
+113 -2
View File
@@ -377,6 +377,14 @@ impl CurveOrder {
}
/// Find the parameter `t` at which `c` crosses height `y`.
///
/// `c` must be y-monotone with `c.p0.y < c.p3.y`, and `y` must be in
/// `c.p0.y..=c.p3.y`. For other inputs, release builds return a parameter in
/// `0.0..=1.0` that need not be a crossing.
///
/// # Panics
///
/// Debug builds panic when `c` or `y` is outside these bounds.
pub fn solve_t_for_y(c: CubicBez, y: f64) -> f64 {
debug_assert!(
c.p0.y <= y && y <= c.p3.y && c.p0.y < c.p3.y,
@@ -426,6 +434,15 @@ pub fn solve_x_for_y(c: CubicBez, y: f64) -> f64 {
///
/// The input curve should be monotonic in `y`, and its range should include
/// `y0` and `y1` (which should be ordered).
///
/// For other inputs, release builds return a curve that starts at height `y0`
/// and ends at height `y1`, but whose ends need not lie on `c`. With `y0 > y1`
/// it runs from `y0` down to `y1`.
///
/// # Panics
///
/// Debug builds panic when `y0` is not less than `y1` or when `c` does not
/// cover `y0..=y1`.
pub fn y_subsegment(c: CubicBez, y0: f64, y1: f64) -> CubicBez {
debug_assert!(y0 < y1);
debug_assert!(c.p0.y <= y0 && y1 <= c.p3.y);
@@ -1397,6 +1414,10 @@ impl AlmostHorizontalDecomposition {
/// Decompose a cubic into almost-horizontal and not-almost-horizontal pieces.
///
/// The cubic `c` must be (approximately) y-monotone and must not be completely horizontal.
///
/// # Panics
///
/// Panics if `c` is completely horizontal or if `c.p3.y` is smaller than `c.p0.y`.
pub fn from_monotone_cubic(c: CubicBez) -> Self {
const THRESHOLD: f64 = 16.0;
let dx = cubic_from_bez_x(c).deriv();
@@ -1538,12 +1559,27 @@ impl AlmostHorizontalDecomposition {
/// The `ish_tolerance` parameter tells us when we're allowed to say that
/// the two curves are close: if they're definitely within `ish_tolerance`
/// of one another, we can say that they're close.
///
/// `sure_tolerance` must be positive and no larger than `ish_tolerance`, and
/// `ish_tolerance` must be finite. Otherwise the returned order says that the
/// curves are close over their whole common range.
///
/// Each curve must be y-monotone. A curve whose `p3.y` is smaller than its
/// `p0.y` is compared as the same curve with its control points reversed.
/// When the vertical ranges of the curves do not overlap, the returned order
/// has no entries.
///
/// # Panics
///
/// Panics if either curve is completely horizontal.
pub fn intersect_cubics(
c0: CubicBez,
c1: CubicBez,
sure_tolerance: f64,
ish_tolerance: f64,
) -> CurveOrder {
let c0 = increasing_in_y(c0);
let c1 = increasing_in_y(c1);
intersect_cubics_with_precomputed_decomp(
c0,
&AlmostHorizontalDecomposition::from_monotone_cubic(c0),
@@ -1554,11 +1590,26 @@ pub fn intersect_cubics(
)
}
// Craft patch (input rules): `intersect_cubics` compares a curve that runs
// down in y as the same curve reversed.
/// Returns `c`, or `c` with its control points reversed if it decreases in y.
fn increasing_in_y(c: CubicBez) -> CubicBez {
if c.p3.y < c.p0.y {
CubicBez::new(c.p3, c.p2, c.p1, c.p0)
} else {
c
}
}
/// A version of [intersect_cubics] that uses a precomputed almost-horizontal
/// decomposition.
///
/// Pre-computing the almost-horizontal decomposition can be much faster, because
/// then you can do it per-curve instead of per-comparison.
///
/// `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].
pub fn intersect_cubics_with_precomputed_decomp(
c0: CubicBez,
c0_div: &AlmostHorizontalDecomposition,
@@ -1571,7 +1622,15 @@ pub fn intersect_cubics_with_precomputed_decomp(
let y1 = c0.p3.y.min(c1.p3.y);
let mut ret = CurveOrder::new(y0);
if y0 < y1 {
let valid_tolerances =
sure_tolerance > 0.0 && sure_tolerance <= ish_tolerance && ish_tolerance.is_finite();
if !valid_tolerances {
// Craft patch (input rules): with a negative `ish_tolerance`,
// `intersect_cubics_rec` can recurse without end.
if y0 <= y1 {
ret.push(y1, Order::Ish, MetricsStatus::default());
}
} else if y0 < y1 {
let c0_div = c0_div
.pieces
.iter()
@@ -1620,7 +1679,7 @@ pub fn intersect_cubics_with_precomputed_decomp(
ret.push(y1, order, MetricsStatus::default());
}
//fix_up_endpoints(&mut ret, c0, c1, eps);
debug_assert_eq!(ret.cmps.last().unwrap().end, y1);
debug_assert!(y0 > y1 || ret.cmps.last().map(|e| e.end) == Some(y1));
ret
}
@@ -1790,6 +1849,58 @@ mod test {
dbg!(intersect_cubics(c0, c1, 0.5e-6, 1.5e-6));
}
// Craft patch (input rules) tests: curves that run down in y, tolerances
// outside the documented rule, and curves with no common y-range.
fn vertical(x: f64, y0: f64, y1: f64) -> CubicBez {
PathSeg::Line(Line::new((x, y0), (x, y1))).to_cubic()
}
#[test]
fn curves_decreasing_in_y() {
let up0 = vertical(0.0, 0.0, 1.0);
let up1 = vertical(1.0, 0.0, 1.0);
let down0 = vertical(0.0, 1.0, 0.0);
let down1 = vertical(1.0, 1.0, 0.0);
for (c0, c1) in [(down0, down1), (down0, up1), (up0, down1)] {
let cmp = intersect_cubics(c0, c1, 1e-6, 2e-6);
assert_eq!(cmp.iter().collect::<Vec<_>>(), [(0.0, 1.0, Order::Left)]);
}
}
#[test]
fn invalid_tolerances() {
let c0 = vertical(0.0, 0.0, 1.0);
let parallel = vertical(1.0, 0.0, 1.0);
let crossing = CubicBez::new(
(1.0, 0.0),
(2.0 / 3.0, 1.0 / 3.0),
(1.0 / 3.0, 2.0 / 3.0),
(0.0, 1.0),
);
let tolerances = [
(-1.0, -1.0),
(-1.0, 1e-6),
(1e-6, -1.0),
(-1e-9, -1e-9),
(0.0, 0.0),
(2e-6, 1e-6),
(1e-6, f64::INFINITY),
(f64::NAN, 1e-6),
];
for (sure, ish) in tolerances {
for c1 in [c0, parallel, crossing] {
let cmp = intersect_cubics(c0, c1, sure, ish);
assert_eq!(cmp.iter().collect::<Vec<_>>(), [(0.0, 1.0, Order::Ish)]);
}
}
}
#[test]
fn disjoint_vertical_ranges() {
let cmp = intersect_cubics(vertical(0.0, 0.0, 1.0), vertical(1.0, 2.0, 3.0), 1e-6, 2e-6);
assert_eq!(cmp.iter().count(), 0);
}
#[test]
fn y_slop_single_seg() {
let order = CurveOrder {
+13
View File
@@ -1453,7 +1453,13 @@ impl<W: WindingNumber> Topology<W> {
}
/// Returns a rectangle bounding all of our segments.
///
/// With no segments, it returns `Rect::ZERO`, as kurbo does for an empty path.
pub fn bounding_box(&self) -> kurbo::Rect {
// Craft patch (input rules): an empty topology has a zero rectangle.
if self.segments.segments().next().is_none() {
return Rect::ZERO;
}
let mut rect = Rect::new(
f64::INFINITY,
f64::INFINITY,
@@ -2288,6 +2294,13 @@ mod tests {
insta::assert_ron_snapshot!(top);
}
// Craft patch (input rules) test.
#[test]
fn empty_bounding_box() {
let top = Topology::from_polylines_binary(EMPTY, EMPTY, 0.01);
assert_eq!(top.bounding_box(), kurbo::Rect::ZERO);
}
#[test]
fn diamond() {
let segs = [[p(0.0, 0.0), p(1.0, 1.0), p(0.0, 2.0), p(-1.0, 1.0)]];