coord: use inherited class for data definitions

keep as many operations centralised in the base coord class as possible.
This commit is contained in:
Danny Robson 2015-01-19 19:13:52 +11:00
parent ca66f831f7
commit 319a8ce616
3 changed files with 78 additions and 135 deletions

View File

@ -14,7 +14,7 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with libgim. If not, see <http://www.gnu.org/licenses/>. * along with libgim. If not, see <http://www.gnu.org/licenses/>.
* *
* Copyright 2012 Danny Robson <danny@nerdcruft.net> * Copyright 2012-2015 Danny Robson <danny@nerdcruft.net>
*/ */
#ifndef __UTIL_COORD_HPP #ifndef __UTIL_COORD_HPP
@ -39,173 +39,120 @@ namespace util {
#if defined(COMPILER_CLANG) #if defined(COMPILER_CLANG)
#pragma GCC diagnostic ignored "-Wgnu" #pragma GCC diagnostic ignored "-Wgnu"
#endif #endif
//---------------------------------------------------------------------
// coord types are not really intended to have arbitrary dimension, so
// don't add specialisations (or a general case) without a decent
// reason.
template <size_t S, typename T> template <size_t S, typename T>
struct coord { struct coord_data;
typedef T value_type;
coord () { ; }
explicit coord (T v)
{ std::fill (std::begin (data), std::end (data), v); }
template <typename ...U>
coord (U ..._u): data{_u...}
{ ; }
coord (const coord<S,T> &rhs) = default;
coord& operator= (const coord<S,T> &rhs) = default;
T data[S];
static constexpr size_t dimension = S;
T& operator[] (size_t i) { return data[i]; }
T operator[] (size_t i) const { return data[i]; }
};
//---------------------------------------------------------------------
template <typename T> template <typename T>
struct coord<1,T> { struct coord_data<1, T>
typedef T value_type; {
coord_data () = default;
coord () { ; } coord_data (T v0):
data { v0 }
explicit coord (T v)
{ std::fill (std::begin (data), std::end (data), v); }
template <typename ...U>
coord (U ..._u): data{_u...}
{ ; } { ; }
coord (const coord<1,T> &rhs) = default;
coord& operator= (const coord<1,T> &rhs) = default;
union { union {
T data[1]; T data[1];
T x; T x;
}; };
static constexpr size_t dimension = 1;
T& operator[] (size_t i) { return data[i]; }
T operator[] (size_t i) const { return data[i]; }
}; };
//---------------------------------------------------------------------
template <typename T> template <typename T>
struct coord<2,T> { struct coord_data<2,T>
typedef T value_type; {
coord_data () = default;
coord () { ; } coord_data (T v0, T v1):
data { v0, v1 }
explicit coord (T v)
{ std::fill (std::begin (data), std::end (data), v); }
template <typename ...U>
coord (U ..._u): data{_u...}
{ ; } { ; }
coord (const coord<2,T> &rhs) = default;
coord& operator= (const coord<2,T> &rhs) = default;
union { union {
T data[2]; T data[2];
struct {
T x; struct { T x, y; };
T y; struct { T s, t; };
};
struct {
T s;
T t;
};
}; };
static constexpr size_t dimension = 2;
T& operator[] (size_t i) { return data[i]; }
T operator[] (size_t i) const { return data[i]; }
}; };
//---------------------------------------------------------------------
template <typename T> template <typename T>
struct coord<3,T> { struct coord_data<3,T>
typedef T value_type; {
coord_data () = default;
coord_data (T v0, T v1, T v2):
data { v0, v1, v2 }
{ ; }
union { union {
T data[3]; T data[3];
struct {
T x; struct { T x, y, z; };
T y; struct { T s, t, p; };
T z; struct { T r, g, b; };
};
struct {
T r;
T g;
T b;
};
}; };
static constexpr size_t dimension = 3;
coord () { ; }
explicit coord (T v)
{ std::fill (std::begin (data), std::end (data), v); }
template <typename... U>
coord (U... u): data{u...}
{ ; }
coord (const coord<3,T> &rhs) = default;
coord& operator= (const coord<3,T> &rhs) = default;
T& operator[] (size_t i) { return data[i]; }
T operator[] (size_t i) const { return data[i]; }
}; };
template <size_t S, typename T>
T dot (const coord<S,T> &a, const coord<S,T> &b)
{
T sum { 0 };
for (size_t i = 0; i < S; ++i)
sum += a.data[i] * b.data[i];
return sum;
}
//---------------------------------------------------------------------
template <typename T> template <typename T>
struct coord<4,T> { struct coord_data<4,T>
typedef T value_type; {
coord_data () = default;
coord_data (T v0, T v1, T v2, T v3):
data { v0, v1, v2, v3 }
{ ; }
union { union {
T data[4]; T data[4];
struct {
T x; struct { T x, y, z, w; };
T y; struct { T s, t, p, q; };
T z; struct { T r, g, b, a; };
T w;
};
struct {
T r;
T g;
T b;
T a;
};
}; };
};
static constexpr size_t dimension = 4;
coord () { ; } //---------------------------------------------------------------------
template <size_t S, typename T>
struct coord : public coord_data<S,T> {
static_assert (S > 0, "coord dimensions must be strictly positive");
typedef T value_type;
static constexpr size_t dimension = S;
using coord_data<S,T>::coord_data;
coord () = default;
explicit coord (T v) explicit coord (T v)
{ std::fill (std::begin (data), std::end (data), v); } { std::fill (std::begin (this->data), std::end (this->data), v); }
template <typename... U> coord (const coord<S,T> &rhs) = default;
coord (U... u): data{u...} coord& operator= (const coord<S,T> &rhs) = default;
{ ; }
coord (const coord<4,T> &rhs) = default; T& operator[] (size_t i) { return this->data[i]; }
coord& operator= (const coord<4,T> &rhs) = default; T operator[] (size_t i) const { return this->data[i]; }
T& operator[] (size_t i) { return data[i]; }
T operator[] (size_t i) const { return data[i]; }
}; };
//---------------------------------------------------------------------
// XXX: Unsure whether this should really be defined for arbitrary
// types in a semantic sense, but practicality suggestes this is the
// best option; point/vector dot product is too useful.
template <size_t S, typename T>
T dot (const coord<S,T> &a, const coord<S,T> &b)
{
return std::inner_product (std::begin (a.data),
std::end (a.data),
std::begin (b.data),
T {0});
}
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
} }
} }

View File

@ -32,8 +32,6 @@ namespace util {
/// An n-dimensional position in space. /// An n-dimensional position in space.
template <size_t S, typename T> template <size_t S, typename T>
struct point : public detail::coord<S,T> { struct point : public detail::coord<S,T> {
static_assert (S > 0, "point dimensions must be strictly positive.");
using detail::coord<S,T>::coord; using detail::coord<S,T>::coord;
// point operators // point operators

View File

@ -30,8 +30,6 @@
namespace util { namespace util {
template <size_t S, typename T> template <size_t S, typename T>
struct vector : public detail::coord<S, T> { struct vector : public detail::coord<S, T> {
static_assert (S > 0, "vector dimensions must be strictly positive");
using detail::coord<S,T>::coord; using detail::coord<S,T>::coord;
// arithmetic operators // arithmetic operators