Non-collecting parallel iterators

This commit is contained in:
Michael Zhang 2023-01-31 17:56:36 -06:00
parent 3b04a7c494
commit 9aa823971e

View file

@ -15,9 +15,7 @@ use anyhow::Result;
use clap::Parser; use clap::Parser;
use itertools::Itertools; use itertools::Itertools;
use ordered_float::NotNan; use ordered_float::NotNan;
use rayon::prelude::{ use rayon::prelude::{IntoParallelIterator, ParallelIterator};
IndexedParallelIterator, IntoParallelIterator, ParallelIterator,
};
use crate::image::Image; use crate::image::Image;
use crate::input_file::parse_input_file; use crate::input_file::parse_input_file;
@ -26,6 +24,7 @@ use crate::scene_data::Object;
use crate::vec3::Vec3; use crate::vec3::Vec3;
use crate::view::Rect; use crate::view::Rect;
/// Viewing distance
const ARBITRARY_D: f64 = 2.0; const ARBITRARY_D: f64 = 2.0;
/// Simple raycaster. /// Simple raycaster.
@ -103,15 +102,13 @@ fn main() -> Result<()> {
} }
}; };
// This is an L because par_bridge is unordered let pixels_iter = (0..scene.image_height)
let pixels = (0..scene.image_height) .into_par_iter()
.cartesian_product(0..scene.image_width) .flat_map(|x| (0..scene.image_width).into_par_iter().map(move |y| (y, x)));
.collect_vec();
// Loop through every single pixel of the output file // Loop through every single pixel of the output file
let pixels = pixels let pixels = pixels_iter
.into_par_iter() .map(|(px, py)| {
.map(|(py, px)| {
let pixel_in_space = translate_pixel(px, py); let pixel_in_space = translate_pixel(px, py);
let ray = Ray::from_endpoints(scene.eye_pos, pixel_in_space); let ray = Ray::from_endpoints(scene.eye_pos, pixel_in_space);
@ -140,6 +137,7 @@ fn main() -> Result<()> {
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
// Construct and emit image
let image = Image { let image = Image {
width: scene.image_width, width: scene.image_width,
height: scene.image_height, height: scene.image_height,