use glam::Vec3;
#[inline(always)]
pub fn sdf_iwp(p: Vec3, scale: f32, thickness: f32) -> f32 {
let sp = p * scale;
let cx = alice_det_math::cos(sp.x);
let cy = alice_det_math::cos(sp.y);
let cz = alice_det_math::cos(sp.z);
let c2x = (2.0 * cx) * cx + -1.0;
let c2y = (2.0 * cy) * cy + -1.0;
let c2z = (2.0 * cz) * cz + -1.0;
let d = 2.0f32 * (cz * cx + (cx * cy + (cy * cz))) + -(c2x + c2y + c2z);
d.abs() / scale - thickness
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_iwp_origin() {
let d = sdf_iwp(Vec3::ZERO, 1.0, 0.1);
assert!((d - 2.9).abs() < 0.01, "Expected ~2.9, got {}", d);
}
#[test]
fn test_iwp_periodicity() {
let tau = 2.0 * std::f32::consts::PI;
let d1 = sdf_iwp(Vec3::new(0.5, 0.3, 0.7), 1.0, 0.1);
let d2 = sdf_iwp(Vec3::new(0.5 + tau, 0.3 + tau, 0.7 + tau), 1.0, 0.1);
assert!((d1 - d2).abs() < 0.01, "Should be periodic");
}
#[test]
fn test_iwp_finite() {
let d = sdf_iwp(Vec3::new(1.0, 2.0, 3.0), 2.0, 0.05);
assert!(d.is_finite());
}
}