Skip to content

Commit 0d4b860

Browse files
committed
improved spatial index
1 parent d02c7a4 commit 0d4b860

5 files changed

Lines changed: 32 additions & 36 deletions

File tree

benches/bench_offset_multiple.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,14 @@ fn main() {
3636
/*
3737
> cargo bench
3838
39+
Base:
3940
Total time for 298 offset operations: 196.589137ms
4041
Average time per operation: 659.695µs
4142
Operations per second: 1515.9
4243
44+
Spatial index:
45+
Total time for 298 offset operations: 190.66736ms
46+
Average time per operation: 639.823µs
47+
Operations per second: 1562.9
48+
4349
*/

benches/bench_offset_multiple1000.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ Total time for 50 offset operations: 19.007039989s
4040
Average time per operation: 380.140799ms
4141
Operations per second: 2.6
4242
43-
SPATIAL INDEX (USE_BRUTE_FORCE = false):
44-
Total time for 50 offset operations: 6.249249758s
45-
Average time per operation: 124.984995ms
46-
Operations per second: 8.0
43+
SPATIAL INDEX (USE_BRUTE_FORCE = false) - polyarcs only:
44+
Total time for 50 offset operations: 5.05559079s
45+
Average time per operation: 101.111815ms
46+
Operations per second: 9.9
4747
48-
SPEEDUP: 3.04x faster with spatial index
48+
SPEEDUP: 3.76x faster with spatial index
4949
*/

benches/bench_offset_multiple200.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,15 @@ fn main() {
3535
/*
3636
> cargo bench --bench bench_offset_multiple200
3737
38-
Base
3938
BRUTE-FORCE (USE_BRUTE_FORCE = true):
4039
Total time for 198 offset operations: 3.431741022s
4140
Average time per operation: 17.332025ms
4241
Operations per second: 57.7
4342
44-
SPATIAL INDEX (USE_BRUTE_FORCE = false):
45-
Total time for 198 offset operations: 1.314992506s
46-
Average time per operation: 6.641376ms
47-
Operations per second: 150.6
43+
SPATIAL INDEX (USE_BRUTE_FORCE = false) - polyarcs only:
44+
Total time for 198 offset operations: 1.236930339s
45+
Average time per operation: 6.247122ms
46+
Operations per second: 160.1
4847
49-
SPEEDUP: 2.61x faster with spatial index
48+
SPEEDUP: 2.77x faster with spatial index
5049
*/

benches/bench_offset_multiple500.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ Total time for 198 offset operations: 14.44784818s
4040
Average time per operation: 72.96893ms
4141
Operations per second: 13.7
4242
43-
SPATIAL INDEX (USE_BRUTE_FORCE = false):
44-
Total time for 198 offset operations: 6.224936148s
45-
Average time per operation: 31.439071ms
46-
Operations per second: 31.8
43+
SPATIAL INDEX (USE_BRUTE_FORCE = false) - polyarcs only:
44+
Total time for 198 offset operations: 5.527450981s
45+
Average time per operation: 27.916419ms
46+
Operations per second: 35.8
4747
48-
SPEEDUP: 2.32x faster with spatial index
48+
SPEEDUP: 2.61x faster with spatial index
4949
*/

src/offset_prune_invalid.rs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ fn offset_prune_invalid_spatial(
3636
.filter(|arc| arc.is_valid(PRUNE_EPSILON))
3737
.collect();
3838

39-
// Build spatial index containing both polyarcs and offsets
40-
let polyarc_count = polyarcs.len();
41-
let mut spatial_index = HilbertRTree::with_capacity(polyarc_count + offsets.len());
39+
// Build spatial index containing only polyarcs (with expansion for search radius)
40+
let mut spatial_index = HilbertRTree::with_capacity(polyarcs.len());
4241

4342
// Add polyarcs to index with offset + epsilon expansion (done once)
4443
let search_radius = off + PRUNE_EPSILON;
@@ -47,19 +46,13 @@ fn offset_prune_invalid_spatial(
4746
spatial_index.add(min_x, max_x, min_y, max_y);
4847
}
4948

50-
// Add offsets to index
51-
for arc in offsets.iter() {
52-
let (min_x, max_x, min_y, max_y) = arc_bounds(arc);
53-
spatial_index.add(min_x, max_x, min_y, max_y);
54-
}
55-
5649
spatial_index.build();
5750

5851
while offsets.len() > 0 {
5952
let offset = offsets.pop().unwrap();
6053
valid.push(offset.clone());
6154

62-
// Query nearby arcs using spatial index
55+
// Query nearby polyarcs using spatial index
6356
let (offset_min_x, offset_max_x, offset_min_y, offset_max_y) =
6457
arc_bounds(&offset);
6558
let mut nearby_indices = Vec::new();
@@ -73,16 +66,14 @@ fn offset_prune_invalid_spatial(
7366

7467
// Check only nearby polyarcs for actual distance
7568
for idx in nearby_indices {
76-
if idx < polyarc_count {
77-
let p = &polyarcs[idx];
78-
if p.id == offset.id {
79-
continue; // skip self offsets
80-
}
81-
let dist = distance_element_element(p, &offset);
82-
if dist < off - PRUNE_EPSILON {
83-
valid.pop();
84-
break;
85-
}
69+
let p = &polyarcs[idx];
70+
if p.id == offset.id {
71+
continue; // skip self offsets
72+
}
73+
let dist = distance_element_element(p, &offset);
74+
if dist < off - PRUNE_EPSILON {
75+
valid.pop();
76+
break;
8677
}
8778
}
8879
}

0 commit comments

Comments
 (0)