mirror of
https://github.com/storytold/craft-libs.git
synced 2026-12-01 02:36:54 +01:00
linesweeper: comparison caches keep one order per pair and drop ended pairs
ComparisonCache stored a CurveOrder for each direction of every compared pair and kept every pair until the sweep ended. A sweep of a 2,001-point star polygon held 4,001,998 cached orders (1,463 MiB). The cache now stores one order per pair and flips it for a lookup in the other direction. evict_ended drops the pairs in which a segment ends above the current height, once the cache has doubled since its last scan. Sweeper::advance calls it on both of its caches, and compute_positions calls it as it takes each height from its queue.
This commit is contained in:
1 parent
0a9d1f48da
commit
7fc724ced7
7 files changed
+323
-29
No files matched your search
@@ -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
|
||||
|
||||
|
||||
Vendored
+13
-7
@@ -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/order.rs`, `src/sweep/sweep_line.rs`, `src/position.rs` | `ComparisonCache` stores one `CurveOrder` per pair of segments instead of one for each direction, and flips it when the pair is looked up in the other order. `ComparisonCache::evict_ended` drops the pairs in which a segment ends above a given height, once the cache has doubled since its last scan. `Sweeper` calls it on both of its caches each time the sweep line moves down, and `compute_positions` calls it each time it takes the next height from its queue. Both caches used to keep every pair the sweep compared until the sweep ended, so a sweep of a 2,001-point star polygon held 4,001,998 cached orders (1,463 MiB). Code comments label the changes `Craft patch (comparison cache size)`. The `one_order_per_pair` and `reversed_lookup_flips_the_stored_order` tests check the stored and flipped orders, `ended_pairs_leave_the_comparison_cache` checks that a sweep of a tall band keeps fewer than a tenth of the pairs, and `evicting_comparisons_keeps_the_sweep_lines` and `evicting_comparisons_keeps_the_correct_positions` check that the sweep lines, output events and positions do not change. |
|
||||
|
||||
`omitted_files` lists the 43 files of the package left out here, with their hashes:
|
||||
|
||||
|
||||
Vendored
+7
-5
@@ -7,7 +7,9 @@
|
||||
"upstream_repository": null,
|
||||
"upstream_revision": "824b0791a750f7b27b8edcbb6c6244d0e629875e",
|
||||
"upstream_directory": "",
|
||||
"local_patches": [],
|
||||
"local_patches": [
|
||||
"In `src/order.rs`, `ComparisonCache` stores one `CurveOrder` per pair of segments and flips it for a lookup in the other order, and `ComparisonCache::evict_ended` drops the pairs in which a segment ends above a given height once the cache has doubled since its last scan; `Sweeper::advance` in `src/sweep/sweep_line.rs` and `compute_positions` in `src/position.rs` call it, so the comparison caches no longer keep every compared pair until the sweep ends."
|
||||
],
|
||||
"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" },
|
||||
@@ -39,9 +41,9 @@
|
||||
{ "path": "src/geom.rs", "source_sha256": "9878ffa6d4a13fa2c9149741c137205ca01acb626a5b9f86a3db75cf91b63a08", "source_git_blob": null, "sha256": "9878ffa6d4a13fa2c9149741c137205ca01acb626a5b9f86a3db75cf91b63a08" },
|
||||
{ "path": "src/lib.rs", "source_sha256": "761c153c3b5c449569ff85c571344b9252ea64f7f0b0424fb4819041b03d13c6", "source_git_blob": null, "sha256": "761c153c3b5c449569ff85c571344b9252ea64f7f0b0424fb4819041b03d13c6" },
|
||||
{ "path": "src/num.rs", "source_sha256": "39ff85da50c500ce91cf6dea81aedc0dcca1db5e7d0506696dde2da118e93a2a", "source_git_blob": null, "sha256": "39ff85da50c500ce91cf6dea81aedc0dcca1db5e7d0506696dde2da118e93a2a" },
|
||||
{ "path": "src/order.rs", "source_sha256": "7e5b04eae8af134abb032e995f1d3c2ac473822923dfee8d463d9ade6ffeb596", "source_git_blob": null, "sha256": "7e5b04eae8af134abb032e995f1d3c2ac473822923dfee8d463d9ade6ffeb596" },
|
||||
{ "path": "src/order.rs", "source_sha256": "7e5b04eae8af134abb032e995f1d3c2ac473822923dfee8d463d9ade6ffeb596", "source_git_blob": null, "sha256": "11253aa656c5d2af3067b9a55f0e8f56e5107dd6befa9eeccc5d109123957fb9" },
|
||||
{ "path": "src/perturbation.rs", "source_sha256": "fa274a822bbdf65f8e53e403eef78034dd87964b3f1d3134a064df4ec8cef9c5", "source_git_blob": null, "sha256": "fa274a822bbdf65f8e53e403eef78034dd87964b3f1d3134a064df4ec8cef9c5" },
|
||||
{ "path": "src/position.rs", "source_sha256": "dfa70840805ecccf5b7eb6122921e46ca60c24cb7b3c17f3b55ffc2549368ab0", "source_git_blob": null, "sha256": "dfa70840805ecccf5b7eb6122921e46ca60c24cb7b3c17f3b55ffc2549368ab0" },
|
||||
{ "path": "src/position.rs", "source_sha256": "dfa70840805ecccf5b7eb6122921e46ca60c24cb7b3c17f3b55ffc2549368ab0", "source_git_blob": null, "sha256": "6a436972dfbf73f2fae8eeebf3d8a9d217c8980339b05a1d7e3fb74a5c8c1571" },
|
||||
{ "path": "src/segments.rs", "source_sha256": "379f9455120492d237cb5a62b3fc06da018a72e2eb75bbfcd61b4cce501d9bbc", "source_git_blob": null, "sha256": "379f9455120492d237cb5a62b3fc06da018a72e2eb75bbfcd61b4cce501d9bbc" },
|
||||
{ "path": "src/snapshots/linesweeper__tests__empty_horizontals.snap", "source_sha256": "e319881853e889badf6524f19af4cd6f112e8dd71bdc516826f4463134a82244", "source_git_blob": null, "sha256": "e319881853e889badf6524f19af4cd6f112e8dd71bdc516826f4463134a82244" },
|
||||
{ "path": "src/snapshots/linesweeper__tests__two_squares.snap", "source_sha256": "01961e0311a5af3b8f13eb52a2a7112560c47cf86771c04e40475dc6f15a0846", "source_git_blob": null, "sha256": "01961e0311a5af3b8f13eb52a2a7112560c47cf86771c04e40475dc6f15a0846" },
|
||||
@@ -62,8 +64,8 @@
|
||||
{ "path": "src/sweep/snapshots/linesweeper__sweep__sweep_line__tests__square.snap", "source_sha256": "b35c3e6a38d5ecdd8acf315ba0b0bd6485cd8b40193fdd76bd446bdfdb6eda77", "source_git_blob": null, "sha256": "b35c3e6a38d5ecdd8acf315ba0b0bd6485cd8b40193fdd76bd446bdfdb6eda77" },
|
||||
{ "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/sweep/sweep_line.rs", "source_sha256": "822b261f682911ea0c0370eaf17d9201a1e639b315359dcb6a3b877a229816e3", "source_git_blob": null, "sha256": "d99a5da5d623de525ef38aeb420c7e5a230c9a4306642b85fb6b93203ff6ba88" },
|
||||
{ "path": "src/topology.rs", "source_sha256": "9c9b48bc3853884425127e2d64e8ed957ba0d27e1696e2c457e95282e52d6a5b", "source_git_blob": null, "sha256": "edba8f797d8d35026d9ff70de85f0ce151191af341234cbff91927064fce4985" },
|
||||
{ "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" },
|
||||
|
||||
Vendored
+121
-13
@@ -10,11 +10,22 @@ use crate::{
|
||||
/// A cache for curve comparisons, so that each pair of curves needs to be compared at most once.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ComparisonCache {
|
||||
inner: FxHashMap<(SegIdx, SegIdx), (CurveOrder, CurveOrder)>,
|
||||
// Craft patch (comparison cache size): each pair stores the order of its
|
||||
// lower-indexed segment relative to the other. A lookup in the other
|
||||
// direction flips it into `flipped`.
|
||||
inner: FxHashMap<(SegIdx, SegIdx), CurveOrder>,
|
||||
flipped: Option<CurveOrder>,
|
||||
// Craft patch (comparison cache size): `evict_ended` scans the cache once
|
||||
// it holds this many pairs.
|
||||
next_purge: usize,
|
||||
accuracy: f64,
|
||||
tolerance: f64,
|
||||
}
|
||||
|
||||
// Craft patch (comparison cache size): the smallest cache that `evict_ended`
|
||||
// scans. Unit tests use a small value so that the sweep tests evict.
|
||||
const MIN_PURGE: usize = if cfg!(test) { 8 } else { 4096 };
|
||||
|
||||
impl ComparisonCache {
|
||||
/// Creates a new comparison cache.
|
||||
///
|
||||
@@ -27,6 +38,8 @@ impl ComparisonCache {
|
||||
pub fn new(tolerance: f64, accuracy: f64) -> Self {
|
||||
ComparisonCache {
|
||||
inner: FxHashMap::default(),
|
||||
flipped: None,
|
||||
next_purge: MIN_PURGE,
|
||||
accuracy,
|
||||
tolerance,
|
||||
}
|
||||
@@ -43,6 +56,8 @@ impl ComparisonCache {
|
||||
pub fn new_without_y_slop(tolerance: f64, accuracy: f64) -> Self {
|
||||
ComparisonCache {
|
||||
inner: FxHashMap::default(),
|
||||
flipped: None,
|
||||
next_purge: MIN_PURGE,
|
||||
accuracy,
|
||||
tolerance,
|
||||
}
|
||||
@@ -63,14 +78,8 @@ impl ComparisonCache {
|
||||
(j, i, true)
|
||||
};
|
||||
|
||||
match self.inner.entry((i, j)) {
|
||||
Entry::Occupied(o) => {
|
||||
if flipped {
|
||||
&mut o.into_mut().1
|
||||
} else {
|
||||
&mut o.into_mut().0
|
||||
}
|
||||
}
|
||||
let stored = match self.inner.entry((i, j)) {
|
||||
Entry::Occupied(o) => o.into_mut(),
|
||||
Entry::Vacant(v) => {
|
||||
let segi = &segments[i];
|
||||
let segj = &segments[j];
|
||||
@@ -96,17 +105,51 @@ impl ComparisonCache {
|
||||
forward.iter().last().unwrap().1,
|
||||
segi.end().y.min(segj.end().y)
|
||||
);
|
||||
let reverse = forward.flip();
|
||||
let v = v.insert((forward, reverse));
|
||||
if flipped { &mut v.1 } else { &mut v.0 }
|
||||
v.insert(forward)
|
||||
}
|
||||
};
|
||||
if flipped {
|
||||
self.flipped.insert(stored.flip())
|
||||
} else {
|
||||
stored
|
||||
}
|
||||
}
|
||||
|
||||
// Craft patch (comparison cache size): removes the pairs in which a
|
||||
// segment ends above `y`. Callers pass heights that never decrease, so
|
||||
// such a pair is not compared again; if it were, the comparison would be
|
||||
// computed again with the same result. The cache is scanned only after it
|
||||
// has doubled since the last scan, so the scans take time proportional to
|
||||
// the number of comparisons.
|
||||
pub(crate) fn evict_ended(&mut self, segments: &Segments, y: f64) {
|
||||
if self.inner.len() < self.next_purge {
|
||||
return;
|
||||
}
|
||||
self.inner
|
||||
.retain(|(i, j), _| segments[*i].end().y >= y && segments[*j].end().y >= y);
|
||||
if self.inner.len() * 4 < self.inner.capacity() {
|
||||
self.inner.shrink_to_fit();
|
||||
}
|
||||
self.next_purge = self.inner.len().saturating_mul(2).max(MIN_PURGE);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) fn cached_pairs(&self) -> usize {
|
||||
self.inner.len()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) fn never_evict(&mut self) {
|
||||
self.next_purge = usize::MAX;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{SegIdx, Segments, curve::Order};
|
||||
use crate::{
|
||||
SegIdx, Segments,
|
||||
curve::{CurveOrder, Order},
|
||||
};
|
||||
|
||||
use super::ComparisonCache;
|
||||
|
||||
@@ -127,4 +170,69 @@ mod tests {
|
||||
Order::Left
|
||||
);
|
||||
}
|
||||
|
||||
// Two cubics that cross each other twice, and a vertical line that
|
||||
// crosses both.
|
||||
fn crossing_segments() -> Segments {
|
||||
let mut segments = Segments::default();
|
||||
let mut path = kurbo::BezPath::new();
|
||||
path.move_to((0.0, 0.0));
|
||||
path.curve_to((3.0, 1.0), (-3.0, 2.0), (0.0, 3.0));
|
||||
segments.add_non_closed_bez_path(&path).unwrap();
|
||||
let mut path = kurbo::BezPath::new();
|
||||
path.move_to((0.5, 0.0));
|
||||
path.curve_to((-2.5, 1.0), (3.5, 2.0), (0.5, 3.0));
|
||||
segments.add_non_closed_bez_path(&path).unwrap();
|
||||
segments.add_points([(0.2, -0.5), (0.3, 3.5)]);
|
||||
segments
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn one_order_per_pair() {
|
||||
let segments = crossing_segments();
|
||||
let mut cmp_cache = ComparisonCache::new(0.1, 0.05);
|
||||
for i in segments.indices() {
|
||||
for j in segments.indices() {
|
||||
if i != j {
|
||||
cmp_cache.compare_segments(&segments, i, j);
|
||||
}
|
||||
}
|
||||
}
|
||||
let n = segments.len();
|
||||
assert_eq!(cmp_cache.inner.len(), n * (n - 1) / 2);
|
||||
for stored in cmp_cache.inner.values() {
|
||||
assert_eq!(
|
||||
std::mem::size_of_val(stored),
|
||||
std::mem::size_of::<CurveOrder>()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reversed_lookup_flips_the_stored_order() {
|
||||
let segments = crossing_segments();
|
||||
let mut forward_first = ComparisonCache::new(0.1, 0.05);
|
||||
let mut reverse_first = ComparisonCache::new(0.1, 0.05);
|
||||
let entries = |order: &CurveOrder| order.iter().collect::<Vec<_>>();
|
||||
for i in segments.indices() {
|
||||
for j in segments.indices() {
|
||||
if i.0 >= j.0 {
|
||||
continue;
|
||||
}
|
||||
let reverse_before = entries(reverse_first.compare_segments(&segments, j, i));
|
||||
let forward = forward_first.compare_segments(&segments, i, j).clone();
|
||||
let reverse_after = forward_first.compare_segments(&segments, j, i).clone();
|
||||
let forward_again = entries(forward_first.compare_segments(&segments, i, j));
|
||||
let flipped: Vec<_> = entries(&forward)
|
||||
.into_iter()
|
||||
.map(|(y0, y1, order)| (y0, y1, order.flip()))
|
||||
.collect();
|
||||
|
||||
assert_eq!(entries(&reverse_after), flipped);
|
||||
assert_eq!(reverse_before, flipped);
|
||||
assert_eq!(forward_again, entries(&forward));
|
||||
assert_eq!(reverse_after.metrics, forward.metrics);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Vendored
+4
@@ -393,6 +393,10 @@ pub(crate) fn compute_positions(
|
||||
if last_y[entry.idx] > y0 {
|
||||
continue;
|
||||
}
|
||||
// Craft patch (comparison cache size): the heights popped from the
|
||||
// queue never decrease, and every neighbor compared from here on
|
||||
// extends below `y0`.
|
||||
cmp.evict_ended(segs, y0);
|
||||
|
||||
let mut y1 = endpoints[entry.idx.second_half()].y;
|
||||
let mut west_scan = vec![entry.idx];
|
||||
|
||||
+108
@@ -467,6 +467,12 @@ impl<'segs> Sweeper<'segs> {
|
||||
self.line.segs.remove(idx);
|
||||
}
|
||||
self.y = y;
|
||||
// Craft patch (comparison cache size): the sweep line holds no
|
||||
// segment that ends above `y`, so their cached pairs are dropped.
|
||||
self.comparisons.get_mut().evict_ended(self.segments, y);
|
||||
self.conservative_comparisons
|
||||
.get_mut()
|
||||
.evict_ended(self.segments, y);
|
||||
self.segs_needing_positions.clear();
|
||||
self.horizontals.clear();
|
||||
}
|
||||
@@ -2092,6 +2098,108 @@ mod tests {
|
||||
assert!(sweep.next_line(&mut bufs).is_none());
|
||||
}
|
||||
|
||||
// A tall closed band whose sides zigzag: each segment shares heights with
|
||||
// only a few others.
|
||||
fn zigzag_band(rows: usize) -> Segments {
|
||||
let left = (0..=rows).map(|k| p((k % 2) as f64, k as f64));
|
||||
let right = (0..=rows).rev().map(|k| p(10.0 + (k % 2) as f64, k as f64));
|
||||
let mut segs = Segments::default();
|
||||
segs.add_closed_polyline(left.chain(right));
|
||||
segs
|
||||
}
|
||||
|
||||
// A star polygon in which every edge crosses every edge it does not share a
|
||||
// vertex with.
|
||||
fn star(points: usize) -> Segments {
|
||||
let step = (points - 1) / 2;
|
||||
let mut segs = Segments::default();
|
||||
segs.add_closed_polyline((0..points).map(|k| {
|
||||
let angle = std::f64::consts::TAU * ((k * step) % points) as f64 / points as f64 + 0.1;
|
||||
p(1000.0 * angle.cos(), 1000.0 * angle.sin())
|
||||
}));
|
||||
segs
|
||||
}
|
||||
|
||||
// Two wavy closed paths of cubics that cross each other many times.
|
||||
fn wavy_cubics() -> Segments {
|
||||
let mut segs = Segments::default();
|
||||
for offset in [0.0, 0.7] {
|
||||
let mut path = BezPath::new();
|
||||
path.move_to((offset, 0.0));
|
||||
for k in 0..40 {
|
||||
let y = 4.0 * k as f64;
|
||||
let x = offset + (k % 3) as f64 * 0.5;
|
||||
path.curve_to(
|
||||
(offset + 3.0, y + 1.0),
|
||||
(offset - 3.0, y + 3.0),
|
||||
(x, y + 4.0),
|
||||
);
|
||||
}
|
||||
path.line_to((offset + 5.0, 160.0));
|
||||
path.line_to((offset + 5.0, 0.0));
|
||||
path.close_path();
|
||||
segs.add_bez_path(&path).unwrap();
|
||||
}
|
||||
segs
|
||||
}
|
||||
|
||||
// Runs a sweep and records every sweep line, its changed intervals and its
|
||||
// output events. Also returns the largest number of pairs the comparison
|
||||
// caches held after a step.
|
||||
fn sweep_record(segs: &Segments, eps: f64, evict: bool) -> (Vec<String>, usize) {
|
||||
let mut record = Vec::new();
|
||||
let mut most_pairs = 0;
|
||||
let mut sweeper = Sweeper::new(segs, eps);
|
||||
if !evict {
|
||||
sweeper.comparisons.get_mut().never_evict();
|
||||
sweeper.conservative_comparisons.get_mut().never_evict();
|
||||
}
|
||||
let mut line_bufs = SweepLineBuffers::default();
|
||||
let mut range_bufs = crate::sweep::SweepLineRangeBuffers::default();
|
||||
while let Some(mut line) = sweeper.next_line(&mut line_bufs) {
|
||||
record.push(format!(
|
||||
"{} {:?} {:?}",
|
||||
line.y(),
|
||||
line.state.line,
|
||||
line.state.changed_intervals
|
||||
));
|
||||
while let Some(mut range) = line.next_range(&mut range_bufs, segs) {
|
||||
while let Some(events) = range.events() {
|
||||
record.extend(events.into_iter().map(|ev| format!("{ev:?}")));
|
||||
}
|
||||
}
|
||||
most_pairs = most_pairs.max(
|
||||
line.state.comparisons.borrow().cached_pairs()
|
||||
+ line.state.conservative_comparisons.borrow().cached_pairs(),
|
||||
);
|
||||
}
|
||||
(record, most_pairs)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ended_pairs_leave_the_comparison_cache() {
|
||||
let segs = zigzag_band(400);
|
||||
let (_, kept) = sweep_record(&segs, 0.1, false);
|
||||
let (_, evicted) = sweep_record(&segs, 0.1, true);
|
||||
assert!(kept >= 400, "{kept}");
|
||||
assert!(evicted * 10 < kept, "{evicted} {kept}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn evicting_comparisons_keeps_the_sweep_lines() {
|
||||
for (segs, eps) in [
|
||||
(zigzag_band(400), 0.1),
|
||||
(star(31), 1e-6),
|
||||
(star(31), 1.0),
|
||||
(wavy_cubics(), 0.01),
|
||||
(wavy_cubics(), 0.25),
|
||||
] {
|
||||
let (kept, _) = sweep_record(&segs, eps, false);
|
||||
let (evicted, _) = sweep_record(&segs, eps, true);
|
||||
assert!(kept == evicted);
|
||||
}
|
||||
}
|
||||
|
||||
proptest! {
|
||||
#[test]
|
||||
fn perturbation_test_f64(perturbations in prop::collection::vec(perturbation(f64_perturbation(0.1)), 1..5)) {
|
||||
|
||||
Vendored
+67
-1
@@ -2270,7 +2270,7 @@ mod tests {
|
||||
},
|
||||
};
|
||||
|
||||
use super::{OneOfTwo, Topology};
|
||||
use super::{Correctness, HalfOutputSegVec, OneOfTwo, Topology};
|
||||
|
||||
fn p(x: f64, y: f64) -> Point {
|
||||
Point::new(x, y)
|
||||
@@ -2679,6 +2679,72 @@ mod tests {
|
||||
//check_intersections(&top);
|
||||
}
|
||||
|
||||
// A column of shapes, each made of two closed paths whose wavy cubic sides
|
||||
// cross each other twice.
|
||||
fn wavy_column(rows: usize) -> BezPath {
|
||||
let mut path = BezPath::new();
|
||||
for k in 0..rows {
|
||||
let y = 10.0 * k as f64;
|
||||
for offset in [0.0, 0.7] {
|
||||
path.move_to((offset, y));
|
||||
path.curve_to(
|
||||
(offset + 3.0, y + 2.0),
|
||||
(offset - 3.0, y + 6.0),
|
||||
(offset, y + 8.0),
|
||||
);
|
||||
path.line_to((offset + 2.0, y + 8.0));
|
||||
path.line_to((offset + 2.0, y));
|
||||
path.close_path();
|
||||
}
|
||||
}
|
||||
path
|
||||
}
|
||||
|
||||
// The positions that `compute_positions_correct` computes, with a cache
|
||||
// that drops ended pairs or one that keeps every pair.
|
||||
fn correct_positions(top: &Topology<i32>, evict: bool) -> (String, usize) {
|
||||
let mut cmp = ComparisonCache::new(top.eps, top.eps / 2.0);
|
||||
if !evict {
|
||||
cmp.never_evict();
|
||||
}
|
||||
let mut endpoints = HalfOutputSegVec::with_size(top.orig_seg.len());
|
||||
for idx in top.orig_seg.indices() {
|
||||
endpoints[idx.first_half()] = top.points[top.point_idx[idx.first_half()]].to_kurbo();
|
||||
endpoints[idx.second_half()] = top.points[top.point_idx[idx.second_half()]].to_kurbo();
|
||||
}
|
||||
let out = crate::position::compute_positions(
|
||||
&top.segments,
|
||||
&top.orig_seg,
|
||||
&mut cmp,
|
||||
&endpoints,
|
||||
&top.build_scan_line_orders(),
|
||||
top.eps / 4.0,
|
||||
);
|
||||
(format!("{out:?}"), cmp.cached_pairs())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn evicting_comparisons_keeps_the_correct_positions() {
|
||||
let path = wavy_column(200);
|
||||
for eps in [0.01, 0.25] {
|
||||
let top = Topology::<i32>::from_paths_with_correctness(
|
||||
[(&path, ())],
|
||||
eps,
|
||||
Correctness::ReallyCorrect,
|
||||
)
|
||||
.unwrap();
|
||||
let (kept, kept_pairs) = correct_positions(&top, false);
|
||||
let (evicted, evicted_pairs) = correct_positions(&top, true);
|
||||
assert!(kept == evicted);
|
||||
assert_eq!(format!("{:?}", top.compute_positions()), evicted);
|
||||
assert!(kept_pairs >= 400, "{kept_pairs}");
|
||||
assert!(
|
||||
evicted_pairs * 10 < kept_pairs,
|
||||
"{evicted_pairs} {kept_pairs}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
proptest! {
|
||||
#[test]
|
||||
fn perturbation_test_f64(perturbations in prop::collection::vec(perturbation(f64_perturbation(0.1)), 1..5)) {
|
||||
|
||||
Reference in new issue
Block a user