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 { ... }
}
Available on crate feature
rand
only.Expand description
Exposes methods to uniformly sample a variety of primitive shapes.
Required Associated Types§
Required Methods§
Sourcefn sample_interior<R: Rng + ?Sized>(&self, rng: &mut R) -> Self::Output
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()));
Sourcefn sample_boundary<R: Rng + ?Sized>(&self, rng: &mut R) -> Self::Output
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§
Sourcefn interior_dist(self) -> impl Distribution<Self::Output>where
Self: Sized,
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);
}
Sourcefn boundary_dist(self) -> impl Distribution<Self::Output>where
Self: Sized,
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);
}
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.