Skip to main content

oxiphysics_geometry/csg/
sdfroundedbox_traits.rs

1//! # SdfRoundedBox - Trait Implementations
2//!
3//! This module contains trait implementations for `SdfRoundedBox`.
4//!
5//! ## Implemented Traits
6//!
7//! - `ImplicitSurface`
8//!
9//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)
10
11use super::functions::ImplicitSurface;
12#[allow(unused_imports)]
13use super::functions::*;
14use super::functions::{length, normalize};
15use super::types::SdfRoundedBox;
16
17impl ImplicitSurface for SdfRoundedBox {
18    fn sdf(&self, p: [f64; 3]) -> f64 {
19        let d = [
20            (p[0] - self.center[0]).abs() - self.half_extents[0],
21            (p[1] - self.center[1]).abs() - self.half_extents[1],
22            (p[2] - self.center[2]).abs() - self.half_extents[2],
23        ];
24        let exterior = length([d[0].max(0.0), d[1].max(0.0), d[2].max(0.0)]);
25        let interior = d[0].max(d[1]).max(d[2]).min(0.0);
26        exterior + interior - self.radius
27    }
28    fn gradient(&self, p: [f64; 3]) -> [f64; 3] {
29        const EPS: f64 = 1e-5;
30        let gx = self.sdf([p[0] + EPS, p[1], p[2]]) - self.sdf([p[0] - EPS, p[1], p[2]]);
31        let gy = self.sdf([p[0], p[1] + EPS, p[2]]) - self.sdf([p[0], p[1] - EPS, p[2]]);
32        let gz = self.sdf([p[0], p[1], p[2] + EPS]) - self.sdf([p[0], p[1], p[2] - EPS]);
33        normalize([gx, gy, gz])
34    }
35}