Skip to content

Commit

Permalink
do not consume triangulation when creating navmesh
Browse files Browse the repository at this point in the history
  • Loading branch information
mockersf committed Aug 16, 2023
1 parent f12a9d2 commit bb51c85
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 17 deletions.
12 changes: 6 additions & 6 deletions benches/triangulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
});
Expand All @@ -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);
})
});
Expand All @@ -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);
})
});
Expand Down Expand Up @@ -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);
})
});
Expand Down Expand Up @@ -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);
})
});
Expand Down Expand Up @@ -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);
})
});
Expand Down
13 changes: 6 additions & 7 deletions src/input/triangulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,13 @@ impl Triangulation {
}
}
}
}

impl From<Triangulation> for Mesh {
fn from(value: Triangulation) -> Self {
/// Convert the triangulation into a [`Mesh`].
pub fn as_navmesh(&self) -> Mesh {
let mut cdt = ConstrainedDelaunayTriangulation::<Point2<f32>>::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);
});

Expand All @@ -154,14 +153,14 @@ impl From<Triangulation> for Mesh {
.filter_map(|face| {
let center = face.center();
let center = Coord::from((center.x, center.y));
value.inner.contains(&center).then(|| {
self.inner.contains(&center).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,
)
})
Expand Down
2 changes: 1 addition & 1 deletion tests/arena-triangulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ fn arena_mesh() -> Mesh {
vec2(26., 7.),
vec2(26., 10.),
]);
triangulation.into()
triangulation.as_navmesh()
}

#[test]
Expand Down
6 changes: 3 additions & 3 deletions tests/triangulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit bb51c85

Please sign in to comment.