linesweeper: contours follows each scan-west chain once instead of once per contour

Topology::contours_for_positions walked the scan-west chain of every new
contour until it found a boundary segment, calling inside twice per link,
and kept nothing between contours. With n bars of one set west of m
small squares of the other and the test "first set and not second", no
bar edge is on the boundary, so each square walked all 2n bar edges:
1,024,240,000 calls to inside for n = m = 16,000.

The boundary test now runs once per segment, and Topology::west_boundary
remembers the nearest boundary segment for every segment it passes, so
each link is followed once per call. The contours do not change.
README.craft.md, SOURCE.json and NOTICE record the change as a local
patch.
This commit is contained in:
dotCooCoo committed 2026-11-07 18:51:32 -08:00
1 parent 058b047760
commit b7ba824ded
4 files changed
+138 -22

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
+13 -7
View File
@@ -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` | `contours_for_positions` finds the nearest boundary segment west of each new contour through `Topology::west_boundary`, which remembers the answer for every segment it passes, so each link of a scan-west chain is followed once per call. Before, every contour walked its whole chain again, and `n` bars of one set west of `m` squares of the other with the test "first set and not second" called `inside` about `4nm` times: 1,024,240,000 calls for `n = m = 16,000`. The contours do not change. The `contour_starts_follow_each_west_chain_once` test counts the calls to `inside` for 200 bars and 200 squares, and `contour_starts_find_the_boundary_past_non_boundary_segments` checks the parents of squares whose chains pass non-boundary segments. |
`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/topology.rs`, `contours_for_positions` finds the nearest boundary segment west of each new contour through `Topology::west_boundary`, which remembers the answer for every segment it passes, so each link of a scan-west chain is followed once per call instead of once per contour."
],
"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": "4b6fe25f91d79da4915fff3c99210a44caf8cd263b0ac8963c18ff3e08069019" },
{ "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" },
+118 -10
View File
@@ -1274,6 +1274,41 @@ impl<W: WindingNumber> Topology<W> {
ret.clamp(0.0, 1.0)
}
/// Returns the nearest segment on `idx`'s scan-west chain (not counting `idx` itself)
/// for which `is_bdy` is true.
///
/// `memo[i]` holds the answer for segment `i` once it is known. Every segment passed
/// over on the way gets the same answer, so a chain shared by several starting segments
/// is walked only once. `stack` is scratch space.
fn west_boundary(
&self,
idx: OutputSegIdx,
is_bdy: &[bool],
memo: &mut [Option<Option<OutputSegIdx>>],
stack: &mut Vec<OutputSegIdx>,
) -> Option<OutputSegIdx> {
stack.clear();
let mut cur = idx;
let found = loop {
if let Some(known) = memo[cur.0] {
break known;
}
match self.scan_west[cur] {
None => break None,
Some(left) if is_bdy[left.0] => break Some(left),
Some(left) => {
stack.push(cur);
cur = left;
}
}
};
memo[cur.0] = Some(found);
for seg in stack.drain(..) {
memo[seg.0] = Some(found);
}
found
}
/// Returns the contours of some set defined by this topology.
///
/// The callback function `inside` takes a winding number and returns `true`
@@ -1321,6 +1356,14 @@ impl<W: WindingNumber> Topology<W> {
let bdy = |idx: OutputSegIdx| -> bool {
inside(&self.winding[idx].clockwise) != inside(&self.winding[idx].counter_clockwise)
};
// Whether each segment is a live boundary segment, and a memo of the nearest one
// on each segment's scan-west chain, so that each link of a chain is followed at
// most once per call instead of once per contour that starts east of it.
let is_bdy: Vec<bool> = (0..self.winding.inner.len())
.map(|i| !self.deleted[OutputSegIdx(i)] && bdy(OutputSegIdx(i)))
.collect();
let mut west_bdy: Vec<Option<Option<OutputSegIdx>>> = vec![None; self.winding.inner.len()];
let mut west_stack = Vec::new();
let mut visited = vec![false; self.winding.inner.len()];
// Keep track of the points that were visited on this walk, so that if we re-visit a
@@ -1332,7 +1375,7 @@ impl<W: WindingNumber> Topology<W> {
continue;
}
if !bdy(idx) {
if !is_bdy[idx.0] {
continue;
}
@@ -1340,14 +1383,7 @@ impl<W: WindingNumber> Topology<W> {
// are relative to existing contours.
let contour_idx = ContourIdx(ret.contours.len());
let mut contour = Contour::default();
let mut west_seg = self.scan_west[idx];
while let Some(left) = west_seg {
if self.deleted[left] || !bdy(left) {
west_seg = self.scan_west[left];
} else {
break;
}
}
let west_seg = self.west_boundary(idx, &is_bdy, &mut west_bdy, &mut west_stack);
if let Some(west) = west_seg {
if let Some(west_contour) = seg_contour[west.0] {
// Is the thing just to our west inside or outside the output set?
@@ -2270,7 +2306,7 @@ mod tests {
},
};
use super::{OneOfTwo, Topology};
use super::{ContourIdx, OneOfTwo, Topology};
fn p(x: f64, y: f64) -> Point {
Point::new(x, y)
@@ -2661,6 +2697,78 @@ mod tests {
// }
// }
// `n` tall bars (the second set) west of a column of `m` small squares (the first set).
fn bars_and_squares(n: usize, m: usize) -> (Vec<[Point; 4]>, Vec<[Point; 4]>) {
let rect =
|x0: f64, y0: f64, x1: f64, y1: f64| [p(x0, y0), p(x1, y0), p(x1, y1), p(x0, y1)];
let height = 3.0 * m as f64 + 3.0;
let bars = (0..n)
.map(|i| {
let x = 3.0 * i as f64;
rect(x, 0.0, x + 1.0, height)
})
.collect();
let x = 3.0 * n as f64 + 10.0;
let squares = (0..m)
.map(|j| {
let y = 3.0 * j as f64 + 1.0;
rect(x, y, x + 1.0, y + 1.0)
})
.collect();
(squares, bars)
}
#[test]
fn contour_starts_follow_each_west_chain_once() {
let (squares, bars) = bars_and_squares(200, 200);
let top = Topology::from_polylines_binary(squares, bars, 1e-6);
let calls = std::cell::Cell::new(0usize);
let contours = top.contours(|w| {
calls.set(calls.get() + 1);
w.shape_a != 0 && w.shape_b == 0
});
// No bar edge is on the boundary of the squares minus the bars, so the west chain of
// every square passes all 400 bar edges. Walking the chain again for each square
// called `inside` about 2 * 400 * 200 times.
let segs = top.segment_indices().count();
assert!(
calls.get() < 10 * segs,
"{} calls to inside for {segs} segments",
calls.get()
);
assert_eq!(contours.contours().count(), 200);
assert!(contours.contours().all(|c| c.outer && c.parent.is_none()));
}
#[test]
fn contour_starts_find_the_boundary_past_non_boundary_segments() {
let (mut set_a, bars) = bars_and_squares(20, 20);
// A frame around everything: the squares are holes in it, and the bars do not
// change the set, so every square's west chain passes the bar edges and ends at the
// frame's left edge.
set_a.push([
p(-5.0, -5.0),
p(200.0, -5.0),
p(200.0, 200.0),
p(-5.0, 200.0),
]);
let top = Topology::from_polylines_binary(set_a, bars, 1e-6);
let contours = top.contours(|w| w.shape_a % 2 != 0);
let count = contours.contours().count();
assert_eq!(count, 21);
let roots: Vec<ContourIdx> = (0..count)
.map(ContourIdx)
.filter(|&i| contours[i].parent.is_none())
.collect();
assert_eq!(roots.len(), 1);
let frame = roots[0];
assert!(contours[frame].outer);
for i in (0..count).map(ContourIdx).filter(|&i| i != frame) {
assert_eq!(contours[i].parent, Some(frame));
assert!(!contours[i].outer);
}
}
fn run_perturbation(ps: Vec<Perturbation<F64Perturbation>>) {
let base = vec![vec![
p(0.0, 0.0),