use glam::Vec3;
#[inline(always)]
pub fn sdf_diamond_surface(p: Vec3, scale: f32, thickness: f32) -> f32 {
let sp = p * scale;
let (sx, cx) = alice_det_math::sin_cos(sp.x);
let (sy, cy) = alice_det_math::sin_cos(sp.y);
let (sz, cz) = alice_det_math::sin_cos(sp.z);
let d = (cx * cy) * sz + ((cx * sy) * cz + ((sx * sy) * sz + (sx * cy * cz)));
d.abs() / scale - thickness
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_diamond_surface_origin() {
let d = sdf_diamond_surface(Vec3::ZERO, 1.0, 0.1);
assert!(
(d + 0.1).abs() < 0.01,
"Origin on surface minus thickness, got {}",
d
);
}
#[test]
fn test_diamond_surface_periodicity() {
let tau = 2.0 * std::f32::consts::PI;
let d1 = sdf_diamond_surface(Vec3::new(0.5, 0.3, 0.7), 1.0, 0.1);
let d2 = sdf_diamond_surface(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_diamond_surface_finite() {
let d = sdf_diamond_surface(Vec3::new(1.0, 2.0, 3.0), 2.0, 0.05);
assert!(d.is_finite());
}
}