use crate::math::Aabb;
pub const EPSILON: f64 = 1.0e-7;
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Points<'a> {
Fractional(u32),
Explicit(&'a [f64]),
Shifted(&'a [f64], f64),
}
impl Points<'_> {
#[inline]
#[must_use]
pub fn cells(&self) -> usize {
match *self {
Points::Fractional(n) => n as usize,
Points::Explicit(p) | Points::Shifted(p, _) => p.len().saturating_sub(1),
}
}
#[inline]
#[must_use]
pub fn get(&self, i: usize) -> f64 {
match *self {
Points::Fractional(n) => i as f64 / f64::from(n),
Points::Explicit(p) => p[i],
Points::Shifted(p, k) => p[i] + k,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct VoxelShape<'a> {
pub axes: [Points<'a>; 3],
pub cells: &'a [u64],
}
pub type Axis = usize;
#[inline]
fn lo(b: &Aabb, axis: Axis) -> f64 {
[b.min_x, b.min_y, b.min_z][axis]
}
#[inline]
fn hi(b: &Aabb, axis: Axis) -> f64 {
[b.max_x, b.max_y, b.max_z][axis]
}
impl<'a> VoxelShape<'a> {
pub const FULL_CUBE: VoxelShape<'static> = VoxelShape {
axes: [Points::Fractional(1); 3],
cells: &[1],
};
#[inline]
fn simple(&self) -> bool {
self.axes.iter().all(|p| matches!(p, Points::Fractional(_)))
}
#[inline]
fn size(&self, axis: Axis) -> usize {
self.axes[axis].cells()
}
#[inline]
#[must_use]
pub fn filled(&self, x: isize, y: isize, z: isize) -> bool {
let (nx, ny, nz) = (self.size(0), self.size(1), self.size(2));
if x < 0 || y < 0 || z < 0 {
return false;
}
let (x, y, z) = (x as usize, y as usize, z as usize);
if x >= nx || y >= ny || z >= nz {
return false;
}
let i = (x * ny + y) * nz + z;
self.cells
.get(i / 64)
.is_some_and(|w| w >> (i % 64) & 1 != 0)
}
#[must_use]
pub fn is_empty(&self) -> bool {
let n = self.size(0) * self.size(1) * self.size(2);
let full = n / 64;
self.cells[..full.min(self.cells.len())]
.iter()
.all(|&w| w == 0)
&& (n.is_multiple_of(64)
|| self
.cells
.get(full)
.is_none_or(|&w| w & ((1u64 << (n % 64)) - 1) == 0))
}
#[inline]
fn point(&self, axis: Axis, i: usize, offset: Option<[f64; 3]>) -> f64 {
let p = self.axes[axis].get(i);
match offset {
Some(o) => p + o[axis],
None => p,
}
}
fn coord_index(&self, axis: Axis, coord: f64, offset: Option<[f64; 3]>) -> isize {
let n = self.size(axis);
if offset.is_none() && self.simple() {
let v = crate::math::floor(coord * n as f64);
return (v as isize).clamp(-1, n as isize);
}
let (mut start, mut len) = (0usize, n + 1);
while len > 0 {
let half = len / 2;
let k = start + half;
let above = k > n || coord < self.point(axis, k, offset);
if above {
len = half;
} else {
start = k + 1;
len -= half + 1;
}
}
start as isize - 1
}
#[must_use]
pub fn max_distance(&self, axis: Axis, b: &Aabb, max: f64, offset: Option<[f64; 3]>) -> f64 {
if self.is_empty() {
return max;
}
if max.abs() < EPSILON {
return 0.0;
}
let (a2, a3) = ((axis + 1) % 3, (axis + 2) % 3);
let (bmax, bmin) = (hi(b, axis), lo(b, axis));
let i = self.coord_index(axis, bmin + EPSILON, offset);
let j = self.coord_index(axis, bmax - EPSILON, offset);
let k = self.coord_index(a2, lo(b, a2) + EPSILON, offset).max(0);
let l = (self.coord_index(a2, hi(b, a2) - EPSILON, offset) + 1).min(self.size(a2) as isize);
let m = self.coord_index(a3, lo(b, a3) + EPSILON, offset).max(0);
let n = (self.coord_index(a3, hi(b, a3) - EPSILON, offset) + 1).min(self.size(a3) as isize);
let o = self.size(axis) as isize;
let cell = |p: isize, q: isize, r: isize| {
let mut c = [0isize; 3];
c[axis] = p;
c[a2] = q;
c[a3] = r;
self.filled(c[0], c[1], c[2])
};
let layer = |p: isize| (k..l).any(|q| (m..n).any(|r| cell(p, q, r)));
if max > 0.0 {
let Some(p) = (j + 1..o).find(|&p| layer(p)) else {
return max;
};
let gap = self.point(axis, p as usize, offset) - bmax;
if gap >= -EPSILON { max.min(gap) } else { max }
} else if max < 0.0 {
let Some(s) = (0..i).rev().find(|&s| layer(s)) else {
return max;
};
let gap = self.point(axis, (s + 1) as usize, offset) - bmin;
if gap <= EPSILON { max.max(gap) } else { max }
} else {
max
}
}
#[must_use]
pub fn intersects(&self, b: &Aabb, offset: Option<[f64; 3]>) -> bool {
if (0..3).any(|axis| hi(b, axis) - lo(b, axis) < EPSILON) || self.is_empty() {
return false;
}
let merged = |axis: Axis| {
let k = self.size(axis) + 1;
let first = |i: usize| self.point(axis, i, offset);
merge_points(k, first, [lo(b, axis), hi(b, axis)])
};
let (mx, my, mz) = (merged(0), merged(1), merged(2));
mx.cells().iter().filter(|c| c.1 == 0).any(|&(x1, _)| {
my.cells().iter().filter(|c| c.1 == 0).any(|&(y1, _)| {
mz.cells()
.iter()
.filter(|c| c.1 == 0)
.any(|&(z1, _)| self.filled(x1, y1, z1))
})
})
}
}
struct Merged {
buf: [(isize, isize); MAX_MERGED],
len: usize,
}
const MAX_MERGED: usize = 64;
impl Merged {
fn cells(&self) -> &[(isize, isize)] {
&self.buf[..self.len]
}
}
fn merge_points(k: usize, first: impl Fn(usize) -> f64, second: [f64; 2]) -> Merged {
let l = second.len();
let (mut i, mut j) = (0usize, 0usize);
let mut d = f64::NAN;
let mut out = Merged {
buf: [(0, 0); MAX_MERGED],
len: 0,
};
loop {
let bl = i < k;
let bl2 = j < l;
if !bl && !bl2 {
break;
}
let take_first = bl && (!bl2 || first(i) < second[j] + EPSILON);
let e = if take_first {
i += 1;
first(i - 1)
} else {
j += 1;
second[j - 1]
};
if (i != 0 && bl || take_first) && (j != 0 && bl2 || !take_first) {
#[allow(clippy::neg_cmp_op_on_partial_ord)]
let new_value = !(d >= e - EPSILON);
let entry = (i as isize - 1, j as isize - 1);
if new_value {
if out.len < MAX_MERGED {
out.buf[out.len] = entry;
}
out.len += 1;
d = e;
} else if out.len > 0 && out.len <= MAX_MERGED {
out.buf[out.len - 1] = entry;
}
}
}
out.len = out.len.saturating_sub(1).min(MAX_MERGED);
out
}
#[cfg(test)]
mod tests {
use super::*;
const SLAB: VoxelShape<'static> = VoxelShape {
axes: [
Points::Fractional(1),
Points::Fractional(2),
Points::Fractional(1),
],
cells: &[0b01],
};
#[test]
fn slab_stops_falling_box() {
let b = Aabb::new(0.2, 1.0, 0.2, 0.8, 2.8, 0.8);
assert_eq!(SLAB.max_distance(1, &b, -1.0, None), -0.5);
assert_eq!(SLAB.max_distance(1, &b, -0.3, None), -0.3);
let off = Some([10.0, 64.0, 10.0]);
let b = Aabb::new(10.2, 65.0, 10.2, 10.8, 66.8, 10.8);
assert_eq!(SLAB.max_distance(1, &b, -1.0, off), -0.5);
assert!(SLAB.intersects(&Aabb::new(10.0, 64.4, 10.0, 11.0, 65.0, 11.0), off));
assert!(!SLAB.intersects(&Aabb::new(10.0, 64.5, 10.0, 11.0, 65.0, 11.0), off));
}
#[test]
fn full_cube_side() {
let b = Aabb::new(-0.7, 0.0, 0.2, -0.1, 1.8, 0.8);
assert_eq!(VoxelShape::FULL_CUBE.max_distance(0, &b, 0.5, None), 0.1);
}
}