From f3f56ed5f183f8461cc91cb6430d97c725ba159c Mon Sep 17 00:00:00 2001 From: Julian T Date: Sun, 7 Feb 2021 00:37:58 +0100 Subject: Move to double to avoid rounding error This rounding errors seems to happen when adding floats very close to 1 to a relatively large number. 325.0 + 0.99999034 = 326.0 This is a problem as this sample should be counted at the 325 pixel. However this will be a lesser problem when filtering is implemented. --- src/camera/film.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src/camera') diff --git a/src/camera/film.rs b/src/camera/film.rs index 2ff7239..7a7f2dc 100644 --- a/src/camera/film.rs +++ b/src/camera/film.rs @@ -132,11 +132,14 @@ impl FilmTile { pub fn add_sample(&mut self, inp: &Vector2f, c: Spectrum) { let point = Vector2i::from(inp.floor()); // Subtract the offset - let point = point - self.bounds.min; + let point = (point - self.bounds.min).cap(self.size.x-1, self.size.y-1); let index = point.x + point.y * self.size.x; - let pixel = self.pixels.get_mut(index as usize).unwrap(); - pixel.add(&c, 1.0); + if let Some(pixel) = self.pixels.get_mut(index as usize) { + pixel.add(&c, 1.0); + } else { + println!("Could not get pixel {} inp: {}, index: {}", point, inp, index); + } } } -- cgit v1.2.3