euv-core 0.56.0

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
use super::*;

/// Axis-aligned 2D bounding rectangle `(min, max)` used by 2D spatial indices and samplers.
#[derive(Clone, Copy, Data, Debug, Default, PartialEq)]
pub struct Bounds2D {
    /// Minimum corner `(min_x, min_y)`.
    #[get(type(copy))]
    pub(crate) min: Vec2,
    /// Maximum corner `(max_x, max_y)`.
    #[get(type(copy))]
    pub(crate) max: Vec2,
}

/// Stored 2D spatial entry associating an entity identifier with its position.
#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
pub struct SpatialItem2D {
    /// Caller-assigned unique entity identifier.
    #[get(type(copy))]
    pub(crate) id: u64,
    /// World-space 2D position.
    #[get(type(copy))]
    pub(crate) position: Vec2,
}

/// Stored 3D spatial entry associating an entity identifier with its bounding box.
#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
pub struct SpatialItem3D {
    /// Caller-assigned unique entity identifier.
    #[get(type(copy))]
    pub(crate) id: u64,
    /// World-space 3D axis-aligned bounding box.
    #[get(type(copy))]
    pub(crate) bounds: AxisAlignedBox3D,
}

/// Subdivision parameters for `Quadtree2D`.
#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
pub struct QuadtreeConfig {
    /// Maximum items held in a leaf before subdividing into four quadrants.
    #[get(type(copy))]
    pub(crate) max_leaf_items: usize,
    /// Maximum subdivision depth guarding against coincident points.
    #[get(type(copy))]
    pub(crate) max_depth: usize,
}

/// Arena-allocated node inside `Quadtree2D`.
#[derive(Clone, Debug)]
pub(crate) struct QuadtreeNode2D {
    /// Spatial region owned by this node.
    pub(crate) bounds: Bounds2D,
    /// Subdivision depth (`0` at root).
    pub(crate) depth: usize,
    /// Child node indices `[SW, SE, NW, NE]`, or `SPATIAL_EMPTY_SLOT` when a leaf.
    pub(crate) children: [usize; 4],
    /// Items stored directly in this node.
    pub(crate) items: Vec<SpatialItem2D>,
}

/// Reusable scratch buffers for `Quadtree2D` best-first k-nearest-neighbor queries.
#[derive(Clone, Debug, Default)]
pub(crate) struct QuadtreeScratch {
    /// Min-heap of `(min_dist_sq, node_index)` for best-first branch-and-bound traversal.
    pub(crate) node_heap: Vec<(f32, usize)>,
    /// Candidate `(dist_sq, id)` results bounded to size `k`.
    pub(crate) best_items: Vec<(f32, u64)>,
}

/// Adaptive 2D quadtree supporting dynamic insert/remove/update, rect, radius, and k-NN queries.
#[derive(Clone, Data, Debug)]
pub struct Quadtree2D {
    /// Root spatial bounds.
    #[get(type(copy))]
    pub(crate) root_bounds: Bounds2D,
    /// Subdivision configuration.
    #[get(type(copy))]
    pub(crate) config: QuadtreeConfig,
    /// Arena of quadtree nodes (index `0` is root).
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) nodes: Vec<QuadtreeNode2D>,
    /// Lookup map from item identifier to its current world position for O(log N) removal/update.
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) positions: HashMap<u64, Vec2>,
    /// Reusable k-NN query scratch buffers.
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) scratch: QuadtreeScratch,
}

/// Subdivision parameters for `Octree3D`.
#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
pub struct OctreeConfig {
    /// Maximum items held in a leaf before subdividing into eight octants.
    #[get(type(copy))]
    pub(crate) max_leaf_items: usize,
    /// Maximum subdivision depth guarding against coincident volumes.
    #[get(type(copy))]
    pub(crate) max_depth: usize,
}

/// Arena-allocated node inside `Octree3D`.
#[derive(Clone, Debug)]
pub(crate) struct OctreeNode3D {
    /// Spatial volume owned by this node.
    pub(crate) bounds: AxisAlignedBox3D,
    /// Subdivision depth (`0` at root).
    pub(crate) depth: usize,
    /// Eight child octant indices or `SPATIAL_EMPTY_SLOT` when a leaf.
    pub(crate) children: [usize; 8],
    /// Items residing in this node (either leaf items or items straddling child split planes).
    pub(crate) items: Vec<SpatialItem3D>,
}

