Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aagu/replace rust-geo-booleanop with geoclipper #41

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.idea/
/target
/Cargo.lock
TODO.md
9 changes: 6 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
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,9 +29,10 @@ smallvec = { version = "1.9", features = ["union", "const_generics"] }
bvh2d = { version = "0.3", git = "https://github.com/mockersf/bvh2d" }
serde = { version = "1.0", features = ["derive"], optional = true }
spade = "2.2"
geo = "0.26.0"
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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