~ttt/ray_tracing_in_one_weekend

aff4611255fd776b5f7f4e41d5fc4a7dd9a2e201 — Tomasz Kłak 2 years ago 569eea8 master
cleanup
3 files changed, 21 insertions(+), 11 deletions(-)

M src/image.rs
M src/sphere.rs
M src/vec3.rs
M src/image.rs => src/image.rs +17 -7
@@ 1,4 1,4 @@
use crate::{Color};
use crate::Color;
use std::collections::HashMap;
use std::sync::Mutex;



@@ 14,7 14,8 @@ impl Image {
        Self {
            pixels: Default::default(),
            samples_per_pixel,
            w, h
            w,
            h,
        }
    }



@@ 26,9 27,15 @@ impl Image {
    }

    pub fn to_ppm(&self, out: &mut impl std::io::Write) -> std::io::Result<()> {
        let mut pixels : Vec<((_, _), Color)> = self.pixels.lock().unwrap().iter().map(|(p, c)| (p.clone(), c.clone())).collect();
        pixels.sort_by(|l, r| l.0.0.cmp(&r.0.0));
        pixels.sort_by(|l, r| r.0.1.cmp(&l.0.1));
        let mut pixels: Vec<((_, _), Color)> = self
            .pixels
            .lock()
            .unwrap()
            .iter()
            .map(|(p, c)| (*p, *c))
            .collect();
        pixels.sort_by(|l, r| l.0 .0.cmp(&r.0 .0));
        pixels.sort_by(|l, r| r.0 .1.cmp(&l.0 .1));
        write!(out, "P3\n{} {}\n255\n", self.w, self.h)?;
        for p in pixels {
            write_color(out, p.1, self.samples_per_pixel)?;


@@ 37,7 44,11 @@ impl Image {
    }
}

fn write_color(out: &mut impl std::io::Write, pixel_color: Color, samples_per_pixel: usize) -> std::io::Result<()> {
fn write_color(
    out: &mut impl std::io::Write,
    pixel_color: Color,
    samples_per_pixel: usize,
) -> std::io::Result<()> {
    let mut r = pixel_color.x();
    let mut g = pixel_color.y();
    let mut b = pixel_color.z();


@@ 56,4 67,3 @@ fn write_color(out: &mut impl std::io::Write, pixel_color: Color, samples_per_pi
        (256.0 * b.clamp(0.0, 0.999)).round() as usize,
    )
}


M src/sphere.rs => src/sphere.rs +1 -1
@@ 64,6 64,6 @@ impl Hittable for Sphere {
            }
        }

        return None;
        None
    }
}

M src/vec3.rs => src/vec3.rs +3 -3
@@ 40,7 40,7 @@ impl Vec3 {
        Self::new(
            random_double_range(r.clone()),
            random_double_range(r.clone()),
            random_double_range(r.clone()),
            random_double_range(r),
        )
    }



@@ 89,7 89,7 @@ impl std::ops::IndexMut<usize> for Vec3 {
impl std::ops::Add<Vec3> for Vec3 {
    type Output = Vec3;
    fn add(self, rhs: Vec3) -> Vec3 {
        let mut c = self.clone();
        let mut c = self;
        c += rhs;
        c
    }


@@ 170,7 170,7 @@ impl std::ops::Div<f64> for Vec3 {
    type Output = Vec3;

    fn div(self, rhs: f64) -> Vec3 {
        let mut c = self.clone();
        let mut c = self;
        c *= 1f64 / rhs;
        c
    }