From 6ac02a9920443c517b7e7212381a5b3bae1d90d2 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Thu, 19 Feb 2015 13:19:27 +1100 Subject: [PATCH] point: add homogenous redim --- point.hpp | 2 ++ point.ipp | 27 +++++++++++++++++++++++++++ test/point.cpp | 10 ++++++++++ 3 files changed, 39 insertions(+) diff --git a/point.hpp b/point.hpp index 34d73680..b244799a 100644 --- a/point.hpp +++ b/point.hpp @@ -61,6 +61,8 @@ namespace util { template point redim (const util::point &fill) const; template point redim (T fill) const; + template point homog (void) const; + static const point ORIGIN; template point cast (void) const; diff --git a/point.ipp b/point.ipp index bef9db60..5c2abf0c 100644 --- a/point.ipp +++ b/point.ipp @@ -111,6 +111,33 @@ namespace util { } + ///------------------------------------------------------------------------ + /// expand point to use homogenous coordinates of a higher dimension. + /// ie, fill with (0,..,0,1) + template + template + point + point::homog (void) const + { + static_assert (D > S, "homog will not overwrite data"); + + point 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 template diff --git a/test/point.cpp b/test/point.cpp index 29ea275a..b46ac108 100644 --- a/test/point.cpp +++ b/test/point.cpp @@ -37,4 +37,14 @@ main (int, char**) { CHECK_EQ (q.data[2], FILL.data[2]); 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); + } }