首页 > 其他分享 >0195-重构代码逻辑

0195-重构代码逻辑

时间:2024-07-17 14:53:40浏览次数:9  
标签:重构 ray 代码 Vector3 pub let f64 self 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,
        }
    }
}

标签:重构,ray,代码,Vector3,pub,let,f64,self,0195
From: https://www.cnblogs.com/jiangbo4444/p/18307357

相关文章

  • 好用的开源移动端低代码平台有哪些
    移动APP、H5、小程序曾风靡一时,结合当前无代码/低代码开发技术,有哪些免费开源的移动端H5/小程序软件,不用写代码即可发布H5页面,笔者对市场上主流的开源H5低代码/无代码工具/框架/组件进行了研究和验证,找到了几款比较好用的移动端H5无代码/低代码设计器,供大家选型参考。1、h5-doori......
  • 初学Python必须知道的14个强大单行代码
    引言:Python的魅力与单行代码的重要性Python以其简洁明了的语法、丰富的内置函数和强大的第三方库深受广大开发者喜爱。尤其对于编程小白来说,学习Python就像打开了一扇通向编程世界的大门。而单行代码,作为Python魅力的一部分,不仅能展现其语言的优雅与高效,更能帮助初学者快速掌......
  • 代码随想录算法训练营第26天 | 回溯02:39. 组合总和、40.组合总和II、131.分割回文串
    代码随想录算法训练营第26天|回溯02:39.组合总和、40.组合总和II、131.分割回文串组合总和https://leetcode.cn/problems/combination-sum/代码随想录https://programmercarl.com/0039.组合总和.html40.组合总和IIhttps://leetcode.cn/problems/combination-sum-ii/desc......
  • Hive自定义函数编写方法(含源代码解读,超详细,易理解)
    一、Hive自定义函数介绍        1.内置函数        Hive自带了一些函数。比如:max/min等,但是数量有限,自己可以通过自定义UDF来方便的扩展。2.自定义函数        当Hive提供的内置函数无法满足你的业务处理需要时,此时就可以考虑使用用户自定义函数(UD......
  • 代码随想录算法训练营第24天 |
    代码随想录算法训练营第24天|回溯基础理论、第77题.组合、216.组合总和III、回溯基础理论代码随想录https://programmercarl.com/回溯算法理论基础.html#题目分类第77题.组合https://leetcode.cn/problems/combinations/description/代码随想录https://programmercarl.c......
  • idea git 提交代码(commit)代码后,没有未push后怎么撤回
    1.方法一 第二步: HEAD~1--->Reset---> 右键项目--》 或者 接下来----》 提醒-----------------  提醒---------   提醒-------------(重要的话说三遍),如果本地写很多代码逻辑,一定做好备份直接选择远程分支,checkoutandrebase(做好本地新写未提交......
  • 【python学习】第三方库之pandas库的定义、特点、功能、使用场景和代码示例
    引言pandas是一个强大的Python库,用于数据分析和数据处理。它基于NumPy,提供了灵活的数据结构(Series和DataFrame)和数据操作功能,是数据科学和机器学习中不可或缺的工具文章目录引言一、安装`pandas`第三方库二、`pandas`的定义三、特点3.1强大的数据结构3.2灵活的数据......
  • 白码低代码平台搭建crm系统实现公海客户自动分配
    实现目标:1、市场部添加的公海客户,自动轮巡分配给销售员2、可设置销售人员自动分配公海客户的客户数上限准备工作:1、在白码低代码平台准备销售员数据表,并准备几个销售员数据。必须字段说明:自动分配客户上限:数字类型,用于限制自动分配公海客户的客户数。已分配客户数:数字类......
  • Python自动化:10行代码免费解锁抖音、快手、小红书平台资源,无水印视频一键下载
    Python自动化:10行代码免费解锁抖音、快手、小红书平台资源,无水印视频一键下载原创 丹心向阳 数海丹心 2024年06月19日07:30 山东摘要:抖音、快手、小红书作为国内顶尖的短视频和娱乐平台,汇聚了巨大的流量和丰富的创意内容。对于自媒体从业人员而言,这些平台上的灵感和视频资......
  • C++ 数据抽象:构建高效、可维护的代码基石
    C++数据抽象:构建高效、可维护的代码基石在软件开发中,数据抽象是一个核心概念,它允许我们隐藏实现细节,仅通过公共接口与外部世界交互。这种封装机制不仅提高了代码的安全性,还促进了代码的复用和可维护性。C++作为一门强大的面向对象编程语言,通过类(Classes)和接口(Interfaces,尽......