src/ahgrid

Spacial index that allows for querying of objects within a radius of a given point

Details about the specific sof this algorithm can be found here:

https://elephantstarballoon.com/post/ahgrid/

Example:

import src/ahgrid
var grid = newAHGrid[tuple[x, y, width, height: int32]]()

discard grid.insert((x: 1'i32, y: 2'i32, width: 3'i32, height: 4'i32))
discard grid.insert((x: 5'i32, y: 6'i32, width: 7'i32, height: 8'i32))

for obj in grid.find(3, 4, 10):
  echo "Found object near point: ", obj

Types

AHGrid[T] = ref object
A 2d spacial index
GridHandle[T] = object
A handle for a value that can be stored in a AHGrid -- used to update that value
SpatialObject = concept obj
    obj.x is int32
    obj.y is int32
    obj.width is int32
    obj.height is int32
A value that can be stored in a 2d AHGrid

Procs

proc `$`(grid: AHGrid): string
proc clear[T](grid: var AHGrid[T])
Removes all values
proc hash(x: CellIndex): Hash {....raises: [], tags: [], forbids: [].}
proc insert[T: SpatialObject](grid: var AHGrid[T]; value: T): GridHandle[T] {.
    inline.}
Add a value to this spacial grid
proc insert[T](grid: var AHGrid[T]; value: T; space: SpatialObject): GridHandle[
    T]
Add a value to this spacial grid
proc newAHGrid[T](initialSize: Positive = defaultInitialSize;
                  minCellSize: int32 = 2): AHGrid[T]
Create a new AHGrid store
proc remove[T](grid: var AHGrid[T]; handle: GridHandle[T])
proc update[T: SpatialObject](handle: var GridHandle[T]) {.inline.}
Updates the spatial indexing for an object
proc update[T](handle: var GridHandle[T]; space: SpatialObject)
Updates the spatial indexing for an object using the specified spatial information

Iterators

iterator find[T](grid: AHGrid[T]; x, y, radius: int32): T
Finds all the values that are approximately within a given radius of a point
iterator find[T](grid: AHGrid[T]; x1, y1, x2, y2: int32): T
Finds all the values within a given rectangle
iterator items[T](grid: AHGrid[T]): T
Iterates all values in this grid