-
Notifications
You must be signed in to change notification settings - Fork 0
/
aabb.h
68 lines (49 loc) · 1.77 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
#pragma once
#include "ray.h"
inline float ffmin(float a, float b) { return a < b ? a : b; }
inline float ffmax(float a, float b) { return a > b ? a : b; }
inline Tmpl8::vec3 ffmin(Tmpl8::vec3 a, float b) { return Tmpl8::vec3(a.x > b ? a.x : b, a.y > b ? a.y : b, a.z > b ? a.z : b); }
inline Tmpl8::vec3 ffmin(Tmpl8::vec3 a, Tmpl8::vec3 b) { return Tmpl8::vec3(a.x > b.x ? a.x : b.x, a.y > b.y ? a.y : b.y, a.z > b.z ? a.z : b.z); }
inline Tmpl8::vec3 ffmax(Tmpl8::vec3 a, float b) { return Tmpl8::vec3(a.x < b ? a.x : b, a.y < b ? a.y : b, a.z < b ? a.z : b); }
inline Tmpl8::vec3 ffmax(Tmpl8::vec3 a, Tmpl8::vec3 b) { return Tmpl8::vec3(a.x < b.x ? a.x : b.x, a.y < b.y ? a.y : b.y, a.z < b.z ? a.z : b.z); }
namespace Tmpl8
{
class aabb
{
public:
aabb() {}
aabb(const vec3& a, const vec3& b) { _min = a; _max = b; }
vec3 min() const { return _min; }
vec3 max() const { return _max; }
bool hit(const Tmpl8::ray& r, float tmin, float tmax) const;
private:
vec3 _min;
vec3 _max;
};
inline bool aabb::hit(const Tmpl8::ray& r, float tmin, float tmax) const
{
//optimized AABB hit method by Andrew Kensler
for (int a = 0; a < 3; a++) {
auto invD = 1.0f / r.direction()[a];
auto t0 = (min()[a] - r.origin()[a]) * invD;
auto t1 = (max()[a] - r.origin()[a]) * invD;
if (invD < 0.0f)
std::swap(t0, t1);
tmin = t0 > tmin ? t0 : tmin;
tmax = t1 < tmax ? t1 : tmax;
if (tmax <= tmin)
return false;
}
return true;
}
inline aabb surroundingBox(aabb box0, aabb box1)
{
point3 small(fmin(box0.min().x, box1.min().x),
fmin(box0.min().y, box1.min().y),
fmin(box0.min().z, box1.min().z));
point3 big(fmax(box0.max().x, box1.max().x),
fmax(box0.max().y, box1.max().y),
fmax(box0.max().z, box1.max().z));
return aabb(small, big);
}
}