Skip to main content

ShapeSample

Trait ShapeSample 

Source
pub trait ShapeSample {
    type Output;

    // Required methods
    fn sample_interior<R: RngExt + ?Sized>(&self, rng: &mut R) -> Self::Output;
    fn sample_boundary<R: RngExt + ?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: RngExt + ?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::rng()));
Source

fn sample_boundary<R: RngExt + ?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::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::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::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);
}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§