[][src]Struct static_aabb2d_index::StaticAABB2DIndex

pub struct StaticAABB2DIndex<T = f64> where
    T: IndexableNum
{ /* fields omitted */ }

Static/fixed size indexing data structure for two dimensional axis aligned bounding boxes.

The index allows for fast construction and fast querying but cannot be modified after creation. This type is constructed from a StaticAABB2DIndexBuilder.

2D axis aligned bounding boxes are represented by two extent points (four values): (min_x, min_y), (max_x, max_y).

Examples

use static_aabb2d_index::*;
// create builder for index containing 4 axis aligned bounding boxes
// index also supports integers and custom types that implement the IndexableNum trait
let mut builder: StaticAABB2DIndexBuilder<f64> = StaticAABB2DIndexBuilder::new(4);
// add bounding boxes to the index
// add takes in (min_x, min_y, max_x, max_y) of the bounding box
builder.add(0.0, 0.0, 2.0, 2.0);
builder.add(-1.0, -1.0, 3.0, 3.0);
builder.add(0.0, 0.0, 1.0, 3.0);
builder.add(4.0, 2.0, 16.0, 8.0);
// note build may return an error if the number of added boxes does not equal the static size
// given at the time the builder was created or the type used fails to cast to/from a u16
let index: StaticAABB2DIndex<f64> = builder.build().unwrap();
// query the created index (min_x, min_y, max_x, max_y)
let query_results = index.query(-1.0, -1.0, -0.5, -0.5);
// query_results holds the index positions of the boxes that overlap with the box given
// (positions are according to the order boxes were added the index builder)
assert_eq!(query_results, vec![1]);
// the query may also be done with a visiting function that can stop the query early
let mut visited_results: Vec<usize> = Vec::new();
let mut visitor = |box_added_pos: usize| -> bool {
    visited_results.push(box_added_pos);
    // return true to continue visiting results, false to stop early
    true
};

index.visit_query(-1.0, -1.0, -0.5, -0.5, &mut visitor);
assert_eq!(visited_results, vec![1]);

Implementations

impl<T> StaticAABB2DIndex<T> where
    T: IndexableNum
[src]

pub fn min_x(&self) -> T[src]

Gets the min_x extent value of the all the bounding boxes in the index.

pub fn min_y(&self) -> T[src]

Gets the min_y extent value of the all the bounding boxes in the index.

pub fn max_x(&self) -> T[src]

Gets the max_x extent value of the all the bounding boxes in the index.

pub fn max_y(&self) -> T[src]

Gets the max_y extent value of the all the bounding boxes in the index.

pub fn count(&self) -> usize[src]

Gets the total count of items that were added to the index.

pub fn query(&self, min_x: T, min_y: T, max_x: T, max_y: T) -> Vec<usize>[src]

Queries the index, returning a collection of indexes to items that overlap with the bounding box given.

min_x, min_y, max_x, and max_y represent the bounding box to use for the query. Indexes returned match with the order items were added to the index using StaticAABB2DIndexBuilder::add.

pub fn query_iter<'a>(
    &'a self,
    min_x: T,
    min_y: T,
    max_x: T,
    max_y: T
) -> impl Iterator<Item = usize> + 'a
[src]

The same as StaticAABB2DIndex::query but instead of returning a Vec of results a lazy iterator is returned which yields the results.

Examples

use static_aabb2d_index::*;
let mut builder = StaticAABB2DIndexBuilder::new(4);
builder
    .add(0.0, 0.0, 2.0, 2.0)
    .add(-1.0, -1.0, 3.0, 3.0)
    .add(0.0, 0.0, 1.0, 3.0)
    .add(4.0, 2.0, 16.0, 8.0);
let index = builder.build().unwrap();
let query_results = index.query_iter(-1.0, -1.0, -0.5, -0.5).collect::<Vec<usize>>();
assert_eq!(query_results, vec![1]);

pub fn visit_query<F>(
    &self,
    min_x: T,
    min_y: T,
    max_x: T,
    max_y: T,
    visitor: &mut F
) where
    F: FnMut(usize) -> bool
[src]

Same as StaticAABB2DIndex::query but instead of returning a collection of indexes a visitor function is called for each index that would be returned. The visitor returns a bool indicating whether to continue visiting (true) or not (false).

pub fn item_boxes(&self) -> &[AABB<T>][src]

Returns all the item AABB that were added to the index by StaticAABB2DIndexBuilder::add.

Use StaticAABB2DIndex::map_all_boxes_index to map a box back to the original index position it was added.

pub fn node_size(&self) -> usize[src]

Gets the node size used for the StaticAABB2DIndex.

The node size is the maximum number of boxes stored as children of each node in the index tree.

pub fn level_bounds(&self) -> &[usize][src]

Gets the level bounds for all the boxes in the StaticAABB2DIndex.

The level bounds are the index positions in StaticAABB2DIndex::all_boxes where a change in the level of the index tree occurs.

pub fn all_boxes(&self) -> &[AABB<T>][src]

Gets all the bounding boxes for the StaticAABB2DIndex.

The boxes are ordered from the bottom of the tree up, so from 0 to StaticAABB2DIndex::count are all the item bounding boxes. Use StaticAABB2DIndex::map_all_boxes_index to map a box back to the original index position it was added or find the start position for the children of a node box.

pub fn map_all_boxes_index(&self, all_boxes_index: usize) -> usize[src]

Gets the original item index position (from the time it was added) from a StaticAABB2DIndex::all_boxes slice index position.

If all_boxes_index is greater than StaticAABB2DIndex::count then it will return the StaticAABB2DIndex::all_boxes starting index of the node's children boxes. See the index_tree_structure.rs example for more information.

pub fn query_with_stack(
    &self,
    min_x: T,
    min_y: T,
    max_x: T,
    max_y: T,
    stack: &mut Vec<usize>
) -> Vec<usize>
[src]

Same as StaticAABB2DIndex::query but accepts an existing Vec to be used as a stack buffer when performing the query to avoid the need for allocation (this is for performance benefit only).

pub fn visit_query_with_stack<F>(
    &self,
    min_x: T,
    min_y: T,
    max_x: T,
    max_y: T,
    visitor: &mut F,
    stack: &mut Vec<usize>
) where
    F: FnMut(usize) -> bool
[src]

Same as StaticAABB2DIndex::visit_query but accepts an existing Vec to be used as a stack buffer when performing the query to avoid the need for allocation (this is for performance benefit only).

Trait Implementations

impl<T: Clone> Clone for StaticAABB2DIndex<T> where
    T: IndexableNum
[src]

impl<T: Debug> Debug for StaticAABB2DIndex<T> where
    T: IndexableNum
[src]

Auto Trait Implementations

impl<T> RefUnwindSafe for StaticAABB2DIndex<T> where
    T: RefUnwindSafe
[src]

impl<T> Send for StaticAABB2DIndex<T> where
    T: Send
[src]

impl<T> Sync for StaticAABB2DIndex<T> where
    T: Sync
[src]

impl<T> Unpin for StaticAABB2DIndex<T> where
    T: Unpin
[src]

impl<T> UnwindSafe for StaticAABB2DIndex<T> where
    T: UnwindSafe
[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.