Skip to content

Commit 207ee7d

Browse files
committed
triangulation from geo polygon, and re-export geo
1 parent dfef8fa commit 207ee7d

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

Diff for: src/input/triangulation.rs

+27-12
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use tracing::instrument;
66

77
use crate::spade::{ConstrainedDelaunayTriangulation, Point2, Triangulation as SpadeTriangulation};
88
pub use geo::LineString;
9-
use geo::{Contains, Coord, Polygon as GeoPolygon, SimplifyVwPreserve};
9+
use geo::{self, Contains, Coord, SimplifyVwPreserve};
1010
use glam::{vec2, Vec2};
1111

1212
use crate::{Layer, Mesh, Polygon, Vertex};
@@ -21,9 +21,9 @@ enum AgentRadius {
2121
/// An helper to create a [`Mesh`] from a list of edges and obstacle, using a constrained Delaunay triangulation.
2222
#[derive(Clone)]
2323
pub struct Triangulation {
24-
inner: GeoPolygon<f32>,
24+
inner: geo::Polygon<f32>,
2525
prebuilt: Option<(
26-
GeoPolygon<f32>,
26+
geo::Polygon<f32>,
2727
ConstrainedDelaunayTriangulation<Point2<f64>>,
2828
)>,
2929
base_layer: Option<Layer>,
@@ -40,10 +40,23 @@ impl std::fmt::Debug for Triangulation {
4040
}
4141

4242
impl Triangulation {
43+
/// Create a new triangulation from a [`geo::Polygon`].
44+
///
45+
/// The exterior of the polygon will be used as the outer edge of the triangulation,
46+
/// and inner polygons will be used as obstacles.
47+
pub fn from_geo_polygon(polygon: geo::Polygon<f32>) -> Triangulation {
48+
Self {
49+
inner: polygon,
50+
prebuilt: None,
51+
base_layer: None,
52+
agent_radius: AgentRadius::None,
53+
}
54+
}
55+
4356
/// Create a new triangulation from a the list of points on its outer edges.
4457
pub fn from_outer_edges(edges: &[Vec2]) -> Triangulation {
4558
Self {
46-
inner: GeoPolygon::new(
59+
inner: geo::Polygon::new(
4760
LineString::from(edges.iter().map(|v| (v.x, v.y)).collect::<Vec<_>>()),
4861
vec![],
4962
),
@@ -56,7 +69,7 @@ impl Triangulation {
5669
/// Create a new triangulation from an existing `Mesh`, cloning the specified [`Layer`].
5770
pub fn from_mesh(mesh: &Mesh, layer: u8) -> Triangulation {
5871
Self {
59-
inner: GeoPolygon::new(LineString::new(Vec::new()), vec![]),
72+
inner: geo::Polygon::new(LineString::new(Vec::new()), vec![]),
6073
prebuilt: None,
6174
base_layer: Some(mesh.layers[layer as usize].clone()),
6275
agent_radius: AgentRadius::None,
@@ -66,7 +79,7 @@ impl Triangulation {
6679
/// Create a new triangulation from an existing `Layer` of a [`Mesh`].
6780
pub fn from_mesh_layer(layer: Layer) -> Triangulation {
6881
Self {
69-
inner: GeoPolygon::new(LineString::new(Vec::new()), vec![]),
82+
inner: geo::Polygon::new(LineString::new(Vec::new()), vec![]),
7083
prebuilt: None,
7184
base_layer: Some(layer),
7285
agent_radius: AgentRadius::None,
@@ -142,10 +155,12 @@ impl Triangulation {
142155
&mut self,
143156
obstacles: impl IntoIterator<Item = impl IntoIterator<Item = Vec2>>,
144157
) {
145-
let (exterior, interiors) =
146-
std::mem::replace(&mut self.inner, GeoPolygon::new(LineString(vec![]), vec![]))
147-
.into_inner();
148-
self.inner = GeoPolygon::new(
158+
let (exterior, interiors) = std::mem::replace(
159+
&mut self.inner,
160+
geo::Polygon::new(LineString(vec![]), vec![]),
161+
)
162+
.into_inner();
163+
self.inner = geo::Polygon::new(
149164
exterior,
150165
interiors
151166
.into_iter()
@@ -207,7 +222,7 @@ impl Triangulation {
207222
}
208223

209224
let exterior = self.inner.exterior().clone();
210-
let mut inner = std::mem::replace(&mut self.inner, GeoPolygon::new(exterior, vec![]));
225+
let mut inner = std::mem::replace(&mut self.inner, geo::Polygon::new(exterior, vec![]));
211226
match self.agent_radius {
212227
AgentRadius::Obstacles(radius, segments, simplification) if radius > 1.0e-5 => {
213228
inner = inner.inflate_obstacles(radius, segments as u32, simplification);
@@ -349,7 +364,7 @@ impl Triangulation {
349364
})
350365
.unwrap_or(true)
351366
&& !inner.interiors().iter().any(|obstacle| {
352-
GeoPolygon::new(obstacle.clone(), vec![]).contains(&center)
367+
geo::Polygon::new(obstacle.clone(), vec![]).contains(&center)
353368
})))
354369
.then(|| {
355370
#[cfg(feature = "tracing")]

Diff for: src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ use vendored_spade as spade;
5454

5555
#[cfg(feature = "async")]
5656
pub use async_helpers::FuturePath;
57+
pub use geo;
5758
pub use input::polyanya_file::PolyanyaFile;
5859
pub use input::triangulation::Triangulation;
5960
pub use input::trimesh::Trimesh;

0 commit comments

Comments
 (0)