Skip to content

Commit

Permalink
Improved Ray-Box intersection
Browse files Browse the repository at this point in the history
Using reciprocal ray direction values.
Using std::fmin and std::fmax instead of 'if' statement.
  • Loading branch information
tay10r committed Mar 29, 2020
1 parent 1d1f019 commit 9a0043c
Showing 1 changed file with 10 additions and 20 deletions.
30 changes: 10 additions & 20 deletions include/FastBVH/BBox.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,15 @@ struct BBox final {
template <typename Float>
bool BBox<Float>::intersect(const Ray<Float>& ray, Float *tnear, Float *tfar) const noexcept {

Float tmin = (min.x - ray.o.x) / ray.d.x;
Float tmax = (max.x - ray.o.x) / ray.d.x;
Float tmin = (min.x - ray.o.x) * ray.inv_d.x;
Float tmax = (max.x - ray.o.x) * ray.inv_d.x;

if (tmin > tmax) {
std::swap(tmin, tmax);
}

Float tymin = (min.y - ray.o.y) / ray.d.y;
Float tymax = (max.y - ray.o.y) / ray.d.y;
Float tymin = (min.y - ray.o.y) * ray.inv_d.y;
Float tymax = (max.y - ray.o.y) * ray.inv_d.y;

if (tymin > tymax) {
std::swap(tymin, tymax);
Expand All @@ -111,16 +111,11 @@ bool BBox<Float>::intersect(const Ray<Float>& ray, Float *tnear, Float *tfar) co
return false;
}

if (tymin > tmin) {
tmin = tymin;
}

if (tymax < tmax) {
tmax = tymax;
}
tmin = std::fmax(tymin, tmin);
tmax = std::fmin(tymax, tmax);

Float tzmin = (min.z - ray.o.z) / ray.d.z;
Float tzmax = (max.z - ray.o.z) / ray.d.z;
Float tzmin = (min.z - ray.o.z) * ray.inv_d.z;
Float tzmax = (max.z - ray.o.z) * ray.inv_d.z;

if (tzmin > tzmax) {
std::swap(tzmin, tzmax);
Expand All @@ -130,13 +125,8 @@ bool BBox<Float>::intersect(const Ray<Float>& ray, Float *tnear, Float *tfar) co
return false;
}

if (tzmin > tmin) {
tmin = tzmin;
}

if (tzmax < tmax) {
tmax = tzmax;
}
tmin = std::fmax(tzmin, tmin);
tmax = std::fmin(tzmax, tmax);

*tnear = tmin;
*tfar = tmax;
Expand Down

0 comments on commit 9a0043c

Please sign in to comment.