From 9aa823971ef63ddeaeed14aba02b5f50058d7fff Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Tue, 31 Jan 2023 17:56:36 -0600 Subject: [PATCH] Non-collecting parallel iterators --- assignment-1/src/main.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/assignment-1/src/main.rs b/assignment-1/src/main.rs index 4b910cc..a3f3a3e 100644 --- a/assignment-1/src/main.rs +++ b/assignment-1/src/main.rs @@ -15,9 +15,7 @@ use anyhow::Result; use clap::Parser; use itertools::Itertools; use ordered_float::NotNan; -use rayon::prelude::{ - IndexedParallelIterator, IntoParallelIterator, ParallelIterator, -}; +use rayon::prelude::{IntoParallelIterator, ParallelIterator}; use crate::image::Image; use crate::input_file::parse_input_file; @@ -26,6 +24,7 @@ use crate::scene_data::Object; use crate::vec3::Vec3; use crate::view::Rect; +/// Viewing distance const ARBITRARY_D: f64 = 2.0; /// Simple raycaster. @@ -103,15 +102,13 @@ fn main() -> Result<()> { } }; - // This is an L because par_bridge is unordered - let pixels = (0..scene.image_height) - .cartesian_product(0..scene.image_width) - .collect_vec(); + let pixels_iter = (0..scene.image_height) + .into_par_iter() + .flat_map(|x| (0..scene.image_width).into_par_iter().map(move |y| (y, x))); // Loop through every single pixel of the output file - let pixels = pixels - .into_par_iter() - .map(|(py, px)| { + let pixels = pixels_iter + .map(|(px, py)| { let pixel_in_space = translate_pixel(px, py); let ray = Ray::from_endpoints(scene.eye_pos, pixel_in_space); @@ -140,6 +137,7 @@ fn main() -> Result<()> { }) .collect::>(); + // Construct and emit image let image = Image { width: scene.image_width, height: scene.image_height,