-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1b17616
commit 15d5513
Showing
5 changed files
with
90 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod independent_sampler; | ||
pub mod stratified_sampler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use crate::sampler::Sampler; | ||
use fastrand::Rng; | ||
|
||
pub struct StratifiedSampler { | ||
rng: Rng, | ||
with_jittering: bool, | ||
samples_2d: Vec<Vec<[f64; 2]>>, // (nb_samples, nb_2d) | ||
current_sample: usize, | ||
current_2d_dim: usize, | ||
} | ||
|
||
impl StratifiedSampler { | ||
fn new(seed: u64) -> Self { | ||
StratifiedSampler { | ||
rng: fastrand::Rng::with_seed(seed), | ||
with_jittering: true, | ||
samples_2d: Vec::new(), | ||
current_sample: 0, | ||
current_2d_dim: 0, | ||
} | ||
} | ||
|
||
fn new_without_jittering(seed: u64) -> Self { | ||
StratifiedSampler { | ||
rng: fastrand::Rng::with_seed(seed), | ||
with_jittering: false, | ||
samples_2d: Vec::new(), | ||
current_sample: 0, | ||
current_2d_dim: 0, | ||
} | ||
} | ||
} | ||
|
||
impl Sampler for StratifiedSampler { | ||
fn prepare(&mut self, nb_1d: usize, nb_2d: usize, nb_samples: usize) { | ||
// TODO 1D | ||
// TODO several 2d dimensions | ||
assert_eq!(nb_2d, 1); | ||
|
||
let root = (nb_samples as f64).sqrt() as usize; | ||
assert_eq!(root * root, nb_samples); | ||
|
||
let mut samples_2d = Vec::new(); | ||
|
||
for i in 0..root { | ||
for j in 0..root { | ||
// Not 0.5 to prevent rays from being parallel to the walls | ||
// TODO 0.5 | ||
let (dx, dy) = if self.with_jittering { | ||
(self.rng.f64(), self.rng.f64()) | ||
} else { | ||
(0.501, 0.501) | ||
}; | ||
|
||
samples_2d.push(vec![[ | ||
(i as f64 + dx) / root as f64, | ||
(j as f64 + dy) / root as f64, | ||
]]); | ||
} | ||
} | ||
|
||
self.samples_2d = samples_2d; | ||
self.current_sample = 0; | ||
self.current_2d_dim = 0; | ||
} | ||
|
||
fn new_sample(&mut self) { | ||
if self.current_2d_dim != 0 { | ||
// TODO and 1D dimension | ||
self.current_sample += 1; | ||
self.current_2d_dim = 0; | ||
} | ||
} | ||
|
||
fn next1d(&mut self) -> f64 { | ||
unimplemented!(); | ||
} | ||
|
||
fn next2d(&mut self) -> [f64; 2] { | ||
self.current_2d_dim += 1; | ||
self.samples_2d[self.current_sample][self.current_2d_dim - 1] | ||
} | ||
} |