region: remove duplicate point/region coverage tests

This commit is contained in:
Danny Robson 2017-08-11 15:15:44 +10:00
parent f36a14973a
commit 31d2e6bfd8
3 changed files with 31 additions and 62 deletions

View File

@ -11,7 +11,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* *
* Copyright 2010-2016 Danny Robson <danny@nerdcruft.net> * Copyright 2010-2017 Danny Robson <danny@nerdcruft.net>
*/ */
@ -23,6 +23,8 @@
#include <array> #include <array>
using util::region;
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <size_t S, typename T> template <size_t S, typename T>
@ -159,41 +161,6 @@ util::region<S,T>::closest (point_t q) const
} }
//-----------------------------------------------------------------------------
template <size_t S, typename T>
bool
util::region<S,T>::includes (point_t q) const
{
for (size_t i = 0; i < S; ++i)
if (q[i] < p[i] || q[i] > p[i] + e[i])
return false;
return true;
}
//-----------------------------------------------------------------------------
template <size_t S, typename T>
bool
util::region<S,T>::contains (point_t q) const
{
for (size_t i = 0; i < S; ++i)
if (q[i] <= p[i] || q[i] >= p[i] + e[i])
return false;
return true;
}
//-----------------------------------------------------------------------------
template <size_t S, typename T>
bool
util::region<S,T>::has (const point_t q) const noexcept
{
return all (q >= p) && all (q < p + e);
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// FIXME: This will fail with an actual infinite range (NaNs will be generated // FIXME: This will fail with an actual infinite range (NaNs will be generated
// in the conditionals). // in the conditionals).
@ -202,7 +169,7 @@ bool
util::region<S,T>::intersects (region<S,T> rhs) const util::region<S,T>::intersects (region<S,T> rhs) const
{ {
for (size_t i = 0; i < S; ++i) for (size_t i = 0; i < S; ++i)
if (p[i] >= rhs.p[i] + rhs.e[i] || if ( p[i] >= rhs.p[i] + rhs.e[i] ||
rhs.p[i] >= p[i] + e[i]) rhs.p[i] >= p[i] + e[i])
{ return false; } { return false; }
@ -253,12 +220,21 @@ util::region<S,T>::intersection (region<S,T> rhs) const
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <size_t S, typename T> template <size_t S, typename T>
bool bool
util::region<S,T>::encloses (const region<S,T> r) const noexcept util::region<S,T>::covers (region<S, T> r) const noexcept
{ {
return all (p <= r.p) && all (p + e >= r.p + r.e); return all (p <= r.p) && all (p + e >= r.p + r.e);
} }
//-----------------------------------------------------------------------------
template <size_t S, typename T>
bool
region<S,T>::covers (const point<S,T> q) const noexcept
{
return all (p <= q) && all (p + e >= q);
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
template <size_t S, typename T> template <size_t S, typename T>
util::region<S,T> util::region<S,T>

View File

@ -70,21 +70,19 @@ namespace util {
point_t closest (point_t) const; point_t closest (point_t) const;
//--------------------------------------------------------------------- //---------------------------------------------------------------------
// Point and region relation queries // exclusive of borders
bool includes (point_t) const; // inclusive of borders bool intersects (region<S,T>) const;
bool contains (point_t) const; // exclusive of borders
bool intersects (region<S,T>) const; // exclusive of borders
bool has (point_t) const noexcept; // inclusive of top and left borders
// Move a point to be within the region bounds
void constrain (point_t&) const;
point_t constrained (point_t) const;
// Compute binary region combinations // Compute binary region combinations
region intersection (region<S,T>) const; region intersection (region<S,T>) const;
// Test if a region lies completely within our space // Test if a region lies completely within our space
bool encloses (region<S,T>) const noexcept; bool covers (region<S,T>) const noexcept;
// Test if a point lies within out space. Inclusive of borders
bool covers (point<S,T>) const noexcept;
// Move a point to be within the region bounds
void constrain (point_t&) const;
point_t constrained (point_t) const;
//--------------------------------------------------------------------- //---------------------------------------------------------------------
// Compute a region `mag` units into the region // Compute a region `mag` units into the region

View File

@ -46,21 +46,16 @@ main (int, char **)
// test boundary cases of includes and contains // test boundary cases of includes and contains
{ {
const util::point2u p0 { 0 }; const util::point2f p0 { 0 };
const util::extent2u e0 { 2 }; const util::extent2f e0 { 2 };
const util::region2u r {p0, e0}; const util::region2f r {p0, e0};
tap.expect (r.includes (util::point2u {1, 1}), "unsigned region centre inclusion"); tap.expect (!r.covers (util::point2f {-1, 1}), "region/point covers, invalid x");
tap.expect (r.includes (util::point2u {0, 0}), "unsigned region base inclusion"); tap.expect (!r.covers (util::point2f { 1, 3}), "region/point covers, invalid y ");
tap.expect (r.includes (util::point2u {2, 2}), "unsigned region corner inclusion");
tap.expect ( r.contains (util::point2u {1, 1}), "unsigned region center contains"); tap.expect (r.covers (util::point2f {1, 1}), "region/point covers, centre");
tap.expect (!r.contains (util::point2u {0, 0}), "unsigned region base contains"); tap.expect (r.covers (util::point2f {0, 0}), "region/point covers, base");
tap.expect (!r.contains (util::point2u {2, 2}), "unsigned region corner contains"); tap.expect (r.covers (util::point2f {2, 2}), "region/point covers, corner");
tap.expect ( r.has (util::point2u {1, 1}), "unsigned region centre has");
tap.expect ( r.has (util::point2u {1, 1}), "unsigned region base has");
tap.expect (!r.has (util::point2u {2, 2}), "unsigned region corner has");
} }
// ensure make_union behaves as expected // ensure make_union behaves as expected