aboutsummaryrefslogtreecommitdiff
path: root/src/hittable/hittable_list.rs
diff options
context:
space:
mode:
authorlamp2023-03-05 21:45:56 +0000
committerlamp2023-03-05 21:45:56 +0000
commit78ddaff5855bf8446adef9e18eb0d7b7ddcee52a (patch)
tree0d0e93cfa28751a2f96518eeb231cf715958e1fa /src/hittable/hittable_list.rs
init
Diffstat (limited to 'src/hittable/hittable_list.rs')
-rw-r--r--src/hittable/hittable_list.rs52
1 files changed, 52 insertions, 0 deletions
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<Arc<dyn Hittable>>,
+}
+
+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<dyn Hittable>) {
+ self.objects.push(object);
+ }
+}
+
+impl Hittable for HittableList {
+ fn hit(&self, ray: &Ray, t_min: f64, t_max: f64) -> Option<HitRecord> {
+ 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<AABB> {
+ let mut output_box: Option<AABB> = 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
+ }
+}