Skip to main content

oxiphysics_geometry/csg/
sdfcone_traits.rs

1//! # SdfCone - Trait Implementations
2//!
3//! This module contains trait implementations for `SdfCone`.
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::{length, normalize};
13use super::types::SdfCone;
14
15impl ImplicitSurface for SdfCone {
16    fn sdf(&self, p: [f64; 3]) -> f64 {
17        let dx = p[0] - self.apex[0];
18        let dy = p[1] - self.apex[1];
19        let dz = p[2] - self.apex[2];
20        let r = (dx * dx + dz * dz).sqrt();
21        let h = dy;
22        let sin_a = self.half_angle.sin();
23        let cos_a = self.half_angle.cos();
24        let q = [(r * r + h * h).sqrt(), 0.0_f64, 0.0];
25        let c = [
26            sin_a * q[0] - cos_a * q[1],
27            cos_a * q[0] + sin_a * q[1],
28            0.0,
29        ];
30        let c_clamped = [c[0].max(0.0), c[1].clamp(0.0, self.height), 0.0];
31        let cone_sdf = c[1].max(0.0)
32            * (-1.0_f64)
33                .min(1.0)
34                .min(if c[0] < 0.0 && c[1] < 0.0 { -1.0 } else { 1.0 });
35        let _ = (c_clamped, cone_sdf);
36        let cone_r_at_h = h.max(0.0).min(self.height) * self.half_angle.tan();
37        let excess_r = r - cone_r_at_h;
38        let h_dist = h - self.height;
39        if h < 0.0 {
40            length([dx, dy, dz])
41        } else if h > self.height {
42            let base_r = self.height * self.half_angle.tan();
43            let dr = (r - base_r).max(0.0);
44            (dr * dr + h_dist * h_dist).sqrt()
45        } else {
46            let side_normal_r = cos_a;
47            let side_normal_h = -sin_a;
48            excess_r * side_normal_r + (h - h * 1.0) * side_normal_h
49        }
50    }
51    fn gradient(&self, p: [f64; 3]) -> [f64; 3] {
52        const EPS: f64 = 1e-5;
53        let gx = self.sdf([p[0] + EPS, p[1], p[2]]) - self.sdf([p[0] - EPS, p[1], p[2]]);
54        let gy = self.sdf([p[0], p[1] + EPS, p[2]]) - self.sdf([p[0], p[1] - EPS, p[2]]);
55        let gz = self.sdf([p[0], p[1], p[2] + EPS]) - self.sdf([p[0], p[1], p[2] - EPS]);
56        normalize([gx, gy, gz])
57    }
58}