line: add AABB intersection test
This commit is contained in:
parent
832472c861
commit
ae484f7e74
28
line.cpp
28
line.cpp
@ -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
|
/// returns the closest parameter along the line to a given point
|
||||||
template <size_t S, typename T>
|
template <size_t S, typename T>
|
||||||
|
3
line.hpp
3
line.hpp
@ -23,6 +23,7 @@
|
|||||||
#include "point.hpp"
|
#include "point.hpp"
|
||||||
#include "vector.hpp"
|
#include "vector.hpp"
|
||||||
#include "plane.hpp"
|
#include "plane.hpp"
|
||||||
|
#include "region.hpp"
|
||||||
|
|
||||||
namespace util {
|
namespace util {
|
||||||
template <size_t S, typename T>
|
template <size_t S, typename T>
|
||||||
@ -31,6 +32,8 @@ namespace util {
|
|||||||
util::vector<S,T> direction);
|
util::vector<S,T> direction);
|
||||||
|
|
||||||
T intersect (plane<S,T>) const;
|
T intersect (plane<S,T>) const;
|
||||||
|
T intersect (region<S,T>) const;
|
||||||
|
|
||||||
T closest (point<S,T>) const;
|
T closest (point<S,T>) const;
|
||||||
|
|
||||||
util::point<S,T> at (T) const;
|
util::point<S,T> at (T) const;
|
||||||
|
Loading…
Reference in New Issue
Block a user