Skip to content

Commit

Permalink
remove merging api that is slower and not needed anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
mockersf committed Jun 28, 2024
1 parent 3733978 commit ea5132b
Show file tree
Hide file tree
Showing 6 changed files with 4 additions and 92 deletions.
6 changes: 2 additions & 4 deletions benches/merger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1970,8 +1970,7 @@ fn random_with_many_obstacles() -> Triangulation {

fn merger_many_overlapping(c: &mut Criterion) {
c.bench_function(&"merger many overlapping".to_string(), |b| {
let mut triangulation = random_with_many_obstacles();
triangulation.merge_overlapping_obstacles();
let triangulation = random_with_many_obstacles();
let mesh: Mesh = triangulation.as_navmesh();
b.iter(|| {
let mut mesh = mesh.clone();
Expand All @@ -1983,8 +1982,7 @@ fn merger_many_overlapping(c: &mut Criterion) {

fn merger_many_overlapping_once(c: &mut Criterion) {
c.bench_function(&"merger many overlapping (once)".to_string(), |b| {
let mut triangulation = random_with_many_obstacles();
triangulation.merge_overlapping_obstacles();
let triangulation = random_with_many_obstacles();
let mesh: Mesh = triangulation.as_navmesh();
b.iter(|| {
let mut mesh = mesh.clone();
Expand Down
6 changes: 1 addition & 5 deletions benches/triangulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ fn triangulation_overlapping(c: &mut Criterion) {
triangulation.add_obstacle(ARENA_OBSTACLES[2].to_vec());
triangulation.add_obstacle(ARENA_OBSTACLES[3].to_vec());
triangulation.add_obstacle(ARENA_OBSTACLES[4].to_vec());
triangulation.merge_overlapping_obstacles();

let mesh: Mesh = triangulation.as_navmesh();
black_box(mesh);
Expand Down Expand Up @@ -258,7 +257,6 @@ fn triangulation_square_overlapping(c: &mut Criterion) {
vec2(7.5, 7.5),
vec2(7.5, 4.0),
]);
triangulation.merge_overlapping_obstacles();
let mesh: Mesh = triangulation.as_navmesh();
black_box(mesh);
})
Expand Down Expand Up @@ -2086,8 +2084,7 @@ fn random_with_many_obstacles() -> Triangulation {
fn triangulation_many_overlapping(c: &mut Criterion) {
c.bench_function(&"triangulation many overlapping".to_string(), |b| {
b.iter(|| {
let mut triangulation = random_with_many_obstacles();
triangulation.merge_overlapping_obstacles();
let triangulation = random_with_many_obstacles();
let mesh: Mesh = triangulation.as_navmesh();
black_box(mesh);
})
Expand All @@ -2100,7 +2097,6 @@ fn triangulation_many_overlapping_simplified(c: &mut Criterion) {
|b| {
b.iter(|| {
let mut triangulation = random_with_many_obstacles();
triangulation.merge_overlapping_obstacles();
triangulation.simplify(0.005);
let mesh: Mesh = triangulation.as_navmesh();
black_box(mesh);
Expand Down
1 change: 0 additions & 1 deletion examples/traced/src/bin/merged.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1826,7 +1826,6 @@ fn main() {
vec2(2.7777152, 2.6639614),
],
]);
triangulation.merge_overlapping_obstacles();
triangulation.simplify(0.005);
let mut mesh = triangulation.as_navmesh();
while mesh.merge_polygons() {}
Expand Down
1 change: 0 additions & 1 deletion examples/traced/src/bin/triangulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1826,7 +1826,6 @@ fn main() {
vec2(2.7777152, 2.6639614),
],
]);
triangulation.merge_overlapping_obstacles();
triangulation.simplify(0.005);
triangulation.as_navmesh();
}
Expand Down
78 changes: 1 addition & 77 deletions src/input/triangulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ use std::collections::VecDeque;
use tracing::instrument;

pub use geo::LineString;
use geo::{
BooleanOps, Contains, Coord, CoordsIter, Intersects, MultiPolygon, Polygon as GeoPolygon,
SimplifyVwPreserve,
};
use geo::{Contains, Coord, Polygon as GeoPolygon, SimplifyVwPreserve};
use glam::{vec2, Vec2};
use spade::{ConstrainedDelaunayTriangulation, Point2, Triangulation as SpadeTriangulation};

Expand All @@ -31,19 +28,13 @@ impl Triangulation {
}

/// Add an obstacle delimited by the list of points on its edges.
///
/// Obstacles *MUST NOT* overlap. If some obstacles do overlap, use [`Triangulation::merge_overlapping_obstacles`]
/// before calling [`Triangulation::as_navmesh`].
pub fn add_obstacle(&mut self, edges: Vec<Vec2>) {
self.inner.interiors_push(LineString::from(
edges.iter().map(|v| (v.x, v.y)).collect::<Vec<_>>(),
));
}

/// Add obstacles delimited by the list of points on their edges.
///
/// Obstacles *MUST NOT* overlap. If some obstacles do overlap, use [`Triangulation::merge_overlapping_obstacles`]
/// before calling [`Triangulation::as_navmesh`].
pub fn add_obstacles(&mut self, obstacles: impl IntoIterator<Item = Vec<Vec2>>) {
let (exterior, interiors) =
std::mem::replace(&mut self.inner, GeoPolygon::new(LineString(vec![]), vec![]))
Expand All @@ -64,73 +55,6 @@ impl Triangulation {
);
}

/// Merge overlapping obstacles.
///
/// This must be called before converting the triangulation into a [`Mesh`] if there are overlapping obstacles,
/// otherwise it will fail.
#[cfg_attr(feature = "tracing", instrument(skip_all))]
pub fn merge_overlapping_obstacles(&mut self) {
let (mut exterior, interiors) =
std::mem::replace(&mut self.inner, GeoPolygon::new(LineString(vec![]), vec![]))
.into_inner();

let mut not_intersecting: Vec<LineString<f32>> = vec![];
for poly in interiors.into_iter() {
let intersecting = not_intersecting
.iter()
.enumerate()
.filter(|(_, other)| poly.intersects(*other))
.map(|(i, _)| i)
.collect::<Vec<_>>();

let to_keep = if intersecting.is_empty() {
poly
} else {
#[cfg(feature = "tracing")]
let _merging_span = tracing::info_span!("merging polygons").entered();

let mut merged = MultiPolygon::<f32>(
intersecting
.iter()
.rev()
.map(|other| GeoPolygon::new(not_intersecting.remove(*other), vec![]))
.collect(),
);
merged = merged.union(&GeoPolygon::new(poly, vec![]).into());
LineString(merged.exterior_coords_iter().collect::<Vec<_>>())
};

if to_keep.intersects(&exterior) {
let new_exterior =
GeoPolygon::new(exterior, vec![]).difference(&GeoPolygon::new(to_keep, vec![]));
// Keep the biggest of the new exterior polygons
if new_exterior.0.len() > 1 {
let mut biggest = 0;
let mut biggest_length = 0;
for (i, poly) in new_exterior.0.iter().enumerate() {
let exterior_length = poly.exterior_coords_iter().len();
if exterior_length > biggest_length {
biggest = i;
biggest_length = exterior_length;
}
}
exterior = LineString(
new_exterior.0[biggest]
.exterior_coords_iter()
.collect::<Vec<_>>(),
);
} else {
exterior =
LineString(new_exterior.0[0].exterior_coords_iter().collect::<Vec<_>>());
}
} else {
not_intersecting.push(to_keep);
}
}

self.inner = GeoPolygon::new(exterior, not_intersecting);
}

/// Simplify the outer edge and obstacles, using a topology-preserving variant of the
/// [Visvalingam-Whyatt algorithm](https://www.tandfonline.com/doi/abs/10.1179/000870493786962263).
///
Expand Down
4 changes: 0 additions & 4 deletions tests/triangulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ fn is_in_mesh_4_obstacles() {
vec2(7.5, 7.5),
vec2(7.5, 5.0),
]);
triangulation.merge_overlapping_obstacles();
triangulation.simplify(0.5);
let mesh: Mesh = triangulation.as_navmesh();

Expand All @@ -76,7 +75,6 @@ fn is_in_mesh_4_obstacles() {
}

#[test]
#[should_panic]
fn is_in_mesh_overlapping() {
let mut triangulation = Triangulation::from_outer_edges(&[
vec2(0.0, 0.0),
Expand Down Expand Up @@ -152,7 +150,6 @@ fn is_in_mesh_overlapping_merged() {
vec2(7.5, 7.5),
vec2(7.5, 4.0),
]);
triangulation.merge_overlapping_obstacles();
let mesh: Mesh = triangulation.as_navmesh();
for i in 0..10 {
for j in 0..10 {
Expand Down Expand Up @@ -197,7 +194,6 @@ fn is_in_mesh_overlapping_simplified() {
vec2(7.5, 7.5),
vec2(7.5, 4.0),
]);
triangulation.merge_overlapping_obstacles();
let polygons_before = triangulation.as_navmesh().polygons;
triangulation.simplify(1.0);
let mesh: Mesh = triangulation.as_navmesh();
Expand Down

0 comments on commit ea5132b

Please sign in to comment.