Skip to main content

spatialrust_search/
traits.rs

1/// One neighbor search result.
2#[derive(Clone, Copy, Debug, PartialEq)]
3pub struct Neighbor {
4    /// Index of the neighbor point in the source cloud.
5    pub index: usize,
6    /// Squared Euclidean distance to the query point.
7    pub distance_squared: f32,
8}
9
10/// Common spatial index operations.
11pub trait SpatialIndex {
12    /// Returns the number of indexed points.
13    fn len(&self) -> usize;
14
15    /// Returns whether the index is empty.
16    fn is_empty(&self) -> bool {
17        self.len() == 0
18    }
19
20    /// Suggested [`SpatialTensor`](spatialrust_core::SpatialTensor) chunk size for parallel queries.
21    fn preferred_chunk_size(&self) -> usize {
22        spatialrust_core::DEFAULT_SPATIAL_TENSOR_CHUNK_SIZE
23    }
24}
25
26/// Exact nearest neighbor queries.
27pub trait NearestNeighborIndex: SpatialIndex {
28    /// Finds the single nearest neighbor.
29    fn nearest_one(&self, x: f32, y: f32, z: f32) -> Option<Neighbor>;
30
31    /// Finds up to `k` nearest neighbors sorted by ascending distance.
32    fn nearest_k(&self, x: f32, y: f32, z: f32, k: usize) -> Vec<Neighbor>;
33}
34
35/// Radius search queries.
36pub trait RadiusSearchIndex: SpatialIndex {
37    /// Finds all neighbors within `radius` (not squared).
38    fn radius_search(&self, x: f32, y: f32, z: f32, radius: f32) -> Vec<Neighbor>;
39}