spatial

package
v1.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 22, 2026 License: MIT Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package spatial is the index behind a Space: a cell grid over a slice of items, answering range queries and finding every two items close enough to matter.

Item and Capability

An Item is what the grid is told of: an entity id, its box as the plane holds it, and its Capability, a set of bits a query matches against — any shared bit is a match, AnyCapability matches all. The public aabbworld.Item and aabbworld.Capability have this exact layout.

Grid

A Grid is told the world by Grid.Rebuild, which records the slice and which id sits where and nothing more; the cells are built at the first Grid.Query after it, from the boxes as they are then. The items must stay put until the next Rebuild, but their boxes may move; Grid.Invalidate says they have, so the next Query rebuilds the cells. Grid.Item and Grid.EntryAABB answer by id from the slice directly.

Grid.Pairs finds every two items sharing a capability whose boxes, grown by a reach of their shorter side, touch. It does not read the cells: it sweeps its own counting-sorted layout of the grown boxes, listing only the items the caller wants, and reports pairs as positions in the slice. The two layouts are kept apart on purpose — each holds only what its reader needs, and one shared layout was measured to cost more than it saves (see BENCHMARKS.md).

Both layouts are counting sorts into cells: count the pieces per cell, prefix-sum the counts into offsets, deal the pieces out. A box straddling a wrapping seam is listed once per piece, and a query or a pair met through more than one piece is reported once, by the cell that owns the top-left corner of the overlap.

Resolution

A Resolution names a square grid by its side, a power of two, so a coordinate finds its cell by a shift. ResolutionFrom rounds a size up to the next power of two; Resolution.Side, Resolution.MaxCoord and Resolution.Cells read it back.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Capability

type Capability uint8

Capability is what may be done with an entry, as a set of up to eight bits. A query matches when any bit is shared; what the bits mean is the caller's business.

const (
	// Plain is an entry that is nothing but geometry; the zero value.
	Plain Capability = 0
	// CanCollide is an entry that takes part in collisions.
	CanCollide Capability = 1 << 1
	// Static is an entry nothing shifts.
	Static Capability = 1 << 2
	// Sensor is an entry only ever detected.
	Sensor Capability = 1 << 3

	// AnyCapability is the query mask that accepts every entry.
	AnyCapability Capability = 0xFF
)

type Grid added in v1.5.0

type Grid struct {
	// contains filtered or unexported fields
}

Grid is a cell grid over the items of its last Rebuild. Pair sweeps read the items as they are; the cells are built from them at the first Query after a Rebuild or an Invalidate.

func NewGrid added in v1.5.0

func NewGrid(surface *iplane.Surface, worldRes, cellRes Resolution) *Grid

NewGrid builds an empty grid over surface, worldRes on a side, cellRes per cell.

func (*Grid) EntryAABB added in v1.5.0

func (g *Grid) EntryAABB(id uid.UID64) (geom.AABB, bool)

EntryAABB is the main box of the entity id names as the grid holds it.

func (*Grid) Invalidate added in v1.5.0

func (g *Grid) Invalidate()

Invalidate marks the cells as behind the boxes of the items, which have moved since they were built.

func (*Grid) Item added in v1.5.0

func (g *Grid) Item(id uid.UID64) (*Item, bool)

Item is the item the entity id names, if the last Rebuild was told of it.

func (*Grid) Items added in v1.5.0

func (g *Grid) Items() []Item

Items is what the last Rebuild was told of, in order.

func (*Grid) Pairs added in v1.5.0

func (g *Grid) Pairs(reach float64, want Capability, fn func(a, b int32))

Pairs calls fn once, lower position first, for every two items sharing want whose boxes grown by reach of their shorter side touch. It reads the boxes as they are now.

func (*Grid) Query added in v1.5.0

func (g *Grid) Query(box geom.AABB, want Capability, fn func(uid.UID64)) int

Query calls fn for every piece sharing want that intersects box, each once, and returns how many. It reads the boxes as they are now, building the cells first if they are behind.

func (*Grid) Rebuild added in v1.5.0

func (g *Grid) Rebuild(items []Item)

Rebuild replaces what the grid holds with items, which must stay put until the next Rebuild. Their boxes may change in the meantime; Invalidate tells the grid so. The cells are built at the first Query that needs them, so several Rebuilds in a row cost one indexing.

type Item added in v1.5.0

type Item struct {
	ID   uid.UID64
	Box  plane.AABB
	Caps Capability
}

Item is one box a Grid is told of at Rebuild.

type Resolution

type Resolution uint8

Resolution is a grid size named after its total cell count (side length squared).

const (
	Size1x1 Resolution = iota
	Size2x2
	Size4x4
	Size8x8
	Size16x16
	Size32x32
	Size64x64
	Size128x128
	Size256x256
	Size512x512
	Size1024x1024
	Size2048x2048
)

func NewResolution

func NewResolution(cord uint8) Resolution

func ResolutionFrom

func ResolutionFrom(number uint32) Resolution

func (Resolution) Cells

func (s Resolution) Cells() int

Cells returns the total number of cells in the grid (side * side).

Example
fmt.Println(Size1x1.Cells())
fmt.Println(Size8x8.Cells())
fmt.Println(Size1024x1024.Cells())
Output:
1
64
1048576

func (Resolution) MaxCoord

func (s Resolution) MaxCoord() uint32

MaxCoord returns the maximum coordinate on each axis, i.e. side - 1.

Example
fmt.Println(Size1x1.MaxCoord())
fmt.Println(Size8x8.MaxCoord())
fmt.Println(Size1024x1024.MaxCoord())
Output:
0
7
1023

func (Resolution) Side

func (s Resolution) Side() uint32

Side returns the side length of the square grid, e.g. Size8x8 → 8.

Example
fmt.Println(Size1x1.Side())
fmt.Println(Size8x8.Side())
fmt.Println(Size1024x1024.Side())
Output:
1
8
1024

func (Resolution) String

func (s Resolution) String() string

String returns a human-readable name like "1x1", "1024x1024".

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL