Skip to main content

oxiphysics_geometry/csg/
sdfcapsule_traits.rs

1//! # SdfCapsule - Trait Implementations
2//!
3//! This module contains trait implementations for `SdfCapsule`.
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;
13#[allow(unused_imports)]
14use super::functions::*;
15use super::types::SdfCapsule;
16
17impl ImplicitSurface for SdfCapsule {
18    fn sdf(&self, p: [f64; 3]) -> f64 {
19        let dx = p[0] - self.center[0];
20        let dy = p[1] - self.center[1];
21        let dz = p[2] - self.center[2];
22        let clamped_y = dy.clamp(-self.half_height, self.half_height);
23        let dist = (dx * dx + (dy - clamped_y) * (dy - clamped_y) + dz * dz).sqrt();
24        dist - self.radius
25    }
26    fn gradient(&self, p: [f64; 3]) -> [f64; 3] {
27        let dx = p[0] - self.center[0];
28        let dy = p[1] - self.center[1];
29        let dz = p[2] - self.center[2];
30        let clamped_y = dy.clamp(-self.half_height, self.half_height);
31        let v = [dx, dy - clamped_y, dz];
32        normalize(v)
33    }
34}