Skip to content

Commit

Permalink
add: parallel feature
Browse files Browse the repository at this point in the history
  • Loading branch information
salam99823 committed Nov 12, 2024
1 parent 554404b commit 594e767
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
14 changes: 5 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
38 changes: 27 additions & 11 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -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<Vec2>) -> Option<Collider> {
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)
}

Expand Down Expand Up @@ -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<Vec2>) -> (Vec<f32>, 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::<Vec<f32>>();
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::<Vec<f32>>();

let x_scale = heights.len() - 1;
(heights, Vec2::new(x_scale as f32, 1.0))
Expand All @@ -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(),
)
Expand All @@ -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()
}

0 comments on commit 594e767

Please sign in to comment.