Merge pull request #1217 from hannobraun/winding

Fix `Triangle::winding`
This commit is contained in:
Hanno Braun 2022-10-13 14:45:50 +02:00 committed by GitHub
commit e668c05afc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 15 deletions

1
Cargo.lock generated
View File

@ -1026,6 +1026,7 @@ dependencies = [
"num-traits",
"parry2d-f64",
"parry3d-f64",
"robust-predicates",
]
[[package]]

View File

@ -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,

View File

@ -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"

View File

@ -1,4 +1,3 @@
use parry2d_f64::utils::point_in_triangle::{corner_direction, Orientation};
use parry3d_f64::query::{Ray, RayCast as _};
use crate::Vector;
@ -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."
)
}
}
@ -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};