From 78ddaff5855bf8446adef9e18eb0d7b7ddcee52a Mon Sep 17 00:00:00 2001 From: lamp Date: Sun, 5 Mar 2023 21:45:56 +0000 Subject: init --- src/hittable/hittable_list.rs | 52 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/hittable/hittable_list.rs (limited to 'src/hittable/hittable_list.rs') diff --git a/src/hittable/hittable_list.rs b/src/hittable/hittable_list.rs new file mode 100644 index 0000000..735509c --- /dev/null +++ b/src/hittable/hittable_list.rs @@ -0,0 +1,52 @@ +use std::sync::Arc; + +use crate::hittable::{HitRecord, Hittable, AABB}; +use crate::ray::Ray; + +pub struct HittableList { + pub objects: Vec>, +} + +impl HittableList { + pub fn new() -> HittableList { + HittableList { + objects: Vec::new(), + } + } + + pub fn clear(&mut self) { + self.objects.clear(); + } + + pub fn add(&mut self, object: Arc) { + self.objects.push(object); + } +} + +impl Hittable for HittableList { + fn hit(&self, ray: &Ray, t_min: f64, t_max: f64) -> Option { + let mut record = None; + let mut closest_so_far = t_max; + + for object in &self.objects { + let temp_rec = object.hit(ray, t_min, closest_so_far); + if let Some(hit_record) = &temp_rec { + closest_so_far = hit_record.t; + record = temp_rec; + } + } + record + } + + fn bounding_box(&self, time_start: f64, time_end: f64) -> Option { + let mut output_box: Option = None; + for object in &self.objects { + let temp_box = object.bounding_box(time_start, time_end)?; + output_box = match output_box { + Some(aabb) => Some(aabb.surrounding_box(&temp_box)), + None => Some(temp_box.clone()), + }; + } + output_box + } +} -- cgit v1.2.3