use super::{Dir, Projection}; use serde::{Deserialize, Serialize}; use vek::*; /// Plane // plane defined by its normal and origin #[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)] pub struct Plane { pub normal: Dir, /// Distance from origin in the direction of normal pub d: f32, } impl Plane { pub fn new(dir: Dir) -> Self { Self::from(dir) } pub fn distance(&self, to: Vec3) -> f32 { self.normal.dot(to) - self.d } // fn center(&self) -> Vec3 { *self.normal * self.d } pub fn projection(&self, v: Vec3) -> Vec3 { v - *self.normal * self.distance(v) } pub fn xy() -> Self { Plane::from(Dir::new(Vec3::unit_z())) } pub fn yz() -> Self { Plane::from(Dir::new(Vec3::unit_x())) } pub fn zx() -> Self { Plane::from(Dir::new(Vec3::unit_y())) } } impl From for Plane { fn from(dir: Dir) -> Self { Plane { normal: dir, d: 0.0, } } } impl Projection for Vec3 { type Output = Vec3; fn projected(self, plane: &Plane) -> Self::Output { plane.projection(self) } } impl Projection for Extent2 where T: Projection, { type Output = Self; fn projected(self, plane: &Plane) -> Self::Output { self.map(|v| v.projected(plane)) } }