vector: prefer constexpr functions over static variables

This commit is contained in:
Danny Robson 2016-12-12 17:04:22 +11:00
parent d2ba672a30
commit 4bf160934c
5 changed files with 25 additions and 17 deletions

View File

@ -34,7 +34,7 @@ main (void)
auto vec = util::vector4f (0.5f);
t.expect_eq (vec, util::normalised (vec), "normalisation of normalised vector");
t.expect_eq (sum (util::vector4f::ONES), 4.f, "elementwise summation");
t.expect_eq (sum (util::vector4f::ones ()), 4.f, "elementwise summation");
return t.status ();
}

View File

@ -111,8 +111,8 @@ main ()
test_polar (tap);
test_euler (tap);
tap.expect (!is_normalised (util::vector3f::ZERO), "zero isn't normalised");
tap.expect (!is_normalised (util::vector3f::ONES), "ones isn't normalised");
tap.expect (!is_normalised (util::vector3f::zeros ()), "zeros isn't normalised");
tap.expect (!is_normalised (util::vector3f::ones ()), "ones isn't normalised");
tap.expect_eq (
util::hypot (util::vector3f{0,1,2} - util::vector3f{3,2,4}),

View File

@ -112,18 +112,6 @@ util::cartesian_to_spherical (vector<3,T> c)
}
///////////////////////////////////////////////////////////////////////////////
template <size_t S, typename T>
const util::vector<S,T>
util::vector<S,T>::ONES (T{1});
//-----------------------------------------------------------------------------
template <size_t S, typename T>
const util::vector<S,T>
util::vector<S,T>::ZERO (T{0});
///////////////////////////////////////////////////////////////////////////////
template <size_t S, typename T>
void

View File

@ -30,8 +30,8 @@ namespace util {
template <size_t D> vector<D,T> homog (void) const;
// constants
static const vector<S,T> ONES;
static const vector<S,T> ZERO;
static constexpr vector<S,T> ones (void);
static constexpr vector<S,T> zeros (void);
void sanity (void) const;
};

View File

@ -46,3 +46,23 @@ util::cross (util::vector<3,T> a, util::vector<3,T> b)
a.x * b.y - a.y * b.x
};
}
///////////////////////////////////////////////////////////////////////////////
template <size_t S, typename T>
constexpr
util::vector<S,T>
util::vector<S,T>::ones (void)
{
return vector<S,T> {1};
}
//-----------------------------------------------------------------------------
template <size_t S, typename T>
constexpr
util::vector<S,T>
util::vector<S,T>::zeros (void)
{
return vector<S,T> {0};
}