From b20910a2522c0e94571a80dca037b051f36a426c Mon Sep 17 00:00:00 2001 From: Nathan Vegdahl Date: Sun, 27 Dec 2015 00:44:59 -0800 Subject: [PATCH] Created BBox, an axis-aligned 3d bounding box type. --- src/bbox.rs | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/float4.rs | 18 +++++++++++++++++ src/main.rs | 3 ++- 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 src/bbox.rs diff --git a/src/bbox.rs b/src/bbox.rs new file mode 100644 index 0000000..c59af24 --- /dev/null +++ b/src/bbox.rs @@ -0,0 +1,56 @@ +#![allow(dead_code)] + +use std; +use std::ops::BitOr; + +use math::Point; +use lerp::{lerp, Lerp}; + +/// A 3D axis-aligned bounding box. +#[derive(Debug, Copy, Clone)] +pub struct BBox { + pub min: Point, + pub max: Point, +} + +impl BBox { + /// Creates a degenerate BBox with +infinity min and -infinity max. + pub fn new() -> BBox { + BBox { + min: Point::new(std::f32::INFINITY, std::f32::INFINITY, std::f32::INFINITY), + max: Point::new(std::f32::NEG_INFINITY, + std::f32::NEG_INFINITY, + std::f32::NEG_INFINITY), + } + } + + /// Creates a BBox with min as the minimum extent and max as the maximum + /// extent. + pub fn from_points(min: Point, max: Point) -> BBox { + BBox { + min: min, + max: max, + } + } +} + + +/// Union of two BBoxes. +impl BitOr for BBox { + type Output = BBox; + + fn bitor(self, rhs: BBox) -> BBox { + BBox::from_points(Point { co: self.min.co.v_min(rhs.min.co) }, + Point { co: self.max.co.v_max(rhs.max.co) }) + } +} + + +impl Lerp for BBox { + fn lerp(self, other: BBox, alpha: f32) -> BBox { + BBox { + min: lerp(self.min, other.min, alpha), + max: lerp(self.max, other.max, alpha), + } + } +} diff --git a/src/float4.rs b/src/float4.rs index 13e6eb1..953d37b 100644 --- a/src/float4.rs +++ b/src/float4.rs @@ -46,6 +46,24 @@ impl Float4 { .max(self.data.get_unchecked(2).max(*self.data.get_unchecked(3))) } } + + pub fn v_min(&self, other: Float4) -> Float4 { + unsafe { + Float4::new(self.data.get_unchecked(0).min(*other.data.get_unchecked(0)), + self.data.get_unchecked(1).min(*other.data.get_unchecked(1)), + self.data.get_unchecked(2).min(*other.data.get_unchecked(2)), + self.data.get_unchecked(3).min(*other.data.get_unchecked(3))) + } + } + + pub fn v_max(&self, other: Float4) -> Float4 { + unsafe { + Float4::new(self.data.get_unchecked(0).max(*other.data.get_unchecked(0)), + self.data.get_unchecked(1).max(*other.data.get_unchecked(1)), + self.data.get_unchecked(2).max(*other.data.get_unchecked(2)), + self.data.get_unchecked(3).max(*other.data.get_unchecked(3))) + } + } } diff --git a/src/main.rs b/src/main.rs index 9d9a54f..ab8282e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ mod math; mod lerp; mod float4; mod ray; +mod bbox; mod image; use std::path::Path; @@ -50,7 +51,7 @@ fn main() { println!("Psychopath {}", VERSION); return; } - + // Write output image let mut img = Image::new(512, 512); img.set(256, 256, (255, 255, 255));