libcruft-util/detail/coord.hpp

248 lines
7.2 KiB
C++
Raw Normal View History

2012-05-30 20:20:43 +10:00
/*
* 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.
*
2012-05-30 20:20:43 +10:00
* 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.
*
2012-05-30 20:20:43 +10:00
* 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>
2012-05-30 20:20:43 +10:00
*/
#ifndef __UTIL_COORD_HPP
#define __UTIL_COORD_HPP
#include "../platform.hpp"
2015-01-13 18:35:18 +11:00
#include <algorithm>
#include <iterator>
#include <cstdlib>
2012-05-30 20:20:43 +10:00
namespace util {
namespace detail {
// 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
2012-05-30 20:20:43 +10:00
//---------------------------------------------------------------------
// tags for accessor names
struct rgba { };
struct xyzw { };
struct stpq { };
struct whd { };
template <size_t S, typename T, typename...>
struct coord_base {
T data[S];
};
2012-05-30 20:20:43 +10:00
template <typename T>
struct coord_base<3,T,rgba> {
union {
T data[3];
struct { T r,g,b; };
};
};
2012-05-30 20:20:43 +10:00
template <typename T>
struct coord_base<4,T,rgba> {
2012-05-30 20:20:43 +10:00
union {
T data[4];
struct { T r,g,b,a; };
2012-05-30 20:20:43 +10:00
};
};
template <typename T>
struct coord_base<2,T,xyzw> {
2012-05-30 20:20:43 +10:00
union {
T data[2];
struct { T x,y; };
};
2012-05-30 20:20:43 +10:00
};
template <typename T>
struct coord_base<3,T,xyzw> {
2012-05-30 20:20:43 +10:00
union {
T data[3];
struct { T x,y,z; };
};
};
2015-01-12 15:59:55 +11:00
template <typename T>
struct coord_base<4,T,xyzw> {
union {
T data[4];
struct { T x,y,z,w; };
};
};
2012-05-30 20:20:43 +10:00
2014-12-30 01:28:17 +11:00
template <typename T>
struct coord_base<2,T,xyzw,stpq> {
union {
T data[2];
struct { T x,y; };
struct { T s,t; };
};
};
2014-12-15 13:29:11 +11:00
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; };
};
2012-05-30 20:20:43 +10:00
};
//---------------------------------------------------------------------
// 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...>
{
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...>
{
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...>
{
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...>
{
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 coord_init<S,T,tags...> {
static_assert (S > 0, "coord dimensions must be strictly positive");
2014-12-15 13:30:02 +11:00
2015-01-12 15:59:14 +11:00
typedef T value_type;
static constexpr size_t dimension = S;
static constexpr size_t elements = S;
2015-01-12 15:59:14 +11:00
using coord_init<S,T,tags...>::coord_init;
coord () = default;
size_t size (void) const { return S; }
explicit coord (T v)
{ std::fill (std::begin (this->data), std::end (this->data), v); }
2014-12-15 13:29:11 +11:00
coord (const coord<S,T,tags...> &rhs) = default;
coord& operator= (const coord<S,T,tags...> &rhs) = default;
T& operator[] (size_t i) { return this->data[i]; }
T operator[] (size_t i) const { return this->data[i]; }
2015-02-19 13:25:59 +11:00
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); }
};
////---------------------------------------------------------------------
// 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, typename ...tag_a, typename ...tag_b>
T dot (const coord<S,T,tag_a...> &a, const coord<S,T,tag_b...> &b)
{
return std::inner_product (std::begin (a.data),
std::end (a.data),
std::begin (b.data),
T {0});
}
2012-05-30 20:20:43 +10:00
#pragma GCC diagnostic pop
}
}
#endif