aboutsummaryrefslogtreecommitdiff
path: root/src/sample/mod.rs
blob: 1a539218228e33b27781dec4f0f0b598e550c5eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use crate::{M_PI, Float};
use crate::core::{Vector3f, Vector2f};

mod uniform;

pub use uniform::UniformSampler;

fn distribute_between(x: Float, a: Float, b: Float) -> Float {
    x * (b - a) + a
}

pub trait Sampler {
    fn get_sample(&mut self) -> Float;

    fn get_sample_2d(&mut self) -> Vector2f {
        Vector2f::new_xy(self.get_sample(), self.get_sample())
    }

    fn clone_and_seed(&mut self) -> Box<dyn Sampler + Send>;

    fn get_unit_vector(&mut self) -> Vector3f {
        let s2d = self.get_sample_2d();

        let lambda = distribute_between(s2d.x, -M_PI, M_PI);
        let costheta = 2.0 * s2d.y - 1.0;
        let sintheta = costheta.acos().sin();

        Vector3f::new_xyz(
            lambda.cos() * sintheta,
            lambda.sin() * sintheta,
            costheta,
            )
    }

    fn get_in_circle(&mut self) -> Vector2f {
        let s2d = self.get_sample_2d();

        let d = s2d.x.sqrt();
        let theta = s2d.y * 2.0 * M_PI;

        Vector2f::new_xy(
            d * theta.cos(),
            d * theta.sin(),
            )
    }
}