From 594e767586494e821009f18fe8cbd96fe1b3703e Mon Sep 17 00:00:00 2001 From: salam Date: Wed, 13 Nov 2024 02:47:29 +0900 Subject: [PATCH] add: `parallel` feature --- Cargo.toml | 14 +++++--------- src/utils.rs | 38 +++++++++++++++++++++++++++----------- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 538943d..6b0db3b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,24 +16,20 @@ cast_precision_loss = { level = "allow", priority = 1 } pedantic = { level = "warn", priority = 0 } [features] -default = ["avian2d", "rapier2d"] +default = ["avian2d", "rapier2d", "parallel"] avian2d = ["dep:avian2d"] rapier2d = ["dep:bevy_rapier2d"] +parallel = ["dep:rayon"] [dependencies] -rayon = "1.10.0" edges = "0.4.0" bevy_rapier2d = { version = "0.27.0", optional = true } avian2d = { version = "0.1.0", optional = true } +bevy_math = { version = "0.14", default-features = false } +bevy_render = { version = "0.14", default-features = false } +rayon = { version = "1.10.0", optional = true } thiserror = "2.0.3" -[dependencies.bevy_math] -version = "0.14" -default-features = false - -[dependencies.bevy_render] -version = "0.14" -default-features = false [dev-dependencies] bevy = "0.14" diff --git a/src/utils.rs b/src/utils.rs index 23044bf..938f8b0 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,19 +1,33 @@ use bevy_render::prelude::Image; use edges::{Edges, Vec2}; + +#[cfg(feature = "parallel")] use rayon::prelude::*; +macro_rules! into_par_iter { + ($t: expr) => {{ + #[cfg(not(feature = "parallel"))] + let it = $t.into_iter(); + + #[cfg(feature = "parallel")] + let it = $t.into_par_iter(); + it + }}; +} + #[cfg(feature = "avian2d")] pub mod avian2d { use super::{heights_and_scale, Vec2}; use avian2d::parry::{math::Point, shape::SharedShape}; + #[cfg(feature = "parallel")] use rayon::prelude::*; pub use avian2d::prelude::Collider; /// Generate `convex_polyline` collider from the points, pub fn convex_polyline_collider(points: Vec) -> Option { - SharedShape::convex_polyline(points.into_par_iter().map(Point::from).collect()) + SharedShape::convex_polyline(into_par_iter!(points).map(Point::from).collect()) .map(Collider::from) } @@ -58,10 +72,16 @@ pub mod rapier2d { /// takes x,y points collects the y values at the top of the image (biggest y) fn heights_and_scale(mut points: Vec) -> (Vec, Vec2) { points.sort_by(|p1, p2| p1.x.partial_cmp(&p2.x).unwrap()); - let heights = points - .par_chunk_by(|p1, p2| (p1.x - p2.x).abs() <= f32::EPSILON) - .map(|chunk| chunk.iter().map(|p| p.y).reduce(f32::max).unwrap()) - .collect::>(); + let heights = { + #[cfg(not(feature = "parallel"))] + let chunk = points.chunk_by(|p1, p2| (p1.x - p2.x).abs() <= f32::EPSILON); + + #[cfg(feature = "parallel")] + let chunk = points.par_chunk_by(|p1, p2| (p1.x - p2.x).abs() <= f32::EPSILON); + chunk + } + .map(|chunk| chunk.iter().map(|p| p.y).reduce(f32::max).unwrap()) + .collect::>(); let x_scale = heights.len() - 1; (heights, Vec2::new(x_scale as f32, 1.0)) @@ -74,9 +94,7 @@ where { let edges = Edges::from(image); collider_fn( - edges - .image_edges(translated) - .into_par_iter() + into_par_iter!(edges.image_edges(translated)) .flatten() .collect(), ) @@ -89,9 +107,7 @@ where R: Send, { let edges = Edges::from(image); - edges - .image_edges(translated) - .into_par_iter() + into_par_iter!(edges.image_edges(translated)) .map(collider_fn) .collect() }