linesweeper: splitting a cubic no longer clamps its handles into each piece's y-range

AlmostHorizontalDecomposition::from_monotone_cubic clamped p1.y and p2.y
of every piece into the piece's y-range. A y-monotone cubic can have
handles outside that range, so the clamp moved the curve the sweep
compares. For M0,5 C5,0 2,0 1,1 C0,2 4,0 5,5 Z at eps 1e-6,
Topology::from_path(..).contours(|w| *w != 0) gave an outline of area
11.0108 instead of 10.9122, with a horizontal cut at y = 1.97218 that
lies 0.2588 from the input. The mirror image in y fails the same way.

The handles are now only kept on the right side of their end points:
p1.y is at least p0.y and p2.y at most p3.y. README.craft.md and
SOURCE.json record the change.
This commit is contained in:
dotCooCoo committed 2026-11-07 09:37:29 -08:00
1 parent 904f55bb21
commit 7626ca64be
3 files changed
+64 -11

No files matched your search

+15 -7
View File
@@ -1,15 +1,23 @@
# 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
changes listed under Local changes. `.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 files under Local changes 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 change.
## Local changes
| Change | Code comment label | Touches | Tests |
|---|---|---|---|
| `AlmostHorizontalDecomposition::from_monotone_cubic` keeps each piece's handles on the right side of its end points instead of clamping them into the piece's y-range, so the pieces the sweep compares follow the input curve | `Craft patch (monotone handles)` | `src/curve/mod.rs` | `curve::test::decomposition_keeps_handles_outside_the_y_range` |
Every change here updates this table and `SOURCE.json` in the same commit.
`omitted_files` lists the 43 files of the package left out here, with their hashes:
+4 -2
View File
@@ -7,7 +7,9 @@
"upstream_repository": null,
"upstream_revision": "824b0791a750f7b27b8edcbb6c6244d0e629875e",
"upstream_directory": "",
"local_patches": [],
"local_patches": [
"In `src/curve/mod.rs`, `AlmostHorizontalDecomposition::from_monotone_cubic` keeps `p1.y` at least `p0.y` and `p2.y` at most `p3.y` for each piece, instead of clamping both into the piece's y-range. A y-monotone cubic can have handles outside that range, and the clamp moved the curve the sweep compares: for `M0,5 C5,0 2,0 1,1 C0,2 4,0 5,5 Z` at eps 1e-6, `Topology::from_path(..).contours(|w| *w != 0)` gave an outline of area 11.0108 instead of 10.9122, with a horizontal cut at y = 1.97218 that lies 0.2588 from the input. `curve::test::decomposition_keeps_handles_outside_the_y_range` covers the shape and its mirror image in y."
],
"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": "4aa43bcd77267dcd1ba77fa898d5e096b9d47b8ab9eb7bd25ace5a73adbb97ab" },
{ "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" },
+45 -2
View File
@@ -1461,10 +1461,15 @@ impl AlmostHorizontalDecomposition {
// Enforce y-monotonicity, from the previous curve to this one, and
// also for the initial and final tangents of this subsegment.
//
// Craft patch (monotone handles): the tangents only need to point
// the right way, so p1.y is kept at least p0.y and p2.y at most
// p3.y. A monotone cubic can have handles outside its own y-range,
// and clamping them into that range moves the curve.
c.p0.y = c.p0.y.clamp(last_y, orig_c.p3.y);
c.p1.y = c.p1.y.clamp(c.p0.y, orig_c.p3.y);
c.p1.y = c.p1.y.max(c.p0.y);
c.p3.y = c.p3.y.clamp(last_y, orig_c.p3.y);
c.p2.y = c.p2.y.clamp(last_y, c.p3.y);
c.p2.y = c.p2.y.min(c.p3.y);
last_y = c.p3.y;
let approx_horiz = match order {
Right => {
@@ -1974,6 +1979,44 @@ mod test {
dbg!(intersect_cubics(c0, c1, sure_tol, ish_tol));
}
// Craft patch (monotone handles) test: after the shape is split into
// y-monotone pieces, some pieces have a handle outside their own y-range.
// The outline must still follow the input curves, for the shape and for
// its mirror image in y.
#[test]
fn decomposition_keeps_handles_outside_the_y_range() {
use kurbo::{BezPath, ParamCurve as _, ParamCurveNearest as _, Shape as _};
for svg in [
"M0,5 C5,0 2,0 1,1 C0,2 4,0 5,5 Z",
"M0,0 C5,5 2,5 1,4 C0,3 4,5 5,0 Z",
] {
let input = BezPath::from_svg(svg).unwrap();
let topology = crate::topology::Topology::<i32>::from_path(&input, 1e-6).unwrap();
let contours = topology.contours(|w| *w != 0);
let mut output = BezPath::new();
for contour in contours.contours() {
output.extend(contour.path.iter());
}
let area = output.area().abs();
assert!((area - 10.9122).abs() < 1e-3, "{svg}: area {area}");
let input_segs: Vec<_> = input.segments().collect();
for seg in output.segments() {
for i in 0..=16 {
let p = seg.eval(i as f64 / 16.0);
let dist = input_segs
.iter()
.map(|s| s.nearest(p, 1e-9).distance_sq)
.fold(f64::INFINITY, f64::min)
.sqrt();
assert!(dist < 1e-6, "{svg}: {p:?} is {dist} from the input");
}
}
}
}
#[test]
fn quadratic_comparison() {
let c0 = CubicBez::new((0.0, 0.0), (10.0, 10.0), (20.0, 20.0), (30.0, 30.0));