0195-重构代码逻辑

环境

  • Time 2022-11-15
  • WSL-Ubuntu 22.04
  • Rust 1.65.0

前言

说明

参考:https://raytracing.github.io/books/RayTracingInOneWeekend.html

目标

main 文件中的逻辑越来越多,考虑将其抽象出来,分成多个文件。

hittable.rs

可以相交的物体,抽象成一个接口。

use crate::ray::Ray;
use crate::vector3::{Point3, Vector3};

pub trait Hit {
    fn hit(&self, ray: &Ray, min: f64, max: f64) -> Option<HitRecord>;
}

pub struct HitRecord {
    pub point: Point3,
    pub normal: Vector3,
    pub t: f64,
}

sphere.rs

球体相交抽象。

use crate::hittable::{Hit, HitRecord};
use crate::{ray::Ray, vector3::Point3};

pub struct Sphere {
    center: Point3,
    radius: f64,
}

impl Sphere {
    pub fn new(center: Point3, radius: f64) -> Sphere {
        Sphere { center, radius }
    }

    fn nearest(&self, ray: &Ray, temp: Temp) -> Option<HitRecord> {
        //  找到最近的点
        let a = ray.direction().dot(ray.direction());

        let mut t = (-temp.b - temp.sqrt) / a;
        if t < temp.min || temp.max < t {
            t = (-temp.b + temp.sqrt) / a;
            if t < temp.min || temp.max < t {
                return None;
            }
        }

        let point = ray.at(t);
        let normal = (point - self.center) / self.radius;
        Some(HitRecord { t, point, normal })
    }
}

impl Hit for Sphere {
    fn hit(&self, ray: &Ray, min: f64, max: f64) -> Option<HitRecord> {
        // 球心到射线起点的向量,
        let oc = ray.origin() - self.center;

        let a = ray.direction().dot(ray.direction());
        let b = oc.dot(ray.direction());
        let c = oc.dot(oc) - self.radius * self.radius;
        let discriminant = b * b - a * c;

        match discriminant < 0.0 {
            true => None,
            false => {
                let sqrt = discriminant.sqrt();
                self.nearest(ray, Temp { sqrt, b, min, max })
            }
        }
    }
}

struct Temp {
    sqrt: f64,
    b: f64,
    min: f64,
    max: f64,
}

main.rs

use hittable::Hit;
use ray::Ray;
use sphere::Sphere;
use vector3::{Color, Point3, Vector3};

mod hittable;
mod ray;
mod sphere;
mod vector3;

fn main() {
    // 图片的比例,和宽高
    const RATIO: f64 = 16.0 / 9.0;
    const WIDTH: u64 = 400;
    const HEIGHT: u64 = (WIDTH as f64 / RATIO) as u64;

    // 相机
    let view_height = 2.0;
    // 图片的比例应该和照射的显示的图形比例一致
    let view_width = RATIO * view_height;
    // 将其固定在 z 轴远离原点一个单位的距离
    let focal_length = 1.0;

    let origin = Point3::default();

    // 水平向量
    let horizontal = Vector3::new(view_width, 0.0, 0.0);
    // 垂直向量
    let vertical = Vector3::new(0.0, view_height, 0.0);

    // 将视角移动到水平和垂直向量构成的面的正中间,并且 z 为 1 的距离
    let lower_left_corner =
        origin - horizontal / 2.0 - vertical / 2.0 - Vector3::new(0.0, 0.0, focal_length);

    // 输出图片,第一行输出 P3,表示像素图
    let mut content = String::from("P3");
    // 输出宽和高,和最大颜色值
    content.push_str(&format!("\n{WIDTH} {HEIGHT}\n255\n"));

    let sphere = Sphere::new(Point3::new(0.0, 0.0, -1.0), 0.5);

    for j in (0..HEIGHT).rev() {
        // 进度
        eprintln!("Scan lines remaining: {j}");
        for i in 0..WIDTH {
            let u = i as f64 / (WIDTH - 1) as f64;
            let v = j as f64 / (HEIGHT - 1) as f64;
            // 从原点到屏幕显示上一个点的射线,然后计算它的颜色。
            // 因为在循环里,所以每个像素都会计算一次
            let ray = Ray::new(lower_left_corner + u * horizontal + v * vertical - origin);
            let color = ray_color(&ray, &sphere);
            content.push_str(&color.format_str());
        }
    }
    println!("{}", content);
    eprintln!("Done.");
}

