#[derive(Debug, Clone, Copy)]
pub struct ClipPlane {
pub normal: [f64; 3],
pub distance: f64,
pub enabled: bool,
}
impl ClipPlane {
pub fn from_point_normal(point: [f64; 3], normal: [f64; 3]) -> Self {
let len = (normal[0] * normal[0] + normal[1] * normal[1] + normal[2] * normal[2]).sqrt();
let n = if len > 1e-12 {
[normal[0] / len, normal[1] / len, normal[2] / len]
} else {
[0.0, 0.0, 1.0]
};
let d = -(n[0] * point[0] + n[1] * point[1] + n[2] * point[2]);
Self { normal: n, distance: d, enabled: true }
}
pub fn x(pos: f64) -> Self {
Self::from_point_normal([pos, 0.0, 0.0], [1.0, 0.0, 0.0])
}
pub fn y(pos: f64) -> Self {
Self::from_point_normal([0.0, pos, 0.0], [0.0, 1.0, 0.0])
}
pub fn z(pos: f64) -> Self {
Self::from_point_normal([0.0, 0.0, pos], [0.0, 0.0, 1.0])
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn from_point_normal() {
let cp = ClipPlane::from_point_normal([1.0, 0.0, 0.0], [1.0, 0.0, 0.0]);
assert!(cp.enabled);
assert!((cp.normal[0] - 1.0).abs() < 1e-12);
assert!((cp.distance + 1.0).abs() < 1e-12);
}
#[test]
fn axis_planes() {
let cp = ClipPlane::x(2.0);
assert!((cp.normal[0] - 1.0).abs() < 1e-12);
assert!((cp.distance + 2.0).abs() < 1e-12);
}
}