Add some faster AABB hit implementations.

Switch e?println to info/trace logging.
This commit is contained in:
2018-09-18 17:48:27 -07:00
parent 4a9754dfdb
commit aa26c79f6d
9 changed files with 253 additions and 31 deletions

View File

@@ -1,5 +1,6 @@
use std;
use std::fmt;
use std::time::Instant;
use rand;
use rand::Rng;
@@ -43,7 +44,7 @@ impl fmt::Display for BVHNode {
fn box_x_compare(ah: &Box<Hit>, bh: &Box<Hit>) -> std::cmp::Ordering {
match (ah.bounding_box(0., 0.), bh.bounding_box(0., 0.)) {
(Some(box_left), Some(box_right)) => {
return box_left.min.x.partial_cmp(&box_right.min.x).unwrap();
return box_left.min().x.partial_cmp(&box_right.min().x).unwrap();
}
_ => panic!("hit missing bounding box"),
}
@@ -52,7 +53,7 @@ fn box_x_compare(ah: &Box<Hit>, bh: &Box<Hit>) -> std::cmp::Ordering {
fn box_y_compare(ah: &Box<Hit>, bh: &Box<Hit>) -> std::cmp::Ordering {
match (ah.bounding_box(0., 0.), bh.bounding_box(0., 0.)) {
(Some(box_left), Some(box_right)) => {
return box_left.min.y.partial_cmp(&box_right.min.y).unwrap();
return box_left.min().y.partial_cmp(&box_right.min().y).unwrap();
}
_ => panic!("hit missing bounding box"),
}
@@ -61,7 +62,7 @@ fn box_y_compare(ah: &Box<Hit>, bh: &Box<Hit>) -> std::cmp::Ordering {
fn box_z_compare(ah: &Box<Hit>, bh: &Box<Hit>) -> std::cmp::Ordering {
match (ah.bounding_box(0., 0.), bh.bounding_box(0., 0.)) {
(Some(box_left), Some(box_right)) => {
return box_left.min.z.partial_cmp(&box_right.min.z).unwrap();
return box_left.min().z.partial_cmp(&box_right.min().z).unwrap();
}
_ => panic!("hit missing bounding box"),
}
@@ -115,6 +116,13 @@ impl BVHNode {
(None, None) => None,
}
}
fn volume(&self) -> f32 {
let bbox = self.bounding_box(0., 0.).unwrap();
(bbox.min().x - bbox.max().x).abs()
* (bbox.min().y - bbox.max().y).abs()
* (bbox.min().z - bbox.max().z).abs()
}
}
impl Hit for BVHNode {
@@ -152,15 +160,25 @@ pub struct BVH {
impl BVH {
pub fn new(l: Vec<Box<Hit>>, t_min: f32, t_max: f32) -> BVH {
BVH {
let count = l.len();
let start = Instant::now();
let bvh = BVH {
root: BVHNode::new(l, t_min, t_max),
}
};
let runtime = start.elapsed();
info!(
"BVH build time {}.{} seconds for {} hitables",
runtime.as_secs(),
runtime.subsec_millis(),
count
);
bvh
}
}
fn print_tree(f: &mut fmt::Formatter, depth: usize, bvhn: &BVHNode) -> fmt::Result {
// TODO(wathiede): recurse and indent
write!(f, "{}{}\n", " ".repeat(depth * 2), bvhn)?;
let vol = bvhn.volume();
write!(f, "{:.*}{}{}\n", 2, vol, " ".repeat(depth * 2), bvhn)?;
if let BVHNode::Branch { left, right, .. } = bvhn {
print_tree(f, depth + 1, left)?;
print_tree(f, depth + 1, right)?;