From 4df639a818b6d2eb1bf0a5f2f5b574ec595c90ac Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 7 Nov 2026 18:41:53 -0800 Subject: [PATCH] linesweeper: contours builds paths only for boundary segments Topology::contours called compute_positions, which with Correctness::GoodEnough builds a BezPath for every output segment. The contour walk only reads the paths of segments on the boundary of the result, so on a union of many overlapping shapes almost all of that work and memory went unused. The source carried a TODO for this. With GoodEnough, contours now builds paths only for the non-deleted segments whose two sides differ under `inside`. ReallyCorrect still positions every segment, since its perturbations depend on neighbors. The contours returned are unchanged. The topology tests contour_positions_cover_only_boundary_segments and contours_match_contours_from_all_positions check which segments get a path and compare contours with contours built from every position for four fill predicates. README.craft.md, SOURCE.json and NOTICE record the change as a local patch. --- NOTICE | 6 +- vendor/linesweeper/README.craft.md | 20 +++--- vendor/linesweeper/SOURCE.json | 6 +- vendor/linesweeper/src/topology.rs | 99 ++++++++++++++++++++++++++++-- 4 files changed, 115 insertions(+), 16 deletions(-) diff --git a/NOTICE b/NOTICE index bd88d43e7a..70c163182c 100644 --- a/NOTICE +++ b/NOTICE @@ -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 diff --git a/vendor/linesweeper/README.craft.md b/vendor/linesweeper/README.craft.md index f683ca53de..364a8e9c27 100644 --- a/vendor/linesweeper/README.craft.md +++ b/vendor/linesweeper/README.craft.md @@ -1,15 +1,21 @@ # 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/topology.rs` | With `Correctness::GoodEnough`, `Topology::contours` builds paths only for the output segments on the boundary of the result instead of for every output segment, which it then did not read. The contours it returns are unchanged. The `contour_positions_cover_only_boundary_segments` and `contours_match_contours_from_all_positions` tests cover the change. | `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 9c71e51add..43025f4121 100644 --- a/vendor/linesweeper/SOURCE.json +++ b/vendor/linesweeper/SOURCE.json @@ -7,7 +7,9 @@ "upstream_repository": null, "upstream_revision": "824b0791a750f7b27b8edcbb6c6244d0e629875e", "upstream_directory": "", - "local_patches": [], + "local_patches": [ + "In `src/topology.rs`, `Topology::contours` with `Correctness::GoodEnough` builds paths only for the output segments on the boundary of the result, not for every output segment." + ], "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" }, @@ -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": "12a3a120b6bd89091387e12efbc1a408fdbaed786d98a7a115dcd4ab9f916081" }, { "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" }, diff --git a/vendor/linesweeper/src/topology.rs b/vendor/linesweeper/src/topology.rs index 8420d015a5..2613a5041b 100644 --- a/vendor/linesweeper/src/topology.rs +++ b/vendor/linesweeper/src/topology.rs @@ -1281,10 +1281,27 @@ impl Topology { /// to compute a boolean "and" using the non-zero winding rule, `inside` should be /// `|w| w.shape_a != 0 && w.shape_b != 0`. pub fn contours(&self, inside: impl Fn(&W) -> bool) -> Contours { - let positions = self.compute_positions(); + let positions = self.contour_positions(&inside); self.contours_for_positions(inside, positions) } + /// Computes paths for the output segments that [`Topology::contours`] reads. + /// + /// With [`Correctness::GoodEnough`], only the segments on the boundary of + /// the set that `inside` defines get a path; the others keep an empty one. + /// With [`Correctness::ReallyCorrect`], every segment gets a path, because + /// the perturbation of one segment depends on its neighbors. + fn contour_positions(&self, inside: &impl Fn(&W) -> bool) -> OutputSegVec { + match self.correctness { + Correctness::GoodEnough => self.compute_positions_good_enough_where(|idx| { + !self.deleted[idx] + && inside(&self.winding[idx].clockwise) + != inside(&self.winding[idx].counter_clockwise) + }), + Correctness::ReallyCorrect => self.compute_positions_correct(), + } + } + fn contours_for_positions( &self, inside: impl Fn(&W) -> bool, @@ -1548,12 +1565,21 @@ impl Topology { } /// Computes paths for all the output segments. - /// - /// TODO: We should allow passing in an "inside" callback and then only do - /// positioning for the segments that are on the boundary. fn compute_positions_good_enough(&self) -> OutputSegVec { + self.compute_positions_good_enough_where(|_| true) + } + + /// Computes paths for the output segments that `wanted` accepts. The other + /// segments keep an empty path. + fn compute_positions_good_enough_where( + &self, + wanted: impl Fn(OutputSegIdx) -> bool, + ) -> OutputSegVec { let mut output = OutputSegVec::with_size(self.orig_seg.len()); for idx in self.orig_seg.indices() { + if !wanted(idx) { + continue; + } let start = self.points[self.point_idx[idx.first_half()]].to_kurbo(); let end = self.points[self.point_idx[idx.second_half()]].to_kurbo(); @@ -2539,6 +2565,71 @@ mod tests { assert!(!Topology::from_polylines_binary(s, EMPTY, 1e-5).has_normal_contours(OneOfTwo::A)); } + fn closed_polyline(points: &[(f64, f64)]) -> BezPath { + let mut path = BezPath::default(); + path.move_to(points[0]); + for &pt in &points[1..] { + path.line_to(pt); + } + path.close_path(); + path + } + + // Overlapping rectangles with aligned tops, a square with a hole, and a + // curve that splits into monotonic pieces, so that some output segments + // are interior, some bound a hole and some can be merged. + fn contour_position_shapes() -> Vec { + let mut curve = BezPath::default(); + curve.move_to((-1., 0.)); + curve.curve_to((-1., -1.), (1., 1.), (1., 0.)); + curve.line_to((2., 4.)); + curve.line_to((-2., 4.)); + curve.close_path(); + vec![ + closed_polyline(&[(0., 0.), (4., 0.), (4., 3.), (0., 3.)]), + closed_polyline(&[(1., 0.), (5., 0.), (5., 2.), (1., 2.)]), + closed_polyline(&[(2., 0.), (3., 0.), (3., 5.), (2., 5.)]), + closed_polyline(&[(6., 0.), (10., 0.), (10., 4.), (6., 4.)]), + closed_polyline(&[(7., 1.), (7., 3.), (9., 3.), (9., 1.)]), + curve, + ] + } + + #[test] + fn contour_positions_cover_only_boundary_segments() { + let shapes = contour_position_shapes(); + let top = Topology::::from_paths(shapes.iter().map(|s| (s, ())), 1e-6).unwrap(); + let inside = |w: &i32| *w != 0; + let positions = top.contour_positions(&inside); + let mut interior = 0; + for idx in top.segment_indices() { + let on_boundary = + inside(&top.winding[idx].clockwise) != inside(&top.winding[idx].counter_clockwise); + assert_eq!( + positions[idx].path.elements().is_empty(), + !on_boundary, + "{idx:?}" + ); + if !on_boundary { + interior += 1; + } + } + assert!(interior > 0); + } + + #[test] + fn contours_match_contours_from_all_positions() { + let shapes = contour_position_shapes(); + let top = Topology::::from_paths(shapes.iter().map(|s| (s, ())), 1e-6).unwrap(); + let predicates: [fn(&i32) -> bool; 4] = + [|w| *w != 0, |w| *w >= 2, |w| *w >= 3, |w| *w % 2 != 0]; + for inside in predicates { + let expected = top.contours_for_positions(inside, top.compute_positions()); + assert!(!expected.contours.is_empty()); + assert_eq!(top.contours(inside), expected); + } + } + #[test] fn invalid_sweep_order_repro() { let polys = [