Add Plane::project_point

This commit is contained in:
Hanno Braun 2025-01-09 18:52:54 +01:00
parent 1b7adfd87f
commit acd13fd925

View File

@ -25,6 +25,22 @@ impl Plane {
self.origin + self.coords.a * u + self.coords.b * v
}
#[allow(unused)] // code to use it is being worked on
pub fn project_point(&self, point: impl Into<Point<3>>) -> Point<2> {
let point = point.into();
let origin_to_point = point - self.origin;
let min_distance_plane_to_point = origin_to_point.dot(&self.normal());
let point_in_plane =
point - self.normal() * min_distance_plane_to_point;
let origin_to_point_in_plane = point_in_plane - self.origin;
let u = origin_to_point_in_plane.dot(&self.u());
let v = origin_to_point_in_plane.dot(&self.v());
Point::from([u, v])
}
pub fn translate(self, offset: impl Into<Vector<3>>) -> Self {
Self {
origin: self.origin + offset,
@ -32,3 +48,23 @@ impl Plane {
}
}
}
#[cfg(test)]
mod tests {
use crate::math::{Bivector, Point, Vector};
use super::Plane;
#[test]
fn project_point() {
let plane = Plane {
origin: Point::from([1., 1., 1.]),
coords: Bivector {
a: Vector::from([1., 0., 0.]),
b: Vector::from([0., 1., 0.]),
},
};
assert_eq!(plane.project_point([2., 2., 2.]), Point::from([1., 1.]));
}
}