Skip to content

Commit

Permalink
Merge pull request #1217 from hannobraun/winding
Browse files Browse the repository at this point in the history
Fix `Triangle::winding`
  • Loading branch information
hannobraun authored Oct 13, 2022
2 parents d1cc3f3 + 670a155 commit e668c05
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/fj-kernel/src/algorithms/triangulate/delaunay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions crates/fj-math/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
29 changes: 15 additions & 14 deletions crates/fj-math/src/triangle.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use parry2d_f64::utils::point_in_triangle::{corner_direction, Orientation};
use parry3d_f64::query::{Ray, RayCast as _};

use crate::Vector;
Expand Down Expand Up @@ -58,9 +57,21 @@ impl<const D: usize> Triangle<D> {

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()
pub fn winding(&self) -> Winding {
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!(
"Points don't form a triangle, but this was verified in the \
constructor."
)
}
}

Expand Down Expand Up @@ -122,16 +133,6 @@ pub enum Winding {
Cw,
}

impl From<Orientation> 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};
Expand Down

0 comments on commit e668c05

Please sign in to comment.