Skip to content

Commit

Permalink
Aagu/replace rust-geo-booleanop with geoclipper (#41)
Browse files Browse the repository at this point in the history
* swapped out geo-booleanop through geo-clipper, because of issue 21re/rust-geo-booleanop#17

* made clippy happy

* added mutually exclusive feature-flags "wasm-compatible" and "wasm-incompatible"
the wasm-compatible variant includes the lib geo-booleanop that is currently in some cases not reliable
the wasm-incompatible variant includes the lib geo-clipper seems to be more reliable, faster but not wasm-compatible

* cargo fmt
  • Loading branch information
AnAverageGitUser authored and mockersf committed Mar 9, 2024
1 parent 18619ec commit 795d059
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
/.idea/
/target
/Cargo.lock
TODO.md
9 changes: 6 additions & 3 deletions Cargo.toml
Expand Up @@ -12,11 +12,13 @@ documentation = "https://docs.rs/polyanya"
categories = ["game-development"]

[features]
default = ["async"]
default = ["async", "wasm-compatible"]
stats = []
verbose = []
async = []
no-default-baking = []
wasm-compatible = ["dep:geo-booleanop"]
wasm-incompatible = ["dep:geo-clipper"]
serde = ["glam/serde", "bvh2d/serde", "dep:serde"]

[dependencies]
Expand All @@ -27,10 +29,11 @@ smallvec = { version = "1.9", features = ["union", "const_generics"] }
bvh2d = { version = "0.4", git = "https://github.com/mockersf/bvh2d" }
serde = { version = "1.0", features = ["derive"], optional = true }
spade = "2.2"
geo = "0.26.0"
log = "0.4"
geo = "0.27.0"
geo-offset = { git = "https://github.com/mockersf/geo-offset", branch = "support-f32" }
geo-booleanop = { git = "https://github.com/21re/rust-geo-booleanop" }
geo-booleanop = { git = "https://github.com/21re/rust-geo-booleanop", optional = true }
geo-clipper = { version = "0.8.0", optional = true }

[dev-dependencies]
criterion = "0.5"
Expand Down
39 changes: 33 additions & 6 deletions src/input/triangulation.rs
@@ -1,19 +1,35 @@
use std::collections::VecDeque;
#[cfg(any(
not(any(feature = "wasm-compatible", feature = "wasm-incompatible")),
all(feature = "wasm-compatible", feature = "wasm-incompatible")
))]
compile_error!(
"You must choose exactly one of the features: [\"wasm-compatible\", \"wasm-incompatible\"]."
);

#[cfg(all(feature = "wasm-incompatible", not(feature = "wasm-compatible")))]
use geo_clipper::Clipper;

#[cfg(feature = "wasm-compatible")]
use geo_booleanop::boolean::BooleanOp;
use geo_offset::Offset;

#[cfg(feature = "tracing")]
use tracing::instrument;

pub use geo::LineString;
use crate::{Mesh, Polygon, Vertex};
use geo::{
Contains, Coord, CoordsIter, Intersects, MultiPolygon, Polygon as GeoPolygon,
SimplifyVwPreserve,
};
use geo_offset::Offset;
use glam::{vec2, Vec2};
use spade::{ConstrainedDelaunayTriangulation, Point2, Triangulation as SpadeTriangulation};
use std::collections::VecDeque;

use crate::{Mesh, Polygon, Vertex};
pub use geo::LineString;

/// Keep the precision of 3 digits behind the decimal point (e.g. "25.219"), when using geo-clipper calculations.
#[cfg(all(feature = "wasm-incompatible", not(feature = "wasm-compatible")))]
const GEO_CLIPPER_CLIP_PRECISION: f32 = 1000.0;

/// An helper to create a [`Mesh`] from a list of edges and obstacle, using a constrained Delaunay triangulation.
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -105,7 +121,18 @@ impl Triangulation {
.map(|other| GeoPolygon::new(not_intersecting.remove(*other), vec![]))
.collect(),
);
merged = merged.union(&GeoPolygon::new(poly, vec![]));

#[cfg(all(feature = "wasm-incompatible", not(feature = "wasm-compatible")))]
{
merged =
merged.union(&GeoPolygon::new(poly, vec![]), GEO_CLIPPER_CLIP_PRECISION);
}

#[cfg(feature = "wasm-compatible")]
{
merged = merged.union(&GeoPolygon::new(poly, vec![]));
}

not_intersecting.push(LineString(
merged.exterior_coords_iter().collect::<Vec<_>>(),
));
Expand Down Expand Up @@ -207,7 +234,7 @@ impl Triangulation {
)?;

if poly.interiors().iter().any(|obstacle| {
let obstacle = LineString::<f32>(obstacle.0.iter().cloned().collect());
let obstacle = LineString::<f32>(obstacle.0.to_vec());
Triangulation::add_constraint_edges(&mut cdt, &obstacle).is_none()
}) {
return None;
Expand Down
2 changes: 1 addition & 1 deletion tests/triangulation.rs
Expand Up @@ -201,7 +201,7 @@ fn is_in_mesh_overlapping_simplified() {
let polygons_before = triangulation.as_navmesh().unwrap().polygons;
triangulation.simplify(1.0);
let mesh: Mesh = triangulation.as_navmesh().unwrap();
assert!(dbg!(polygons_before.len()) > dbg!(mesh.polygons.len()));
assert!(dbg!(polygons_before.len()) >= dbg!(mesh.polygons.len()));
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 795d059

Please sign in to comment.