veloren_common/util/
plane.rs

1use super::{Dir, Projection};
2use serde::{Deserialize, Serialize};
3use vek::*;
4
5/// Plane
6
7// plane defined by its normal and origin
8#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
9pub struct Plane {
10    pub normal: Dir,
11    /// Distance from origin in the direction of normal
12    pub d: f32,
13}
14
15impl Plane {
16    pub fn new(dir: Dir) -> Self { Self::from(dir) }
17
18    pub fn distance(&self, to: Vec3<f32>) -> f32 { self.normal.dot(to) - self.d }
19
20    // fn center(&self) -> Vec3<f32> { *self.normal * self.d }
21
22    pub fn projection(&self, v: Vec3<f32>) -> Vec3<f32> { v - *self.normal * self.distance(v) }
23
24    pub fn xy() -> Self { Plane::from(Dir::new(Vec3::unit_z())) }
25
26    pub fn yz() -> Self { Plane::from(Dir::new(Vec3::unit_x())) }
27
28    pub fn zx() -> Self { Plane::from(Dir::new(Vec3::unit_y())) }
29}
30
31impl From<Dir> for Plane {
32    fn from(dir: Dir) -> Self {
33        Plane {
34            normal: dir,
35            d: 0.0,
36        }
37    }
38}
39
40impl Projection<Plane> for Vec3<f32> {
41    type Output = Vec3<f32>;
42
43    fn projected(self, plane: &Plane) -> Self::Output { plane.projection(self) }
44}
45
46impl<T> Projection<Plane> for Extent2<T>
47where
48    T: Projection<Plane, Output = T>,
49{
50    type Output = Self;
51
52    fn projected(self, plane: &Plane) -> Self::Output { self.map(|v| v.projected(plane)) }
53}