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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use crate::{Float, Vec2, rand_utils};
/// Choose each sample point uniformly at random
pub struct UniformSampler {
/// How many samples have been given?
state: i32,
/// How many samples was asked?
samples: i32,
}
impl UniformSampler {
/// Constructs an uniform sampler with `samples` samples
#[allow(dead_code)]
pub fn new(samples: i32) -> Self {
Self { samples, state: 0 }
}
}
impl Iterator for UniformSampler {
type Item = Vec2;
fn next(&mut self) -> Option<Self::Item> {
if self.state == self.samples {
None
} else {
self.state += 1;
Some(rand_utils::unit_square())
}
}
}
/// Divide unit square to `n`x`n` strata and provide one sample from each strata.
pub struct JitteredSampler {
/// Width of one strata
scale: Float,
/// How many samples have been given?
state: i32,
/// How many strata per dimension?
strata_dim: i32,
/// How many samples have been asked for? Should be a square,
/// otherwise gets rounded down to the nearest square.
samples: i32,
}
impl JitteredSampler {
/// Constructs a jittered sampler with `floor(sqrt(samples))^2` samples
pub fn new(samples: i32) -> Self {
let dim = (samples as Float).sqrt() as i32;
Self {
scale: (dim as Float).recip(),
samples: dim * dim,
strata_dim: dim,
state: 0,
}
}
}
impl Iterator for JitteredSampler {
type Item = Vec2;
fn next(&mut self) -> Option<Self::Item> {
if self.state == self.samples {
None
} else {
let offset = self.scale
* Vec2::new(
(self.state % self.strata_dim) as Float,
(self.state / self.strata_dim) as Float,
);
self.state += 1;
Some(self.scale * rand_utils::unit_square() + offset)
}
}
}