Add ray-box intersection

This commit is contained in:
Nikita Lisitsa 2020-09-23 22:25:39 +03:00
parent fb0d926a73
commit 1571e9032b

View file

@ -3,6 +3,8 @@
#include <psemek/util/empty.hpp>
#include <psemek/geom/simplex.hpp>
#include <psemek/geom/ray.hpp>
#include <psemek/geom/box.hpp>
#include <psemek/geom/orientation.hpp>
#include <psemek/geom/contains.hpp>
@ -102,4 +104,28 @@ namespace psemek::geom
return false;
}
template <typename T, std::size_t N>
interval<T> intersection(ray<T, N> const & r, box<T, N> const & b)
{
auto t = interval<T>::full();
for (std::size_t i = 0; i < N; ++i)
{
T tmin = (b[i].min - r.origin[i]) / r.direction[i];
T tmax = (b[i].max - r.origin[i]) / r.direction[i];
if (tmin > tmax) std::swap(tmin, tmax);
t &= interval<T>{tmin, tmax};
}
return t;
}
template <typename T, std::size_t N>
bool intersect(ray<T, N> const & r, box<T, N> const & b)
{
return !intersection(r, b).empty();
}
}