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::*;

/// Hashes integer 2D cell coordinates into a well-distributed slot hash.
///
/// # Arguments
///
/// - `i32` - Cell X coordinate.
/// - `i32` - Cell Y coordinate.
///
/// # Returns
///
/// - `usize` - Mixed hash index.
pub(crate) fn spatial_hash_2d(cx: i32, cy: i32) -> usize {
    let hx: u64 = (cx as i64 as u64).wrapping_mul(0x9E37_79B9_7F4A_7C15);
    let hy: u64 = (cy as i64 as u64).wrapping_mul(0xBF58_476D_1CE4_E5B9);
    let mixed: u64 = hx ^ hy.rotate_left(27);
    (mixed ^ (mixed >> 31)) as usize
}

/// Hashes integer 3D cell coordinates into a well-distributed slot hash.
///
/// # Arguments
///
/// - `i32` - Cell X coordinate.
/// - `i32` - Cell Y coordinate.
/// - `i32` - Cell Z coordinate.
///
/// # Returns
///
/// - `usize` - Mixed hash index.
pub(crate) fn spatial_hash_3d(cx: i32, cy: i32, cz: i32) -> usize {
    let hx: u64 = (cx as i64 as u64).wrapping_mul(0x9E37_79B9_7F4A_7C15);
    let hy: u64 = (cy as i64 as u64).wrapping_mul(0xBF58_476D_1CE4_E5B9);
    let hz: u64 = (cz as i64 as u64).wrapping_mul(0x94D0_49BB_1331_11EB);
    let mixed: u64 = hx ^ hy.rotate_left(21) ^ hz.rotate_left(43);
    (mixed ^ (mixed >> 31)) as usize
}

/// Tests whether two 3D axis-aligned bounding boxes overlap inclusively.
///
/// # Arguments
///
/// - `AxisAlignedBox3D` - First bounding box.
/// - `AxisAlignedBox3D` - Second bounding box.
///
/// # Returns
///
/// - `bool` - `true` when the boxes share at least one point.
pub(crate) fn aabb3d_intersects(lhs: AxisAlignedBox3D, rhs: AxisAlignedBox3D) -> bool {
    let lmin: Vec3 = lhs.get_min();
    let lmax: Vec3 = lhs.get_max();
    let rmin: Vec3 = rhs.get_min();
    let rmax: Vec3 = rhs.get_max();
    lmin.get_x() <= rmax.get_x()
        && lmax.get_x() >= rmin.get_x()
        && lmin.get_y() <= rmax.get_y()
        && lmax.get_y() >= rmin.get_y()
        && lmin.get_z() <= rmax.get_z()
        && lmax.get_z() >= rmin.get_z()
}

/// Tests whether `outer` completely encloses `inner` in 3D space.
///
/// # Arguments
///
/// - `AxisAlignedBox3D` - Enclosing box.
/// - `AxisAlignedBox3D` - Inner candidate box.
///
/// # Returns
///
/// - `bool` - `true` when `inner` lies completely within `outer`.
pub(crate) fn aabb3d_contains_box(outer: AxisAlignedBox3D, inner: AxisAlignedBox3D) -> bool {
    let omin: Vec3 = outer.get_min();
    let omax: Vec3 = outer.get_max();
    let imin: Vec3 = inner.get_min();
    let imax: Vec3 = inner.get_max();
    imin.get_x() >= omin.get_x()
        && imax.get_x() <= omax.get_x()
        && imin.get_y() >= omin.get_y()
        && imax.get_y() <= omax.get_y()
        && imin.get_z() >= omin.get_z()
        && imax.get_z() <= omax.get_z()
}

/// Computes squared minimum distance from `point` to the closest point on `bounds`.
///
/// # Arguments
///
/// - `AxisAlignedBox3D` - 3D bounding box.
/// - `Vec3` - Query point.
///
/// # Returns
///
/// - `f32` - Zero when inside the box, otherwise squared Euclidean distance to the nearest face/corner.
pub(crate) fn aabb3d_min_dist_sq(bounds: AxisAlignedBox3D, point: Vec3) -> f32 {
    let bmin: Vec3 = bounds.get_min();
    let bmax: Vec3 = bounds.get_max();
    let clamped: Vec3 = Vec3::new(
        point.get_x().clamp(bmin.get_x(), bmax.get_x()),
        point.get_y().clamp(bmin.get_y(), bmax.get_y()),
        point.get_z().clamp(bmin.get_z(), bmax.get_z()),
    );
    point.sub(clamped).length_sq()
}

/// Tests whether `bounds` intersects a sphere defined by `center` and `radius`.
///
/// # Arguments
///
/// - `AxisAlignedBox3D` - 3D bounding box.
/// - `Vec3` - Sphere center.
/// - `f32` - Sphere radius.
///
/// # Returns
///
/// - `bool` - `true` when the box overlaps the closed sphere.
pub(crate) fn aabb3d_intersects_sphere(
    bounds: AxisAlignedBox3D,
    center: Vec3,
    radius: f32,
) -> bool {
    if !radius.is_finite() || radius < 0.0 {
        return false;
    }
    aabb3d_min_dist_sq(bounds, center) <= radius * radius
}

/// Computes squared Euclidean distance between two 2D points.
///
/// # Arguments
///
/// - `Vec2` - First 2D point.
/// - `Vec2` - Second 2D point.
///
/// # Returns
///
/// - `f32` - Squared distance.
pub(crate) fn spatial_dist_sq_2d(lhs: Vec2, rhs: Vec2) -> f32 {
    let dx: f32 = lhs.get_x() - rhs.get_x();
    let dy: f32 = lhs.get_y() - rhs.get_y();
    dx * dx + dy * dy
}

/// Pushes `(dist_sq, node_idx)` onto a binary min-heap for best-first tree traversal.
///
/// # Arguments
///
/// - `&mut Vec<(f32, usize)>` - Reusable min-heap buffer.
/// - `f32` - Lower-bound squared distance key.
/// - `usize` - Node index.
pub(crate) fn spatial_heap_push(heap: &mut Vec<(f32, usize)>, dist_sq: f32, node_idx: usize) {
    let mut pos: usize = heap.len();
    heap.push((dist_sq, node_idx));
    while pos > 0 {
        let parent: usize = (pos - 1) >> 1;
        if heap[pos].0 < heap[parent].0 {
            heap.swap(pos, parent);
            pos = parent;
        } else {
            break;
        }
    }
}

/// Pops the closest `(dist_sq, node_idx)` pair from a binary min-heap.
///
/// # Arguments
///
/// - `&mut Vec<(f32, usize)>` - Reusable min-heap buffer.
///
/// # Returns
///
/// - `Option<(f32, usize)>` - Smallest-distance entry if non-empty.
pub(crate) fn spatial_heap_pop(heap: &mut Vec<(f32, usize)>) -> Option<(f32, usize)> {
    if heap.is_empty() {
        return None;
    }
    let top: (f32, usize) = heap.swap_remove(0);
    let len: usize = heap.len();
    let mut pos: usize = 0;
    loop {
        let left: usize = (pos << 1) + 1;
        if left >= len {
            break;
        }
        let right: usize = left + 1;
        let smallest: usize = if right < len && heap[right].0 < heap[left].0 {
            right
        } else {
            left
        };
        if heap[smallest].0 < heap[pos].0 {
            heap.swap(pos, smallest);
            pos = smallest;
        } else {
            break;
        }
    }
    Some(top)
}