spatial

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2026 License: MIT Imports: 10 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 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

Examples

Constants

This section is empty.

Variables

View Source
var (
	NewVec    = geom.NewVec
	NewAABB   = geom.NewAABB
	NewAABBAt = geom.NewAABBAt
)

Functions

func MortonCodeAreaConsume

func MortonCodeAreaConsume(aabb AABB, fn func(int, MortonCode))

Types

type AABB

type AABB = geom.AABB

Index is a spatial index over a 2D power-of-two grid: boxes in, range queries out.

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 CellCodec

type CellCodec interface {
	Encode(x, y uint32) (int, error)
	Decode(int) (x, y uint32)
}

type CellCodecKind

type CellCodecKind uint8

CellCodecKind selects which CellCodec a bucketGrid uses; the zero value is LinearCellCodec.

const (
	LinearCellCodec CellCodecKind = iota
	MortonCellCodec
)

type EntriesMove

type EntriesMove struct {
	Old []Entry
	New []Entry
}

Index is a spatial index over a 2D power-of-two grid: boxes in, range queries out.

func NewEntriesMove

func NewEntriesMove(capHint int) EntriesMove

func (*EntriesMove) Append

func (u *EntriesMove) Append(id uid.UID64, old, new AABB)

type Entry

type Entry struct {
	AABB
	Id uid.UID64
}

Index is a spatial index over a 2D power-of-two grid: boxes in, range queries out.

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

func (m *GridIndexManager) EntryAABB(entryID uid.UID64) (geom.AABB, bool)

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

func (m *GridIndexManager) QueryRange(aabb geom.AABB, collector func(uid.UID64)) int

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

func (m *GridIndexManager) QueueUpdate(id uid.UID64, aabb plane.AABB, markDirty bool)

type GridIndexer

type GridIndexer interface {
	Index
	CalculateGridIndex(vec Vec) int
}

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)

func (LinearCodeCodec) Encode

func (lc LinearCodeCodec) Encode(x, y uint32) (int, error)

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)

func (MortonCodeCodec) Encode

func (mc MortonCodeCodec) Encode(x, y uint32) (int, error)

type Option

type Option func(*bucketGrid) error

func WithBucketCapacity

func WithBucketCapacity(bucketCapacity int) Option

func WithBucketCapacityFactor

func WithBucketCapacityFactor(capacityFactor float64) Option

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".

type Vec

type Vec = geom.Vec

Index is a spatial index over a 2D power-of-two grid: boxes in, range queries out.

Jump to

Keyboard shortcuts

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