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, Run 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. A Run is many of them in the caller's own records: ids beside a column of records of one stride, with the box and the capability at offsets in each. The public aabbworld.Item, aabbworld.Run and aabbworld.Capability have this exact layout.
Grid ¶
A Grid is told the world by Grid.RebuildRuns — runs, then items, at positions counted on across them — or Grid.Rebuild, items alone; either records where the records lie and nothing more. The cells are built at the first Grid.Query after it, from the boxes as they are then. The records 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.Items lists each position as an Entry — a pointer to its box, its id and its capability — at the first call after a Rebuild; Grid.Item and Grid.EntryAABB answer by id from it, mapping the ids at the first call too.
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 ¶
- func Wide() bool
- type Capability
- type Entry
- type Grid
- func (g *Grid) EntryAABB(id uid.UID64) (geom.AABB, bool)
- func (g *Grid) Invalidate()
- func (g *Grid) Item(id uid.UID64) (*Entry, bool)
- func (g *Grid) Items() []Entry
- func (g *Grid) Pairs(reach float32, want Capability, fn func(a, b int32))
- func (g *Grid) Query(box geom.AABB, want Capability, fn func(uid.UID64)) int
- func (g *Grid) Ready()
- func (g *Grid) Rebuild(items []Item)
- func (g *Grid) RebuildRuns(runs []Run, items []Item)
- type Item
- type Resolution
- type Run
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 Entry ¶
type Entry struct {
Box *plane.AABB
ID uid.UID64
Caps Capability
}
Entry is one item of the last Rebuild: its box where it lies, its id and its capability.
type Grid ¶ added in v1.5.0
type Grid struct {
// contains filtered or unexported fields
}
Grid is a cell grid over the records of its last Rebuild, positioned in order across its runs and then its items. Pair sweeps read the records 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
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
Item is the item the entity id names, if the last Rebuild was told of it.
func (*Grid) Pairs ¶ added in v1.5.0
func (g *Grid) Pairs(reach float32, 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
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) Ready ¶ added in v1.15.0
func (g *Grid) Ready()
Ready brings the cells, the items and the ids' positions up to the last Rebuild or Invalidate: the first reader after them does, under the lock, the others waiting; then every reader only reads, so several goroutines may read the grid at once while nothing writes it.
func (*Grid) Rebuild ¶ added in v1.5.0
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.
func (*Grid) RebuildRuns ¶ added in v1.14.0
RebuildRuns is Rebuild over the records of runs and then items: the memory of both must hold the same records in the same places until the next Rebuild, and their capabilities with them.
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".
type Run ¶ added in v1.14.0
type Run struct {
IDs []uid.UID64
Base unsafe.Pointer
Stride uintptr
BoxOff uintptr
CapsOff uintptr
}
Run is a column of records in memory the caller owns: record i starts i·Stride bytes past Base, with its box at BoxOff and its Capability at CapsOff, and IDs[i] names it.