Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add normal method to Triangle<3> #600

Merged
merged 1 commit into from
May 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]));
}
}