-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaabb.h
72 lines (55 loc) · 2.12 KB
/
aabb.h
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
#ifndef AABB_H
#define AABB_H
#include "interval.h"
#include "ray.h"
#include "rtweekend.h"
#include "vec3.h"
class aabb {
public:
interval x, y, z;
aabb() {} // The default AABB is empty, since intervals are empty by default.
aabb(const interval& ix, const interval& iy, const interval& iz) : x(ix), y(iy), z(iz) {}
aabb(const point3& a, const point3& b) {
// Treat the two points a and b as extrema for the bounding box, so we don't require a
// particular minimum/maximum coordinate order.
x = interval(std::fmin(a[0], b[0]), std::fmax(a[0], b[0]));
y = interval(std::fmin(a[1], b[1]), std::fmax(a[1], b[1]));
z = interval(std::fmin(a[2], b[2]), std::fmax(a[2], b[2]));
}
aabb(const aabb& box0, const aabb& box1) {
x = interval(box0.x, box1.x);
y = interval(box0.y, box1.y);
z = interval(box0.z, box1.z);
}
aabb pad() {
// Return an AABB that has no side narrower than some delta, padding if necessary.
double delta = 0.0001;
interval new_x = (x.size() >= delta) ? x : x.expand(delta);
interval new_y = (y.size() >= delta) ? y : y.expand(delta);
interval new_z = (z.size() >= delta) ? z : z.expand(delta);
return aabb(new_x, new_y, new_z);
}
const interval& axis(int n) const {
if (n == 0) return x;
if (n == 1) return y;
return z;
}
bool hit(const ray& r, interval ray_t) const {
for (int a = 0; a < 3; a++) {
auto invD = 1 / r.direction()[a];
auto orig = r.origin()[a];
auto t0 = (axis(a).min - orig) * invD;
auto t1 = (axis(a).max - orig) * invD;
if (invD < 0) std::swap(t0, t1);
if (t0 > ray_t.min) ray_t.min = t0;
if (t1 < ray_t.max) ray_t.max = t1;
if (ray_t.max <= ray_t.min) return false;
}
return true;
}
};
aabb operator+(const aabb& bbox, const vec3& offset) {
return aabb(bbox.x + offset.x(), bbox.y + offset.y(), bbox.z + offset.z());
}
aabb operator+(const vec3& offset, const aabb& bbox) { return bbox + offset; }
#endif