use std::sync::LazyLock;
pub use ghast_data::Aabb;
const SIN_TABLE_LEN: usize = 65536;
static SIN_TABLE: LazyLock<Box<[f32; SIN_TABLE_LEN]>> = LazyLock::new(|| {
let mut t = Box::new([0.0f32; SIN_TABLE_LEN]);
for (i, v) in t.iter_mut().enumerate() {
*v = (i as f64 * std::f64::consts::PI * 2.0 / SIN_TABLE_LEN as f64).sin() as f32;
}
t
});
#[inline]
#[cfg_attr(not(feature = "v26_1"), allow(dead_code))]
pub(crate) fn sin_table() -> &'static [f32; SIN_TABLE_LEN] {
&SIN_TABLE
}
const SIN_SCALE: f32 = 10430.378;
#[inline]
#[must_use]
pub fn sin(rad: f32) -> f32 {
SIN_TABLE[((rad * SIN_SCALE) as i32 & 0xFFFF) as usize]
}
#[inline]
#[must_use]
pub fn cos(rad: f32) -> f32 {
SIN_TABLE[((rad * SIN_SCALE + 16384.0) as i32 & 0xFFFF) as usize]
}
#[inline]
#[must_use]
pub fn sqrt_f32(v: f32) -> f32 {
f64::from(v).sqrt() as f32
}
#[inline]
#[must_use]
pub fn sqrt_f64_as_f32(v: f64) -> f32 {
v.sqrt() as f32
}
#[inline]
#[must_use]
pub fn floor(v: f64) -> i32 {
let i = v as i32;
if v < f64::from(i) { i - 1 } else { i }
}
#[inline]
#[must_use]
pub fn java_min(a: f64, b: f64) -> f64 {
if a.is_nan() {
return a;
}
if a == 0.0 && b == 0.0 && b.is_sign_negative() {
return b;
}
if a <= b { a } else { b }
}
#[inline]
#[must_use]
pub fn java_max(a: f64, b: f64) -> f64 {
if a.is_nan() {
return a;
}
if a == 0.0 && b == 0.0 && a.is_sign_negative() {
return b;
}
if a >= b { a } else { b }
}
#[inline]
#[must_use]
pub fn sorted_box(x0: f64, y0: f64, z0: f64, x1: f64, y1: f64, z1: f64) -> Aabb {
Aabb::new(
java_min(x0, x1),
java_min(y0, y1),
java_min(z0, z1),
java_max(x0, x1),
java_max(y0, y1),
java_max(z0, z1),
)
}
pub trait BoxOps {
fn offset(&self, dx: f64, dy: f64, dz: f64) -> Aabb;
fn stretch(&self, dx: f64, dy: f64, dz: f64) -> Aabb;
fn grow(&self, x: f64, y: f64, z: f64) -> Aabb;
fn shrink(&self, x: f64, y: f64, z: f64) -> Aabb;
fn intersects(&self, other: &Aabb) -> bool;
fn clip_x(&self, mover: &Aabb, d: f64) -> f64;
fn clip_y(&self, mover: &Aabb, d: f64) -> f64;
fn clip_z(&self, mover: &Aabb, d: f64) -> f64;
}
#[inline]
fn clip_axis(lo: f64, hi: f64, m_lo: f64, m_hi: f64, d: f64) -> f64 {
if d > 0.0 && m_hi <= lo {
let gap = lo - m_hi;
if gap < d { gap } else { d }
} else if d < 0.0 && m_lo >= hi {
let gap = hi - m_lo;
if gap > d { gap } else { d }
} else {
d
}
}
#[inline]
#[allow(clippy::neg_cmp_op_on_partial_ord)]
fn overlaps(a0: f64, a1: f64, b0: f64, b1: f64) -> bool {
!(b1 <= a0) && !(b0 >= a1)
}
impl BoxOps for Aabb {
#[inline]
fn offset(&self, dx: f64, dy: f64, dz: f64) -> Aabb {
sorted_box(
self.min_x + dx,
self.min_y + dy,
self.min_z + dz,
self.max_x + dx,
self.max_y + dy,
self.max_z + dz,
)
}
#[inline]
fn stretch(&self, dx: f64, dy: f64, dz: f64) -> Aabb {
let (mut x0, mut y0, mut z0) = (self.min_x, self.min_y, self.min_z);
let (mut x1, mut y1, mut z1) = (self.max_x, self.max_y, self.max_z);
if dx < 0.0 {
x0 += dx;
} else if dx > 0.0 {
x1 += dx;
}
if dy < 0.0 {
y0 += dy;
} else if dy > 0.0 {
y1 += dy;
}
if dz < 0.0 {
z0 += dz;
} else if dz > 0.0 {
z1 += dz;
}
sorted_box(x0, y0, z0, x1, y1, z1)
}
#[inline]
fn grow(&self, x: f64, y: f64, z: f64) -> Aabb {
sorted_box(
self.min_x - x,
self.min_y - y,
self.min_z - z,
self.max_x + x,
self.max_y + y,
self.max_z + z,
)
}
#[inline]
fn shrink(&self, x: f64, y: f64, z: f64) -> Aabb {
sorted_box(
self.min_x + x,
self.min_y + y,
self.min_z + z,
self.max_x - x,
self.max_y - y,
self.max_z - z,
)
}
#[inline]
fn intersects(&self, o: &Aabb) -> bool {
overlaps(self.min_x, self.max_x, o.min_x, o.max_x)
&& overlaps(self.min_y, self.max_y, o.min_y, o.max_y)
&& overlaps(self.min_z, self.max_z, o.min_z, o.max_z)
}
#[inline]
fn clip_x(&self, m: &Aabb, d: f64) -> f64 {
if overlaps(self.min_y, self.max_y, m.min_y, m.max_y)
&& overlaps(self.min_z, self.max_z, m.min_z, m.max_z)
{
clip_axis(self.min_x, self.max_x, m.min_x, m.max_x, d)
} else {
d
}
}
#[inline]
fn clip_y(&self, m: &Aabb, d: f64) -> f64 {
if overlaps(self.min_x, self.max_x, m.min_x, m.max_x)
&& overlaps(self.min_z, self.max_z, m.min_z, m.max_z)
{
clip_axis(self.min_y, self.max_y, m.min_y, m.max_y, d)
} else {
d
}
}
#[inline]
fn clip_z(&self, m: &Aabb, d: f64) -> f64 {
if overlaps(self.min_x, self.max_x, m.min_x, m.max_x)
&& overlaps(self.min_y, self.max_y, m.min_y, m.max_y)
{
clip_axis(self.min_z, self.max_z, m.min_z, m.max_z, d)
} else {
d
}
}
}
#[cfg_attr(not(any(feature = "v1_8", feature = "v1_12")), allow(dead_code))]
#[inline]
#[must_use]
pub(crate) fn vec_component(v: f64) -> f64 {
if v == 0.0 { 0.0 } else { v }
}
#[cfg_attr(not(any(feature = "v1_8", feature = "v1_12")), allow(dead_code))]
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct Vec3 {
pub x: f64,
pub y: f64,
pub z: f64,
}
#[cfg_attr(not(any(feature = "v1_8", feature = "v1_12")), allow(dead_code))]
impl Vec3 {
pub(crate) const ZERO: Vec3 = Vec3 {
x: 0.0,
y: 0.0,
z: 0.0,
};
#[inline]
pub(crate) fn new(x: f64, y: f64, z: f64) -> Vec3 {
Vec3 {
x: vec_component(x),
y: vec_component(y),
z: vec_component(z),
}
}
#[inline]
pub(crate) fn add(self, x: f64, y: f64, z: f64) -> Vec3 {
Vec3::new(self.x + x, self.y + y, self.z + z)
}
#[inline]
pub(crate) fn length(self) -> f64 {
f64::from(sqrt_f64_as_f32(
self.x * self.x + self.y * self.y + self.z * self.z,
))
}
#[inline]
pub(crate) fn normalize(self) -> Vec3 {
let len = self.length();
if len < 1.0e-4 {
Vec3::ZERO
} else {
Vec3::new(self.x / len, self.y / len, self.z / len)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sin_table_matches_java() {
let mut h: u64 = 1_469_598_103_934_665_603;
for v in SIN_TABLE.iter() {
h = (h ^ (v.to_bits() as i32 as i64 as u64)).wrapping_mul(1_099_511_628_211);
}
assert_eq!(h, 0x3174_5480_87cb_fee1);
}
#[test]
fn floor_matches_java() {
assert_eq!(floor(-0.5), -1);
assert_eq!(floor(-1.0), -1);
assert_eq!(floor(2.9), 2);
assert_eq!(floor(1e300), i32::MAX);
assert_eq!(floor(f64::NAN), 0);
}
#[test]
fn min_max_zero_signs() {
assert!(java_min(0.0, -0.0).is_sign_negative());
assert!(java_min(-0.0, 0.0).is_sign_negative());
assert!(java_max(-0.0, 0.0).is_sign_positive());
assert!(java_max(0.0, -0.0).is_sign_positive());
}
}