diff --git a/rtiow/renderer/src/aabb.rs b/rtiow/renderer/src/aabb.rs index 9936da3..2ffa4ce 100644 --- a/rtiow/renderer/src/aabb.rs +++ b/rtiow/renderer/src/aabb.rs @@ -42,6 +42,13 @@ impl fmt::Debug for AABB { } impl AABB { + // Create AABB with min = f32::MAX and max = -f32::MAX. It is expected the caller will use grow to create + // a vaild AABB. + pub fn infinite() -> AABB { + AABB { + bounds: [Vec3::from(f32::MAX), -Vec3::from(f32::MAX)], + } + } pub fn new>(min: V, max: V) -> AABB { let min: Vec3 = min.into(); let max: Vec3 = max.into(); @@ -51,12 +58,21 @@ impl AABB { AABB { bounds: [min, max] } } + pub fn area(&self) -> f32 { + let e = self.max() - self.min(); + e.x * e.y + e.y * e.z + e.z * e.x + } pub fn volume(&self) -> f32 { (self.min().x - self.max().x).abs() * (self.min().y - self.max().y).abs() * (self.min().z - self.max().z).abs() } + pub fn grow(&mut self, v: Vec3) { + self.bounds[0] = vec3::min(self.bounds[0], v); + self.bounds[1] = vec3::max(self.bounds[1], v); + } + pub fn longest_axis(&self) -> usize { let xd = (self.min().x - self.max().x).abs(); let yd = (self.min().y - self.max().y).abs();