Code Monkey home page Code Monkey logo

flat_spatial's Introduction

flat_spatial

Build Status Crates.io Docs.rs

flat_spatial is a crate dedicated to dynamic spatial partitioning structures that are not based on trees (which are recursive) but on simple flat structures such as a grid of cells (also called bins).
Using grids or other flat structures makes for very fast updates (constant time) and even faster queries, provided the cell size is adapted to the problem.

Picking the right cell size is very important:

  • If the cell size is too small, the grid will be too fine and the queries will be slow as they need to iterate over all matching cells.
  • If the cell size is too big, the grid will be too coarse and the queries will be slow as they need to iterate over all matching objects.

Try to pick a cell size that gives an average of 10-20 objects per cell on average. Note that empty cells don't consume any memory, but they do consume some query time as we need to check if they exist.

MSRV: 1.60

Grid

The idea of a grid is to have a HashMap of cells which store the positions of the inserted objects.
Performing queries is as simple as looking up which cells are affected and returning their associated objects.
Since it's so simple, the grid supports dynamic capabilities such as position update or object removal based on handles (using slotmap). The position updates are lazy for better performance, so maintain() needs to be called to update the grid.

It is recommended to have queries roughly the same size as the cell size.

AABBGrid

The aabbgrid is like a grid but it stores Axis-Aligned Bounding Boxes (AABB) instead of positions. This implemented as a HashMap of cells which store the AABB that touches it. For each cell an AABB touches, it is added to the cell. Try to keep the aabb sizes as small as possible.

Adding/updating/removing isn't lazy, no need to call maintain.

Example

Here is a very basic example of the grid:

fn main() {
    use flat_spatial::Grid;
    
    let mut g: Grid<(), [f32; 2]> = Grid::new(10);
    let a = g.insert([3.0, 3.0], ());
    let _b = g.insert([12.0, -8.0], ());
    
    let around: Vec<_> = g.query_around([2.0, 2.0], 5.0)
                          .map(|(id, _pos)| id)
                          .collect();
     
    assert_eq!(vec![a], around);
}

flat_spatial's People

Contributors

uriopass avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

setzer22 tynberry

flat_spatial's Issues

Parallel structures

Currently, the cycle
maintain -> collision detection -> position updates/removals -> maintain -> ...
has a lot of sequential operations (remove, set_position and maintain), severely limiting usage in highly parallel environment (my 24-core ryzen).

Implement some sort of parallel feature or structure to be faster. However since maintain/set_position/remove are all data operations I worry that it cannot be done and that It will be bandwidth limited.

Maybe having a concurrent hashmap of cells with a concurrent hashset of objects in the cell could permit maintain-free and parallel set_position/removals ? The overhead might be worth it with a lot of cores.

cell_range

As an alternative to XYRange consider

pub fn cell_range((x1, y1): CellIdx, (x2, y2): CellIdx) -> impl Iterator<Item = CellIdx> {
    (y1..y2+1).flat_map(move |y| (x1..x2+1).zip(std::iter::repeat(y)))
}

Add fuzzing tests

Would be interested to see how fuzzing works and how to apply it here.

Thick primitives

Implement a grid to contain thick primitives such as circles, rectangles, ... efficiently while still allowing some sort of dynamism (removal, position update).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.