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;
13use super::types::SdfCapsule;
14
15impl ImplicitSurface for SdfCapsule {
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 clamped_y = dy.clamp(-self.half_height, self.half_height);
21        let dist = (dx * dx + (dy - clamped_y) * (dy - clamped_y) + dz * dz).sqrt();
22        dist - self.radius
23    }
24    fn gradient(&self, p: [f64; 3]) -> [f64; 3] {
25        let dx = p[0] - self.center[0];
26        let dy = p[1] - self.center[1];
27        let dz = p[2] - self.center[2];
28        let clamped_y = dy.clamp(-self.half_height, self.half_height);
29        let v = [dx, dy - clamped_y, dz];
30        normalize(v)
31    }
32}