From d9533ee033bd40f985676821c403aaae12f2a06f Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Mon, 29 Jul 2024 18:58:12 +0200 Subject: [PATCH] Add `Triangle::is_valid` The method doesn't make sense yet, as `Triangle::new` explicitly forbids degenerate triangles. This is about to change though, and adding this method is preparation for that. --- crates/fj-math/src/triangle.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/crates/fj-math/src/triangle.rs b/crates/fj-math/src/triangle.rs index 0dcd417e3..55a768634 100644 --- a/crates/fj-math/src/triangle.rs +++ b/crates/fj-math/src/triangle.rs @@ -1,3 +1,4 @@ +use approx::AbsDiffEq; use parry3d_f64::query::{Ray, RayCast as _}; use crate::Vector; @@ -41,6 +42,26 @@ impl Triangle { self.points } + /// # Determine whether the triangle is valid + /// + /// A triangle is valid, if it is not degenerate. In a degenerate triangle, + /// the three points do not form an actual triangle, but a line or even a + /// single point. + /// + /// ## Implementation Note + /// + /// Right now, this function computes the area of the triangle, and compares + /// it against [`Scalar`]'s default epsilon value. This might not be + /// flexible enough for all use cases. + /// + /// Long-term, it might become necessary to add some way to override the + /// epsilon value used within this function. + pub fn is_valid(&self) -> bool { + let [a, b, c] = self.points; + let area = (b - a).outer(&(c - a)).magnitude(); + area > Scalar::default_epsilon() + } + /// Normalize the triangle /// /// Returns a new `Triangle` instance with the same points, but the points