Skip to content

Commit

Permalink
Implement ShapeSample for Extrusion<T> (#13567)
Browse files Browse the repository at this point in the history
# Objective

- Implement `ShapeSample` for `Extrusion<T>`
  • Loading branch information
lynn-lumen authored May 30, 2024
1 parent 4065098 commit 32a44e1
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions crates/bevy_math/src/sampling/shape_sampling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,34 @@ impl ShapeSample for Capsule3d {
}
}

impl<P: Primitive2d + Measured2d + ShapeSample<Output = Vec2>> ShapeSample for Extrusion<P> {
type Output = Vec3;

fn sample_interior<R: Rng + ?Sized>(&self, rng: &mut R) -> Self::Output {
let base_point = self.base_shape.sample_interior(rng);
let depth = rng.gen_range(-self.half_depth..self.half_depth);
base_point.extend(depth)
}

fn sample_boundary<R: Rng + ?Sized>(&self, rng: &mut R) -> Self::Output {
let base_area = self.base_shape.area();
let total_area = self.area();

let random = rng.gen_range(0.0..total_area);
match random {
x if x < base_area => self.base_shape.sample_interior(rng).extend(self.half_depth),
x if x < 2. * base_area => self
.base_shape
.sample_interior(rng)
.extend(-self.half_depth),
_ => self
.base_shape
.sample_boundary(rng)
.extend(rng.gen_range(-self.half_depth..self.half_depth)),
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 32a44e1

Please sign in to comment.