Add UNIT and MAX region objects

This commit is contained in:
Danny Robson 2012-06-15 16:38:57 +10:00
parent c0dcdd89d9
commit baf8ded43d
3 changed files with 35 additions and 6 deletions

View File

@ -39,6 +39,7 @@ region<T>::region (T _x, T _y, size_type _w, size_type _h):
w (_w), w (_w),
h (_h) h (_h)
{ {
DEBUG_ONLY (sanity ());
} }
@ -111,15 +112,15 @@ region<T>::contains (const point<2> &p) const {
} }
// FIXME: This will fail with an actual infinite range (NaNs will be generated
// in the conditionals).
template <typename T> template <typename T>
bool bool
region<T>::overlaps (const region<T> &rhs) const { region<T>::overlaps (const region<T> &rhs) const {
//return !overlap (rhs).empty (); return x < rhs.x + rhs.w &&
rhs.x < x + w &&
return x < sign_cast<T> (rhs.w) + rhs.x && y < rhs.y + rhs.h &&
rhs.x < sign_cast<T> ( w) + x && rhs.y < y + h;
y < sign_cast<T> (rhs.h) + rhs.y &&
rhs.y < sign_cast<T> ( h) + y;
} }
@ -156,6 +157,8 @@ region<T>::operator== (const region& rhs) const
template <typename T> template <typename T>
void void
region<T>::sanity (void) const { region<T>::sanity (void) const {
CHECK_SOFT (w > 0);
CHECK_SOFT (h > 0);
static_assert(!std::is_floating_point<T>::value, static_assert(!std::is_floating_point<T>::value,
"Floating point types need width and height checks"); "Floating point types need width and height checks");
} }
@ -229,6 +232,22 @@ namespace util {
} }
//-----------------------------------------------------------------------------
template <typename T>
const region<T>
region<T>::MAX (std::numeric_limits<T>::lowest (),
std::numeric_limits<T>::lowest (),
std::numeric_limits<T>::has_infinity ? std::numeric_limits<T>::infinity () :
std::numeric_limits<T>::max (),
std::numeric_limits<T>::has_infinity ? std::numeric_limits<T>::infinity () :
std::numeric_limits<T>::max ());
template <typename T>
const region<T>
region<T>::UNIT (0, 0, 1, 1);
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T> template <typename T>
std::ostream& std::ostream&
@ -244,6 +263,7 @@ namespace util {
template struct region<int64_t>; template struct region<int64_t>;
template struct region<uint32_t>; template struct region<uint32_t>;
template struct region<uint64_t>; template struct region<uint64_t>;
template struct region<float>;
template struct region<double>; template struct region<double>;
template std::ostream& operator<< (std::ostream&, const region< int32_t>&); template std::ostream& operator<< (std::ostream&, const region< int32_t>&);

View File

@ -62,6 +62,9 @@ namespace util {
bool operator !=(const region<T>& rhs) const bool operator !=(const region<T>& rhs) const
{ return !(*this == rhs); } { return !(*this == rhs); }
static const region<T> MAX;
static const region<T> UNIT;
void sanity (void) const; void sanity (void) const;
#if 0 #if 0

View File

@ -14,6 +14,12 @@ main (int, char **) {
CHECK_HARD (!a.overlaps (b)); CHECK_HARD (!a.overlaps (b));
} }
CHECK_HARD (region<double>::MAX.overlaps (region<double>::UNIT));
CHECK_HARD (region< float>::MAX.overlaps (region< float>::UNIT));
CHECK_EQ (region<double>::UNIT.area (), 1.0);
CHECK_EQ (region< float>::UNIT.area (), 1.0f);
CHECK_HARD (region<int> (0, 0, 2, 2).includes (point2(1.0, 1.0))); CHECK_HARD (region<int> (0, 0, 2, 2).includes (point2(1.0, 1.0)));
CHECK_HARD (region<int> (0, 0, 2, 2).includes (point2(0.0, 0.0))); CHECK_HARD (region<int> (0, 0, 2, 2).includes (point2(0.0, 0.0)));
CHECK_HARD (region<int> (0, 0, 2, 2).includes (point2(2.0, 2.0))); CHECK_HARD (region<int> (0, 0, 2, 2).includes (point2(2.0, 2.0)));