From bb51c85777b670524ef65fb84b7fa158a729b82f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Wed, 16 Aug 2023 09:05:50 +0200 Subject: [PATCH] do not consume triangulation when creating navmesh --- benches/triangulation.rs | 12 ++++++------ src/input/triangulation.rs | 13 ++++++------- tests/arena-triangulation.rs | 2 +- tests/triangulation.rs | 6 +++--- 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/benches/triangulation.rs b/benches/triangulation.rs index 98ce09e..72e876f 100644 --- a/benches/triangulation.rs +++ b/benches/triangulation.rs @@ -142,7 +142,7 @@ fn triangulation(c: &mut Criterion) { triangulation.add_obstacle(ARENA_OBSTACLES[3].to_vec()); triangulation.add_obstacle(ARENA_OBSTACLES[4].to_vec()); - let mesh: Mesh = triangulation.into(); + let mesh: Mesh = triangulation.as_navmesh(); black_box(mesh); }) }); @@ -161,7 +161,7 @@ fn triangulation_bulk(c: &mut Criterion) { ARENA_OBSTACLES[3].to_vec(), ARENA_OBSTACLES[4].to_vec(), ]); - let mesh: Mesh = triangulation.into(); + let mesh: Mesh = triangulation.as_navmesh(); black_box(mesh); }) }); @@ -180,7 +180,7 @@ fn triangulation_overlapping(c: &mut Criterion) { triangulation.add_obstacle(ARENA_OBSTACLES[4].to_vec()); triangulation.merge_overlapping_obstacles(); - let mesh: Mesh = triangulation.into(); + let mesh: Mesh = triangulation.as_navmesh(); black_box(mesh); }) }); @@ -219,7 +219,7 @@ fn triangulation_square(c: &mut Criterion) { vec2(7.5, 7.5), vec2(7.5, 5.01), ]); - let mesh: Mesh = triangulation.into(); + let mesh: Mesh = triangulation.as_navmesh(); black_box(mesh); }) }); @@ -259,7 +259,7 @@ fn triangulation_square_overlapping(c: &mut Criterion) { vec2(7.5, 4.0), ]); triangulation.merge_overlapping_obstacles(); - let mesh: Mesh = triangulation.into(); + let mesh: Mesh = triangulation.as_navmesh(); black_box(mesh); }) }); @@ -2083,7 +2083,7 @@ fn triangulation_many_overlapping(c: &mut Criterion) { ], ]); triangulation.merge_overlapping_obstacles(); - let mesh: Mesh = triangulation.into(); + let mesh: Mesh = triangulation.as_navmesh(); black_box(mesh); }) }); diff --git a/src/input/triangulation.rs b/src/input/triangulation.rs index 509faa6..7abf02a 100644 --- a/src/input/triangulation.rs +++ b/src/input/triangulation.rs @@ -137,14 +137,13 @@ impl Triangulation { } } } -} -impl From for Mesh { - fn from(value: Triangulation) -> Self { + /// Convert the triangulation into a [`Mesh`]. + pub fn as_navmesh(&self) -> Mesh { let mut cdt = ConstrainedDelaunayTriangulation::>::new(); - Triangulation::add_constraint_edges(&mut cdt, value.inner.exterior()); + Triangulation::add_constraint_edges(&mut cdt, self.inner.exterior()); - value.inner.interiors().iter().for_each(|obstacle| { + self.inner.interiors().iter().for_each(|obstacle| { Triangulation::add_constraint_edges(&mut cdt, obstacle); }); @@ -154,14 +153,14 @@ impl From for Mesh { .filter_map(|face| { let center = face.center(); let center = Coord::from((center.x, center.y)); - value.inner.contains(¢er).then(|| { + self.inner.contains(¢er).then(|| { face_to_polygon.insert(face.index(), face_to_polygon.len() as isize); Polygon::new( face.vertices() .iter() .map(|vertex| vertex.index() as u32) .collect(), - // TODO: can this be set to the correct value + // TODO: can this be set to the correct value? false, ) }) diff --git a/tests/arena-triangulation.rs b/tests/arena-triangulation.rs index bce33a3..af93f19 100644 --- a/tests/arena-triangulation.rs +++ b/tests/arena-triangulation.rs @@ -136,7 +136,7 @@ fn arena_mesh() -> Mesh { vec2(26., 7.), vec2(26., 10.), ]); - triangulation.into() + triangulation.as_navmesh() } #[test] diff --git a/tests/triangulation.rs b/tests/triangulation.rs index b64e426..219558f 100644 --- a/tests/triangulation.rs +++ b/tests/triangulation.rs @@ -15,7 +15,7 @@ fn is_in_mesh() { vec2(7.5, 7.5), vec2(7.5, 2.5), ]); - let mesh: Mesh = triangulation.into(); + let mesh: Mesh = triangulation.as_navmesh(); for i in 0..10 { for j in 0..10 { if i > 2 && i < 8 && j > 2 && j < 8 { @@ -59,7 +59,7 @@ fn is_in_mesh_4_obstacles() { vec2(7.5, 7.5), vec2(7.5, 5.0), ]); - let mesh: Mesh = triangulation.into(); + let mesh: Mesh = triangulation.as_navmesh(); for i in 0..10 { for j in 0..10 { if i > 2 && i < 8 && j > 2 && j < 8 { @@ -104,7 +104,7 @@ fn is_in_mesh_overlapping() { vec2(7.5, 4.0), ]); triangulation.merge_overlapping_obstacles(); - let mesh: Mesh = triangulation.into(); + let mesh: Mesh = triangulation.as_navmesh(); for i in 0..10 { for j in 0..10 { if i > 2 && i < 8 && j > 2 && j < 8 {