tiramisu/spatial

Spatial partitioning data structures for efficient spatial queries.

Provides octree and AABB (Axis-Aligned Bounding Box) for:

Quick Example

import tiramisu/spatial

// Create octree for world bounds
let world_bounds = spatial.aabb(
  min: vec3.Vec3(-100.0, -100.0, -100.0),
  max: vec3.Vec3(100.0, 100.0, 100.0),
)
let tree = spatial.new_octree(bounds: world_bounds, capacity: 8)

// Insert enemies
let tree = spatial.insert(tree, enemy_pos, enemy_id)

// Find enemies near player
let nearby = spatial.query_radius(tree, player_pos, radius: 10.0)

Types

Axis-Aligned Bounding Box

pub type AABB {
  AABB(min: vec3.Vec3(Float), max: vec3.Vec3(Float))
}

Constructors

Octree node for spatial partitioning Divides 3D space into 8 octants recursively

pub opaque type Octree(a)

Values

pub fn aabb(
  min min: vec3.Vec3(Float),
  max max: vec3.Vec3(Float),
) -> AABB

Create an AABB from min and max points

pub fn aabb_center(bounds: AABB) -> vec3.Vec3(Float)

Get the center of an AABB

pub fn aabb_contains_point(
  bounds: AABB,
  point: vec3.Vec3(Float),
) -> Bool

Check if a point is inside an AABB

pub fn aabb_from_center(
  center: vec3.Vec3(Float),
  half_extents: vec3.Vec3(Float),
) -> AABB

Create an AABB from center and half-extents

pub fn aabb_intersects(a: AABB, b: AABB) -> Bool

Check if two AABBs intersect

pub fn aabb_size(bounds: AABB) -> vec3.Vec3(Float)

Get the size (dimensions) of an AABB

pub fn octree_bounds(tree: Octree(a)) -> AABB

Get the bounds of the octree

pub fn octree_count(tree: Octree(a)) -> Int

Count total items in the octree

pub fn octree_insert(
  tree: Octree(a),
  position: vec3.Vec3(Float),
  item: a,
) -> Octree(a)

Insert an item at a position into the octree

pub fn octree_new(bounds: AABB, capacity: Int) -> Octree(a)

Create a new empty octree

Parameters

  • bounds: The spatial region this octree covers
  • capacity: Maximum items per node before subdividing (typically 8-16)

Example

let bounds = aabb(
  min: vec3.Vec3(-100.0, -100.0, -100.0),
  max: vec3.Vec3(100.0, 100.0, 100.0)
)
let tree = octree_new(bounds, capacity: 8)
pub fn octree_query(
  tree: Octree(a),
  query_bounds: AABB,
) -> List(#(vec3.Vec3(Float), a))

Query all items within a bounding box region

pub fn octree_query_all(
  tree: Octree(a),
) -> List(#(vec3.Vec3(Float), a))

Query all items in the octree (useful for iteration)

pub fn octree_query_radius(
  tree: Octree(a),
  center: vec3.Vec3(Float),
  radius: Float,
) -> List(#(vec3.Vec3(Float), a))

Query all items within a radius of a point

Search Document