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 {
|
2015-01-12 15:59:14 +11:00
|
|
|
typedef T value_type;
|
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
coord () { ; }
|
2012-05-30 20:20:43 +10:00
|
|
|
|
2015-01-13 18:33:52 +11:00
|
|
|
explicit coord (T v)
|
2014-12-30 01:28:17 +11:00
|
|
|
{ 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
|
|
|
{ ; }
|
|
|
|
|
2015-01-13 18:34:27 +11:00
|
|
|
coord (const coord<S,T> &rhs) = default;
|
|
|
|
coord& operator= (const coord<S,T> &rhs) = default;
|
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
T data[S];
|
2014-12-15 13:29:11 +11:00
|
|
|
|
2015-01-13 18:34:45 +11:00
|
|
|
static constexpr size_t dimension = S;
|
2015-01-12 15:59:55 +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> {
|
2015-01-12 15:59:14 +11:00
|
|
|
typedef T value_type;
|
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
coord () { ; }
|
2012-05-30 20:20:43 +10:00
|
|
|
|
2015-01-13 18:33:52 +11:00
|
|
|
explicit coord (T v)
|
2014-12-30 01:28:17 +11:00
|
|
|
{ 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
|
|
|
{ ; }
|
|
|
|
|
2015-01-13 18:34:27 +11:00
|
|
|
coord (const coord<1,T> &rhs) = default;
|
|
|
|
coord& operator= (const coord<1,T> &rhs) = default;
|
|
|
|
|
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
|
|
|
|
2015-01-13 18:34:45 +11:00
|
|
|
static constexpr size_t dimension = 1;
|
2015-01-12 15:59:55 +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> {
|
2015-01-12 15:59:14 +11:00
|
|
|
typedef T value_type;
|
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
coord () { ; }
|
2012-05-30 20:20:43 +10:00
|
|
|
|
2015-01-13 18:33:52 +11:00
|
|
|
explicit coord (T v)
|
2014-12-30 01:28:17 +11:00
|
|
|
{ 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
|
|
|
{ ; }
|
|
|
|
|
2015-01-13 18:34:27 +11:00
|
|
|
coord (const coord<2,T> &rhs) = default;
|
|
|
|
coord& operator= (const coord<2,T> &rhs) = default;
|
|
|
|
|
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
|
|
|
|
2015-01-13 18:34:45 +11:00
|
|
|
static constexpr size_t dimension = 2;
|
2015-01-12 15:59:55 +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> {
|
2015-01-12 15:59:14 +11:00
|
|
|
typedef T value_type;
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2015-01-13 18:34:45 +11:00
|
|
|
static constexpr size_t dimension = 3;
|
2015-01-12 15:59:55 +11:00
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
coord () { ; }
|
2012-05-30 20:20:43 +10:00
|
|
|
|
2015-01-13 18:33:52 +11:00
|
|
|
explicit coord (T v)
|
2014-12-30 01:28:17 +11:00
|
|
|
{ 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
|
|
|
|
2015-01-13 18:34:27 +11:00
|
|
|
coord (const coord<3,T> &rhs) = default;
|
|
|
|
coord& operator= (const coord<3,T> &rhs) = default;
|
|
|
|
|
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> {
|
2015-01-12 15:59:14 +11:00
|
|
|
typedef T value_type;
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2015-01-13 18:34:45 +11:00
|
|
|
static constexpr size_t dimension = 4;
|
2015-01-12 15:59:55 +11:00
|
|
|
|
2014-12-15 20:10:56 +11:00
|
|
|
coord () { ; }
|
2014-08-18 22:08:32 +10:00
|
|
|
|
2015-01-13 18:33:52 +11:00
|
|
|
explicit coord (T v)
|
2014-12-30 01:28:17 +11:00
|
|
|
{ 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
|
|
|
|
2015-01-13 18:34:27 +11:00
|
|
|
coord (const coord<4,T> &rhs) = default;
|
|
|
|
coord& operator= (const coord<4,T> &rhs) = default;
|
|
|
|
|
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
|