use glam::{Vec2, Vec3};
#[inline(always)]
pub fn sdf_cut_hollow_sphere(p: Vec3, radius: f32, cut_height: f32, thickness: f32) -> f32 {
let w = radius
.mul_add(radius, -(cut_height * cut_height))
.max(0.0)
.sqrt();
let q = Vec2::new(p.x.hypot(p.z), p.y);
if cut_height * q.x < w * q.y {
(q - Vec2::new(w, cut_height)).length() - thickness
} else {
(q.length() - radius).abs() - thickness
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cut_hollow_sphere_shell() {
let d = sdf_cut_hollow_sphere(Vec3::new(1.0, 0.0, 0.0), 1.0, 0.5, 0.05);
assert!(d.abs() < 0.06, "On surface should be ~thickness, got {}", d);
}
#[test]
fn test_cut_hollow_sphere_origin() {
let d = sdf_cut_hollow_sphere(Vec3::ZERO, 1.0, 0.5, 0.05);
assert!(d > 0.0, "Origin should be outside (hollow), got {}", d);
}
#[test]
fn test_cut_hollow_sphere_far() {
let d = sdf_cut_hollow_sphere(Vec3::new(5.0, 0.0, 0.0), 1.0, 0.5, 0.05);
assert!(d > 0.0, "Far point should be outside, got {}", d);
}
}