1
1
use fmt:: Debug ;
2
2
use std:: fmt;
3
- use std:: {
4
- cmp:: { max, min} ,
5
- collections:: BinaryHeap ,
6
- } ;
3
+ use std:: { cmp:: min, collections:: BinaryHeap } ;
7
4
8
5
use crate :: { try_control, ControlFlow , IndexableNum , NeighborVisitor , QueryVisitor , AABB } ;
9
6
62
59
/// Static/fixed size indexing data structure for two dimensional axis aligned bounding boxes.
63
60
///
64
61
/// The index allows for fast construction and fast querying but cannot be modified after creation.
65
- /// This type is constructed from a [StaticAABB2DIndexBuilder].
62
+ /// This type is constructed from a [` StaticAABB2DIndexBuilder` ].
66
63
///
67
64
/// 2D axis aligned bounding boxes are represented by two extent points (four values):
68
65
/// (min_x, min_y), (max_x, max_y).
@@ -157,7 +154,7 @@ where
157
154
} ;
158
155
}
159
156
160
- let node_size = min ( max ( node_size, 2 ) , 65535 ) ;
157
+ let node_size = node_size. clamp ( 2 , 65535 ) ;
161
158
162
159
let mut n = num_items;
163
160
let mut num_nodes = num_items;
@@ -186,19 +183,19 @@ where
186
183
}
187
184
}
188
185
189
- /// Construct a new [StaticAABB2DIndexBuilder] to fit exactly the specified `count` number of
186
+ /// Construct a new [` StaticAABB2DIndexBuilder` ] to fit exactly the specified `count` number of
190
187
/// items.
191
188
#[ inline]
192
189
pub fn new ( count : usize ) -> Self {
193
190
StaticAABB2DIndexBuilder :: init ( count, 16 )
194
191
}
195
192
196
- /// Construct a new [StaticAABB2DIndexBuilder] to fit exactly the specified `count` number of
193
+ /// Construct a new [` StaticAABB2DIndexBuilder` ] to fit exactly the specified `count` number of
197
194
/// items and use `node_size` for the index tree shape.
198
195
///
199
196
/// Each node in the index tree has a maximum size which may be adjusted by `node_size` for
200
197
/// performance reasons, however the default value of 16 when calling
201
- /// `StaticAABB2DIndexBuilder::new` is tested to be optimal in most cases.
198
+ /// [ `StaticAABB2DIndexBuilder::new`] is tested to be optimal in most cases.
202
199
///
203
200
/// If `node_size` is less than 2 then 2 is used, if `node_size` is greater than 65535 then
204
201
/// 65535 is used.
@@ -212,7 +209,8 @@ where
212
209
///
213
210
/// For performance reasons the sanity checks of `min_x <= max_x` and `min_y <= max_y` are only
214
211
/// debug asserted. If an invalid box is added it may lead to a panic or unexpected behavior
215
- /// from the constructed [StaticAABB2DIndex].
212
+ /// from the constructed [`StaticAABB2DIndex`].
213
+ #[ inline]
216
214
pub fn add ( & mut self , min_x : T , min_y : T , max_x : T , max_y : T ) -> & mut Self {
217
215
// catch adding past num_items (error will be returned when build is called)
218
216
if self . pos >= self . num_items {
@@ -231,13 +229,13 @@ where
231
229
self
232
230
}
233
231
234
- /// Build the [StaticAABB2DIndex] with the boxes that have been added.
232
+ /// Build the [` StaticAABB2DIndex` ] with the boxes that have been added.
235
233
///
236
234
/// If the number of added items does not match the count given at the time the builder was
237
- /// created then a [StaticAABB2DIndexBuildError::ItemCountError] will be returned.
235
+ /// created then a [` StaticAABB2DIndexBuildError::ItemCountError` ] will be returned.
238
236
///
239
237
/// If the numeric type T fails to cast to/from a u16 for any reason then a
240
- /// [StaticAABB2DIndexBuildError::NumericCastError] will be returned.
238
+ /// [` StaticAABB2DIndexBuildError::NumericCastError` ] will be returned.
241
239
pub fn build ( mut self ) -> Result < StaticAABB2DIndex < T > , StaticAABB2DIndexBuildError > {
242
240
if self . pos != self . num_items {
243
241
return Err ( StaticAABB2DIndexBuildError :: ItemCountError {
@@ -379,7 +377,7 @@ where
379
377
/// Maps 2d space to 1d hilbert curve space.
380
378
///
381
379
/// 2d space is `x: [0 -> n-1]` and `y: [0 -> n-1]`, 1d hilbert curve value space is
382
- /// `d: [0 -> n^2 - 1]`, where n = 2^16, so `x` and `y` must be between 0 and [u16::MAX]
380
+ /// `d: [0 -> n^2 - 1]`, where n = 2^16, so `x` and `y` must be between 0 and [` u16::MAX` ]
383
381
/// (65535 or 2^16 - 1).
384
382
pub fn hilbert_xy_to_index ( x : u16 , y : u16 ) -> u32 {
385
383
let x = x as u32 ;
@@ -762,13 +760,15 @@ where
762
760
763
761
/// Type alias for priority queue used for nearest neighbor searches.
764
762
///
765
- /// See: [StaticAABB2DIndex::visit_neighbors_with_queue].
763
+ /// See: [` StaticAABB2DIndex::visit_neighbors_with_queue` ].
766
764
pub type NeighborPriorityQueue < T > = BinaryHeap < NeighborsState < T > > ;
767
765
768
766
/// Holds state for priority queue used in nearest neighbors query.
769
767
///
770
768
/// Note this type is public for use in passing in an existing priority queue but
771
769
/// all fields and constructor are private for internal use only.
770
+ ///
771
+ /// See also: [`StaticAABB2DIndex::visit_neighbors_with_queue`].
772
772
#[ derive( Debug , Copy , Clone , PartialEq ) ]
773
773
pub struct NeighborsState < T >
774
774
where
@@ -828,18 +828,18 @@ where
828
828
self . boxes . last ( ) . copied ( )
829
829
}
830
830
831
- /// Gets the total count of items that were added to the index.
831
+ /// Gets the total count of items that were added to the index during construction .
832
832
#[ inline]
833
833
pub fn count ( & self ) -> usize {
834
834
self . num_items
835
835
}
836
836
837
- /// Queries the index, returning a collection of indexes to items that overlap with the bounding
837
+ /// Queries the index, returning a collection of indices to items that overlap with the bounding
838
838
/// box given.
839
839
///
840
840
/// `min_x`, `min_y`, `max_x`, and `max_y` represent the bounding box to use for the query.
841
841
/// Indexes returned match with the order items were added to the index using
842
- /// [StaticAABB2DIndexBuilder::add].
842
+ /// [` StaticAABB2DIndexBuilder::add` ].
843
843
#[ inline]
844
844
pub fn query ( & self , min_x : T , min_y : T , max_x : T , max_y : T ) -> Vec < usize > {
845
845
let mut results = Vec :: new ( ) ;
@@ -850,8 +850,8 @@ where
850
850
results
851
851
}
852
852
853
- /// The same as [StaticAABB2DIndex::query] but instead of returning a [Vec] of results a lazy
854
- /// iterator is returned which yields the results.
853
+ /// The same as [` StaticAABB2DIndex::query` ] but instead of returning a [` Vec` ] of results a
854
+ /// iterator is returned which yields the results by lazily querying the index .
855
855
///
856
856
/// # Examples
857
857
/// ```
@@ -877,8 +877,9 @@ where
877
877
QueryIterator :: < ' a , T > :: new ( self , min_x, min_y, max_x, max_y)
878
878
}
879
879
880
- /// The same as [StaticAABB2DIndex::query_iter] but allows using an existing buffer for stack
881
- /// traversal.
880
+ /// The same as [`StaticAABB2DIndex::query_iter`] but allows using an existing buffer for stack
881
+ /// traversal. This is useful for performance when many queries will be done repeatedly to avoid
882
+ /// allocating a new stack for each query (this is for performance benefit only).
882
883
#[ inline]
883
884
pub fn query_iter_with_stack < ' a > (
884
885
& ' a self ,
@@ -891,9 +892,13 @@ where
891
892
QueryIteratorStackRef :: < ' a , T > :: new ( self , stack, min_x, min_y, max_x, max_y)
892
893
}
893
894
894
- /// Same as [StaticAABB2DIndex::query] but instead of returning a collection of indexes a
895
+ /// Same as [` StaticAABB2DIndex::query` ] but instead of returning a collection of indices a
895
896
/// `visitor` function is called for each index that would be returned. The `visitor` returns a
896
897
/// control flow indicating whether to continue visiting or break.
898
+ ///
899
+ /// The [`ControlFlow`] and [`QueryVisitor`] traits are implemented to allow passing in a
900
+ /// function [`FnMut`] visitor that returns no value (all results will be visited ) or a
901
+ /// [`ControlFlow`] to break early.
897
902
#[ inline]
898
903
pub fn visit_query < V , C > ( & self , min_x : T , min_y : T , max_x : T , max_y : T , visitor : & mut V ) -> C
899
904
where
@@ -908,16 +913,24 @@ where
908
913
self . visit_query_with_stack_impl ( min_x, min_y, max_x, max_y, visitor, & mut stack)
909
914
}
910
915
911
- /// Returns all the item [AABB] that were added to the index by [StaticAABB2DIndexBuilder::add].
916
+ /// Returns all the item [`AABB`] that were added to the index by
917
+ /// [`StaticAABB2DIndexBuilder::add`] during construction.
912
918
///
913
- /// Use [StaticAABB2DIndex::map_all_boxes_index] to map a box back to the original index
914
- /// position it was added.
919
+ /// Use [` StaticAABB2DIndex::item_indices`] or [`StaticAABB2DIndex::all_box_indices`] to map a
920
+ /// box's positional index to the original index position the item was added.
915
921
#[ inline]
916
922
pub fn item_boxes ( & self ) -> & [ AABB < T > ] {
917
923
& self . boxes [ 0 ..self . num_items ]
918
924
}
919
925
920
- /// Gets the node size used for the [StaticAABB2DIndex].
926
+ /// Used to map an item box index position from [`StaticAABB2DIndex::item_boxes`] back to the
927
+ /// original index position the item was added.
928
+ #[ inline]
929
+ pub fn item_indices ( & self ) -> & [ usize ] {
930
+ & self . indices [ 0 ..self . num_items ]
931
+ }
932
+
933
+ /// Gets the node size used for the index.
921
934
///
922
935
/// The node size is the maximum number of boxes stored as children of each node in the index
923
936
/// tree.
@@ -926,40 +939,38 @@ where
926
939
self . node_size
927
940
}
928
941
929
- /// Gets the level bounds for all the boxes in the [StaticAABB2DIndex] .
942
+ /// Gets the level bounds for all the boxes in the index .
930
943
///
931
- /// The level bounds are the index positions in [StaticAABB2DIndex::all_boxes] where a change in
932
- /// the level of the index tree occurs.
944
+ /// The level bounds are the index positions in [` StaticAABB2DIndex::all_boxes` ] where a change
945
+ /// in the level of the index tree occurs.
933
946
#[ inline]
934
947
pub fn level_bounds ( & self ) -> & [ usize ] {
935
948
& self . level_bounds
936
949
}
937
950
938
- /// Gets all the bounding boxes for the [StaticAABB2DIndex] .
951
+ /// Gets all the bounding boxes for the index .
939
952
///
940
953
/// The boxes are ordered from the bottom of the tree up, so from 0 to
941
- /// [StaticAABB2DIndex::count] are all the item bounding boxes. Use
942
- /// [StaticAABB2DIndex::map_all_boxes_index ] to map a box back to the original index position it
954
+ /// [` StaticAABB2DIndex::count` ] are all the item bounding boxes. Use
955
+ /// [` StaticAABB2DIndex::all_box_indices` ] to map a box back to the original index position it
943
956
/// was added or find the start position for the children of a node box.
944
957
#[ inline]
945
958
pub fn all_boxes ( & self ) -> & [ AABB < T > ] {
946
959
& self . boxes
947
960
}
948
961
949
- /// Gets the original item index position (from the time it was added) from a
950
- /// [StaticAABB2DIndex::all_boxes] slice index position.
951
- ///
952
- /// If `all_boxes_index` is greater than [StaticAABB2DIndex::count] then it will return the
953
- /// [StaticAABB2DIndex::all_boxes] starting index of the node's children boxes.
954
- /// See the index_tree_structure.rs example for more information.
962
+ /// Used to map an item box index position from [`StaticAABB2DIndex::all_boxes`] back to the
963
+ /// original index position the item was added. Or if indexing past [`StaticAABB2DIndex::count`]
964
+ /// it will yield the [`StaticAABB2DIndex::all_boxes`] starting index of the node's children
965
+ /// boxes. See the `index_tree_structure.rs` example for more information.
955
966
#[ inline]
956
- pub fn map_all_boxes_index ( & self , all_boxes_index : usize ) -> usize {
957
- self . indices [ all_boxes_index ]
967
+ pub fn all_box_indices ( & self ) -> & [ usize ] {
968
+ & self . indices
958
969
}
959
970
960
- /// Same as [StaticAABB2DIndex::query] but accepts an existing [Vec] to be used as a stack
961
- /// buffer when performing the query to avoid the need for allocation (this is for performance
962
- /// benefit only).
971
+ /// Same as [` StaticAABB2DIndex::query` ] but allows using an existing buffer for stack
972
+ /// traversal. This is useful for performance when many queries will be done repeatedly to avoid
973
+ /// allocating a new stack for each query (this is for performance benefit only).
963
974
#[ inline]
964
975
pub fn query_with_stack (
965
976
& self ,
@@ -977,9 +988,9 @@ where
977
988
results
978
989
}
979
990
980
- /// Same as [StaticAABB2DIndex::visit_query] but accepts an existing [Vec] to be used as a stack
981
- /// buffer when performing the query to avoid the need for allocation (this is for performance
982
- /// benefit only).
991
+ /// Same as [` StaticAABB2DIndex::visit_query` ] but allows using an existing buffer for stack
992
+ /// traversal. This is useful for performance when many queries will be done repeatedly to avoid
993
+ /// allocating a new stack for each query (this is for performance benefit only).
983
994
#[ inline]
984
995
pub fn visit_query_with_stack < V , C > (
985
996
& self ,
@@ -1062,8 +1073,8 @@ where
1062
1073
/// * Because distances are squared (`dx * dx + dy * dy`) be cautious of smaller numeric types
1063
1074
/// overflowing (e.g. it's easy to overflow an i32 with squared distances).
1064
1075
/// * If the point is inside of an item's bounding box then the euclidean distance is 0.
1065
- /// * If repeatedly calling this method then [StaticAABB2DIndex::visit_neighbors_with_queue] can
1066
- /// be used to avoid repeated allocations for the priority queue used internally.
1076
+ /// * If repeatedly calling this method then [` StaticAABB2DIndex::visit_neighbors_with_queue`]
1077
+ /// can be used to avoid repeated allocations for the priority queue used internally.
1067
1078
#[ inline]
1068
1079
pub fn visit_neighbors < V , C > ( & self , x : T , y : T , visitor : & mut V ) -> C
1069
1080
where
@@ -1078,7 +1089,7 @@ where
1078
1089
self . visit_neighbors_with_queue_impl ( x, y, visitor, & mut queue)
1079
1090
}
1080
1091
1081
- /// Works the same as [StaticAABB2DIndex::visit_neighbors] but accepts an existing binary heap
1092
+ /// Works the same as [` StaticAABB2DIndex::visit_neighbors` ] but accepts an existing binary heap
1082
1093
/// to be used as a priority queue to avoid allocations.
1083
1094
#[ inline]
1084
1095
pub fn visit_neighbors_with_queue < V , C > (
0 commit comments