diff --git a/line.cpp b/line.cpp index c1c84a00..9998dee7 100644 --- a/line.cpp +++ b/line.cpp @@ -46,6 +46,34 @@ util::line::intersect (plane q) const } +///---------------------------------------------------------------------------- +/// returns the distance from origin to AABB intersection +/// +/// returns NaN on miss +/// returns -ve if behind +template +T +util::line::intersect (region 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::quiet_NaN (); + + return tmin; +} + + ///---------------------------------------------------------------------------- /// returns the closest parameter along the line to a given point template diff --git a/line.hpp b/line.hpp index 1b5d017d..477e45b3 100644 --- a/line.hpp +++ b/line.hpp @@ -23,6 +23,7 @@ #include "point.hpp" #include "vector.hpp" #include "plane.hpp" +#include "region.hpp" namespace util { template @@ -31,6 +32,8 @@ namespace util { util::vector direction); T intersect (plane) const; + T intersect (region) const; + T closest (point) const; util::point at (T) const;