Skip to main content

oxiphysics_geometry/csg/
csgsmoothintersection_traits.rs

1//! # CsgSmoothIntersection - Trait Implementations
2//!
3//! This module contains trait implementations for `CsgSmoothIntersection`.
4//!
5//! ## Implemented Traits
6//!
7//! - `ImplicitSurface`
8//!
9//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)
10
11use super::functions::ImplicitSurface;
12use super::functions::{clamp, mix, normalize};
13use super::types::CsgSmoothIntersection;
14
15impl ImplicitSurface for CsgSmoothIntersection {
16    fn sdf(&self, p: [f64; 3]) -> f64 {
17        let da = self.a.sdf(p);
18        let db = self.b.sdf(p);
19        let h = clamp(0.5 - 0.5 * (db - da) / self.k, 0.0, 1.0);
20        mix(db, da, h) + self.k * h * (1.0 - h)
21    }
22    fn gradient(&self, p: [f64; 3]) -> [f64; 3] {
23        const EPS: f64 = 1e-5;
24        let dx = self.sdf([p[0] + EPS, p[1], p[2]]) - self.sdf([p[0] - EPS, p[1], p[2]]);
25        let dy = self.sdf([p[0], p[1] + EPS, p[2]]) - self.sdf([p[0], p[1] - EPS, p[2]]);
26        let dz = self.sdf([p[0], p[1], p[2] + EPS]) - self.sdf([p[0], p[1], p[2] - EPS]);
27        normalize([dx, dy, dz])
28    }
29}