Struct rustic_zen::sampler::Sampler

source ·
pub struct Sampler<T: Copy, R: Rng> { /* private fields */ }
Expand description

Wrapper for a bounded stocastically sampled value

a Sampler encloses the distribution function and bounds for a random variable of type T, such that is can be sampled with an external random number generator of type R

Several constructors are provided for common distributions of values, however if a custom one is needed it can be provided as a closure and set of bounds to Sampler::from_fn

Implementations§

source§

impl<T, R> Sampler<T, R>
where R: Rng, T: Copy + Send + Sync + 'static,

source

pub fn new_const<A>(c: A) -> Self
where A: Into<T>,

Creates a new Sampler with a constant distibution.

When sampled the Sampler will always return c

source§

impl<T, R> Sampler<T, R>
where R: Rng, T: Copy + Float + SampleUniform + Send + Sync + 'static,

source

pub fn new_range<A, B>(a: A, b: B) -> Self
where A: Into<T>, B: Into<T>,

Creates a new Sampler with a uniform distibution.

When sampled the Sampler will always return a value between a and b (inclusive)

source§

impl<T, R> Sampler<T, R>
where R: Rng, T: Copy + Float + From<f32> + Send + Sync + 'static, StandardNormal: Distribution<T>,

source

pub fn new_gaussian<A, B>(mean: A, std_dev: B) -> Self
where A: Copy + Into<T>, B: Copy + Into<T>,

Creates a new Sampler with a normal (gaussian) distibution.

Real gaussian’s have infinate bounds, this can cause problems in the raytracing engine, so this implementation wraps at 3 standard deviations.

When sampled the Sampler will always return a value between mean - 3 * std_dev and mean + 3 * std_dev (inclusive)

source§

impl<T, R> Sampler<T, R>
where R: Rng, T: Copy + Float + SampleUniform + FromPrimitive + From<f32> + Send + Sync + 'static,

source

pub fn new_blackbody<A>(temperature: A) -> Self
where A: Into<T>,

Creates a new Sampler with a blackbody radiation curve distibution.

This is only really useful for creating realistic white lights of a given colour temprature, or stars.

This sampler should be used carefully; it can lead to unexpected results when used on a value other than Light.wavelength. It is also substancially slower than the other Sampler types. When Sampled it will return a value between 0 and T::MAX (probably larger than you want) This can cause all sorts of weird problems for collision detection, so please don’t use this on a SamplerPoint

source§

impl<T, R> Sampler<T, R>
where T: Copy, R: Rng,

source

pub fn from_fn( f: Arc<dyn Fn(&mut R) -> T + Send + Sync>, lower: T, upper: T ) -> Self

Creates a new Sampler with a distribution dictated by f

f must always return a value within the bounds specified by lower & upper

This is very low level access to how the renderer works; you’re in control, if you break it it’s your fault!

source

pub fn sample(&self, rng: &mut R) -> T

Get a sampled value from this Sampler using random number generator rng

source

pub fn bounds(&self) -> (T, T)

Get the bounds of a possible sampled value (useful for spacial partitioning and limit checks etc)

Trait Implementations§

source§

impl<T: Clone + Copy, R: Clone + Rng> Clone for Sampler<T, R>

source§

fn clone(&self) -> Sampler<T, R>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T, R> From<(T, T)> for Sampler<T, R>
where T: Copy + Float + SampleUniform + Send + Sync + From<f32> + 'static, R: Rng,

A tuple of type (T,T) will be interpreted as a uniform distribution if turned into a sampler.

§Example:

extern crate rand;

use rustic_zen::sampler::Sampler;
use rand::prelude::*;
let mut r = rand::thread_rng();

let s: Sampler<f64, ThreadRng> = (5.0,10.0).into();

assert!(s.sample(&mut r) >= 5.0); // will always be true
assert!(s.sample(&mut r) <= 10.0); // will always be true
source§

fn from(value: (T, T)) -> Self

Converts to this type from the input type.
source§

impl<T, R> From<T> for Sampler<T, R>
where T: Copy + From<T> + Send + Sync + 'static, R: Rng,

A single value of type T will be interpreted as a constant distribution if turned into a sampler.

§Example:

extern crate rand;

use rustic_zen::sampler::Sampler;
use rand::prelude::*;
let mut r = rand::thread_rng();

let s: Sampler<f64, ThreadRng> = 5.0.into();

assert_eq!(s.sample(&mut r), 5.0); // will always be true
source§

fn from(value: T) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<T, R> Freeze for Sampler<T, R>
where T: Freeze,

§

impl<T, R> !RefUnwindSafe for Sampler<T, R>

§

impl<T, R> Send for Sampler<T, R>
where T: Send,

§

impl<T, R> Sync for Sampler<T, R>
where T: Sync,

§

impl<T, R> Unpin for Sampler<T, R>
where T: Unpin,

§

impl<T, R> !UnwindSafe for Sampler<T, R>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<!> for T

source§

fn from(t: !) -> T

Converts to this type from the input type.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Pointable for T

source§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V