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
|
|
|
|
|
2012-06-13 14:43:46 +10:00
|
|
|
#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
|
2012-06-13 14:43:46 +10:00
|
|
|
#if defined(COMPILER_GCC)
|
|
|
|
#pragma GCC diagnostic ignored "-pedantic"
|
|
|
|
#endif
|
|
|
|
#if defined(COMPILER_CLANG)
|
|
|
|
#pragma GCC diagnostic ignored "-Wgnu"
|
|
|
|
#endif
|
2014-12-15 20:10:56 +11:00
|
|
|
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); }
|
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
template <typename ...U>
|
|
|
|
coord (U ..._u): data{_u...}
|
2012-05-30 20:20:43 +10:00
|
|
|
{ ; }
|
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
T data[S];
|
2014-12-15 13:29:11 +11:00
|
|
|
|
2014-12-15 20:10:56 +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
|
|
|
};
|
|
|
|
|
2014-12-15 20:10:56 +11: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); }
|
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
template <typename ...U>
|
|
|
|
coord (U ..._u): data{_u...}
|
2012-05-30 20:20:43 +10:00
|
|
|
{ ; }
|
|
|
|
|
|
|
|
union {
|
2014-12-15 20:10:56 +11:00
|
|
|
T data[1];
|
|
|
|
T x;
|
2012-05-30 20:20:43 +10:00
|
|
|
};
|
2014-12-15 13:29:11 +11:00
|
|
|
|
2014-12-15 20:10:56 +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
|
|
|
};
|
|
|
|
|
2014-12-15 20:10:56 +11: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); }
|
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
template <typename ...U>
|
|
|
|
coord (U ..._u): data{_u...}
|
2012-05-30 20:20:43 +10:00
|
|
|
{ ; }
|
|
|
|
|
|
|
|
union {
|
2014-12-15 20:10:56 +11:00
|
|
|
T data[2];
|
2012-05-30 20:20:43 +10:00
|
|
|
struct {
|
2014-12-15 20:10:56 +11:00
|
|
|
T x;
|
|
|
|
T y;
|
2012-05-30 20:20:43 +10:00
|
|
|
};
|
2013-07-30 23:50:56 +10:00
|
|
|
struct {
|
2014-12-16 15:28:02 +11:00
|
|
|
T s;
|
2014-12-15 20:10:56 +11:00
|
|
|
T t;
|
2013-07-30 23:50:56 +10:00
|
|
|
};
|
2012-05-30 20:20:43 +10:00
|
|
|
};
|
2014-12-15 13:29:11 +11:00
|
|
|
|
2014-12-15 20:10:56 +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
|
|
|
};
|
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
template <typename T>
|
|
|
|
struct coord<3,T> {
|
2012-05-30 20:20:43 +10:00
|
|
|
union {
|
2014-12-15 20:10:56 +11:00
|
|
|
T data[3];
|
2012-05-30 20:20:43 +10:00
|
|
|
struct {
|
2014-12-15 20:10:56 +11:00
|
|
|
T x;
|
|
|
|
T y;
|
|
|
|
T z;
|
2012-05-30 20:20:43 +10:00
|
|
|
};
|
2014-12-16 15:28:02 +11:00
|
|
|
struct {
|
|
|
|
T r;
|
|
|
|
T g;
|
|
|
|
T b;
|
|
|
|
};
|
2012-05-30 20:20:43 +10:00
|
|
|
};
|
|
|
|
|
2014-12-15 20:10:56 +11: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); }
|
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
template <typename... U>
|
|
|
|
coord (U... u): data{u...}
|
2012-05-30 20:20:43 +10:00
|
|
|
{ ; }
|
2014-12-15 13:29:11 +11:00
|
|
|
|
2014-12-15 20:10:56 +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
|
|
|
};
|
2014-08-18 22:08:32 +10:00
|
|
|
|
2014-12-15 20:10:56 +11: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
|
|
|
{
|
2014-12-15 20:10:56 +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;
|
|
|
|
}
|
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
template <typename T>
|
|
|
|
struct coord<4,T> {
|
2014-08-18 22:08:32 +10:00
|
|
|
union {
|
2014-12-15 20:10:56 +11:00
|
|
|
T data[4];
|
2014-08-18 22:08:32 +10:00
|
|
|
struct {
|
2014-12-15 20:10:56 +11:00
|
|
|
T x;
|
|
|
|
T y;
|
|
|
|
T z;
|
|
|
|
T w;
|
2014-08-18 22:08:32 +10:00
|
|
|
};
|
2014-12-16 15:28:02 +11:00
|
|
|
struct {
|
|
|
|
T r;
|
|
|
|
T g;
|
|
|
|
T b;
|
|
|
|
T a;
|
|
|
|
};
|
2014-08-18 22:08:32 +10:00
|
|
|
};
|
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
coord () { ; }
|
2014-08-18 22:08:32 +10:00
|
|
|
|
2014-12-30 01:28:17 +11:00
|
|
|
coord (T v)
|
|
|
|
{ std::fill (std::begin (data), std::end (data), v); }
|
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
template <typename... U>
|
|
|
|
coord (U... u): data{u...}
|
2014-08-18 22:08:32 +10:00
|
|
|
{ ; }
|
2014-12-15 13:29:11 +11:00
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
T& operator[] (size_t i) { return data[i]; }
|
|
|
|
T operator[] (size_t i) const { return data[i]; }
|
2014-08-18 22:08:32 +10:00
|
|
|
};
|
2012-05-30 20:20:43 +10:00
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|