/// Adaptive 3D octree supporting dynamic insert/remove/update, AABB, sphere, and ray queries.
#[derive(Clone, Data, Debug)]
pub struct Octree3D {
    /// Root spatial volume.
    #[get(type(copy))]
    pub(crate) root_bounds: AxisAlignedBox3D,
    /// Subdivision configuration.
    #[get(type(copy))]
    pub(crate) config: OctreeConfig,
    /// Arena of octree nodes (index `0` is root).
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) nodes: Vec<OctreeNode3D>,
    /// Lookup map from item identifier to its current bounding box.
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) item_bounds: HashMap<u64, AxisAlignedBox3D>,
}

/// Open-addressed cell bucket inside `SpatialHashGrid2D`.
#[derive(Clone, Copy, Debug)]
pub(crate) struct SpatialHashBucket2D {
    /// Integer cell X coordinate.
    pub(crate) cx: i32,
    /// Integer cell Y coordinate.
    pub(crate) cy: i32,
    /// Head index into `SpatialHashGrid2D::entries`, or `SPATIAL_EMPTY_SLOT` when unoccupied.
    pub(crate) head: usize,
}

/// Linked-list node inside `SpatialHashGrid2D`.
#[derive(Clone, Copy, Debug)]
pub(crate) struct SpatialHashEntry2D {
    /// Stored item identifier.
    pub(crate) id: u64,
    /// Item bounding rectangle (point entries store `min == max`).
    pub(crate) bounds: Bounds2D,
    /// Next entry index in the same cell, or `SPATIAL_EMPTY_SLOT` at tail.
    pub(crate) next: usize,
}

/// O(1) uniform-cell 2D spatial hash grid with reusable scratch buffers and zero steady-state allocations.
#[derive(Clone, Data, Debug)]
pub struct SpatialHashGrid2D {
    /// Uniform cell side length.
    #[get(type(copy))]
    pub(crate) cell_size: f32,
    /// Reciprocal `1.0 / cell_size`.
    #[get(type(copy))]
    pub(crate) inv_cell_size: f32,
    /// Number of occupied buckets in the active frame.
    #[get(type(copy))]
    pub(crate) occupied_buckets: usize,
    /// Open-addressed power-of-two bucket table.
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) buckets: Vec<SpatialHashBucket2D>,
    /// Indices of buckets occupied since the last `clear`.
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) touched_buckets: Vec<usize>,
    /// Contiguous intrusive linked-list entry pool.
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) entries: Vec<SpatialHashEntry2D>,
    /// Reusable deduplicated query result scratch buffer.
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) query_scratch: Vec<u64>,
}

/// Open-addressed cell bucket inside `SpatialHashGrid3D`.
#[derive(Clone, Copy, Debug)]
pub(crate) struct SpatialHashBucket3D {
    /// Integer cell X coordinate.
    pub(crate) cx: i32,
    /// Integer cell Y coordinate.
    pub(crate) cy: i32,
    /// Integer cell Z coordinate.
    pub(crate) cz: i32,
    /// Head index into `SpatialHashGrid3D::entries`, or `SPATIAL_EMPTY_SLOT` when unoccupied.
    pub(crate) head: usize,
}

/// Linked-list node inside `SpatialHashGrid3D`.
#[derive(Clone, Copy, Debug)]
pub(crate) struct SpatialHashEntry3D {
    /// Stored item identifier.
    pub(crate) id: u64,
    /// Item 3D bounding box (point entries store `min == max`).
    pub(crate) bounds: AxisAlignedBox3D,
    /// Next entry index in the same cell, or `SPATIAL_EMPTY_SLOT` at tail.
    pub(crate) next: usize,
}

/// O(1) uniform-cell 3D spatial hash grid with reusable scratch buffers and zero steady-state allocations.
#[derive(Clone, Data, Debug)]
pub struct SpatialHashGrid3D {
    /// Uniform cell side length.
    #[get(type(copy))]
    pub(crate) cell_size: f32,
    /// Reciprocal `1.0 / cell_size`.
    #[get(type(copy))]
    pub(crate) inv_cell_size: f32,
    /// Number of occupied buckets in the active frame.
    #[get(type(copy))]
    pub(crate) occupied_buckets: usize,
    /// Open-addressed power-of-two bucket table.
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) buckets: Vec<SpatialHashBucket3D>,
    /// Indices of buckets occupied since the last `clear`.
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) touched_buckets: Vec<usize>,
    /// Contiguous intrusive linked-list entry pool.
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) entries: Vec<SpatialHashEntry3D>,
    /// Reusable deduplicated query result scratch buffer.
    #[get(pub(crate))]
    #[get_mut(pub(crate))]
    #[set(pub(crate))]
    pub(crate) query_scratch: Vec<u64>,
}