chore: clean code

This commit is contained in:
dengqn 2025-08-12 17:04:01 +08:00
parent 4a58168482
commit 37b793287d
8 changed files with 26 additions and 38 deletions

View File

@ -42,13 +42,13 @@ impl<'a> Camera<'a> {
Camera { Camera {
image_width, image_width,
image_height, image_height,
aspect_ratio, // aspect_ratio,
viewport_width, // viewport_width,
viewport_height, // viewport_height,
camera_center, camera_center,
focal_length, // focal_length,
viewport_u, // viewport_u,
viewport_v, // viewport_v,
viewport_u_delta, viewport_u_delta,
viewport_v_delta, viewport_v_delta,
viewport_top_left_pixel_center, viewport_top_left_pixel_center,
@ -89,10 +89,12 @@ impl<'a> Camera<'a> {
clamp(color.z, 0.0, 1.0), 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 // img_content
} }

View File

@ -12,7 +12,6 @@ mod ray;
mod sphere; mod sphere;
mod types_defined; mod types_defined;
mod vec3; mod vec3;
mod write_file_util;
mod math_utils; mod math_utils;
mod ppm_writer; mod ppm_writer;
mod material; mod material;
@ -50,7 +49,7 @@ fn camera_render() {
let mut world = HittableList::new(); let mut world = HittableList::new();
let plane_m = Some(MaterialKind::Lambertian(Lambertian{albedo: Color::new(0.899, 0.899, 0.999)})); 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 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_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)})); let left_behind_m = Some(MaterialKind::Lambertian(Lambertian{albedo: Color::new(0.799, 0.599, 0.599)}));

View File

@ -1,4 +1,4 @@
use crate::math_utils::{near_zero, reflect}; use crate::math_utils::{reflect};
use crate::types_defined::{Color, HitRecord, Ray, Vec3}; use crate::types_defined::{Color, HitRecord, Ray, Vec3};

View File

@ -10,11 +10,11 @@ pub fn clamp(value: f32, low: f32, high: f32) -> f32 {
return value; return value;
} }
pub fn near_zero(v: Vec3) -> bool { // pub fn near_zero(v: Vec3) -> bool {
const EPSILON: f32 = 1e-4; // 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 { pub fn reflect(v: Vec3, n: Vec3) -> Vec3 {
v - ((2.0 * v.dot(n)) * n) v - ((2.0 * v.dot(n)) * n)

View File

@ -30,7 +30,7 @@ impl<W: Write> PPMWriter<W> {
} }
/// 完成写入并刷新缓冲区 /// 完成写入并刷新缓冲区
pub fn finish(mut self) -> Result<()> { pub fn finish(&mut self) -> Result<()> {
self.writter.flush() self.writter.flush()
} }
} }

View File

@ -1,6 +1,6 @@
use crate::hittable; use crate::hittable;
use crate::material::{Lambertian, Material, MaterialKind, Metal}; use crate::material::{MaterialKind};
use crate::types_defined::{Color, HitRecord, Point, Ray, Sphere}; use crate::types_defined::{HitRecord, Point, Ray, Sphere};
impl Sphere { impl Sphere {
pub fn new(c: Point, r: f32, m: Option<MaterialKind>) -> Self { pub fn new(c: Point, r: f32, m: Option<MaterialKind>) -> Self {

View File

@ -1,6 +1,5 @@
use std::{fs::File, io::{BufWriter, Write}}; use std::{fs::File, io::{BufWriter}};
use rand::distributions::Open01; use crate::material::{MaterialKind};
use crate::material::{Material, MaterialKind};
use crate::ppm_writer::PPMWriter; use crate::ppm_writer::PPMWriter;
/* /*
@ -35,13 +34,13 @@ pub struct HitRecord {
pub struct Camera<'a> { pub struct Camera<'a> {
pub image_width: i32, pub image_width: i32,
pub image_height: i32, pub image_height: i32,
pub aspect_ratio: f32, // pub aspect_ratio: f32,
pub viewport_width: f32, // pub viewport_width: f32,
pub viewport_height: f32, // pub viewport_height: f32,
pub camera_center: Point, pub camera_center: Point,
pub focal_length: Vec3, // pub focal_length: Vec3,
pub viewport_u: Vec3, // pub viewport_u: Vec3,
pub viewport_v: Vec3, // pub viewport_v: Vec3,
pub viewport_u_delta: Vec3, pub viewport_u_delta: Vec3,
pub viewport_v_delta: Vec3, pub viewport_v_delta: Vec3,
pub viewport_top_left_pixel_center: Vec3, pub viewport_top_left_pixel_center: Vec3,

View File

@ -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保存失败")
}
}