g/frustum: simplify aabb intersection test

This commit is contained in:
Danny Robson 2018-03-28 20:42:43 +11:00
parent 48e007c545
commit a4634a771b
2 changed files with 9 additions and 11 deletions

View File

@ -47,20 +47,17 @@ namespace util::geom {
intersects (const frustum<T> &f, const aabb<S,T> &b)
{
for (const auto &p: f.planes) {
const auto n = normal (p);
const auto positive = n > 0;
// find the distance to the point, if it's positive it's inside
// the frustum.
const auto d = max (
p.coefficients * b.lo.template redim<S+1> (1),
p.coefficients * b.hi.template redim<S+1> (1)
);
const auto lo = select (positive, b.lo, b.hi);
const auto hi = select (positive, b.hi, b.lo);
if (dot (n, lo) + p.coefficients[S] > 0)
return false; // OUTSIDE
if (dot (n, hi) + p.coefficients[S] >= 0)
return true; //INTERSECT
if (sum (d) < 0)
return false;
}
// INSIDE
return true;
}
};

View File

@ -25,6 +25,7 @@ main ()
{ { { -1, -1, -1 }, { 1, 1, 1 } }, true, "covers origin with unit radius" },
{ { { -9, -9, -9 }, { -8, -8, -8 } }, false, "out of view negative" },
{ { { -1, -1, 2 }, { 1, 1, 3 } }, false, "past far plane" },
{ { { 0, 0, 0 }, { 0, 0, 0 } }, false, "before near plane" },
};
for (const auto &t: TESTS) {