line: add AABB intersection test

This commit is contained in:
Danny Robson 2015-03-06 17:53:08 +11:00
parent 832472c861
commit ae484f7e74
2 changed files with 31 additions and 0 deletions

View File

@ -46,6 +46,34 @@ util::line<S,T>::intersect (plane<S,T> q) const
}
///----------------------------------------------------------------------------
/// returns the distance from origin to AABB intersection
///
/// returns NaN on miss
/// returns -ve if behind
template <size_t S, typename T>
T
util::line<S,T>::intersect (region<S,T> r) const
{
auto t1 = (r.base () - p) / d;
auto t2 = (r.away () - p) / d;
auto vmin = min (t1, t2);
auto vmax = min (t1, t2);
auto tmin = min (vmax);
auto tmax = max (vmin);
if (tmax < 0)
return tmax;
if (tmin > tmax)
return std::numeric_limits<T>::quiet_NaN ();
return tmin;
}
///----------------------------------------------------------------------------
/// returns the closest parameter along the line to a given point
template <size_t S, typename T>

View File

@ -23,6 +23,7 @@
#include "point.hpp"
#include "vector.hpp"
#include "plane.hpp"
#include "region.hpp"
namespace util {
template <size_t S, typename T>
@ -31,6 +32,8 @@ namespace util {
util::vector<S,T> direction);
T intersect (plane<S,T>) const;
T intersect (region<S,T>) const;
T closest (point<S,T>) const;
util::point<S,T> at (T) const;