1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use std::f32;
use std::ops::{Index, IndexMut};
use linalg::{self, Point, Vector, Ray, Axis};
#[derive(Clone, Copy, Debug)]
pub struct BBox {
pub min: Point,
pub max: Point,
}
impl BBox {
pub fn new() -> BBox {
BBox { min: Point::broadcast(f32::INFINITY), max: Point::broadcast(f32::NEG_INFINITY) }
}
pub fn singular(p: Point) -> BBox {
BBox { min: p, max: p }
}
pub fn span(min: Point, max: Point) -> BBox {
BBox { min: min, max: max }
}
pub fn box_union(&self, b: &BBox) -> BBox {
BBox { min: Point::new(f32::min(self.min.x, b.min.x), f32::min(self.min.y, b.min.y),
f32::min(self.min.z, b.min.z)),
max: Point::new(f32::max(self.max.x, b.max.x), f32::max(self.max.y, b.max.y),
f32::max(self.max.z, b.max.z))
}
}
pub fn point_union(&self, p: &Point) -> BBox {
BBox { min: Point::new(f32::min(self.min.x, p.x), f32::min(self.min.y, p.y),
f32::min(self.min.z, p.z)),
max: Point::new(f32::max(self.max.x, p.x), f32::max(self.max.y, p.y),
f32::max(self.max.z, p.z))
}
}
pub fn max_extent(&self) -> Axis {
let d = self.max - self.min;
if d.x > d.y && d.x > d.z {
Axis::X
} else if d.y > d.z {
Axis::Y
} else {
Axis::Z
}
}
pub fn lerp(&self, tx: f32, ty: f32, tz: f32) -> Point {
Point::new(linalg::lerp(tx, &self.min.x, &self.max.x), linalg::lerp(ty, &self.min.y, &self.max.y),
linalg::lerp(tz, &self.min.z, &self.max.z))
}
pub fn offset(&self, p: &Point) -> Vector {
(*p - self.min) / (self.max - self.min)
}
pub fn surface_area(&self) -> f32 {
let d = self.max - self.min;
2.0 * (d.x * d.y + d.x * d.z + d.y * d.z)
}
pub fn fast_intersect(&self, r: &Ray, inv_dir: &Vector, neg_dir: &[usize; 3]) -> bool {
let mut tmin = (self[neg_dir[0]].x - r.o.x) * inv_dir.x;
let mut tmax = (self[1 - neg_dir[0]].x - r.o.x) * inv_dir.x;
let tymin = (self[neg_dir[1]].y - r.o.y) * inv_dir.y;
let tymax = (self[1 - neg_dir[1]].y - r.o.y) * inv_dir.y;
if tmin > tymax || tymin > tmax {
return false;
}
if tymin > tmin {
tmin = tymin;
}
if tymax < tmax {
tmax = tymax;
}
let tzmin = (self[neg_dir[2]].z - r.o.z) * inv_dir.z;
let tzmax = (self[1 - neg_dir[2]].z - r.o.z) * inv_dir.z;
if tmin > tzmax || tzmin > tmax {
return false;
}
if tzmin > tmin {
tmin = tzmin;
}
if tzmax < tmax {
tmax = tzmax;
}
tmin < r.max_t && tmax > r.min_t
}
}
impl Index<usize> for BBox {
type Output = Point;
fn index(&self, i: usize) -> &Point {
match i {
0 => &self.min,
1 => &self.max,
_ => panic!("Invalid index into point"),
}
}
}
impl IndexMut<usize> for BBox {
fn index_mut(&mut self, i: usize) -> &mut Point {
match i {
0 => &mut self.min,
1 => &mut self.max,
_ => panic!("Invalid index into point"),
}
}
}