use glam::{Vec2, Vec3};
#[inline(always)]
pub fn sdf_rounded_x(p: Vec3, width: f32, round_radius: f32, half_height: f32) -> f32 {
let q = Vec2::new(p.x.abs(), p.z.abs());
let s = (q.x + q.y).min(width) * 0.5;
let d_2d = (q - Vec2::splat(s)).length() - round_radius;
let d_y = p.y.abs() - half_height;
let w = Vec2::new(d_2d.max(0.0), d_y.max(0.0));
d_2d.max(d_y).min(0.0) + w.length()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_rounded_x_center_inside() {
let d = sdf_rounded_x(Vec3::ZERO, 1.0, 0.2, 0.5);
assert!(d < 0.0, "Center should be inside, got {}", d);
}
#[test]
fn test_rounded_x_far_outside() {
let d = sdf_rounded_x(Vec3::new(5.0, 0.0, 0.0), 1.0, 0.2, 0.5);
assert!(d > 0.0, "Far point should be outside, got {}", d);
}
#[test]
fn test_rounded_x_symmetry_xz() {
let d1 = sdf_rounded_x(Vec3::new(0.3, 0.1, 0.2), 1.0, 0.2, 0.5);
let d2 = sdf_rounded_x(Vec3::new(-0.3, 0.1, -0.2), 1.0, 0.2, 0.5);
assert!((d1 - d2).abs() < 0.001, "Should be symmetric in XZ");
}
#[test]
fn test_rounded_x_symmetry_y() {
let d1 = sdf_rounded_x(Vec3::new(0.3, 0.2, 0.1), 1.0, 0.2, 0.5);
let d2 = sdf_rounded_x(Vec3::new(0.3, -0.2, 0.1), 1.0, 0.2, 0.5);
assert!((d1 - d2).abs() < 0.001, "Should be symmetric in Y");
}
}