Skip to content

Commit

Permalink
Implement Triangulate for Face
Browse files Browse the repository at this point in the history
  • Loading branch information
hannobraun committed Sep 7, 2022
1 parent 6d58254 commit a085baf
Showing 1 changed file with 54 additions and 43 deletions.
97 changes: 54 additions & 43 deletions crates/fj-kernel/src/algorithms/triangulate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,51 +50,62 @@ where
let tolerance = tolerance.into();

for face in self {
if let Some(triangles) = face.triangles() {
for &(triangle, color) in triangles {
mesh.push_triangle(triangle, color);
}
continue;
}
face.triangulate_into_mesh(tolerance, mesh, debug_info);
}
}
}

let surface = face.surface();
let approx = face.approx(tolerance);

let points: Vec<_> = approx
.points
.into_iter()
.map(|(point_surface, point_global)| TriangulationPoint {
point_surface,
point_global,
})
.collect();
let face_as_polygon = Polygon::new(*surface)
.with_exterior(
approx
.exterior
.points
.into_iter()
.map(|(point_surface, _)| point_surface),
)
.with_interiors(approx.interiors.into_iter().map(|interior| {
interior
.points
.into_iter()
.map(|(point_surface, _)| point_surface)
}));

let mut triangles = delaunay::triangulate(points);
triangles.retain(|triangle| {
face_as_polygon.contains_triangle(
triangle.map(|point| point.point_surface),
debug_info,
)
});

for triangle in triangles {
let points = triangle.map(|point| point.point_global);
mesh.push_triangle(points, face.color());
impl Triangulate for Face {
fn triangulate_into_mesh(
self,
tolerance: impl Into<Tolerance>,
mesh: &mut Mesh<Point<3>>,
debug_info: &mut DebugInfo,
) {
if let Some(triangles) = self.triangles() {
for &(triangle, color) in triangles {
mesh.push_triangle(triangle, color);
}
return;
}

let surface = self.surface();
let approx = self.approx(tolerance.into());

let points: Vec<_> = approx
.points
.into_iter()
.map(|(point_surface, point_global)| TriangulationPoint {
point_surface,
point_global,
})
.collect();
let face_as_polygon = Polygon::new(*surface)
.with_exterior(
approx
.exterior
.points
.into_iter()
.map(|(point_surface, _)| point_surface),
)
.with_interiors(approx.interiors.into_iter().map(|interior| {
interior
.points
.into_iter()
.map(|(point_surface, _)| point_surface)
}));

let mut triangles = delaunay::triangulate(points);
triangles.retain(|triangle| {
face_as_polygon.contains_triangle(
triangle.map(|point| point.point_surface),
debug_info,
)
});

for triangle in triangles {
let points = triangle.map(|point| point.point_global);
mesh.push_triangle(points, self.color());
}
}
}
Expand Down

0 comments on commit a085baf

Please sign in to comment.