Replace arc
All checks were successful
CI / test (push) Successful in 26s
Render Image / render (push) Successful in 28s

This commit is contained in:
k 2026-04-28 16:21:18 -04:00
parent 5857244ca8
commit 8df48d3824
3 changed files with 13 additions and 15 deletions

View file

@ -9,7 +9,6 @@ use render::{World,Camera};
use vec3::{Point3,Vec3};
use material::{Metal,Lambertian,Material};
use std::sync::Arc;
fn main() {
@ -19,16 +18,16 @@ fn main() {
let viewport_height = 2.0;
let m = Lambertian::new(Vec3::new(0.5,0.5,0.5));
let world_m: Arc <dyn Material> = Arc::new(m);
let world_m: Box <dyn Material> = Box::new(m);
let m = Lambertian::new(Vec3::new(0.1, 0.2, 0.5));
let center_m: Arc <dyn Material> = Arc::new(m);
let center_m: Box <dyn Material> = Box::new(m);
let m = Metal::new(Vec3::new(0.8, 0.8, 0.8), 0.3);
let left_m: Arc <dyn Material> = Arc::new(m);
let left_m: Box <dyn Material> = Box::new(m);
let m = Metal::new(Vec3::new(0.8, 0.6, 0.2), 0.5);
let right_m: Arc <dyn Material> = Arc::new(m);
let right_m: Box <dyn Material> = Box::new(m);
let mut camera = Camera::new(aspect_ratio,image_width,focal_length,Point3::new(-1.0,1.0,1.0),Point3::new(0.0,0.0,-1.0),90.0);
let mut world = World::new();

View file

@ -2,22 +2,21 @@ use crate::vec3::{Point3, Ray, Vec3, Interval};
use crate::material::{Material,Scatter};
use rand::prelude::*;
use std::sync::Arc;
pub trait Hittable: Send + Sync{
fn hit(&self, ray: Ray, interval:Interval) -> Option<HitRecord>;
fn hit(&self, ray: Ray, interval:Interval) -> Option<HitRecord<'_>>;
}
pub struct HitRecord {
pub struct HitRecord<'a> {
point: Point3,
normal: Vec3,
root: f64,
front_face: bool,
material: Arc<dyn Material>,
material: &'a dyn Material,
}
impl HitRecord {
impl HitRecord <'_> {
pub fn normal(&self) -> Vec3 {
self.normal
}
@ -44,17 +43,17 @@ impl HitRecord {
pub struct Sphere {
radius: f64,
center: Point3,
material: Arc<dyn Material>
material: Box<dyn Material>
}
impl Sphere {
pub fn new(radius: f64, center: Point3, material: Arc<dyn Material>) -> Self {
pub fn new(radius: f64, center: Point3, material: Box<dyn Material>) -> Self {
Sphere { radius, center , material}
}
}
impl Hittable for Sphere {
fn hit(&self, ray: Ray, interval:Interval) -> Option<HitRecord> {
fn hit(&self, ray: Ray, interval:Interval) -> Option<HitRecord<'_>> {
let oc = self.center - ray.origin();
let a = ray.direction().length_squared();
let h = Vec3::dot(ray.direction(), oc.to_vec());
@ -80,7 +79,7 @@ impl Hittable for Sphere {
point,
normal,
front_face: false,
material: self.material.clone()
material: self.material.as_ref(),
};
tmp.set_face_normal(ray, normal);
Some(tmp)

View file

@ -21,7 +21,7 @@ impl World {
self.objects.push(Arc::new(object));
}
pub fn hit(&self, ray: Ray, interval:Interval) -> Option<HitRecord> {
pub fn hit(&self, ray: Ray, interval:Interval) -> Option<HitRecord<'_>> {
let mut hit_anything = None;
let mut closest_so_far = interval.max();