// 光线的颜色计算
fn ray_color(ray: &Ray, hittable: &dyn Hit) -> Color {
    let option = hittable.hit(ray, 0.0, f64::INFINITY);

    if let Some(record) = option {
        return 0.5 * (record.normal + Color::new(1.0, 1.0, 1.0));
    }

    // 射线的单位向量
    let unit = ray.direction().unit();
    // 因为需要得到上下渐变的背景图,所以需要对 y 进行插值。
    let t = 0.5 * (unit.y + 1.0);
    // 线性插值,根据不同的光线得到在下面这个范围里的不同的颜色,并且是渐变色。
    (1.0 - t) * Color::new(1.0, 1.0, 1.0) + t * Color::new(0.5, 0.7, 1.0)
}

总结

在功能保持不变的情况下,将代码逻辑进行了抽象。

附录

ray.rs

use crate::vector3::{Point3, Vector3};

pub struct Ray {
    pub origin: Point3,
    pub direction: Vector3,
}

impl Ray {
    pub fn new(direction: Vector3) -> Ray {
        Ray {
            origin: Point3::default(),
            direction,
        }
    }

    pub fn origin(&self) -> Point3 {
        self.origin
    }

    pub fn direction(&self) -> Vector3 {
        self.direction
    }

    pub fn at(&self, t: f64) -> Point3 {
        self.origin + t * self.direction
    }
}

vector3.rs

use std::ops::{Add, Div, Mul, Sub};

pub type Color = Vector3;
pub type Point3 = Vector3;

#[derive(Default, Clone, Copy)]
pub struct Vector3 {
    pub x: f64,
    pub y: f64,
    pub z: f64,
}

impl Vector3 {
    pub fn new(x: f64, y: f64, z: f64) -> Self {
        Self { x, y, z }
    }

    pub fn format_str(&self) -> String {
        let x = (255.999 * self.x) as u64;
        let y = (255.999 * self.y) as u64;
        let z = (255.999 * self.z) as u64;

        format!("{x} {y} {z}\n")
    }

    /// 向量的长度
    pub fn length(self) -> f64 {
        self.dot(self).sqrt()
    }

    /// 向量的点乘
    pub fn dot(self, other: Vector3) -> f64 {
        self.x * other.x + self.y * other.y + self.z * other.z
    }

    ///  单位向量
    pub fn unit(self) -> Vector3 {
        self / self.length()
    }
}

// 向量的加法
impl Add for Vector3 {
    type Output = Self;

    fn add(self, rhs: Self) -> Self {
        Vector3 {
            x: self.x + rhs.x,
            y: self.y + rhs.y,
            z: self.z + rhs.z,
        }
    }
}

// 向量的减法
impl Sub for Vector3 {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self {
        Self {
            x: self.x - rhs.x,
            y: self.y - rhs.y,
            z: self.z - rhs.z,
        }
    }
}

// 向量和数字的乘法
impl Mul<f64> for Vector3 {
    type Output = Self;

    fn mul(self, rhs: f64) -> Self {
        Self {
            x: self.x * rhs,
            y: self.y * rhs,
            z: self.z * rhs,
        }
    }
}

// 向量和数字的乘法
impl Mul<Vector3> for f64 {
    type Output = Vector3;

    fn mul(self, rhs: Vector3) -> Vector3 {
        rhs * self
    }
}

// 向量的除法
impl Div<f64> for Vector3 {
    type Output = Self;

    fn div(self, rhs: f64) -> Self {
        Self {
            x: self.x / rhs,
            y: self.y / rhs,
            z: self.z / rhs,
        }
    }
}
posted @   jiangbo4444  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2020-07-17 【JavaScript】对象基础
点击右上角即可分享
微信分享提示