region: prefer constexpr functions over static variables

This commit is contained in:
Danny Robson 2016-12-12 17:07:53 +11:00
parent b5932c4537
commit 2dcb315ce6
4 changed files with 36 additions and 31 deletions

View File

@ -232,8 +232,8 @@ util::region<S,T>::intersection (region<S,T> rhs) const
point_t a, b;
for (size_t i = 0; i < S; ++i) {
a[i] = max (p[i], rhs.p[i]);
b[i] = min (p[i] + e[i], rhs.p[i] + rhs.e[i]);
a[i] = util::max (p[i], rhs.p[i]);
b[i] = util::min (p[i] + e[i], rhs.p[i] + rhs.e[i]);
if (b[i] < a[i])
throw std::logic_error ("no overlap");
@ -336,28 +336,7 @@ util::region<S,T>::operator== (region rhs) const
}
///----------------------------------------------------------------------------
/// The largest specifiable finite region.
///
/// Starts at half the minimum value to allow the width to cover some positive
/// range rather than just cancelling out the lowest value for signed types.
///
/// Specifically does not allow infinities. Use/define INFINITE when required.
template <size_t S, typename T>
const util::region<S,T>
util::region<S,T>::MAX (
util::point<S,T> {std::numeric_limits<T>::lowest () / 2},
util::extent<S,T> {std::numeric_limits<T>::max ()}
);
template <size_t S, typename T>
const util::region<S,T>
util::region<S,T>::UNIT (util::point<S,T>{0}, util::extent<S,T>{1});
//-----------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////
template <size_t S, typename T>
std::ostream&
util::operator<< (std::ostream &os, const util::region<S,T> &rhs) {

View File

@ -105,8 +105,8 @@ namespace util {
{ return !(*this == rhs); }
// Utility constants
static const region<S,T> MAX;
static const region<S,T> UNIT;
static constexpr region<S,T> max (void);
static constexpr region<S,T> unit (void);
void sanity (void) const;
};

View File

@ -31,3 +31,29 @@ util::region<S,T>::cast (void) const
e.template cast<U> ()
};
}
///////////////////////////////////////////////////////////////////////////////
template <size_t S, typename T>
constexpr
util::region<S,T>
util::region<S,T>::max (void)
{
return {
util::point <S,T> {std::numeric_limits<T>::lowest () / 2},
util::extent<S,T> {std::numeric_limits<T>::max ()}
};
}
//-----------------------------------------------------------------------------
template <size_t S, typename T>
constexpr
util::region<S,T>
util::region<S,T>::unit (void)
{
return {
point_t::origin (),
extent_t {1}
};
}

View File

@ -23,11 +23,11 @@ main (int, char **)
tap.expect (!a.intersects (b), "simple 2d intersection");
}
tap.expect (util::region2d::MAX.intersects (util::region2d::UNIT), "maximal region2d intersection");
tap.expect (util::region2f::MAX.intersects (util::region2f::UNIT), "maximal region2f intersection");
tap.expect (util::region2d::max ().intersects (util::region2d::unit ()), "maximal region2d intersection");
tap.expect (util::region2f::max ().intersects (util::region2f::unit ()), "maximal region2f intersection");
tap.expect_eq (util::region2d::UNIT.area (), 1.0, "unit region2d area");
tap.expect_eq (util::region2f::UNIT.area (), 1.0f, "unit region2f area");
tap.expect_eq (util::region2d::unit ().area (), 1.0, "unit region2d area");
tap.expect_eq (util::region2f::unit ().area (), 1.0f, "unit region2f area");
util::point2u p0 { 0 };
util::extent2u e0 { 2 };