Merge pull request #455 from hannobraun/delaunay

Move Delaunay triangulation code to dedicated module
This commit is contained in:
Hanno Braun 2022-04-11 18:22:23 +02:00 committed by GitHub
commit fb78a9b29f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 49 deletions

View File

@ -0,0 +1,52 @@
use fj_math::Scalar;
use parry2d_f64::utils::point_in_triangle::{corner_direction, Orientation};
use spade::HasPosition;
use crate::geometry;
/// Create a Delaunay triangulation of all points
pub fn triangulate(
points: Vec<geometry::Point<2>>,
) -> Vec<[geometry::Point<2>; 3]> {
use spade::Triangulation as _;
let triangulation = spade::DelaunayTriangulation::<_>::bulk_load(points)
.expect("Inserted invalid values into triangulation");
let mut triangles = Vec::new();
for triangle in triangulation.inner_faces() {
let [v0, v1, v2] = triangle.vertices().map(|vertex| *vertex.data());
let orientation = corner_direction(
&v0.native().to_na(),
&v1.native().to_na(),
&v2.native().to_na(),
);
let triangle = match orientation {
Orientation::Ccw => [v0, v1, v2],
Orientation::Cw => [v0, v2, v1],
Orientation::None => {
panic!(
"Triangle returned from triangulation isn't actually a \
triangle"
);
}
};
triangles.push(triangle);
}
triangles
}
// Enables the use of `geometry::Point` in the triangulation.
impl HasPosition for geometry::Point<2> {
type Scalar = Scalar;
fn position(&self) -> spade::Point2<Self::Scalar> {
spade::Point2 {
x: self.native().u,
y: self.native().v,
}
}
}

View File

@ -1,12 +1,11 @@
mod delaunay;
mod polygon;
mod ray;
use fj_interop::debug::DebugInfo;
use fj_math::{Scalar, Triangle};
use parry2d_f64::utils::point_in_triangle::{corner_direction, Orientation};
use spade::HasPosition;
use crate::{geometry, shape::Shape, topology::Face};
use crate::{shape::Shape, topology::Face};
use self::polygon::Polygon;
@ -54,7 +53,7 @@ pub fn triangulate(
},
));
let mut triangles = delaunay(points);
let mut triangles = delaunay::triangulate(points);
triangles.retain(|triangle| {
face_as_polygon.contains_triangle(
triangle.map(|point| point.native()),
@ -74,51 +73,6 @@ pub fn triangulate(
}
}
/// Create a Delaunay triangulation of all points
fn delaunay(points: Vec<geometry::Point<2>>) -> Vec<[geometry::Point<2>; 3]> {
use spade::Triangulation as _;
let triangulation = spade::DelaunayTriangulation::<_>::bulk_load(points)
.expect("Inserted invalid values into triangulation");
let mut triangles = Vec::new();
for triangle in triangulation.inner_faces() {
let [v0, v1, v2] = triangle.vertices().map(|vertex| *vertex.data());
let orientation = corner_direction(
&v0.native().to_na(),
&v1.native().to_na(),
&v2.native().to_na(),
);
let triangle = match orientation {
Orientation::Ccw => [v0, v1, v2],
Orientation::Cw => [v0, v2, v1],
Orientation::None => {
panic!(
"Triangle returned from triangulation isn't actually a \
triangle"
);
}
};
triangles.push(triangle);
}
triangles
}
// Enables the use of `geometry::Point` in the triangulation.
impl HasPosition for geometry::Point<2> {
type Scalar = Scalar;
fn position(&self) -> spade::Point2<Self::Scalar> {
spade::Point2 {
x: self.native().u,
y: self.native().v,
}
}
}
#[cfg(test)]
mod tests {
use fj_interop::debug::DebugInfo;