forked from BachiLi/redner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaabb.h
More file actions
246 lines (216 loc) · 7.14 KB
/
aabb.h
File metadata and controls
246 lines (216 loc) · 7.14 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#pragma once
#include "redner.h"
#include "vector.h"
#include "cuda_utils.h"
#include "ray.h"
#include <iostream>
struct AABB3 {
DEVICE AABB3(
const Vector3 &p_min = Vector3{ infinity<Real>(), infinity<Real>(), infinity<Real>()},
const Vector3 &p_max = Vector3{-infinity<Real>(), -infinity<Real>(), -infinity<Real>()})
: p_min(p_min), p_max(p_max) {}
DEVICE
const Vector3& operator[](int i) const {
return i == 0 ? p_min : p_max;
}
Vector3 p_min;
Vector3 p_max;
};
struct AABB6 {
DEVICE AABB6(
const Vector3 &p_min = Vector3{ infinity<Real>(), infinity<Real>(), infinity<Real>()},
const Vector3 &d_min = Vector3{ infinity<Real>(), infinity<Real>(), infinity<Real>()},
const Vector3 &p_max = Vector3{-infinity<Real>(), -infinity<Real>(), -infinity<Real>()},
const Vector3 &d_max = Vector3{-infinity<Real>(), -infinity<Real>(), -infinity<Real>()})
: p_min(p_min), d_min(d_min), p_max(p_max), d_max(d_max) {}
Vector3 p_min, d_min;
Vector3 p_max, d_max;
};
template<typename T>
DEVICE
inline T convert_aabb(const AABB6 &b) {
assert(false);
}
template<>
DEVICE
inline AABB3 convert_aabb(const AABB6 &b) {
return AABB3{b.p_min, b.p_max};
}
template<>
DEVICE
inline AABB6 convert_aabb(const AABB6 &b) {
return b;
}
struct Sphere {
Vector3 center;
Real radius;
};
DEVICE
inline Vector3 corner(const AABB3 &b, int i) {
Vector3 ret;
ret[0] = ((i & 1) == 0) ? b.p_min[0] : b.p_max[0];
ret[1] = ((i & 2) == 0) ? b.p_min[1] : b.p_max[1];
ret[2] = ((i & 4) == 0) ? b.p_min[2] : b.p_max[2];
return ret;
}
DEVICE
inline AABB3 merge(const AABB3 &b, const Vector3 &p) {
return AABB3{
Vector3{
min(b.p_min[0], p[0]),
min(b.p_min[1], p[1]),
min(b.p_min[2], p[2])},
Vector3{
max(b.p_max[0], p[0]),
max(b.p_max[1], p[1]),
max(b.p_max[2], p[2])}};
}
DEVICE
inline AABB3 merge(const AABB3 &b0, const AABB3 &b1) {
return AABB3{
Vector3{
min(b0.p_min[0], b1.p_min[0]),
min(b0.p_min[1], b1.p_min[1]),
min(b0.p_min[2], b1.p_min[2])},
Vector3{
max(b0.p_max[0], b1.p_max[0]),
max(b0.p_max[1], b1.p_max[1]),
max(b0.p_max[2], b1.p_max[2])}};
}
DEVICE
inline AABB6 merge(const AABB6 &b0, const AABB6 &b1) {
return AABB6{
Vector3{
min(b0.p_min[0], b1.p_min[0]),
min(b0.p_min[1], b1.p_min[1]),
min(b0.p_min[2], b1.p_min[2])},
Vector3{
min(b0.d_min[0], b1.d_min[0]),
min(b0.d_min[1], b1.d_min[1]),
min(b0.d_min[2], b1.d_min[2])},
Vector3{
max(b0.p_max[0], b1.p_max[0]),
max(b0.p_max[1], b1.p_max[1]),
max(b0.p_max[2], b1.p_max[2])},
Vector3{
max(b0.d_max[0], b1.d_max[0]),
max(b0.d_max[1], b1.d_max[1]),
max(b0.d_max[2], b1.d_max[2])}};
}
DEVICE
inline Sphere compute_bounding_sphere(const AABB3 &b) {
auto c = 0.5f * (b.p_max + b.p_min);
auto r = distance(c, b.p_max);
return Sphere{c, r};
}
DEVICE
inline Sphere compute_bounding_sphere(const AABB6 &b) {
auto c = 0.5f * (b.p_max + b.p_min);
auto r = distance(c, b.p_max);
return Sphere{c, r};
}
DEVICE
inline bool inside(const AABB3 &b, const Vector3 &p) {
return p.x >= b.p_min.x && p.x <= b.p_max.x &&
p.y >= b.p_min.y && p.y <= b.p_max.y &&
p.z >= b.p_min.z && p.z <= b.p_max.z;
}
DEVICE
inline bool inside(const AABB6 &b, const Vector3 &p) {
return p.x >= b.p_min.x && p.x <= b.p_max.x &&
p.y >= b.p_min.y && p.y <= b.p_max.y &&
p.z >= b.p_min.z && p.z <= b.p_max.z;
}
DEVICE
inline bool inside(const Sphere &b, const Vector3 &p) {
return distance(p, b.center) <= b.radius;
}
DEVICE
inline Vector3 center(const AABB3 &b) {
return 0.5f * (b.p_max + b.p_min);
}
DEVICE
inline bool intersect(const Sphere &s, const AABB3 &b) {
// "A Simple Method for Box-Sphere Intersection Testing", Jim Arvo
// https://github.com/erich666/GraphicsGems/blob/master/gems/BoxSphere.c
auto d_min = Real(0);
auto r2 = square(s.radius);
for(int i = 0; i < 3; i++) {
if (s.center[i] < b.p_min[i]) {
d_min += square(s.center[i] - b.p_min[i]);
} else if (s.center[i] > b.p_max[i]) {
d_min += square(s.center[i] - b.p_max[i]);
}
if (d_min <= r2) {
return true;
}
}
return false;
}
DEVICE
inline bool intersect(const AABB3 &b, const Ray &r, Real expand_dist = 0) {
// From https://github.com/mmp/pbrt-v3/blob/master/src/core/geometry.h
auto t0 = r.tmin, t1 = r.tmax;
for (int i = 0; i < 3; i++) {
// Update interval for _i_th bounding box slab
auto inv_ray_dir = 1 / r.dir[i];
auto t_near = (b.p_min[i] - expand_dist - r.org[i]) * inv_ray_dir;
auto t_far = (b.p_max[i] + expand_dist - r.org[i]) * inv_ray_dir;
// Update parametric interval from slab intersection $t$ values
if (t_near > t_far) {
swap_(t_near, t_far);
}
// Update t_far to ensure robust ray bounds intersection
t_far *= (1 + 1e-6f);
t0 = t_near > t0 ? t_near : t0;
t1 = t_far < t1 ? t_far : t1;
if (t0 > t1) {
return false;
}
}
return true;
}
DEVICE
inline bool intersect(const AABB6 &b, const Ray &r, Real expand_dist = 0) {
return intersect(convert_aabb<AABB3>(b), r, expand_dist);
}
DEVICE
inline bool intersect(const AABB3 &b, const Ray &r,
const Vector3 &inv_dir, const TVector3<bool> dir_is_neg,
Real expand_dist = 0) {
// From https://github.com/mmp/pbrt-v3/blob/master/src/core/geometry.h
auto t_min = (b[dir_is_neg[0]].x - r.org.x) * inv_dir.x;
auto t_max = (b[1 - dir_is_neg[0]].x - r.org.x) * inv_dir.x;
auto ty_min = (b[dir_is_neg[1]].y - r.org.y) * inv_dir.y;
auto ty_max = (b[1 - dir_is_neg[1]].y - r.org.y) * inv_dir.y;
// Update tMax and tyMax to ensure robust bounds intersection
t_max *= (1 + 1e-6f);
t_min *= (1 + 1e-6f);
if (t_min > ty_max || ty_min > t_max) {
return false;
}
if (ty_min > t_min) {
t_min = ty_min;
}
if (ty_max < t_max) {
t_max = ty_max;
}
// Check for ray intersection against z slab
auto tz_min = (b[dir_is_neg[2]].z - r.org.z) * inv_dir.z;
auto tz_max = (b[1 - dir_is_neg[2]].z - r.org.z) * inv_dir.z;
// Update tzMax to ensure robust bounds intersection
tz_max *= (1 + 1e-6f);
if (t_min > tz_max || tz_min > t_max) return false;
if (tz_min > t_min) t_min = tz_min;
if (tz_max < t_max) t_max = tz_max;
return (t_min < r.tmax) && (t_max > r.tmin);
}
DEVICE
inline bool intersect(const AABB6 &b, const Ray &r,
const Vector3 &inv_dir, const TVector3<bool> dir_is_neg,
Real expand_dist = 0) {
return intersect(convert_aabb<AABB3>(b), r,
inv_dir, dir_is_neg, expand_dist);
}
std::ostream& operator<<(std::ostream &os, const AABB3 &bounds);
std::ostream& operator<<(std::ostream &os, const AABB6 &bounds);