pub struct ViewportIndex { /* private fields */ }Expand description
R-tree spatial index with a lazy-rebuild incremental-update
policy. Construct with ViewportIndex::new and populate with
ViewportIndex::rebuild.
Implementations§
Source§impl ViewportIndex
impl ViewportIndex
pub fn new() -> Self
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Number of points currently indexed (or pending in the dirty buffer — the state map is the source of truth).
pub fn is_empty(&self) -> bool
Sourcepub fn rebuild(&mut self, points: &[IndexPoint])
pub fn rebuild(&mut self, points: &[IndexPoint])
Bulk-load the index from points. O(n log n) via rstar’s
STR packing. Clears any pending updates.
Sourcepub fn update_positions(&mut self, changes: &[(u32, Vec2)])
pub fn update_positions(&mut self, changes: &[(u32, Vec2)])
Apply a batch of (id, new_position) updates. Updates are
stored in an internal position cache immediately; the
R-tree itself is rebuilt only when the dirty count crosses
max(1000, n / 20). Amortised O(1) per update.
Sourcepub fn query(
&self,
min: Vec2,
max: Vec2,
limit: usize,
order: ScoreKey,
) -> Vec<u32>
pub fn query( &self, min: Vec2, max: Vec2, limit: usize, order: ScoreKey, ) -> Vec<u32>
Return the ids whose current position falls inside the
axis-aligned box [min, max], optionally truncated and
ordered.
The tree may be slightly out-of-date relative to the state
map between rebuilds — callers who need point-in-time
accuracy should call rebuild before querying. For the
viewport use case a handful of frames of drift is fine
(the user’s viewport is typically moving faster than the
simulation is drifting).
Sourcepub fn query_with_scores(
&self,
min: Vec2,
max: Vec2,
limit: usize,
order: ScoreKey,
) -> Vec<(u32, f32)>
pub fn query_with_scores( &self, min: Vec2, max: Vec2, limit: usize, order: ScoreKey, ) -> Vec<(u32, f32)>
Return the (id, score) pairs inside the axis-aligned box,
optionally truncated and ordered.
Added in Phase 223 Wave 3. Same semantics as Self::query
but exposes the associated score so the PyO3 surface can
return both without a second lookup. The id-only Self::query
stays as a convenience for callers (including the WASM crate)
that already ignore the score.
Sourcepub fn snapshot(&self) -> Vec<IndexPoint>
pub fn snapshot(&self) -> Vec<IndexPoint>
Snapshot the indexed points — a clone of the internal state map’s values, in unspecified order.
Phase 223 Wave 3: used by the PyO3 surface to hand the point
set to bincode for Redis snapshotting. The tree itself isn’t
serialised; callers rebuild it in O(n log n) from the returned
slice via Self::rebuild.