use super::*;
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
}
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
}
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()
}
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()
}
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()
}
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
}
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
}
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;
}
}
}
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)
}