use glam::{DVec3, Vec3};
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, bytemuck::Pod, bytemuck::Zeroable)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct GpuFacetPlane {
pub normal: [f32; 3],
pub d: f32,
}
impl GpuFacetPlane {
#[must_use]
pub fn new(normal: Vec3, d: f32) -> Self {
let n = normal.normalize();
Self {
normal: [n.x, n.y, n.z],
d,
}
}
#[must_use]
pub fn to_halfspace_f64(self) -> (DVec3, f64) {
(
DVec3::new(
f64::from(self.normal[0]),
f64::from(self.normal[1]),
f64::from(self.normal[2]),
),
-f64::from(self.d),
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn to_halfspace_f64_inverts_the_gpu_facet_plane_sign_convention() {
let mast = 0.6_f32;
let plane = GpuFacetPlane::new(Vec3::Y, -mast);
let (n, m) = plane.to_halfspace_f64();
assert!((n - DVec3::Y).length() < 1e-12);
assert!((m - f64::from(mast)).abs() < 1e-12);
}
}