Merge #691 (fix/linesweeper-contours-recompute-positions) into integration/linesweeper

This commit is contained in:
dotCooCoo committed 2026-11-09 13:58:23 -08:00
commit 14cd3ca5af
4 files changed
+108 -4

No files matched your search

+3
View File
@@ -301,6 +301,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
+5
View File
@@ -217,6 +217,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
@@ -274,6 +278,7 @@ Every change here updates this table and `SOURCE.json` in the same commit.
| `src/curve/mod.rs` | `solve_t_for_y` divides every height by 32 before it builds the cubic's coefficients when the largest height or the target has a magnitude above `f64::MAX / 32`. Before, a segment with heights above about `f64::MAX / 3` gave an infinite coefficient, and polycool's critical point search recursed until the process aborted with a stack overflow. `Topology::from_path` reached it on two overlapping rectangles near y = 1e308. The `solve_for_t_near_the_float_limit` test solves straight segments with heights from 5.5e307 up to `f64::MAX`. `intersect_cubics_rec` and `AlmostHorizontalDecomposition::from_monotone_cubic` take the midpoint of two heights with `f64::midpoint` instead of `(a + b) / 2.0`. Before, two heights above about 9e307 summed to infinity, the comparison of two overlapping segments had no entries, and a release build panicked on the empty list. The `compare_near_the_float_limit` test compares two lines that start at heights up to 1.7e308. |
| `src/lib.rs`, `src/order.rs`, `src/sweep/sweep_line.rs`, `src/topology.rs` | `ComparisonCache` counts its calls, so `Sweeper` counts its segment comparisons, and `Sweeper` also counts the segment positions it checks against horizontal segments and contour endpoints while finding a sweep line's changed intervals, and one unit for every 16 segments that linear searches of the sweep line step over. `Sweeper::set_work_limit` sets the most work it may do; past it, `Sweeper::next_line` returns `None`, also between the enter, exit and intersection events of one sweep line and between the comparisons and position checks that find its changed intervals, never returns a sweep line past the limit, and keeps returning `None` if the limit is raised; `Sweeper::work_limit_exceeded` returns `true`. `Topology::from_paths_with_work_limit` returns the new `Error::WorkLimitExceeded` when its sweep passes the limit. The sweep had no limit: the union of 400 identical circles made 1.36 billion comparisons in 19 sweep lines and took 66 to 83 s and 1.3 GB in a release build. `Topology::from_paths` and the other constructors set no limit. The `work_limit` and `work_limit_stops_within_a_sweep_line` tests sweep 20 stacked circles with a limit of 1,000 comparisons, and `work_limit_returns_no_line_past_the_limit` sweeps 3 stacked circles with every limit below the sweep's total. `work_limit_counts_the_horizontal_scans` sweeps 150 bars crossed by the top and bottom edges of 30 wide rectangles, where the sweep makes 1,462 comparisons but checks at least 18,000 positions, and stops it at half its total work. `work_limit_counts_linear_searches` sweeps 2,000 disjoint rectangles sharing their top and bottom heights, where each exit searches the whole sweep line, and stops it at half its total work. |
| `src/topology.rs` | `Topology::can_merge` merges two output segments only when the neighbor at the shared point is the adjacent piece of the same input segment. Before, after coincident segments were deleted, a piece of one input curve could be merged with a piece of another, and the output rebuilt a sub-curve from the wrong curve's parameters: two arches whose right legs agree to within about 1e-6 (area about 0.00096) gave a contour of area 900 at eps 1e-6, and a path followed by its own reverse panicked in `segs_to_path`. `segs_to_path` also starts at the first segment instead of panicking when every segment looks mergeable. The `merge_only_pieces_of_one_input_curve` and `path_followed_by_its_reverse` tests cover both inputs. |
| `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:
+3
View File
@@ -57,6 +57,7 @@
"In `src/curve/mod.rs`, `solve_t_for_y` divides every height by 32 before it builds the cubic's coefficients when a height or the target has a magnitude above `f64::MAX / 32`, so a segment with heights above about `f64::MAX / 3` no longer gives an infinite coefficient that makes polycool recurse until the stack overflows. `intersect_cubics_rec` and `AlmostHorizontalDecomposition::from_monotone_cubic` take the midpoint of two heights with `f64::midpoint`, so heights above about 9e307 no longer sum to infinity and leave a curve comparison with no entries."
"In `src/sweep/sweep_line.rs`, `src/order.rs`, `src/topology.rs` and `src/lib.rs`, `Sweeper` counts its segment comparisons and the segment positions it checks against horizontal segments and contour endpoints, and one unit per 16 segments that linear searches of the sweep line step over, and `Sweeper::set_work_limit` stops the sweep, between the events of one sweep line and between the comparisons and position checks that find its changed intervals too, once it passes the limit, so no sweep line comes back past the limit; `Topology::from_paths_with_work_limit` then returns the new `Error::WorkLimitExceeded`, so stacked or nearly coincident shapes no longer sweep for minutes with no way to stop them."
"In `src/topology.rs`, `Topology::can_merge` merges two output segments only when the neighbor at the shared point is the adjacent piece of the same input segment, so pieces of different input curves are no longer merged, and `segs_to_path` starts at the first segment instead of panicking when every segment looks mergeable."
"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" },
@@ -177,6 +178,8 @@
{ "path": "src/topology.rs", "source_sha256": "9c9b48bc3853884425127e2d64e8ed957ba0d27e1696e2c457e95282e52d6a5b", "source_git_blob": null, "sha256": "2872a6734e551b69f1df7ac2975896002beb04624eae34fce6d296691b15fbfb" },
{ "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": "79d67044371de95cf5a024a956596ff607ab1a9c5a7cabf1a32b9e3da72e338e" },
{ "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": "12a3a120b6bd89091387e12efbc1a408fdbaed786d98a7a115dcd4ab9f916081" },
{ "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" },
+97 -4
View File
@@ -1459,10 +1459,27 @@ impl<W: WindingNumber> Topology<W> {
/// [`Topology::contours`] does, or [`Error::InconsistentWindingNumbers`]
/// when the winding numbers disagree around a point.
pub fn try_contours(&self, inside: impl Fn(&W) -> bool) -> Result<Contours, Error> {
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<PositionedOutputSeg> {
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,
@@ -1809,12 +1826,21 @@ impl<W: WindingNumber> Topology<W> {
}
/// 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<PositionedOutputSeg> {
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<PositionedOutputSeg> {
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();
@@ -3056,6 +3082,73 @@ 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<BezPath> {
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::<i32>::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::<i32>::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())
.unwrap();
assert!(!expected.contours.is_empty());
assert_eq!(top.contours(inside), expected);
}
}
#[test]
fn invalid_sweep_order_repro() {
let polys = [