coord: add the select function

This commit is contained in:
Danny Robson 2017-07-31 15:41:33 +10:00
parent 2fad1715cf
commit 0016c83dab
2 changed files with 35 additions and 0 deletions

View File

@ -854,6 +854,30 @@ namespace util {
}
///------------------------------------------------------------------------
/// returns an instance of K elementwise using a when s is true, and b
/// otherwise. ie, k[i] = s[i] ? a[i] : b[i];
///
/// corresponds to the function `select' from OpenCL.
template <
size_t S,
typename T,
template <size_t,typename> class K,
typename = std::enable_if_t<
is_coord_v<K<S,T>>,
void
>
>
constexpr
K<S,T>
select (vector<S,bool> s, K<S,T> a, K<S,T> b)
{
K<S,T> k {};
for (size_t i = 0; i < S; ++i)
k[i] = s[i] ? a[i] : b[i];
return k;
}
///////////////////////////////////////////////////////////////////////////
template <
size_t S,

View File

@ -53,5 +53,16 @@ main (void)
);
}
{
const util::point3f a { -1, 2, 0 };
const util::point3f b { 1, 0, 2 };
const util::point3f lo { -1, 0, 0 };
const util::point3f hi { 1, 2, 2 };
tap.expect_eq (select (a < b, a, b), lo, "select with points and min");
tap.expect_eq (select (a > b, a, b), hi, "select with points and max");
};
return tap.status ();
}