Created BBox, an axis-aligned 3d bounding box type.

This commit is contained in:
Nathan Vegdahl 2015-12-27 00:44:59 -08:00
parent 3f614f3728
commit b20910a252
3 changed files with 76 additions and 1 deletions

56
src/bbox.rs Normal file
View File

@ -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),
}
}
}

View File

@ -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)))
}
}
}

View File

@ -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));