Documentation
¶
Overview ¶
Package spatial provides a discrete spatial index over a 2D power-of-two grid for storing and querying objects by integer coordinates, with support for range queries (AABB) and bulk operations.
Index ¶
- Variables
- func MortonCodeAreaConsume(aabb AABB, fn func(int, MortonCode))
- type AABB
- type Capability
- type CellCodec
- type CellCodecKind
- type EntriesMove
- type Entry
- type GridIndexConfig
- type GridIndexManager
- func (m *GridIndexManager) EntryAABB(entryID uid.UID64) (geom.AABB, bool)
- func (m *GridIndexManager) Flush(onDirty func(geom.AABB))
- func (m *GridIndexManager) Pairs(reach float64, want Capability, fn func(a, b uid.UID64))
- func (m *GridIndexManager) QueryRange(aabb geom.AABB, collector func(uid.UID64)) int
- func (m *GridIndexManager) QueryRangeWith(aabb geom.AABB, want Capability, collector func(uid.UID64)) int
- func (m *GridIndexManager) QueueInsert(id uid.UID64, aabb plane.AABB)
- func (m *GridIndexManager) QueueRemove(id uid.UID64)
- func (m *GridIndexManager) QueueSetCapabilities(id uid.UID64, c Capability)
- func (m *GridIndexManager) QueueUpdate(id uid.UID64, aabb plane.AABB, markDirty bool)
- type GridIndexer
- type Index
- type LinearCodeCodec
- type MortonCode
- type MortonCodeCodec
- type Option
- type Resolution
- type Vec
Examples ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func MortonCodeAreaConsume ¶
func MortonCodeAreaConsume(aabb AABB, fn func(int, MortonCode))
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 — what one is until told otherwise. Plain Capability = 1 << 0 // AnyCapability is the query mask that accepts every entry. AnyCapability Capability = 0xFF )
type CellCodecKind ¶
type CellCodecKind uint8
CellCodecKind selects which CellCodec a bucketGrid uses; the zero value is LinearCellCodec.
const ( LinearCellCodec CellCodecKind = iota MortonCellCodec )
type EntriesMove ¶
Index is a spatial index over a 2D power-of-two grid: boxes in, range queries out.
func NewEntriesMove ¶
func NewEntriesMove(capHint int) EntriesMove
type GridIndexConfig ¶
type GridIndexConfig struct {
Resolution Resolution
BucketResolution Resolution
BucketCapacity int
OpsBufferSize int
// CellCodec selects the grid-cell codec; the zero value is LinearCellCodec.
CellCodec CellCodecKind
}
type GridIndexManager ¶
type GridIndexManager struct {
// contains filtered or unexported fields
}
GridIndexManager buffers spatial updates and applies them in bulk. Queue* are safe from any goroutine; Flush and every read belong to one.
func NewGridIndexManager ¶
func NewGridIndexManager(space *iplane.Surface, cfg GridIndexConfig) (*GridIndexManager, error)
func (*GridIndexManager) EntryAABB ¶
EntryAABB returns the indexed box of entryID; call it from the goroutine that calls Flush.
func (*GridIndexManager) Flush ¶
func (m *GridIndexManager) Flush(onDirty func(geom.AABB))
Flush applies the queued ops; call it from a single, fixed goroutine.
func (*GridIndexManager) Pairs ¶
func (m *GridIndexManager) Pairs(reach float64, want Capability, fn func(a, b uid.UID64))
Pairs calls fn once, lower index first, for every two entries sharing want whose reaches touch.
func (*GridIndexManager) QueryRange ¶
QueryRange calls collector for every entry intersecting aabb and returns how many.
func (*GridIndexManager) QueryRangeWith ¶
func (m *GridIndexManager) QueryRangeWith(aabb geom.AABB, want Capability, collector func(uid.UID64)) int
QueryRangeWith is QueryRange restricted to entries sharing a capability with want.
func (*GridIndexManager) QueueInsert ¶
func (m *GridIndexManager) QueueInsert(id uid.UID64, aabb plane.AABB)
func (*GridIndexManager) QueueRemove ¶
func (m *GridIndexManager) QueueRemove(id uid.UID64)
func (*GridIndexManager) QueueSetCapabilities ¶
func (m *GridIndexManager) QueueSetCapabilities(id uid.UID64, c Capability)
QueueSetCapabilities queues id's capabilities, in order with every other change.
func (*GridIndexManager) QueueUpdate ¶
type GridIndexer ¶
Index is a spatial index over a 2D power-of-two grid: boxes in, range queries out.
type Index ¶
type Index interface {
// BulkInsert – insert many objects at once.
BulkInsert(entries []Entry)
// BulkRemove – remove whatever is stored at the given positions.
BulkRemove(entries []Entry)
// BulkMove – update objects (typically same Value, different XY).
BulkMove(moves EntriesMove)
// QueryRange calls collector for every object within aabb; collector must not modify the Index.
QueryRange(aabb AABB, collector func(uid.UID64)) int
// QueryRangeWith – as QueryRange, but only objects sharing a capability with want.
QueryRangeWith(aabb AABB, want Capability, collector func(uid.UID64)) int
// Count – number of objects in the structure.
Count() int
// Bounds – global bounds of the handled space.
Bounds() AABB
Optimize()
Clear()
}
Index is a spatial index over a 2D power-of-two grid: boxes in, range queries out.
func NewBucketGrid ¶
func NewBucketGrid( overallResolution Resolution, bucketsResolution Resolution, opts ...Option, ) (Index, error)
type LinearCodeCodec ¶
type LinearCodeCodec struct {
// contains filtered or unexported fields
}
func NewLinearCodec ¶
func NewLinearCodec(res Resolution) LinearCodeCodec
func (LinearCodeCodec) Decode ¶
func (lc LinearCodeCodec) Decode(index int) (x, y uint32)
type MortonCode ¶
type MortonCode uint64
func MortonCodeArea ¶
func MortonCodeArea(aabb AABB) []MortonCode
func NewMortonCode ¶
func NewMortonCode(x, y uint32) MortonCode
Example ¶
for _, mCode := range []MortonCode{
NewMortonCode(4, 1),
NewMortonCode(5, 5),
NewMortonCode(0b1001, 0b10101),
} {
fmt.Printf("%b\n", mCode)
}
Output: 10010 110011 1001100011
func (MortonCode) Decode ¶
func (c MortonCode) Decode() (x, y uint32)
Example ¶
for _, mCode := range []MortonCode{
NewMortonCode(4, 1),
NewMortonCode(5, 5),
NewMortonCode(0b1001, 0b10101),
} {
x, y := mCode.Decode()
fmt.Printf("(%d,%d)\n", x, y)
}
Output: (4,1) (5,5) (9,21)
func (MortonCode) IncX ¶
func (c MortonCode) IncX() MortonCode
func (MortonCode) IncY ¶
func (c MortonCode) IncY() MortonCode
func (MortonCode) Offset ¶
func (c MortonCode) Offset(dx, dy int32) MortonCode
type MortonCodeCodec ¶
type MortonCodeCodec struct {
// contains filtered or unexported fields
}
func NewMortonCodec ¶
func NewMortonCodec(res Resolution) MortonCodeCodec
func (MortonCodeCodec) Decode ¶
func (mc MortonCodeCodec) Decode(code int) (x, y uint32)
type Option ¶
type Option func(*bucketGrid) error
func WithBucketCapacity ¶
func WithMortonCodec ¶
func WithMortonCodec() Option
WithMortonCodec swaps the default linear cell codec for a Z-order one.
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".