geom/aabb: rename p0,p1 to lo,hi

this makes their meaning more explicit
This commit is contained in:
Danny Robson 2018-03-13 15:42:29 +11:00
parent 583e4aa26d
commit 95d13b9fe4
3 changed files with 63 additions and 84 deletions

View File

@ -24,78 +24,13 @@
using util::geom::aabb;
///////////////////////////////////////////////////////////////////////////////
template <size_t S, typename T>
aabb<S,T>::aabb (const point<S,T> _p0, const point<S,T> _p1):
p0 (_p0),
p1 (_p1)
{
debug::sanity (*this);
}
///////////////////////////////////////////////////////////////////////////////
template <size_t S, typename T>
T
aabb<S,T>::diameter (void) const
{
return magnitude ().diameter ();
}
//-----------------------------------------------------------------------------
template <size_t S, typename T>
util::extent<S,T>
aabb<S,T>::magnitude (void) const
{
return (p1 - p0).template as<util::extent> ();
}
//-----------------------------------------------------------------------------
template <size_t S, typename T>
util::point<S,T>
aabb<S,T>::closest (const point<S,T> q) const
{
return limit (q, p0, p1);
}
//-----------------------------------------------------------------------------
template <size_t S, typename T>
void
aabb<S,T>::cover (const point<S,T> p)
{
p0 = min (p, p0);
p1 = max (p, p1);
}
///////////////////////////////////////////////////////////////////////////////
template <size_t S, typename T>
aabb<S,T>
aabb<S,T>::operator+ (const vector<S,T> v) const
{
return { p0 + v, p1 + v };
}
//-----------------------------------------------------------------------------
template <size_t S, typename T>
aabb<S,T>
aabb<S,T>::operator- (const vector<S,T> v) const
{
return { p0 - v, p1 - v };
}
///////////////////////////////////////////////////////////////////////////////
namespace util::debug {
template <size_t S, typename T>
struct validator<aabb<S,T>> {
static bool is_valid (const aabb<S,T> &b)
{
return all (b.p0 <= b.p1);
return all (b.lo <= b.hi);
}
};
}
@ -106,7 +41,7 @@ template <size_t S, typename T>
std::ostream&
util::geom::operator<< (std::ostream &os, util::geom::aabb<S,T> b)
{
return os << "[ " << b.p0 << ", " << b.p1 << " ]";
return os << "[ " << b.lo << ", " << b.hi << " ]";
}

View File

@ -18,8 +18,9 @@
#ifndef CRUFT_UTIL_GEOM_AABB_HPP
#define CRUFT_UTIL_GEOM_AABB_HPP
#include "../point.hpp"
#include "../debug.hpp"
#include "../extent.hpp"
#include "../point.hpp"
#include <cstdint>
@ -27,30 +28,73 @@ namespace util::geom {
///////////////////////////////////////////////////////////////////////////
/// represents an axis-aligned bounding-box through two opposing corners.
///
/// p0 must be less-than-or-equal to p1. equality is allowed so that we
/// can represent zero sized bounding-boxes.
/// `lo' must be less-than-or-equal to `hi'. equality is allowed so that
/// we can represent zero sized bounding-boxes.
template <size_t S, typename T>
struct aabb {
aabb () = default;
aabb (point<S,T>, point<S,T>);
aabb (point<S,T> _lo, point<S,T> _hi):
lo (_lo),
hi (_hi)
{
CHECK (all (lo <= hi));
}
extent<S,T> magnitude (void) const;
T diameter (void) const;
///////////////////////////////////////////////////////////////////////
extent<S,T>
magnitude (void) const
{
return (hi - lo).template as<util::extent> ();
}
//---------------------------------------------------------------------
T
diameter (void) const
{
return magnitude ().diameter ();
}
//---------------------------------------------------------------------
/// tests whether a point lies within the region, inclusive of borders
constexpr bool
inclusive (point<S,T> p) const noexcept
{ return all (p0 <= p && p1 >= p); }
{ return all (lo <= p && hi >= p); }
point<S,T> closest (point<S,T>) const;
void cover (point<S,T>);
//---------------------------------------------------------------------
point<S,T>
closest (point<S,T> query) const
{
return limit (query, lo, hi);
}
aabb<S,T> operator+ (vector<S,T>) const;
aabb<S,T> operator- (vector<S,T>) const;
::util::point<S,T> p0;
::util::point<S,T> p1;
//---------------------------------------------------------------------
void cover (point<S,T> p)
{
lo = min (p, lo);
hi = max (p, hi);
}
//---------------------------------------------------------------------
aabb<S,T> operator+ (vector<S,T> v) const
{
return { lo + v, hi + v };
}
aabb<S,T> operator- (vector<S,T> v) const
{
return { lo - v, hi - v };
}
::util::point<S,T> lo;
::util::point<S,T> hi;
};
@ -59,7 +103,7 @@ namespace util::geom {
constexpr bool
operator== (const aabb<S,T> &a, const aabb<S,T> &b) noexcept
{
return a.p0 == b.p0 && a.p1 == b.p1;
return a.lo == b.lo && a.hi == b.hi;
}
@ -90,7 +134,7 @@ namespace util::geom {
point<S,T> p;
std::generate (p.begin (), p.end (), [&] (void) { return d (g); });
return p * (b.p1 - b.p0) + b.p0.template as<util::vector> ();
return p * (b.hi - b.lo) + b.lo.template as<util::vector> ();
}
};
}

View File

@ -46,8 +46,8 @@ template <size_t S, typename T>
T
ray<S,T>::intersect (aabb<S,T> r) const
{
auto t1 = (r.p0 - origin) / direction;
auto t2 = (r.p1 - origin) / direction;
auto t1 = (r.lo - origin) / direction;
auto t2 = (r.hi - origin) / direction;
auto vmin = min (t1, t2);
auto vmax = max (t1, t2);