pyLEAFS.spatialhash module

Generic spatial hash over a point set.

A SpatialHash buckets an arbitrary (n, D) set of points into the regions of a shared Grid, then answers Moore-neighbourhood queries. It is deliberately agnostic about what the points are — resources, foragers, predators — so the same structure serves every layer. Unlike the resource field’s internal buffer, this is a lightweight index rebuilt from a positions array on demand.

class pyLEAFS.spatialhash.SpatialHash(grid, positions=None)[source]

Bases: object

Bucket a point set into grid regions for neighbourhood queries.

Parameters:
  • grid (Grid) – Shared spatial grid.

  • positions (ndarray, shape (n, D), optional) – Initial point set. May be rebuilt later with build().

Examples

>>> g = Grid((5, 5), L=10.0)
>>> pts = np.array([[1.0, 1.0], [12.0, 3.0], [1.5, 1.5]])
>>> sh = SpatialHash(g, pts)
>>> idx = sh.query(np.array([1.2, 1.2]))
>>> sorted(idx.tolist())
[0, 2]
build(positions)[source]

(Re)index positions into per-region buckets.

Parameters:

positions (ndarray, shape (n, D)) – Physical positions to index.

query(pos)[source]

Indices of points in the Moore neighbourhood of pos.

Parameters:

pos (ndarray, shape (D,)) – Physical query position.

Returns:

Indices into the indexed positions array of every point whose region is in the Moore neighbourhood of pos. Not distance filtered — the caller refines with actual displacements.

Return type:

ndarray of int

query_within(pos, radius)[source]

Indices of indexed points within radius of pos.

Refines query() by minimum-image distance. Assumes radius is no larger than one region side so the Moore neighbourhood suffices.