point: add homogenous redim

This commit is contained in:
Danny Robson 2015-02-19 13:19:27 +11:00
parent 049aaf84c9
commit 6ac02a9920
3 changed files with 39 additions and 0 deletions

View File

@ -61,6 +61,8 @@ namespace util {
template <size_t D> point<D,T> redim (const util::point<D,T> &fill) const; template <size_t D> point<D,T> redim (const util::point<D,T> &fill) const;
template <size_t D> point<D,T> redim (T fill) const; template <size_t D> point<D,T> redim (T fill) const;
template <size_t D> point<D,T> homog (void) const;
static const point<S,T> ORIGIN; static const point<S,T> ORIGIN;
template<typename U> point<S,U> cast (void) const; template<typename U> point<S,U> cast (void) const;

View File

@ -111,6 +111,33 @@ namespace util {
} }
///------------------------------------------------------------------------
/// expand point to use homogenous coordinates of a higher dimension.
/// ie, fill with (0,..,0,1)
template <size_t S, typename T>
template <size_t D>
point<D,T>
point<S,T>::homog (void) const
{
static_assert (D > S, "homog will not overwrite data");
point<D,T> out;
// Copy the existing data
auto c = std::copy (this->begin (),
this->end (),
out.begin ());
// Fill until the second last element with zeros
auto f = std::fill_n (c, D - S - 1, T{0});
// Last element should be one
*f = T{1};
return out;
}
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
template <size_t S, typename T> template <size_t S, typename T>
template <typename U> template <typename U>

View File

@ -37,4 +37,14 @@ main (int, char**) {
CHECK_EQ (q.data[2], FILL.data[2]); CHECK_EQ (q.data[2], FILL.data[2]);
CHECK_EQ (q.data[3], FILL.data[3]); CHECK_EQ (q.data[3], FILL.data[3]);
} }
{
const point2f p (3, 4);
const point4f q = p.homog<4> ();
CHECK_EQ (q.x, 3);
CHECK_EQ (q.y, 4);
CHECK_EQ (q.z, 0);
CHECK_EQ (q.w, 1);
}
} }