Skip to content

Commit

Permalink
Fix for issue #21: Limit the inverse computation to epsilon to avoid …
Browse files Browse the repository at this point in the history
…false positives in the node-ray intersection function
  • Loading branch information
madmann91 committed Dec 15, 2020
1 parent 46d9e5e commit 3b2f3d0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion include/bvh/node_intersectors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ struct FastNodeIntersector : public NodeIntersector<Bvh, FastNodeIntersector<Bvh
FastNodeIntersector(const Ray<Scalar>& ray)
: NodeIntersector<Bvh, FastNodeIntersector<Bvh>>(ray)
{
inverse_direction = ray.direction.inverse();
inverse_direction = ray.direction.safe_inverse();
scaled_origin = -ray.origin * inverse_direction;
}

Expand Down
8 changes: 8 additions & 0 deletions include/bvh/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <algorithm>
#include <array>
#include <cmath>
#include <limits>
#include <type_traits>

#include "bvh/platform.hpp"
Expand Down Expand Up @@ -65,6 +66,13 @@ struct Vector {
return Vector([this] (size_t i) { return Scalar(1) / values[i]; });
}

bvh__always_inline__ Vector safe_inverse() const {
static constexpr auto threshold = std::numeric_limits<Scalar>::epsilon();
return Vector([&] (size_t i) {
return Scalar(1) / (std::fabs(values[i]) < threshold ? threshold : values[i]);
});
}

bvh__always_inline__ Vector& operator += (const Vector& other) {
return *this = *this + other;
}
Expand Down
2 changes: 1 addition & 1 deletion test/benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ void render(
traversal_steps += statistics.traversal_steps;
intersections += statistics.intersections;
}
if(!hit) {
if (!hit) {
pixels[index] = pixels[index + 1] = pixels[index + 2] = 0;
} else {
if (CollectStatistics) {
Expand Down

0 comments on commit 3b2f3d0

Please sign in to comment.