From baf8ded43d7c28c5a4ba6dc96a879ee3551a6e6c Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Fri, 15 Jun 2012 16:38:57 +1000 Subject: [PATCH] Add UNIT and MAX region objects --- region.cpp | 32 ++++++++++++++++++++++++++------ region.hpp | 3 +++ test/region.cpp | 6 ++++++ 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/region.cpp b/region.cpp index dbe7d76d..9ca19a9b 100644 --- a/region.cpp +++ b/region.cpp @@ -39,6 +39,7 @@ region::region (T _x, T _y, size_type _w, size_type _h): w (_w), h (_h) { + DEBUG_ONLY (sanity ()); } @@ -111,15 +112,15 @@ region::contains (const point<2> &p) const { } +// FIXME: This will fail with an actual infinite range (NaNs will be generated +// in the conditionals). template bool region::overlaps (const region &rhs) const { - //return !overlap (rhs).empty (); - - return x < sign_cast (rhs.w) + rhs.x && - rhs.x < sign_cast ( w) + x && - y < sign_cast (rhs.h) + rhs.y && - rhs.y < sign_cast ( h) + y; + return x < rhs.x + rhs.w && + rhs.x < x + w && + y < rhs.y + rhs.h && + rhs.y < y + h; } @@ -156,6 +157,8 @@ region::operator== (const region& rhs) const template void region::sanity (void) const { + CHECK_SOFT (w > 0); + CHECK_SOFT (h > 0); static_assert(!std::is_floating_point::value, "Floating point types need width and height checks"); } @@ -229,6 +232,22 @@ namespace util { } +//----------------------------------------------------------------------------- +template +const region +region::MAX (std::numeric_limits::lowest (), + std::numeric_limits::lowest (), + std::numeric_limits::has_infinity ? std::numeric_limits::infinity () : + std::numeric_limits::max (), + std::numeric_limits::has_infinity ? std::numeric_limits::infinity () : + std::numeric_limits::max ()); + + +template +const region +region::UNIT (0, 0, 1, 1); + + //----------------------------------------------------------------------------- template std::ostream& @@ -244,6 +263,7 @@ namespace util { template struct region; template struct region; template struct region; + template struct region; template struct region; template std::ostream& operator<< (std::ostream&, const region< int32_t>&); diff --git a/region.hpp b/region.hpp index 16b6c981..231d23ea 100644 --- a/region.hpp +++ b/region.hpp @@ -62,6 +62,9 @@ namespace util { bool operator !=(const region& rhs) const { return !(*this == rhs); } + static const region MAX; + static const region UNIT; + void sanity (void) const; #if 0 diff --git a/test/region.cpp b/test/region.cpp index aaf7456f..afd7427c 100644 --- a/test/region.cpp +++ b/test/region.cpp @@ -14,6 +14,12 @@ main (int, char **) { CHECK_HARD (!a.overlaps (b)); } + CHECK_HARD (region::MAX.overlaps (region::UNIT)); + CHECK_HARD (region< float>::MAX.overlaps (region< float>::UNIT)); + + CHECK_EQ (region::UNIT.area (), 1.0); + CHECK_EQ (region< float>::UNIT.area (), 1.0f); + CHECK_HARD (region (0, 0, 2, 2).includes (point2(1.0, 1.0))); CHECK_HARD (region (0, 0, 2, 2).includes (point2(0.0, 0.0))); CHECK_HARD (region (0, 0, 2, 2).includes (point2(2.0, 2.0)));