-
Notifications
You must be signed in to change notification settings - Fork 0
/
aabb.h
117 lines (107 loc) · 2.98 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
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
#ifndef AABB_H
#define AABB_H
#include "rtweekend.h"
class aabb {
public:
HOSTDEV aabb() {}
HOSTDEV aabb(const point3 &a, const point3 &b)
{
minimum = a;
maximum = b;
}
HOSTDEV point3 min() const { return minimum; }
HOSTDEV point3 max() const { return maximum; }
HOSTDEV inline bool hit(const ray &r, FP_T t_min, FP_T t_max) const
{
#if 0
// Andrew Kensler variant. Unroll does not help
#pragma unroll 3
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) {
#ifndef USE_CUDA
std::swap(t0, t1);
#else
auto tmp = t0;
t0 = t1;
t1 = tmp;
#endif
}
t_min = t0 > t_min ? t0 : t_min;
t_max = t1 < t_max ? t1 : t_max;
if (t_max <= t_min) return false;
}
#else
// rallen-Manually unrolled a=0,1,2 since this was
// a hotspot in nsight-compute
auto invD = 1.0f / r.direction()[0];
auto t0 = (min()[0] - r.origin()[0]) * invD;
auto t1 = (max()[0] - r.origin()[0]) * invD;
if (invD < 0.0f) {
#ifndef USE_CUDA
std::swap(t0, t1);
#else
auto tmp = t0;
t0 = t1;
t1 = tmp;
#endif
}
t_min = t0 > t_min ? t0 : t_min;
t_max = t1 < t_max ? t1 : t_max;
if (t_max <= t_min) return false;
// a = 1
invD = 1.0f / r.direction()[1];
t0 = (min()[1] - r.origin()[1]) * invD;
t1 = (max()[1] - r.origin()[1]) * invD;
if (invD < 0.0f) {
#ifndef USE_CUDA
std::swap(t0, t1);
#else
auto tmp = t0;
t0 = t1;
t1 = tmp;
#endif
}
t_min = t0 > t_min ? t0 : t_min;
t_max = t1 < t_max ? t1 : t_max;
if (t_max <= t_min) return false;
// a = 2
invD = 1.0f / r.direction()[2];
t0 = (min()[2] - r.origin()[2]) * invD;
t1 = (max()[2] - r.origin()[2]) * invD;
if (invD < 0.0f) {
#ifndef USE_CUDA
std::swap(t0, t1);
#else
auto tmp = t0;
t0 = t1;
t1 = tmp;
#endif
}
t_min = t0 > t_min ? t0 : t_min;
t_max = t1 < t_max ? t1 : t_max;
if (t_max <= t_min) return false;
#endif
return true;
}
HOSTDEV void print(int i) const
{
printf("aabb %i ", i);
minimum.print();
printf(" - ");
maximum.print();
}
point3 minimum;
point3 maximum;
};
HOSTDEV aabb surrounding_box(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);
}
#endif