Skip to main content

oxiphysics_geometry/implicit_geometry/
sdfpyramid_traits.rs

1//! # SdfPyramid - Trait Implementations
2//!
3//! This module contains trait implementations for `SdfPyramid`.
4//!
5//! ## Implemented Traits
6//!
7//! - `Sdf`
8//!
9//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)
10
11#[allow(unused_imports)]
12use super::functions::*;
13#[allow(unused_imports)]
14use super::functions::*;
15use super::types::SdfPyramid;
16
17impl Sdf for SdfPyramid {
18    fn dist(&self, p: [f64; 3]) -> f64 {
19        let m2 = self.half_base * self.half_base + self.height * self.height;
20        let px = p[0].abs();
21        let pz = p[2].abs();
22        let (px, pz) = if pz > px { (pz, px) } else { (px, pz) };
23        let px = px - self.half_base;
24        let py = p[1] - self.height;
25        let k = clamp_f(-py * self.half_base - px * self.height, 0.0, m2);
26        let dx = px - (-self.half_base * k / m2);
27        let dy = py - self.height * k / m2;
28        let d1 = ((px * self.height - py * (-self.half_base)).max(0.0).powi(2)
29            + (pz + self.half_base).max(0.0).powi(2))
30        .sqrt();
31        let d2 = len2([dx, dy]);
32        let s = if px * (-self.height) - py * (-self.half_base) > 0.0 {
33            -1.0
34        } else {
35            1.0
36        };
37        _ = d1;
38        s * d2.min(d1)
39    }
40}