coord: split coord header into components

This commit is contained in:
Danny Robson 2015-03-06 01:46:01 +11:00
parent 99ba406b4d
commit e461654de8
12 changed files with 546 additions and 367 deletions

View File

@ -16,7 +16,12 @@ UTIL_FILES = \
bitwise.hpp \
colour.cpp \
colour.hpp \
detail/coord.hpp \
coord.hpp \
coord/base.hpp \
coord/init.hpp \
coord/names.hpp \
coord/ops.hpp \
coord/store.hpp \
debug.cpp \
debug.hpp \
endian.cpp \

View File

@ -29,8 +29,8 @@
namespace util {
/// An RGBA colour POD type.
template <typename T>
struct colour : public coord<4,T,detail::rgba> {
using coord<4,T,detail::rgba>::coord;
struct colour : public coord::base<4,T,coord::rgba> {
using coord::base<4,T,coord::rgba>::base;
static const colour WHITE;
static const colour BLACK;

359
coord.hpp
View File

@ -20,362 +20,7 @@
#ifndef __UTIL_COORD_HPP
#define __UTIL_COORD_HPP
#include "platform.hpp"
#include "preprocessor.hpp"
#include <algorithm>
#include <iterator>
#include <type_traits>
#include <cstdlib>
namespace util {
namespace detail {
///////////////////////////////////////////////////////////////////////
// tags for accessor names
struct rgba { };
struct xyzw { };
struct stpq { };
struct whd { };
///////////////////////////////////////////////////////////////////////
// Disable GCC warnings about validity of anonyous structures in
// unions. Push comes to shove I'll manually redsign everything to
// keep this syntax anyway.
#pragma GCC diagnostic push
#if defined(COMPILER_GCC)
#pragma GCC diagnostic ignored "-pedantic"
#endif
#if defined(COMPILER_CLANG)
#pragma GCC diagnostic ignored "-Wgnu"
#endif
template <size_t S, typename T, typename...>
struct coord_base {
T data[S];
};
template <typename T>
struct coord_base<3,T,rgba> {
union {
T data[3];
struct { T r,g,b; };
};
};
template <typename T>
struct coord_base<4,T,rgba> {
union {
T data[4];
struct { T r,g,b,a; };
};
};
template <typename T>
struct coord_base<2,T,xyzw> {
union {
T data[2];
struct { T x,y; };
};
};
template <typename T>
struct coord_base<3,T,xyzw> {
union {
T data[3];
struct { T x,y,z; };
};
};
template <typename T>
struct coord_base<4,T,xyzw> {
union {
T data[4];
struct { T x,y,z,w; };
};
};
template <typename T>
struct coord_base<2,T,xyzw,stpq> {
union {
T data[2];
struct { T x,y; };
struct { T s,t; };
};
};
template <typename T>
struct coord_base<3,T,xyzw,stpq> {
union {
T data[3];
struct { T x,y,z; };
struct { T s,t,p; };
};
};
template <typename T>
struct coord_base<4,T,xyzw,stpq> {
union {
T data[4];
struct { T x,y,z,w; };
struct { T s,t,p,q; };
};
};
template <typename T>
struct coord_base<2,T,whd> {
union {
T data[2];
struct { T w,h; };
};
};
template <typename T>
struct coord_base<3,T,whd> {
union {
T data[3];
struct { T w,h,d; };
};
};
#pragma GCC diagnostic pop
///////////////////////////////////////////////////////////////////////
// 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, typename...>
struct coord_init;
////---------------------------------------------------------------------
template <typename T, typename ...tags>
struct coord_init<1,T,tags...> : public coord_base<1,T,tags...>
{
using coord_base<1,T,tags...>::coord_base;
coord_init () = default;
coord_init (T v0):
coord_base<1,T,tags...> ({v0})
{ ; }
};
////---------------------------------------------------------------------
template <typename T, typename ...tags>
struct coord_init<2,T,tags...> : public coord_base<2,T,tags...>
{
using coord_base<2,T,tags...>::coord_base;
coord_init () = default;
coord_init (T v0, T v1):
coord_base<2,T,tags...> ({ v0, v1 })
{ ; }
};
////---------------------------------------------------------------------
template <typename T, typename ...tags>
struct coord_init<3,T,tags...> : public coord_base<3,T,tags...>
{
using coord_base<3,T,tags...>::coord_base;
coord_init () = default;
coord_init (T v0, T v1, T v2):
coord_base<3,T,tags...> ({v0, v1, v2})
{ ; }
};
////---------------------------------------------------------------------
template <typename T, typename ...tags>
struct coord_init<4,T,tags...> : public coord_base<4,T,tags...>
{
using coord_base<4,T,tags...>::coord_base;
coord_init () = default;
coord_init (T v0, T v1, T v2, T v3):
coord_base<4,T,tags...> ({ v0, v1, v2, v3 })
{ ; }
};
}
/////////////////////////////////////////////////////////////////////////
template <size_t S, typename T, typename ...tags>
struct coord : public detail::coord_init<S,T,tags...> {
static_assert (S > 0, "coord dimensions must be strictly positive");
typedef T value_type;
static constexpr size_t dimension = S;
static constexpr size_t elements = S;
size_t size (void) const { return S; }
// constructors
using detail::coord_init<S,T,tags...>::coord_init;
coord () = default;
explicit coord (T v)
{ std::fill (std::begin (this->data), std::end (this->data), v); }
coord (const coord<S,T,tags...> &rhs) = default;
coord& operator= (const coord<S,T,tags...> &rhs) = default;
// element accessors
T& operator[] (size_t i) { return this->data[i]; }
T operator[] (size_t i) const { return this->data[i]; }
const T* begin (void) const { return std::begin (this->data); }
const T* end (void) const { return std::end (this->data); }
T* begin (void) { return std::begin (this->data); }
T* end (void) { return std::end (this->data); }
};
///////////////////////////////////////////////////////////////////////
// operation traits
template <size_t,typename> class point;
template <size_t,typename> class extent;
template <size_t,typename> class vector;
template <
size_t S,
typename T,
template <size_t,typename> class A,
template <size_t,typename> class B
>
struct coord_traits { };
template <size_t S, typename T> struct coord_traits<S,T,extent,extent> { typedef extent<S,T> result; };
template <size_t S, typename T> struct coord_traits<S,T,extent,vector> { typedef extent<S,T> result; };
template <size_t S, typename T> struct coord_traits<S,T,point,extent> { typedef point<S,T> result; };
template <size_t S, typename T> struct coord_traits<S,T,point,vector> { typedef point<S,T> result; };
template <size_t S, typename T> struct coord_traits<S,T,vector,vector> { typedef vector<S,T> result; };
///////////////////////////////////////////////////////////////////////////
// vector operators
#define ELEMENT_OP(OP) \
template < \
size_t S, \
typename T, \
template <size_t,typename> class A, \
template <size_t,typename> class B \
> \
typename coord_traits<S,T,A,B>::result \
operator OP (A<S,T> a, B<S,T> b) \
{ \
typename coord_traits<S,T,A,B>::result out; \
for (size_t i = 0; i < S; ++i) \
out[i] = a[i] OP b[i]; \
return out; \
} \
\
template < \
size_t S, \
typename T, \
template <size_t,typename> class A, \
template <size_t,typename> class B \
> \
typename coord_traits<S,T,A,B>::result& \
operator PASTE(OP,=) (A<S,T>& a, B<S,T> b) \
{ \
for (size_t i = 0; i < S; ++i) \
a[i] PASTE(OP,=) b[i]; \
return a; \
}
ELEMENT_OP(+)
ELEMENT_OP(-)
ELEMENT_OP(*)
ELEMENT_OP(/)
#undef ELEMENT_OP
///////////////////////////////////////////////////////////////////////////
// scalar operators
#define SCALAR_OP(OP) \
template <size_t S, typename T, template <size_t,typename> class K> \
K<S,T> \
operator OP (T t, K<S,T> k) \
{ \
K<S,T> out; \
for (size_t i = 0; i < S; ++i) \
out[i] = t OP k[i]; \
return out; \
} \
\
template <size_t S, typename T, template <size_t,typename> class K> \
K<S,T> \
operator OP (K<S,T> k, T t) \
{ \
K<S,T> out; \
for (size_t i = 0; i < S; ++i) \
out[i] = t OP k[i]; \
return out; \
}
SCALAR_OP(+)
SCALAR_OP(-)
SCALAR_OP(*)
SCALAR_OP(/)
#undef SCALAR_OP
#define SCALAR_OP(OP) \
template <size_t S, typename T, template <size_t,typename> class K> \
K<S,T>& \
operator OP (K<S,T> &k, T t) \
{ \
for (size_t i = 0; i < S; ++i) \
k[i] OP t; \
return k; \
}
SCALAR_OP(+=)
SCALAR_OP(-=)
SCALAR_OP(*=)
SCALAR_OP(/=)
#undef SCALAR_OP
///////////////////////////////////////////////////////////////////////////
// logic operators
template <size_t S, typename T, template <size_t,typename> class K>
bool operator== (K<S,T> a, K<S,T> b)
{ return std::equal (std::begin (a), std::end (a), std::begin (b)); }
template <size_t S, typename T, template <size_t,typename> class K>
bool operator!= (K<S,T> a, K<S,T> b)
{ return !(a == b); }
///////////////////////////////////////////////////////////////////////////
// special operators
template <size_t S, typename T> vector<S,T> operator- (point<S,T> a, point<S,T> b)
{
vector<S,T> out;
for (size_t i = 0; i < S; ++i)
out[i] = a[i] - b[i];
return out;
}
template <size_t S, typename T, template<size_t,typename> class A, template <size_t,typename> class B>
T dot (A<S,T> a, B<S,T> b)
{
T sum { 0 };
for (size_t i = 0; i < S; ++i)
sum += a[i] * b[i];
return sum;
}
}
#include "coord/base.hpp"
#include "coord/ops.hpp"
#endif

63
coord/base.hpp Normal file
View File

@ -0,0 +1,63 @@
/*
* 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 2012-2015 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __UTIL_COORD_BASE_HPP
#define __UTIL_COORD_BASE_HPP
#include "init.hpp"
#include <algorithm>
#include <cstdlib>
namespace util { namespace coord {
/////////////////////////////////////////////////////////////////////////
template <size_t S, typename T, typename ...tags>
struct base : public init <S,T,tags...> {
static_assert (S > 0, "coord dimensions must be strictly positive");
typedef T value_type;
static constexpr size_t dimension = S;
static constexpr size_t elements = S;
size_t size (void) const { return S; }
// constructors
using init<S,T,tags...>::init;
base () = default;
explicit base (T v)
{ std::fill (std::begin (this->data), std::end (this->data), v); }
base (const base<S,T,tags...> &rhs) = default;
base& operator= (const base<S,T,tags...> &rhs) = default;
// element accessors
T& operator[] (size_t i) { return this->data[i]; }
T operator[] (size_t i) const { return this->data[i]; }
const T* begin (void) const { return std::begin (this->data); }
const T* end (void) const { return std::end (this->data); }
T* begin (void) { return std::begin (this->data); }
T* end (void) { return std::end (this->data); }
};
} }
#endif

79
coord/init.hpp Normal file
View File

@ -0,0 +1,79 @@
/*
* 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 2015 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __UTIL_COORD_INIT_HPP
#define __UTIL_COORD_INIT_HPP
#include "store.hpp"
#include <cstdlib>
namespace util { namespace coord {
template <size_t S, typename T, typename...>
struct init;
//-------------------------------------------------------------------------
template <typename T, typename ...tags>
struct init<1,T,tags...> : public store<1,T,tags...>
{
using store<1,T,tags...>::store;
init () = default;
init (T v0):
store<1,T,tags...> ({v0})
{ ; }
};
//-------------------------------------------------------------------------
template <typename T, typename ...tags>
struct init<2,T,tags...> : public store<2,T,tags...>
{
using store<2,T,tags...>::store;
init () = default;
init (T v0, T v1):
store<2,T,tags...> ({ v0, v1 })
{ ; }
};
//-------------------------------------------------------------------------
template <typename T, typename ...tags>
struct init<3,T,tags...> : public store<3,T,tags...>
{
using store<3,T,tags...>::store;
init () = default;
init (T v0, T v1, T v2):
store<3,T,tags...> ({v0, v1, v2})
{ ; }
};
//-------------------------------------------------------------------------
template <typename T, typename ...tags>
struct init<4,T,tags...> : public store<4,T,tags...>
{
using store<4,T,tags...>::store;
init () = default;
init (T v0, T v1, T v2, T v3):
store<4,T,tags...> ({ v0, v1, v2, v3 })
{ ; }
};
} }
#endif

32
coord/names.hpp Normal file
View File

@ -0,0 +1,32 @@
/*
* 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 2015 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __UTIL_COORD_NAMES_HPP
#define __UTIL_COORD_NAMES_HPP
namespace util { namespace coord {
///////////////////////////////////////////////////////////////////////
// tags for accessor names
struct rgba { };
struct xyzw { };
struct stpq { };
struct whd { };
} }
#endif

211
coord/ops.hpp Normal file
View File

@ -0,0 +1,211 @@
/*
* 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 2012-2015 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __UTIL_COORDS_OPS
#define __UTIL_COORDS_OPS
#include "preprocessor.hpp"
#include <cstdlib>
namespace util {
// forward declerations for traits
template <size_t,typename> class point;
template <size_t,typename> class extent;
template <size_t,typename> class vector;
///////////////////////////////////////////////////////////////////////
// operation traits
namespace coord {
template <
size_t S,
typename T,
template <size_t,typename> class A,
template <size_t,typename> class B
>
struct traits { };
//-------------------------------------------------------------------------
template <size_t S, typename T> struct traits<S,T,extent,extent> { typedef extent<S,T> result; };
template <size_t S, typename T> struct traits<S,T,extent,vector> { typedef extent<S,T> result; };
template <size_t S, typename T> struct traits<S,T,point,extent> { typedef point<S,T> result; };
template <size_t S, typename T> struct traits<S,T,point,vector> { typedef point<S,T> result; };
template <size_t S, typename T> struct traits<S,T,vector,vector> { typedef vector<S,T> result; };
}
///////////////////////////////////////////////////////////////////////////
// vector operators
#define ELEMENT_OP(OP) \
template < \
size_t S, \
typename T, \
template <size_t,typename> class A, \
template <size_t,typename> class B \
> \
typename coord::traits<S,T,A,B>::result \
operator OP (A<S,T> a, B<S,T> b) \
{ \
typename coord::traits<S,T,A,B>::result out; \
for (size_t i = 0; i < S; ++i) \
out[i] = a[i] OP b[i]; \
return out; \
} \
\
template < \
size_t S, \
typename T, \
template <size_t,typename> class A, \
template <size_t,typename> class B \
> \
typename coord::traits<S,T,A,B>::result& \
operator PASTE(OP,=) (A<S,T>& a, B<S,T> b) \
{ \
for (size_t i = 0; i < S; ++i) \
a[i] PASTE(OP,=) b[i]; \
return a; \
}
ELEMENT_OP(+)
ELEMENT_OP(-)
ELEMENT_OP(*)
ELEMENT_OP(/)
#undef ELEMENT_OP
///////////////////////////////////////////////////////////////////////////
// scalar operators
#define SCALAR_OP(OP) \
template < \
size_t S, \
typename T, \
template <size_t,typename> class K \
> \
K<S,T> \
operator OP (T t, K<S,T> k) \
{ \
K<S,T> out; \
for (size_t i = 0; i < S; ++i) \
out[i] = t OP k[i]; \
return out; \
} \
\
template < \
size_t S, \
typename T, \
template <size_t,typename> class K \
> \
K<S,T> \
operator OP (K<S,T> k, T t) \
{ \
K<S,T> out; \
for (size_t i = 0; i < S; ++i) \
out[i] = t OP k[i]; \
return out; \
}
SCALAR_OP(+)
SCALAR_OP(-)
SCALAR_OP(*)
SCALAR_OP(/)
#undef SCALAR_OP
#define SCALAR_OP(OP) \
template < \
size_t S, \
typename T, \
template <size_t,typename> class K \
> \
K<S,T>& \
operator OP (K<S,T> &k, T t) \
{ \
for (size_t i = 0; i < S; ++i) \
k[i] OP t; \
return k; \
}
SCALAR_OP(+=)
SCALAR_OP(-=)
SCALAR_OP(*=)
SCALAR_OP(/=)
#undef SCALAR_OP
///////////////////////////////////////////////////////////////////////////
// logic operators
template <
size_t S,
typename T,
template <size_t,typename> class K
>
bool
operator== (K<S,T> a, K<S,T> b)
{
return std::equal (std::begin (a),
std::end (a),
std::begin (b));
}
//-------------------------------------------------------------------------
template <
size_t S,
typename T,
template <size_t,typename> class K
>
bool
operator!= (K<S,T> a, K<S,T> b)
{
return !(a == b);
}
///////////////////////////////////////////////////////////////////////////
// special operators
template <
size_t S,
typename T
>
vector<S,T>
operator- (point<S,T> a, point<S,T> b)
{
vector<S,T> out;
for (size_t i = 0; i < S; ++i)
out[i] = a[i] - b[i];
return out;
}
//-------------------------------------------------------------------------
template <
size_t S,
typename T,
template<size_t,typename> class A,
template <size_t,typename> class B
>
T dot (A<S,T> a, B<S,T> b)
{
T sum { 0 };
for (size_t i = 0; i < S; ++i)
sum += a[i] * b[i];
return sum;
}
}
#endif

145
coord/store.hpp Normal file
View File

@ -0,0 +1,145 @@
/*
* 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 2015 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __UTIL_COORD_STORE_HPP
#define __UTIL_COORD_STORE_HPP
#include "names.hpp"
#include "../platform.hpp"
#include <cstdlib>
namespace util { namespace coord {
///////////////////////////////////////////////////////////////////////
// Disable GCC warnings about validity of anonyous structures in
// unions. Push comes to shove I'll manually redsign everything to
// keep this syntax anyway.
#pragma GCC diagnostic push
#if defined(COMPILER_GCC)
#pragma GCC diagnostic ignored "-pedantic"
#endif
#if defined(COMPILER_CLANG)
#pragma GCC diagnostic ignored "-Wgnu"
#endif
///////////////////////////////////////////////////////////////////////////
template <size_t S, typename T, typename...>
struct store {
T data[S];
};
///////////////////////////////////////////////////////////////////////////
template <typename T>
struct store<3,T,rgba> {
union {
T data[3];
struct { T r,g,b; };
};
};
template <typename T>
struct store<4,T,rgba> {
union {
T data[4];
struct { T r,g,b,a; };
};
};
///////////////////////////////////////////////////////////////////////////
template <typename T>
struct store<2,T,xyzw> {
union {
T data[2];
struct { T x,y; };
};
};
template <typename T>
struct store<3,T,xyzw> {
union {
T data[3];
struct { T x,y,z; };
};
};
template <typename T>
struct store<4,T,xyzw> {
union {
T data[4];
struct { T x,y,z,w; };
};
};
///////////////////////////////////////////////////////////////////////////
template <typename T>
struct store<2,T,xyzw,stpq> {
union {
T data[2];
struct { T x,y; };
struct { T s,t; };
};
};
template <typename T>
struct store<3,T,xyzw,stpq> {
union {
T data[3];
struct { T x,y,z; };
struct { T s,t,p; };
};
};
template <typename T>
struct store<4,T,xyzw,stpq> {
union {
T data[4];
struct { T x,y,z,w; };
struct { T s,t,p,q; };
};
};
///////////////////////////////////////////////////////////////////////////
template <typename T>
struct store<2,T,whd> {
union {
T data[2];
struct { T w,h; };
};
};
template <typename T>
struct store<3,T,whd> {
union {
T data[3];
struct { T w,h,d; };
};
};
} }
#endif

View File

@ -30,9 +30,9 @@ namespace util {
* A pure two-dimensional size, without positioning
*/
template <size_t S, typename T>
struct extent : public coord<S,T,detail::whd>
struct extent : public coord::base<S,T,coord::whd>
{
using coord<S,T,detail::whd>::coord;
using coord::base<S,T,coord::whd>::base;
extent () = default;
extent (vector<S,T>);

View File

@ -32,9 +32,9 @@
namespace util {
/// An n-dimensional position in space.
template <size_t S, typename T>
struct point : public coord<S,T,detail::xyzw>
struct point : public coord::base<S,T,coord::xyzw>
{
using coord<S,T,detail::xyzw>::coord;
using coord::base<S,T,coord::xyzw>::base;
// point operators
template <typename U> typename std::common_type<T,U>::type distance (const point<S,U> &) const;

View File

@ -137,5 +137,4 @@ namespace util {
std::ostream& operator<< (std::ostream&, const util::region<S,T>&);
}
#endif

View File

@ -29,8 +29,8 @@
namespace util {
template <size_t S, typename T>
struct vector : public coord<S,T,detail::xyzw,detail::stpq> {
using coord<S,T,detail::xyzw,detail::stpq>::coord;
struct vector : public coord::base<S,T,coord::xyzw,coord::stpq> {
using coord::base<S,T,coord::xyzw,coord::stpq>::base;
bool is_zero (void) const;