libcruft-util/detail/coord.hpp

174 lines
4.5 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.
*
* 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 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __UTIL_COORD_HPP
#define __UTIL_COORD_HPP
#include "../platform.hpp"
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
template <size_t S, typename T>
struct coord {
coord () { ; }
2012-05-30 20:20:43 +10:00
2014-12-30 01:28:17 +11:00
coord (T v)
{ std::fill (std::begin (data), std::end (data), v); }
template <typename ...U>
coord (U ..._u): data{_u...}
2012-05-30 20:20:43 +10:00
{ ; }
T data[S];
2014-12-15 13:29:11 +11:00
T& operator[] (size_t i) { return data[i]; }
T operator[] (size_t i) const { return data[i]; }
2012-05-30 20:20:43 +10:00
};
template <typename T>
struct coord<1,T> {
coord () { ; }
2012-05-30 20:20:43 +10:00
2014-12-30 01:28:17 +11:00
coord (T v)
{ std::fill (std::begin (data), std::end (data), v); }
template <typename ...U>
coord (U ..._u): data{_u...}
2012-05-30 20:20:43 +10:00
{ ; }
union {
T data[1];
T x;
2012-05-30 20:20:43 +10:00
};
2014-12-15 13:29:11 +11:00
T& operator[] (size_t i) { return data[i]; }
T operator[] (size_t i) const { return data[i]; }
2012-05-30 20:20:43 +10:00
};
template <typename T>
struct coord<2,T> {
coord () { ; }
2012-05-30 20:20:43 +10:00
2014-12-30 01:28:17 +11:00
coord (T v)
{ std::fill (std::begin (data), std::end (data), v); }
template <typename ...U>
coord (U ..._u): data{_u...}
2012-05-30 20:20:43 +10:00
{ ; }
union {
T data[2];
2012-05-30 20:20:43 +10:00
struct {
T x;
T y;
2012-05-30 20:20:43 +10:00
};
struct {
T s;
T t;
};
2012-05-30 20:20:43 +10:00
};
2014-12-15 13:29:11 +11:00
T& operator[] (size_t i) { return data[i]; }
T operator[] (size_t i) const { return data[i]; }
2012-05-30 20:20:43 +10:00
};
template <typename T>
struct coord<3,T> {
2012-05-30 20:20:43 +10:00
union {
T data[3];
2012-05-30 20:20:43 +10:00
struct {
T x;
T y;
T z;
2012-05-30 20:20:43 +10:00
};
struct {
T r;
T g;
T b;
};
2012-05-30 20:20:43 +10:00
};
coord () { ; }
2012-05-30 20:20:43 +10:00
2014-12-30 01:28:17 +11:00
coord (T v)
{ std::fill (std::begin (data), std::end (data), v); }
template <typename... U>
coord (U... u): data{u...}
2012-05-30 20:20:43 +10:00
{ ; }
2014-12-15 13:29:11 +11:00
T& operator[] (size_t i) { return data[i]; }
T operator[] (size_t i) const { return data[i]; }
2012-05-30 20:20:43 +10:00
};
template <size_t S, typename T>
T dot (const coord<S,T> &a, const coord<S,T> &b)
2014-12-15 13:30:02 +11:00
{
T sum { 0 };
2014-12-15 13:30:02 +11:00
for (size_t i = 0; i < S; ++i)
sum += a.data[i] * b.data[i];
return sum;
}
template <typename T>
struct coord<4,T> {
union {
T data[4];
struct {
T x;
T y;
T z;
T w;
};
struct {
T r;
T g;
T b;
T a;
};
};
coord () { ; }
2014-12-30 01:28:17 +11:00
coord (T v)
{ std::fill (std::begin (data), std::end (data), v); }
template <typename... U>
coord (U... u): data{u...}
{ ; }
2014-12-15 13:29:11 +11:00
T& operator[] (size_t i) { return data[i]; }
T operator[] (size_t i) const { return data[i]; }
};
2012-05-30 20:20:43 +10:00
#pragma GCC diagnostic pop
}
}
#endif