coord: move cast/redim operations to coord

This commit is contained in:
Danny Robson 2015-04-09 17:58:47 +10:00
parent 5428c93b9a
commit bd88832df3
9 changed files with 70 additions and 183 deletions

View File

@ -183,7 +183,6 @@ UTIL_FILES = \
uri.cpp \ uri.cpp \
vector.cpp \ vector.cpp \
vector.hpp \ vector.hpp \
vector.ipp \
version.cpp \ version.cpp \
version.hpp \ version.hpp \
view.cpp \ view.cpp \

View File

@ -21,6 +21,7 @@
#define __UTIL_COORD_BASE_HPP #define __UTIL_COORD_BASE_HPP
#include "init.hpp" #include "init.hpp"
#include "../maths.hpp"
#include <algorithm> #include <algorithm>
#include <cstdlib> #include <cstdlib>
@ -66,6 +67,67 @@ namespace util { namespace coord {
std::copy (begin (), end (), k.begin ()); std::copy (begin (), end (), k.begin ());
return k; return k;
} }
//---------------------------------------------------------------------
template <typename U>
KLASS<S,U>
cast (void) const
{
KLASS<S,U> out;
std::copy (std::begin (this->data),
std::end (this->data),
std::begin (out.data));
return out;
}
///////////////////////////////////////////////////////////////////////
// redimension
template <size_t D>
KLASS<D,T>
redim (void) const
{
KLASS<D,T> out;
std::copy_n (std::begin (this->data),
min (S, D),
std::begin (out.data));
return out;
}
//---------------------------------------------------------------------
template<size_t D>
KLASS<D,T>
redim (const KLASS<D,T> fill) const
{
KLASS<D,T> out;
static constexpr auto L1 = min (S, D);
static constexpr auto L2 = D - L1;
std::copy_n (std::begin (this->data),
L1,
std::begin (out.data));
std::copy_n (fill.data + L1,
L2,
out.data + L1);
return out;
}
//---------------------------------------------------------------------
template <size_t D>
KLASS<D,T>
redim (T fill) const
{
KLASS<D,T> out;
auto cursor = std::copy_n (std::begin (this->data),
min (S, D),
std::begin (out.data));
std::fill (cursor, std::end (out.data), fill);
return out;
}
}; };
} } } }

View File

@ -50,9 +50,6 @@ namespace util {
bool empty (void) const; bool empty (void) const;
template <typename U>
extent<S,U> cast (void) const;
static const extent MAX; static const extent MAX;
static const extent MIN; static const extent MIN;
}; };

View File

@ -26,17 +26,6 @@
#include <algorithm> #include <algorithm>
//-----------------------------------------------------------------------------
template <size_t S, typename T>
template <typename U>
util::extent<S,U>
util::extent<S,T>::cast (void) const
{
util::extent<S,U> out;
std::copy (std::begin (this->data), std::end (this->data), std::begin (out.data));
return out;
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <size_t S, typename T> template <size_t S, typename T>

View File

@ -44,16 +44,10 @@ namespace util {
vector<S,T> to (const point&) const; vector<S,T> to (const point&) const;
vector<S,T> from (const point&) const; vector<S,T> from (const point&) const;
template <size_t D> point<D,T> redim (void) 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> homog (void) 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;
void sanity (void) const; void sanity (void) const;
}; };

View File

