From 78ddaff5855bf8446adef9e18eb0d7b7ddcee52a Mon Sep 17 00:00:00 2001 From: lamp Date: Sun, 5 Mar 2023 21:45:56 +0000 Subject: init --- src/hittable/aabb.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/hittable/aabb.rs (limited to 'src/hittable/aabb.rs') diff --git a/src/hittable/aabb.rs b/src/hittable/aabb.rs new file mode 100644 index 0000000..5c5c9fa --- /dev/null +++ b/src/hittable/aabb.rs @@ -0,0 +1,46 @@ +use crate::ray::Ray; +use crate::vec3::Point3; + +#[derive(Clone)] +pub struct AABB { + pub minimum: Point3, + pub maximum: Point3, +} + +impl AABB { + pub fn hit(&self, ray: &Ray, t_min: f64, t_max: f64) -> bool { + let mut t_min = t_min; + let mut t_max = t_max; + for a in 0..3 { + let inv_d = 1.0 / ray.direction.get(a).unwrap(); + let mut t0 = (self.minimum.get(a).unwrap() - ray.origin.get(a).unwrap()) * inv_d; + let mut t1 = (self.maximum.get(a).unwrap() - ray.origin.get(a).unwrap()) * inv_d; + if inv_d < 0.0 { + // TODO: destructuring assignments are unstable :( + //(t0, t1) = (t1, t0); + std::mem::swap(&mut t0, &mut t1); + } + t_min = if t0 > t_min { t0 } else { t_min }; + t_max = if t1 < t_max { t1 } else { t_max }; + if t_max <= t_min { + return false; + } + } + true + } + + pub fn surrounding_box(&self, other: &AABB) -> AABB { + AABB { + minimum: Point3 { + x: self.minimum.x.min(other.minimum.x), + y: self.minimum.y.min(other.minimum.y), + z: self.minimum.z.min(other.minimum.z), + }, + maximum: Point3 { + x: self.maximum.x.max(other.maximum.x), + y: self.maximum.y.max(other.maximum.y), + z: self.maximum.z.max(other.maximum.z), + }, + } + } +} -- cgit v1.2.3