From ddebd55fcdf17e68f16e51d30af58bece8a7c9d3 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Mon, 6 Apr 2020 15:23:41 +0200 Subject: [PATCH] library: render: utils: use Unit for vectors --- pathtracer/src/render/utils.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/pathtracer/src/render/utils.rs b/pathtracer/src/render/utils.rs index 905f117..4ed4c3a 100644 --- a/pathtracer/src/render/utils.rs +++ b/pathtracer/src/render/utils.rs @@ -72,8 +72,7 @@ impl RefractionInfo { /// Returns a random ray in the hemisphere described by a normal unit-vector, and the probability /// to have picked that direction. -#[allow(unused)] // FIXME: remove once used -pub fn sample_hemisphere(normal: Vector) -> (Vector, f32) { +pub fn sample_hemisphere(normal: Unit) -> (Unit, f32) { let mut rng = thread_rng(); let azimuth = rng.gen::() * std::f32::consts::PI * 2.; // Cosine weighted importance sampling @@ -94,11 +93,11 @@ pub fn sample_hemisphere(normal: Vector) -> (Vector, f32) { let normal_b = normal.cross(&normal_t); // Perform the matrix calculation by hand... - let scattered = Vector::new( + let scattered = Unit::new_normalize(Vector::new( x * normal_b.x + y * normal.x + z * normal_t.x, x * normal_b.y + y * normal.y + z * normal_t.y, x * normal_b.z + y * normal.z + z * normal_t.z, - ); + )); // The probability to have picked the ray is inversely proportional to cosine of the angle with // the normal @@ -133,7 +132,7 @@ mod test { // NOTE(Bruno): should use some test-case generation for failure-reproduction purposes... let mut rng = thread_rng(); for _ in 0..100 { - let normal = Vector::new(rng.gen(), rng.gen(), rng.gen()); + let normal = Unit::new_normalize(Vector::new(rng.gen(), rng.gen(), rng.gen())); for _ in 0..100 { let (sample, proportion) = sample_hemisphere(normal); let cos_angle = normal.dot(&sample);