tiramisu/spatial
Spatial partitioning data structures for efficient spatial queries.
Provides octree and AABB (Axis-Aligned Bounding Box) for:
- Finding objects within a region (10-100x faster than linear search)
- Finding nearby objects
- Frustum culling optimization
- Broad-phase collision detection
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
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_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 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 coverscapacity
: 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)