66 lines
1.9 KiB
Rust
66 lines
1.9 KiB
Rust
//! 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<f64>,
|
|
|
|
/// The constant, offset of the plane from the origin.
|
|
constant: f64,
|
|
}
|
|
|
|
impl Plane {
|
|
/// Creates a new plane from its normal and its constant.
|
|
pub fn from_coordinates(a: f64, b: f64, c: f64, w: f64) -> 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<f64>, constant: f64) -> 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<f64>, point: Vector3<f64>) -> Plane {
|
|
Plane::from_normal_and_constant(normal, - point.dot(normal))
|
|
}
|
|
|
|
|
|
/// Creates a new plane from three points.
|
|
pub fn from_points(p1: Vector3<f64>, p2: Vector3<f64>, p3: Vector3<f64>) -> 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<f64> {
|
|
self.normal
|
|
}
|
|
|
|
/// Returns the constant of the plane.
|
|
pub fn constant(&self) -> f64 {
|
|
self.constant
|
|
}
|
|
|
|
/// Returns the distance between the plane and the point.
|
|
pub fn distance_to_point(&self, point: Vector3<f64>) -> f64 {
|
|
self.normal.dot(point) + self.constant
|
|
}
|
|
}
|