@ -63,54 +63,6 @@ namespace util {
} }
//-------------------------------------------------------------------------
template<size_t S, typename T>
template<size_t D>
point<D,T> point<S,T>::redim (void) const {
point<D,T> out;
std::copy_n (std::begin (this->data),
min (S, D),
std::begin (out.data));
return out;
}
//-------------------------------------------------------------------------
template<size_t S, typename T>
template<size_t D>
point<D,T> point<S,T>::redim (const point<D,T> &fill) const {
point<D,T> out;
static constexpr auto L1 = min (S, D);
static constexpr auto L2 = D - L1;
std::copy_n (std::begin (this->data),
L1,
std::begin (out.data));
std::copy_n (fill.data + L1,
L2,
out.data + L1);
return out;
}
//-------------------------------------------------------------------------
template <size_t S, typename T>
template <size_t D>
point<D,T> point<S,T>::redim (T fill) const
{
point<D,T> out;
auto cursor = std::copy_n (std::begin (this->data),
min (S, D),
std::begin (out.data));
std::fill (cursor, std::end (out.data), fill);
return out;
}
///------------------------------------------------------------------------ ///------------------------------------------------------------------------
/// expand point to use homogenous coordinates of a higher dimension. /// expand point to use homogenous coordinates of a higher dimension.
/// ie, fill with (0,..,0,1) /// ie, fill with (0,..,0,1)
@ -136,18 +88,4 @@ namespace util {
return out; return out;
} }
//-------------------------------------------------------------------------
template <size_t S, typename T>
template <typename U>
point<S,U>
point<S,T>::cast (void) const
{
point<S,U> out;
std::copy (std::begin (this->data),
std::end (this->data),
std::begin (out.data));
return out;
}
} }

View File

@ -38,6 +38,14 @@ main (int, char**) {
CHECK_EQ (q.data[3], FILL.data[3]); CHECK_EQ (q.data[3], FILL.data[3]);
} }
// Simple linking check for coord type casting. Relies on truncation.
{
const point2f pf (0.5f, 0.2f);
const point2u pu (0, 0);
CHECK_EQ (pf.template cast<point2u::value_type> (), pu);
}
{ {
const point2f p (3, 4); const point2f p (3, 4);
const point4f q = p.homog<4> (); const point4f q = p.homog<4> ();

View File

@ -45,16 +45,9 @@ namespace util {
vector<S,T>& normalise (void); vector<S,T>& normalise (void);
vector<S,T> normalised [[gnu::warn_unused_result]] (void) const; vector<S,T> normalised [[gnu::warn_unused_result]] (void) const;
// size operations
template <size_t D> vector<D,T> redim (void) const;
template <size_t D> vector<D,T> redim (const util::vector<D,T> &fill) const;
template <size_t D> vector<D,T> redim (T fill) const;
// constants // constants
static const vector<S,T> ZERO; static const vector<S,T> ZERO;
template<typename U> vector<S,U> cast (void) const;
void sanity (void) const; void sanity (void) const;
}; };
@ -85,7 +78,5 @@ namespace util {
typedef vector<3,double> vector4d; typedef vector<3,double> vector4d;
} }
#include "vector.ipp"
#endif #endif

View File

@ -1,91 +0,0 @@
/*
* This file is part of libgim.
*
* libgim is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2014 Danny Robson <danny@nerdcruft.net>
*/
#ifdef __UTIL_VECTOR_IPP
#error "twice included ipp"
#endif
#define __UTIL_VECTOR_IPP
#include "maths.hpp"
namespace util {
//-------------------------------------------------------------------------
template<size_t S, typename T>
template<size_t D>
vector<D,T> vector<S,T>::redim (void) const
{
vector<D,T> out;
std::copy_n (std::begin (this->data),
min (S, D),
std::begin (out.data));
return out;
}
//-------------------------------------------------------------------------
template<size_t S, typename T>
template<size_t D>
vector<D,T> vector<S,T>::redim (const vector<D,T> &fill) const
{
vector<D,T> out;
static constexpr auto L1 = min (S, D);
static constexpr auto L2 = D - L1;
std::copy_n (std::begin (this->data),
L1,
std::begin (out.data));
std::copy_n (fill.data + L1,
L2,
out.data + L1);
return out;
}
//-------------------------------------------------------------------------
template <size_t S, typename T>
template <size_t D>
vector<D,T> vector<S,T>::redim (T fill) const
{
vector<D,T> out;
auto cursor = std::copy_n (std::begin (this->data),
min (S, D),
std::begin (out.data));
std::fill (cursor, std::end (out.data), fill);
return out;
}
//-------------------------------------------------------------------------
template <size_t S, typename T>
template <typename U>
vector<S,U>
vector<S,T>::cast (void) const
{
vector<S,U> out;
std::copy (std::begin (this->data),
std::end (this->data),
std::begin (out.data));
return out;
}
}