pub struct AABBGrid<O: Copy, AB: AABB> { /* private fields */ }
Expand description

AABBGrid is a generic aabb-based spatial partitioning structure that uses a generic storage of cells which acts as a grid instead of a tree.

Fast queries

In theory, AABBGrid should be faster than a quadtree/r-tree because it has no log costs (calculating the cells around a point is trivial).
However, it only works if the cell size is adapted to the problem, much like how a tree has to be balanced to be efficient.

Dynamicity

AABBGrid's allows eager removals and position updates, however for big aabbs (spanning many cells) this can be expensive, so beware.

Use this grid for mostly static objects with the occasional removal/position update if needed.

A SlotMap is used for objects managing, adding a level of indirection between aabbs and objects. SlotMap is used because removal doesn’t alter handles given to the user, while still having constant time access. However it requires O to be copy, but SlotMap's author stated that they were working on a similar map where Copy isn’t required.

About object management

In theory, you don’t have to use the object management directly, you can make your custom Handle -> Object map by specifying “()” to be the object type. (This can be useful if your object is not Copy) Since () is zero sized, it should probably optimize away a lot of the object management code.

use flat_spatial::AABBGrid;
use euclid::default::Rect;

let mut g: AABBGrid<(), Rect<f32>> = AABBGrid::new(10);
let handle = g.insert(Rect::new([0.0, 0.0].into(), [10.0, 10.0].into()), ());
// Use handle however you want

Implementations§

source§

impl<O: Copy, AB: AABB> AABBGrid<O, AB>

source

pub fn new(cell_size: i32) -> Self

Creates an empty grid. The cell size should be about the same magnitude as your queries size.

source

pub fn clear(&mut self) -> impl Iterator<Item = (AB, O)>

Clears the grid.

source

pub fn insert(&mut self, aabb: AB, obj: O) -> AABBGridHandle

Inserts a new object with a position and an associated object Returns the unique and stable handle to be used with get_obj

source

pub fn set_aabb(&mut self, handle: AABBGridHandle, aabb: AB)

Updates the aabb of an object.

source

pub fn remove(&mut self, handle: AABBGridHandle) -> Option<O>

Removes an object from the grid.

source

pub fn handles(&self) -> impl Iterator<Item = AABBGridHandle> + '_

Iterate over all handles

source

pub fn objects(&self) -> impl Iterator<Item = &O> + '_

Iterate over all objects

source

pub fn get(&self, id: AABBGridHandle) -> Option<&StoreObject<O, AB>>

Returns a reference to the associated object and its position, using the handle.

source

pub fn get_mut(&mut self, id: AABBGridHandle) -> Option<&mut StoreObject<O, AB>>

Returns a mutable reference to the associated object and its position, using the handle.

source

pub fn storage(&self) -> &SparseStorage<AABBGridCell>

The underlying storage

source

pub fn query( &self, aabb: AB ) -> impl Iterator<Item = (AABBGridHandle, &AB, &O)> + '_

Queries for objects intersecting a given AABB.

source

pub fn query_broad(&self, bbox: AB) -> impl Iterator<Item = AABBGridHandle> + '_

Queries for all objects in the cells intersecting the given AABB

source

pub fn query_visitor( &self, aabb: AB, visitor: impl FnMut(AABBGridHandle, &AB, &O) )

Queries for objects intersecting a given AABB. Uses a visitor for slightly better performance.

source

pub fn query_broad_visitor(&self, bbox: AB, visitor: impl FnMut(AABBGridHandle))

Queries for all objects in the cells intersecting the given AABB Uses a visitor for slightly better performance.

source

pub fn len(&self) -> usize

Returns the number of objects currently available

source

pub fn is_empty(&self) -> bool

Checks if the grid contains objects or not

Trait Implementations§

source§

impl<O: Clone + Copy, AB: Clone + AABB> Clone for AABBGrid<O, AB>

source§

fn clone(&self) -> AABBGrid<O, AB>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<O, AB> RefUnwindSafe for AABBGrid<O, AB>where AB: RefUnwindSafe, O: RefUnwindSafe,

§

impl<O, AB> Send for AABBGrid<O, AB>where AB: Send, O: Send,

§

impl<O, AB> Sync for AABBGrid<O, AB>where AB: Sync, O: Sync,

§

impl<O, AB> Unpin for AABBGrid<O, AB>where AB: Unpin, O: Unpin,

§

impl<O, AB> UnwindSafe for AABBGrid<O, AB>where AB: UnwindSafe, O: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.