alice-sdf 6.0.0

ALICE-SDF: 3D/Spatial Data Specialist — Store laws, not polygons. GLSL/WGSL/HLSL transpile, SVO, GPU marching cubes, texture-fit, procedural CSG. Optional bridges to alice-physics / alice-codec / libasp / alice-cache behind features.
Documentation
//! Engrave operation for SDFs (Deep Fried Edition)
//!
//! Based on hg_sdf's fOpEngrave. Engraves shape b into shape a.
//!
//! Author: Moroya Sakamoto

use crate::compiled::real::Real;

/// Engrave operation on two SDFs
///
/// Engraves shape b into the surface of shape a.
/// Formula: `max(a, (a + r - abs(b)) * SQRT_0_5)`
/// - `r`: engrave depth
#[inline(always)]
pub fn sdf_engrave(a: f32, b: f32, r: f32) -> f32 {
    sdf_engrave_r::<f32>(a, b, r)
}

/// Engrave (generic over [`Real`]).
#[inline(always)]
pub fn sdf_engrave_r<R: Real>(a: R, b: R, r: f32) -> R {
    a.max((a + R::splat(r) - b.abs()) * R::splat(std::f32::consts::FRAC_1_SQRT_2))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_engrave_far_outside() {
        // Far outside a: engrave has no effect
        let d = sdf_engrave(5.0, 0.5, 0.1);
        assert!(
            (d - 5.0).abs() < 0.01,
            "Far outside should be ~a, got {}",
            d
        );
    }

    #[test]
    fn test_engrave_finite() {
        let d = sdf_engrave(0.1, -0.2, 0.3);
        assert!(d.is_finite());
    }

    #[test]
    fn test_engrave_preserves_outside() {
        // When a is large positive, result should be a
        let d = sdf_engrave(10.0, 0.0, 0.1);
        assert!((d - 10.0).abs() < 0.01);
    }
}