From 58329a83f6bfc652e36514655a49e9b28c4df587 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Thu, 13 Oct 2022 14:19:17 +0200 Subject: [PATCH 1/5] Add dependency on `robust-predicates` to `fj-math` --- Cargo.lock | 1 + crates/fj-math/Cargo.toml | 1 + 2 files changed, 2 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 823d90e79..d94f38b01 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1026,6 +1026,7 @@ dependencies = [ "num-traits", "parry2d-f64", "parry3d-f64", + "robust-predicates", ] [[package]] diff --git a/crates/fj-math/Cargo.toml b/crates/fj-math/Cargo.toml index 73342ea30..6aeed7f46 100644 --- a/crates/fj-math/Cargo.toml +++ b/crates/fj-math/Cargo.toml @@ -17,3 +17,4 @@ nalgebra = "0.31.2" num-traits = "0.2.15" parry2d-f64 = "0.10.0" parry3d-f64 = "0.10.0" +robust-predicates = "0.1.3" From cd1844fc4f735e51ea5477dd6b94fd0b9cb3a9ae Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Thu, 13 Oct 2022 14:20:29 +0200 Subject: [PATCH 2/5] Fix `Triangle<2>::winding_direction` For some reason, the Parry function gave a wrong result. --- crates/fj-math/src/triangle.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/crates/fj-math/src/triangle.rs b/crates/fj-math/src/triangle.rs index cbafd436e..2c3cffb32 100644 --- a/crates/fj-math/src/triangle.rs +++ b/crates/fj-math/src/triangle.rs @@ -1,4 +1,4 @@ -use parry2d_f64::utils::point_in_triangle::{corner_direction, Orientation}; +use parry2d_f64::utils::point_in_triangle::Orientation; use parry3d_f64::query::{Ray, RayCast as _}; use crate::Vector; @@ -59,8 +59,17 @@ impl Triangle { impl Triangle<2> { /// Returns the direction of the line through the points of the triangle. pub fn winding_direction(&self) -> Winding { - let [v0, v1, v2] = self.points.map(|point| point.to_na()); - corner_direction(&v0, &v1, &v2).into() + let [pa, pb, pc] = self.points.map(|point| point.into()); + let orient2d = robust_predicates::orient2d(&pa, &pb, &pc); + + if orient2d < 0. { + return Winding::Cw; + } + if orient2d > 0. { + return Winding::Ccw; + } + + unreachable!("not a triangle") } } From fdd53a88aab7c19bdb8287f4d2c7ca4d75057d9d Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Thu, 13 Oct 2022 14:21:27 +0200 Subject: [PATCH 3/5] Simplify method name --- crates/fj-kernel/src/algorithms/triangulate/delaunay.rs | 2 +- crates/fj-math/src/triangle.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/fj-kernel/src/algorithms/triangulate/delaunay.rs b/crates/fj-kernel/src/algorithms/triangulate/delaunay.rs index bc45a4dee..bc1fcad1f 100644 --- a/crates/fj-kernel/src/algorithms/triangulate/delaunay.rs +++ b/crates/fj-kernel/src/algorithms/triangulate/delaunay.rs @@ -22,7 +22,7 @@ pub fn triangulate( v2.point_surface, ]) .expect("invalid triangle") - .winding_direction(); + .winding(); let required_winding = match coord_handedness { Handedness::LeftHanded => Winding::Cw, diff --git a/crates/fj-math/src/triangle.rs b/crates/fj-math/src/triangle.rs index 2c3cffb32..78f9836f4 100644 --- a/crates/fj-math/src/triangle.rs +++ b/crates/fj-math/src/triangle.rs @@ -58,7 +58,7 @@ impl Triangle { impl Triangle<2> { /// Returns the direction of the line through the points of the triangle. - pub fn winding_direction(&self) -> Winding { + pub fn winding(&self) -> Winding { let [pa, pb, pc] = self.points.map(|point| point.into()); let orient2d = robust_predicates::orient2d(&pa, &pb, &pc); From cc7936b29b38033596c48a38f73b7963b704c677 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Thu, 13 Oct 2022 14:22:19 +0200 Subject: [PATCH 4/5] Improve panic message --- crates/fj-math/src/triangle.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/fj-math/src/triangle.rs b/crates/fj-math/src/triangle.rs index 78f9836f4..54504870e 100644 --- a/crates/fj-math/src/triangle.rs +++ b/crates/fj-math/src/triangle.rs @@ -69,7 +69,10 @@ impl Triangle<2> { return Winding::Ccw; } - unreachable!("not a triangle") + unreachable!( + "Points don't form a triangle, but this was verified in the \ + constructor." + ) } } From 670a155386eff18e9c88b9c8d739d5ace6d81460 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Thu, 13 Oct 2022 14:23:49 +0200 Subject: [PATCH 5/5] Remove unused code --- crates/fj-math/src/triangle.rs | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/crates/fj-math/src/triangle.rs b/crates/fj-math/src/triangle.rs index 54504870e..fb763017f 100644 --- a/crates/fj-math/src/triangle.rs +++ b/crates/fj-math/src/triangle.rs @@ -1,4 +1,3 @@ -use parry2d_f64::utils::point_in_triangle::Orientation; use parry3d_f64::query::{Ray, RayCast as _}; use crate::Vector; @@ -134,16 +133,6 @@ pub enum Winding { Cw, } -impl From for Winding { - fn from(o: Orientation) -> Self { - match o { - Orientation::Ccw => Winding::Ccw, - Orientation::Cw => Winding::Cw, - Orientation::None => unreachable!("not a triangle"), - } - } -} - #[cfg(test)] mod tests { use crate::{Point, Vector};