Add UniformHemisphere random distribution
This commit is contained in:
parent
41cc032bed
commit
8badbaa3ca
|
|
@ -4,6 +4,9 @@ pub use uniform_square::UniformSquare;
|
|||
mod unit_disc;
|
||||
pub use unit_disc::UnitDisc;
|
||||
|
||||
mod uniform_hemisphere;
|
||||
pub use uniform_hemisphere::UniformHemisphere;
|
||||
|
||||
pub trait RandomDistribution<T> {
|
||||
fn value(&self) -> T;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,48 @@
|
|||
use rand::distributions::{Open01, OpenClosed01};
|
||||
use rand::{thread_rng, Rng};
|
||||
|
||||
use crate::math::Vec3;
|
||||
|
||||
use super::RandomDistribution;
|
||||
|
||||
pub struct UniformHemisphere {}
|
||||
|
||||
impl UniformHemisphere {
|
||||
pub fn new() -> UniformHemisphere {
|
||||
UniformHemisphere {}
|
||||
}
|
||||
}
|
||||
|
||||
impl RandomDistribution<Vec3> for UniformHemisphere {
|
||||
fn value(&self) -> Vec3 {
|
||||
let mut rng = thread_rng();
|
||||
let mut result = Vec3::new(
|
||||
2.0 * rng.sample::<f64, _>(Open01) - 1.0,
|
||||
2.0 * rng.sample::<f64, _>(Open01) - 1.0,
|
||||
rng.sample::<f64, _>(OpenClosed01),
|
||||
);
|
||||
while result.norm_squared() > 1.0 {
|
||||
result = Vec3::new(
|
||||
2.0 * rng.sample::<f64, _>(Open01) - 1.0,
|
||||
2.0 * rng.sample::<f64, _>(Open01) - 1.0,
|
||||
rng.sample::<f64, _>(OpenClosed01),
|
||||
);
|
||||
}
|
||||
result.normalize()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn print_values() {
|
||||
let target = UniformHemisphere::new();
|
||||
for _ in 0..1000 {
|
||||
let value = target.value();
|
||||
println!("{}, {}, {}", value.x(), value.y(), value.z());
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue