Skip to content

Commit fb066d0

Browse files
committed
Add both implementations of aabb::hit()
See issue RayTracing#817
1 parent 1cfc889 commit fb066d0

File tree

1 file changed

+36
-12
lines changed

1 file changed

+36
-12
lines changed

src/common/aabb.h

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,43 @@ class aabb {
4242
point3 min() const { return minimum; }
4343
point3 max() const { return maximum; }
4444

45-
bool hit(const ray& r, interval ray_t) const {
46-
for (int a = 0; a < 3; a++) {
47-
auto t0 = fmin((minimum[a] - r.origin()[a]) / r.direction()[a],
48-
(maximum[a] - r.origin()[a]) / r.direction()[a]);
49-
auto t1 = fmax((minimum[a] - r.origin()[a]) / r.direction()[a],
50-
(maximum[a] - r.origin()[a]) / r.direction()[a]);
51-
ray_t.min = fmax(t0, ray_t.min);
52-
ray_t.max = fmin(t1, ray_t.max);
53-
if (ray_t.max <= ray_t.min)
54-
return false;
45+
#if 1
46+
// GitHub Issue #817
47+
// For some reason I haven't figured out yet, this version is 10x faster than the
48+
// version below. I'll come back and figure out why (and in the process, probably figure
49+
// out how to configure CMake to create a profile build). Parking this here for now, to
50+
// be removed before the v4 release.
51+
52+
bool hit(const ray& r, interval ray_t) const {
53+
for (int a = 0; a < 3; a++) {
54+
auto t0 = fmin((minimum[a] - r.origin()[a]) / r.direction()[a],
55+
(maximum[a] - r.origin()[a]) / r.direction()[a]);
56+
auto t1 = fmax((minimum[a] - r.origin()[a]) / r.direction()[a],
57+
(maximum[a] - r.origin()[a]) / r.direction()[a]);
58+
ray_t.min = fmax(t0, ray_t.min);
59+
ray_t.max = fmin(t1, ray_t.max);
60+
if (ray_t.max <= ray_t.min)
61+
return false;
62+
}
63+
return true;
5564
}
56-
return true;
57-
}
65+
#else
66+
bool hit(const ray& r, interval ray_t) const {
67+
auto r_origin = r.origin();
68+
auto r_dir = r.direction();
69+
for (int a = 0; a < 3; a++) {
70+
auto invD = 1.0f / r_dir[a];
71+
auto orig = r_origin[a];
72+
auto t0 = (minimum[a] - orig) * invD;
73+
auto t1 = (maximum[a] - orig) * invD;
74+
if (invD < 0)
75+
std::swap(t0, t1);
76+
if (fmin(t1, ray_t.max) <= fmax(t0, ray_t.min))
77+
return false;
78+
}
79+
return true;
80+
}
81+
#endif
5882

5983
public:
6084
point3 minimum;

0 commit comments

Comments
 (0)