Skip to content

Commit

Permalink
Add normal method to Triangle<3>
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisprice authored and hannobraun committed May 18, 2022
1 parent c5cdcda commit cb17050
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion crates/fj-math/src/triangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<P, const D: usize> From<[P; 3]> for Triangle<D>
Expand Down Expand Up @@ -118,7 +127,7 @@ impl From<Orientation> for Winding {

#[cfg(test)]
mod tests {
use crate::Point;
use crate::{Point, Vector};

use super::Triangle;

Expand Down Expand Up @@ -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]));
}
}

0 comments on commit cb17050

Please sign in to comment.