mini_collide/
plane.rs

1use crate::Triangle;
2use mini_math::{Point, Vector3};
3
4/// An infinite plane
5#[derive(Debug)]
6pub struct Plane {
7    /// The normal that lies perpendicular to the plane
8    pub normal: Vector3,
9    /// The distance from the plane to the origin
10    pub d: f32,
11}
12
13impl Plane {
14    /// Construct a plane given the components of the plan equation
15    pub fn new(normal: Vector3, d: f32) -> Self {
16        Self { normal, d }
17    }
18
19    /// Constructs a plane from three points that lie on the plane
20    pub fn from_points(p0: Point, p1: Point, p2: Point) -> Self {
21        let normal = -(p1 - p0).cross(p2 - p0).normalized();
22        let d = Vector3::from(p0).dot(normal);
23        Self { normal, d }
24    }
25
26    /// Constructs a plane from a point that lies on the plane, and the normal to the plane
27    pub fn from_point_and_normal(p: Point, normal: Vector3) -> Self {
28        Self {
29            normal,
30            d: Vector3::from(p).dot(normal),
31        }
32    }
33}
34
35impl From<&Triangle> for Plane {
36    fn from(t: &Triangle) -> Self {
37        Plane::from_points(t.a, t.b, t.c)
38    }
39}