Split function into simpler ones

This commit is contained in:
Hanno Braun 2025-02-21 20:50:45 +01:00
parent 2a43f8bc24
commit 08b44c4928

View File

@ -10,7 +10,8 @@ use crate::{
pub fn triangulate(vertices: &[Handle<Vertex>], surface: &Plane) -> TriMesh { pub fn triangulate(vertices: &[Handle<Vertex>], surface: &Plane) -> TriMesh {
// This is a placeholder implementation that only supports convex faces. // This is a placeholder implementation that only supports convex faces.
let triangles = triangles(vertices, surface); let points = points(vertices, surface);
let triangles = triangles(&points);
let mut mesh = TriMesh::new(); let mut mesh = TriMesh::new();
mesh.triangles.extend(triangles); mesh.triangles.extend(triangles);
@ -18,12 +19,13 @@ pub fn triangulate(vertices: &[Handle<Vertex>], surface: &Plane) -> TriMesh {
mesh mesh
} }
fn triangles(vertices: &[Handle<Vertex>], surface: &Plane) -> Vec<Triangle> { fn points(
let mut triangulation = spade::ConstrainedDelaunayTriangulation::<_>::new(); vertices: &[Handle<Vertex>],
surface: &Plane,
triangulation ) -> Vec<TriangulationPoint> {
.add_constraint_edges( vertices
vertices.iter().map(|vertex| { .iter()
.map(|vertex| {
// Here, we project a 3D point (from the vertex) into the face's // Here, we project a 3D point (from the vertex) into the face's
// surface, creating a 2D point. Through the surface, this 2D // surface, creating a 2D point. Through the surface, this 2D
// point has a position in 3D space. // point has a position in 3D space.
@ -47,9 +49,15 @@ fn triangles(vertices: &[Handle<Vertex>], surface: &Plane) -> Vec<Triangle> {
point_surface, point_surface,
point_vertex: vertex.point, point_vertex: vertex.point,
} }
}), })
true, .collect()
) }
fn triangles(points: &[TriangulationPoint]) -> Vec<Triangle> {
let mut triangulation = spade::ConstrainedDelaunayTriangulation::<_>::new();
triangulation
.add_constraint_edges(points.iter().copied(), true)
.unwrap(); .unwrap();
triangulation triangulation