Trait bevy_math::sampling::shape_sampling::ShapeSample

source ·
pub trait ShapeSample {
    type Output;

    // Required methods
    fn sample_interior<R: Rng + ?Sized>(&self, rng: &mut R) -> Self::Output;
    fn sample_boundary<R: Rng + ?Sized>(&self, rng: &mut R) -> Self::Output;

    // Provided methods
    fn interior_dist(self) -> impl Distribution<Self::Output>
       where Self: Sized { ... }
    fn boundary_dist(self) -> impl Distribution<Self::Output>
       where Self: Sized { ... }
}
Expand description

Exposes methods to uniformly sample a variety of primitive shapes.

Required Associated Types§

source

type Output

The type of vector returned by the sample methods, Vec2 for 2D shapes and Vec3 for 3D shapes.

Required Methods§

source

fn sample_interior<R: Rng + ?Sized>(&self, rng: &mut R) -> Self::Output

Uniformly sample a point from inside the area/volume of this shape, centered on 0.

Shapes like Cylinder, Capsule2d and Capsule3d are oriented along the y-axis.

§Example
let square = Rectangle::new(2.0, 2.0);

// Returns a Vec2 with both x and y between -1 and 1.
println!("{:?}", square.sample_interior(&mut rand::thread_rng()));
source

fn sample_boundary<R: Rng + ?Sized>(&self, rng: &mut R) -> Self::Output

Uniformly sample a point from the surface of this shape, centered on 0.

Shapes like Cylinder, Capsule2d and Capsule3d are oriented along the y-axis.

§Example
let square = Rectangle::new(2.0, 2.0);

// Returns a Vec2 where one of the coordinates is at ±1,
//  and the other is somewhere between -1 and 1.
println!("{:?}", square.sample_boundary(&mut rand::thread_rng()));

Provided Methods§

source

fn interior_dist(self) -> impl Distribution<Self::Output>
where Self: Sized,

Extract a Distribution whose samples are points of this shape’s interior, taken uniformly.

§Example
let square = Rectangle::new(2.0, 2.0);
let rng = rand::thread_rng();

// Iterate over points randomly drawn from `square`'s interior:
for random_val in square.interior_dist().sample_iter(rng).take(5) {
    println!("{:?}", random_val);
}
source

fn boundary_dist(self) -> impl Distribution<Self::Output>
where Self: Sized,

Extract a Distribution whose samples are points of this shape’s boundary, taken uniformly.

§Example
let square = Rectangle::new(2.0, 2.0);
let rng = rand::thread_rng();

// Iterate over points randomly drawn from `square`'s boundary:
for random_val in square.boundary_dist().sample_iter(rng).take(5) {
    println!("{:?}", random_val);
}

Object Safety§

This trait is not object safe.

Implementors§