pub struct Grid<O, V2: Vec2> { /* private fields */ }
Expand description

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

Fast queries

In theory, Grid 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

Grid’s big advantage is that it is dynamic, supporting lazy positions updates and object removal in constant time. Once objects are in, there is almost no allocation happening.

Compare that to most immutable spatial partitioning structures out there, which pretty much require to rebuild the entire tree every time.

A SlotMap is used for objects managing, adding a level of indirection between points 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.

Examples

Here is a basic example that shows most of its capabilities:

use flat_spatial::Grid;

let mut g: Grid<i32, [f32; 2]> = Grid::new(10); // Creates a new grid with a cell width of 10 with an integer as extra data
let a = g.insert([0.0, 0.0], 0); // Inserts a new element with data: 0

{
    let mut before = g.query_around([0.0, 0.0], 5.0).map(|(id, _pos)| id); // Queries for objects around a given point
    assert_eq!(before.next(), Some(a));
    assert_eq!(g.get(a).unwrap().1, &0);
}
let b = g.insert([0.0, 0.0], 1); // Inserts a new element, assigning a new unique and stable handle, with data: 1

g.remove(a); // Removes a value using the handle given by `insert`
             // This won't have an effect until g.maintain() is called

g.maintain(); // Maintains the grid, which applies all removals and position updates (not needed for insertions)

assert_eq!(g.handles().collect::<Vec<_>>(), vec![b]); // We check that the "a" object has been removed

let after: Vec<_> = g.query_around([0.0, 0.0], 5.0).map(|(id, _pos)| id).collect(); // And that b is query-able
assert_eq!(after, vec![b]);

assert_eq!(g.get(b).unwrap().1, &1); // We also check that b still has his data associated
assert_eq!(g.get(a), None); // But that a doesn't exist anymore

Implementations

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

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

Lazily sets the position of an object (if it is not marked for deletion). This won’t be taken into account until maintain() is called.

Lazily removes an object from the grid. This won’t be taken into account until maintain() is called.

Example
use flat_spatial::Grid;
let mut g: Grid<(), [f32; 2]> = Grid::new(10);
let h = g.insert([5.0, 3.0], ());
g.remove(h);

Directly removes an object from the grid. This is equivalent to remove() then maintain() but is much faster (O(1))

Example
use flat_spatial::Grid;
let mut g: Grid<(), [f32; 2]> = Grid::new(10);
let h = g.insert([5.0, 3.0], ());
g.remove(h);

Clear all objects from the grid. Returns the objects and their positions.

Maintains the world, updating all the positions (and moving them to corresponding cells) and removing necessary objects and empty cells. Runs in linear time O(N) where N is the number of objects.

Example
use flat_spatial::Grid;
let mut g: Grid<(), [f32; 2]> = Grid::new(10);
let h = g.insert([5.0, 3.0], ());
g.remove(h);

assert!(g.get(h).is_some());
g.maintain();
assert!(g.get(h).is_none());

Iterate over all handles

Iterate over all objects

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

Example
use flat_spatial::Grid;
let mut g: Grid<i32, [f32; 2]> = Grid::new(10);
let h = g.insert([5.0, 3.0], 42);
assert_eq!(g.get(h), Some(([5.0, 3.0], &42)));

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

Example
use flat_spatial::Grid;
let mut g: Grid<i32, [f32; 2]> = Grid::new(10);
let h = g.insert([5.0, 3.0], 42);
*g.get_mut(h).unwrap().1 = 56;
assert_eq!(g.get(h).unwrap().1, &56);

The underlying storage

Queries for all objects in the cells intersecting an axis-aligned rectangle defined by lower left (ll) and upper right (ur) Try to keep the rect’s width/height of similar magnitudes to the cell size for better performance.

Example
use flat_spatial::Grid;

let mut g: Grid<(), [f32; 2]> = Grid::new(10);
let a = g.insert([0.0, 0.0], ());
let b = g.insert([5.0, 5.0], ());

let around: Vec<_> = g.query([-1.0, -1.0].into(), [1.0, 1.0].into()).map(|(id, _pos)| id).collect();

assert_eq!(vec![a, b], around);

query_visitor is similar to query, but uses a visitor function to be slightly more performant.

Returns the number of objects currently available (removals that were not confirmed with maintain() are still counted)

Checks if the grid contains objects or not (removals that were not confirmed with maintain() are still counted)

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.

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

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.