Skip to main content

oxiphysics_geometry/csg/
sdfcappedcylinder_traits.rs

1//! # SdfCappedCylinder - Trait Implementations
2//!
3//! This module contains trait implementations for `SdfCappedCylinder`.
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::normalize;
13use super::types::SdfCappedCylinder;
14
15impl ImplicitSurface for SdfCappedCylinder {
16    fn sdf(&self, p: [f64; 3]) -> f64 {
17        let dx = p[0] - self.center[0];
18        let dy = p[1] - self.center[1];
19        let dz = p[2] - self.center[2];
20        let r_dist = (dx * dx + dz * dz).sqrt() - self.radius;
21        let h_dist = dy.abs() - self.half_height;
22        let ex = r_dist.max(0.0);
23        let ey = h_dist.max(0.0);
24        r_dist.max(h_dist).min(0.0) + (ex * ex + ey * ey).sqrt()
25    }
26    fn gradient(&self, p: [f64; 3]) -> [f64; 3] {
27        const EPS: f64 = 1e-5;
28        let dx = self.sdf([p[0] + EPS, p[1], p[2]]) - self.sdf([p[0] - EPS, p[1], p[2]]);
29        let dy = self.sdf([p[0], p[1] + EPS, p[2]]) - self.sdf([p[0], p[1] - EPS, p[2]]);
30        let dz = self.sdf([p[0], p[1], p[2] + EPS]) - self.sdf([p[0], p[1], p[2] - EPS]);
31        normalize([dx, dy, dz])
32    }
33}