//! Module containing the plane struct. use math::vector::Vector3; /// A 3D plane. #[derive(Copy, Clone)] pub struct Plane { /// The normal of the plane. normal: Vector3, /// The constant, offset of the plane from the origin. constant: f32, } impl Plane { /// Creates a new plane from its normal and its constant. pub fn from_coordinates(a: f32, b: f32, c: f32, w: f32) -> Plane { let mut p = Plane { normal: Vector3::new(a, b, c), constant: w, }; p.normalize(); p } /// Creates a new plane from its normal and its constant. pub fn from_normal_and_constant(normal: Vector3, constant: f32) -> Plane { Plane::from_coordinates(normal.x(), normal.y(), normal.z(), constant) } /// Creates a new plane from its normal and a point of the plane. pub fn from_normal_and_point(normal: Vector3, point: Vector3) -> Plane { Plane::from_normal_and_constant(normal, - point.dot(normal)) } /// Creates a new plane from three points. pub fn from_points(p1: Vector3, p2: Vector3, p3: Vector3) -> Plane { let p1p2 = p2 - p1; let p1p3 = p3 - p1; Plane::from_normal_and_point(p1p2.cross_product(p1p3), p1) } /// Normalizes the plane. pub fn normalize(&mut self) { let normal_inverse = 1.0 / self.normal.norm(); self.normal *= normal_inverse; self.constant *= normal_inverse; } /// Returns the normal of the plane. pub fn normal(&self) -> Vector3 { self.normal } /// Returns the constant of the plane. pub fn constant(&self) -> f32 { self.constant } /// Returns the distance between the plane and the point. pub fn distance_to_point(&self, point: Vector3) -> f32 { self.normal.dot(point) + self.constant } }