From 37b793287d1d0d773acac7dfee23fd447fcd935b Mon Sep 17 00:00:00 2001 From: dengqn Date: Tue, 12 Aug 2025 17:04:01 +0800 Subject: [PATCH] chore: clean code --- src/camera.rs | 16 +++++++++------- src/main.rs | 3 +-- src/material.rs | 2 +- src/math_utils.rs | 8 ++++---- src/ppm_writer.rs | 2 +- src/sphere.rs | 4 ++-- src/types_defined.rs | 17 ++++++++--------- src/write_file_util.rs | 12 ------------ 8 files changed, 26 insertions(+), 38 deletions(-) delete mode 100644 src/write_file_util.rs diff --git a/src/camera.rs b/src/camera.rs index 59ab37a..535e0d6 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -42,13 +42,13 @@ impl<'a> Camera<'a> { Camera { image_width, image_height, - aspect_ratio, - viewport_width, - viewport_height, + // aspect_ratio, + // viewport_width, + // viewport_height, camera_center, - focal_length, - viewport_u, - viewport_v, + // focal_length, + // viewport_u, + // viewport_v, viewport_u_delta, viewport_v_delta, viewport_top_left_pixel_center, @@ -89,10 +89,12 @@ impl<'a> Camera<'a> { clamp(color.z, 0.0, 1.0), ); - self.ppm_file_writer.write(color_clamped.to_color()); + let _ = self.ppm_file_writer.write(color_clamped.to_color()); } } + let _ = self.ppm_file_writer.finish(); + // img_content } diff --git a/src/main.rs b/src/main.rs index cfc1d51..7b23ee7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,6 @@ mod ray; mod sphere; mod types_defined; mod vec3; -mod write_file_util; mod math_utils; mod ppm_writer; mod material; @@ -50,7 +49,7 @@ fn camera_render() { let mut world = HittableList::new(); let plane_m = Some(MaterialKind::Lambertian(Lambertian{albedo: Color::new(0.899, 0.899, 0.999)})); - let plane2_m = Some(MaterialKind::Metal(Metal{albedo: Color::new(0.784,0.784,0.784)})); + // let plane2_m = Some(MaterialKind::Metal(Metal{albedo: Color::new(0.784,0.784,0.784)})); let center_m = Some(MaterialKind::Lambertian(Lambertian{albedo: Color::new(0.5, 0.5, 0.5)})); let left_m = Some(MaterialKind::Metal(Metal{albedo: Color::new(0.799, 0.599, 0.799)})); let left_behind_m = Some(MaterialKind::Lambertian(Lambertian{albedo: Color::new(0.799, 0.599, 0.599)})); diff --git a/src/material.rs b/src/material.rs index d741b5d..fe1fd62 100644 --- a/src/material.rs +++ b/src/material.rs @@ -1,4 +1,4 @@ -use crate::math_utils::{near_zero, reflect}; +use crate::math_utils::{reflect}; use crate::types_defined::{Color, HitRecord, Ray, Vec3}; diff --git a/src/math_utils.rs b/src/math_utils.rs index 1fd024f..d873f76 100644 --- a/src/math_utils.rs +++ b/src/math_utils.rs @@ -10,11 +10,11 @@ pub fn clamp(value: f32, low: f32, high: f32) -> f32 { return value; } -pub fn near_zero(v: Vec3) -> bool { - const EPSILON: f32 = 1e-4; +// pub fn near_zero(v: Vec3) -> bool { +// const EPSILON: f32 = 1e-4; - v.x.abs() < EPSILON && v.y.abs() < EPSILON && v.z.abs() < EPSILON -} +// v.x.abs() < EPSILON && v.y.abs() < EPSILON && v.z.abs() < EPSILON +// } pub fn reflect(v: Vec3, n: Vec3) -> Vec3 { v - ((2.0 * v.dot(n)) * n) diff --git a/src/ppm_writer.rs b/src/ppm_writer.rs index 5caee1f..49e09e8 100644 --- a/src/ppm_writer.rs +++ b/src/ppm_writer.rs @@ -30,7 +30,7 @@ impl PPMWriter { } /// 完成写入并刷新缓冲区 - pub fn finish(mut self) -> Result<()> { + pub fn finish(&mut self) -> Result<()> { self.writter.flush() } } \ No newline at end of file diff --git a/src/sphere.rs b/src/sphere.rs index 76741a5..92f7f1e 100644 --- a/src/sphere.rs +++ b/src/sphere.rs @@ -1,6 +1,6 @@ use crate::hittable; -use crate::material::{Lambertian, Material, MaterialKind, Metal}; -use crate::types_defined::{Color, HitRecord, Point, Ray, Sphere}; +use crate::material::{MaterialKind}; +use crate::types_defined::{HitRecord, Point, Ray, Sphere}; impl Sphere { pub fn new(c: Point, r: f32, m: Option) -> Self { diff --git a/src/types_defined.rs b/src/types_defined.rs index a92b699..8328f6e 100644 --- a/src/types_defined.rs +++ b/src/types_defined.rs @@ -1,6 +1,5 @@ -use std::{fs::File, io::{BufWriter, Write}}; -use rand::distributions::Open01; -use crate::material::{Material, MaterialKind}; +use std::{fs::File, io::{BufWriter}}; +use crate::material::{MaterialKind}; use crate::ppm_writer::PPMWriter; /* @@ -35,13 +34,13 @@ pub struct HitRecord { pub struct Camera<'a> { pub image_width: i32, pub image_height: i32, - pub aspect_ratio: f32, - pub viewport_width: f32, - pub viewport_height: f32, + // pub aspect_ratio: f32, + // pub viewport_width: f32, + // pub viewport_height: f32, pub camera_center: Point, - pub focal_length: Vec3, - pub viewport_u: Vec3, - pub viewport_v: Vec3, + // pub focal_length: Vec3, + // pub viewport_u: Vec3, + // pub viewport_v: Vec3, pub viewport_u_delta: Vec3, pub viewport_v_delta: Vec3, pub viewport_top_left_pixel_center: Vec3, diff --git a/src/write_file_util.rs b/src/write_file_util.rs deleted file mode 100644 index ceca186..0000000 --- a/src/write_file_util.rs +++ /dev/null @@ -1,12 +0,0 @@ -use std::{fs::File, io::{BufWriter, Write}}; - -pub fn write_image(content: String, file_path: String) { - if let Ok(file) = File::create(file_path) { - let mut writer = BufWriter::new(file); - if let Err(e) = writer.write(content.as_bytes()) { - println!("写出失败:{:#}", e) - } - } else { - println!("PPM保存失败") - } -} \ No newline at end of file