diff --git a/crates/fj-math/src/triangle.rs b/crates/fj-math/src/triangle.rs index 5775d9d6b..92dc8ec37 100644 --- a/crates/fj-math/src/triangle.rs +++ b/crates/fj-math/src/triangle.rs @@ -87,6 +87,15 @@ impl Triangle<3> { .cast_local_ray(&ray, max_toi, solid) .map(|f| f.into()) } + + /// Compute the triangle's normal + pub fn normal(&self) -> Vector<3> { + self.to_parry() + .normal() + .expect("triangle is valid (validated on construction)") + .into_inner() + .into() + } } impl From<[P; 3]> for Triangle @@ -118,7 +127,7 @@ impl From for Winding { #[cfg(test)] mod tests { - use crate::Point; + use crate::{Point, Vector}; use super::Triangle; @@ -155,4 +164,11 @@ mod tests { let c = Point::from([2.0, 2.0, 2.0]); let _triangle = Triangle::from([a, b, c]); } + + #[test] + fn normal() { + let triangle = + Triangle::from([[0.0, 0.0, 0.0], [2.0, 1.0, 0.0], [2.0, 0.0, 0.0]]); + assert_eq!(triangle.normal(), Vector::from([0.0, 0.0, -1.0])); + } }