2015-03-06 01:46:01 +11:00
|
|
|
/*
|
2015-04-13 18:05:28 +10:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2015-03-06 01:46:01 +11:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-03-06 01:46:01 +11:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2015-03-06 01:46:01 +11:00
|
|
|
*
|
2017-08-27 12:32:00 +10:00
|
|
|
* Copyright 2012-2017 Danny Robson <danny@nerdcruft.net>
|
2015-03-06 01:46:01 +11:00
|
|
|
*/
|
|
|
|
|
2017-08-27 12:32:00 +10:00
|
|
|
#ifndef CRUFT_UTIL_COORDS_OPS
|
|
|
|
#define CRUFT_UTIL_COORDS_OPS
|
2015-03-06 01:46:01 +11:00
|
|
|
|
2016-08-11 14:22:29 +10:00
|
|
|
#include "./fwd.hpp"
|
|
|
|
|
2016-10-12 22:58:13 +11:00
|
|
|
#include "../debug.hpp"
|
2015-03-06 17:49:35 +11:00
|
|
|
#include "../maths.hpp"
|
2015-05-29 15:51:08 +10:00
|
|
|
#include "../types/bits.hpp"
|
2015-03-06 01:46:01 +11:00
|
|
|
|
2017-09-12 14:17:30 +10:00
|
|
|
#include "../preprocessor.hpp"
|
|
|
|
|
2017-06-16 17:38:55 +10:00
|
|
|
#include <algorithm>
|
2015-09-29 18:06:52 +10:00
|
|
|
#include <cmath>
|
2016-08-15 18:46:04 +10:00
|
|
|
#include <cstdlib>
|
2017-06-16 17:38:55 +10:00
|
|
|
#include <iterator>
|
2017-08-27 12:32:00 +10:00
|
|
|
#include <functional>
|
2015-03-06 01:46:01 +11:00
|
|
|
|
|
|
|
namespace util {
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
// operation traits
|
|
|
|
namespace coord {
|
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
template <std::size_t,typename> class A,
|
|
|
|
template <std::size_t,typename> class B
|
2015-03-06 01:46:01 +11:00
|
|
|
>
|
2016-08-11 14:23:32 +10:00
|
|
|
struct result { };
|
2015-03-06 01:46:01 +11:00
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
2017-08-28 12:25:23 +10:00
|
|
|
template <> struct result<colour,colour> { template <std::size_t S, typename T> using type = colour<S,T>; };
|
|
|
|
template <> struct result<extent,extent> { template <std::size_t S, typename T> using type = extent<S,T>; };
|
|
|
|
template <> struct result<extent,vector> { template <std::size_t S, typename T> using type = extent<S,T>; };
|
|
|
|
template <> struct result<point,extent> { template <std::size_t S, typename T> using type = point <S,T>; };
|
|
|
|
template <> struct result<point,vector> { template <std::size_t S, typename T> using type = point <S,T>; };
|
|
|
|
template <> struct result<vector,point> { template <std::size_t S, typename T> using type = point <S,T>; };
|
|
|
|
template <> struct result<vector,vector> { template <std::size_t S, typename T> using type = vector<S,T>; };
|
2016-08-11 14:23:32 +10:00
|
|
|
|
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
template <std::size_t,typename> class A,
|
|
|
|
template <std::size_t,typename> class B
|
2016-08-11 14:23:32 +10:00
|
|
|
>
|
|
|
|
using result_t = typename result<A,B>::type;
|
2016-08-11 14:58:04 +10:00
|
|
|
|
|
|
|
|
2016-08-11 14:58:46 +10:00
|
|
|
//---------------------------------------------------------------------
|
2017-08-28 12:25:23 +10:00
|
|
|
template <template <std::size_t,typename> class K>
|
2016-08-11 14:58:46 +10:00
|
|
|
struct has_norm : public std::false_type { };
|
|
|
|
|
|
|
|
template <> struct has_norm<vector> : public std::true_type { };
|
|
|
|
|
2017-08-28 12:25:23 +10:00
|
|
|
template <template <std::size_t,typename> class K>
|
2016-08-11 14:58:46 +10:00
|
|
|
constexpr auto has_norm_v = has_norm<K>::value;
|
|
|
|
|
|
|
|
|
2016-08-11 14:58:04 +10:00
|
|
|
//---------------------------------------------------------------------
|
2017-08-28 12:25:23 +10:00
|
|
|
template <template <std::size_t,typename> class K>
|
2016-08-11 14:58:04 +10:00
|
|
|
struct has_scalar_op : public std::false_type { };
|
|
|
|
|
|
|
|
template <> struct has_scalar_op<colour> : public std::true_type { };
|
|
|
|
template <> struct has_scalar_op<extent> : public std::true_type { };
|
|
|
|
template <> struct has_scalar_op<point> : public std::true_type { };
|
|
|
|
template <> struct has_scalar_op<vector> : public std::true_type { };
|
|
|
|
|
2017-08-28 12:25:23 +10:00
|
|
|
template <template <std::size_t,typename> class K>
|
2016-08-11 14:58:04 +10:00
|
|
|
constexpr auto has_scalar_op_v = has_scalar_op<K>::value;
|
2015-03-06 01:46:01 +11:00
|
|
|
}
|
|
|
|
|
2016-09-22 16:20:32 +10:00
|
|
|
template <class> struct is_coord : std::false_type { };
|
2015-11-04 23:22:49 +11:00
|
|
|
|
2017-08-28 12:25:23 +10:00
|
|
|
template <std::size_t S, typename T> struct is_coord<point<S,T>> : std::true_type { };
|
|
|
|
template <std::size_t S, typename T> struct is_coord<extent<S,T>> : std::true_type { };
|
|
|
|
template <std::size_t S, typename T> struct is_coord<vector<S,T>> : std::true_type { };
|
|
|
|
template <std::size_t S, typename T> struct is_coord<colour<S,T>> : std::true_type { };
|
2015-11-04 23:22:49 +11:00
|
|
|
|
2016-09-22 16:20:32 +10:00
|
|
|
template <class K>
|
2016-03-14 19:54:25 +11:00
|
|
|
constexpr bool
|
|
|
|
is_coord_v = is_coord<K>::value;
|
|
|
|
|
2017-03-23 14:36:39 +11:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
template <typename T>
|
|
|
|
constexpr
|
|
|
|
std::enable_if_t<is_coord_v<T>, std::size_t>
|
|
|
|
arity (void)
|
|
|
|
{
|
|
|
|
return T::dimension;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
constexpr
|
|
|
|
std::enable_if_t<std::is_arithmetic<T>::value, std::size_t>
|
|
|
|
arity (void)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-28 12:58:18 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
/// create a coord from supplied arguments, optionally specifying the
|
|
|
|
/// underlying type.
|
|
|
|
///
|
|
|
|
/// much like experimental::make_array we use a void type to signal we
|
|
|
|
/// need to deduce the underlying type.
|
|
|
|
#define MAKE_COORD(KLASS) \
|
|
|
|
template < \
|
|
|
|
typename _T = void, \
|
|
|
|
typename ...Args \
|
|
|
|
> \
|
|
|
|
constexpr auto \
|
|
|
|
make_##KLASS (Args &&...args) \
|
|
|
|
{ \
|
|
|
|
using T = std::conditional_t< \
|
|
|
|
std::is_void_v<_T>, \
|
|
|
|
std::common_type_t<Args...>, \
|
|
|
|
_T \
|
|
|
|
>; \
|
|
|
|
\
|
|
|
|
return KLASS<sizeof...(Args),T> { \
|
|
|
|
std::forward<Args> (args)... \
|
|
|
|
}; \
|
|
|
|
}
|
|
|
|
|
|
|
|
MAKE_COORD(extent)
|
|
|
|
MAKE_COORD(point)
|
|
|
|
MAKE_COORD(colour)
|
|
|
|
MAKE_COORD(vector)
|
|
|
|
|
|
|
|
#undef MAKE_COORD
|
|
|
|
|
|
|
|
template <
|
|
|
|
template <std::size_t,typename> class K,
|
|
|
|
typename ...Args
|
|
|
|
>
|
|
|
|
constexpr auto
|
|
|
|
make_coord (Args &&...args)
|
|
|
|
{
|
|
|
|
using T = std::common_type_t<Args...>;
|
|
|
|
return K<sizeof...(Args),T> { std::forward<Args> (args)... };
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-06 01:46:01 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// vector operators
|
2015-04-09 18:00:40 +10:00
|
|
|
#define ELEMENT_OP(OP) \
|
|
|
|
template < \
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S, \
|
2015-04-09 18:00:40 +10:00
|
|
|
typename T, \
|
2015-05-28 10:18:07 +10:00
|
|
|
typename U, \
|
2017-08-28 12:25:23 +10:00
|
|
|
template <std::size_t,typename> class A, \
|
|
|
|
template <std::size_t,typename> class B, \
|
2016-03-14 19:54:25 +11:00
|
|
|
typename = std::enable_if_t< \
|
2016-09-22 16:20:32 +10:00
|
|
|
is_coord_v<A<S,T>> && is_coord_v<B<S,U>>, \
|
2015-11-04 23:22:49 +11:00
|
|
|
void \
|
2016-03-14 19:54:25 +11:00
|
|
|
> \
|
2015-04-09 18:00:40 +10:00
|
|
|
> \
|
2016-03-14 19:01:59 +11:00
|
|
|
constexpr \
|
2015-10-19 17:06:19 +11:00
|
|
|
auto \
|
2015-05-28 10:18:07 +10:00
|
|
|
operator OP (A<S,T> a, B<S,U> b) \
|
2015-04-09 18:00:40 +10:00
|
|
|
{ \
|
2016-08-11 14:23:32 +10:00
|
|
|
typename coord::result<A,B>::template type< \
|
2016-03-14 19:54:25 +11:00
|
|
|
S,std::common_type_t<T,U> \
|
2016-03-14 19:01:59 +11:00
|
|
|
> out {}; \
|
2017-08-28 12:25:23 +10:00
|
|
|
for (std::size_t i = 0; i < S; ++i) \
|
2015-04-09 18:00:40 +10:00
|
|
|
out[i] = a[i] OP b[i]; \
|
|
|
|
return out; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
template < \
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S, \
|
2015-04-09 18:00:40 +10:00
|
|
|
typename T, \
|
2015-10-19 17:06:19 +11:00
|
|
|
typename U, \
|
2017-08-28 12:25:23 +10:00
|
|
|
template <std::size_t,typename> class A, \
|
|
|
|
template <std::size_t,typename> class B, \
|
2016-03-14 19:54:25 +11:00
|
|
|
typename = std::enable_if_t< \
|
2016-09-22 16:20:32 +10:00
|
|
|
is_coord_v<A<S,T>> && \
|
|
|
|
is_coord_v<B<S,U>> && \
|
2015-11-04 23:22:49 +11:00
|
|
|
std::is_same< \
|
2016-03-14 19:54:25 +11:00
|
|
|
std::common_type_t<T,U>, T \
|
2015-11-04 23:22:49 +11:00
|
|
|
>::value, \
|
|
|
|
void \
|
2016-03-14 19:54:25 +11:00
|
|
|
> \
|
2015-04-09 18:00:40 +10:00
|
|
|
> \
|
2015-11-04 23:22:49 +11:00
|
|
|
auto& \
|
2015-10-19 17:06:19 +11:00
|
|
|
operator PASTE(OP,=) (A<S,T>& a, B<S,U> b) \
|
2015-04-09 18:00:40 +10:00
|
|
|
{ \
|
2017-08-28 12:25:23 +10:00
|
|
|
for (std::size_t i = 0; i < S; ++i) \
|
2015-04-09 18:00:40 +10:00
|
|
|
a[i] PASTE(OP,=) b[i]; \
|
|
|
|
return a; \
|
2015-03-06 01:46:01 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
ELEMENT_OP(+)
|
|
|
|
ELEMENT_OP(-)
|
|
|
|
ELEMENT_OP(*)
|
|
|
|
ELEMENT_OP(/)
|
2015-07-21 01:39:01 +10:00
|
|
|
ELEMENT_OP(%)
|
2015-03-06 01:46:01 +11:00
|
|
|
#undef ELEMENT_OP
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// scalar operators
|
2015-10-19 17:06:19 +11:00
|
|
|
#define SCALAR_OP(OP) \
|
|
|
|
template < \
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S, \
|
2015-10-19 17:06:19 +11:00
|
|
|
typename T, \
|
|
|
|
typename U, \
|
2017-08-28 12:25:23 +10:00
|
|
|
template <std::size_t,typename> class K, \
|
2016-03-14 19:54:25 +11:00
|
|
|
typename = std::enable_if_t< \
|
2016-08-11 14:58:04 +10:00
|
|
|
coord::has_scalar_op_v<K>, void \
|
2016-03-14 19:54:25 +11:00
|
|
|
> \
|
2015-10-19 17:06:19 +11:00
|
|
|
> \
|
2016-03-14 19:01:59 +11:00
|
|
|
constexpr \
|
2015-10-19 17:06:19 +11:00
|
|
|
auto \
|
|
|
|
operator OP (U u, K<S,T> k) \
|
|
|
|
{ \
|
2016-03-14 19:54:25 +11:00
|
|
|
K<S,std::common_type_t<T,U>> out{}; \
|
2015-11-04 23:22:49 +11:00
|
|
|
\
|
2017-08-28 12:25:23 +10:00
|
|
|
for (std::size_t i = 0; i < S; ++i) \
|
2015-10-19 17:06:19 +11:00
|
|
|
out[i] = u OP k[i]; \
|
|
|
|
return out; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
template < \
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S, \
|
2015-10-19 17:06:19 +11:00
|
|
|
typename T, \
|
|
|
|
typename U, \
|
2017-08-28 12:25:23 +10:00
|
|
|
template <std::size_t,typename> class K, \
|
2016-03-14 19:54:25 +11:00
|
|
|
typename = std::enable_if_t< \
|
2016-08-11 14:58:04 +10:00
|
|
|
coord::has_scalar_op_v<K>,void \
|
2016-03-14 19:54:25 +11:00
|
|
|
> \
|
2015-10-19 17:06:19 +11:00
|
|
|
> \
|
2016-03-14 19:01:59 +11:00
|
|
|
constexpr \
|
2015-10-19 17:06:19 +11:00
|
|
|
auto \
|
|
|
|
operator OP (K<S,T> k, U u) \
|
|
|
|
{ \
|
2016-03-14 19:54:25 +11:00
|
|
|
K<S,std::common_type_t<T,U>> out {}; \
|
2015-11-04 23:22:49 +11:00
|
|
|
\
|
2017-08-28 12:25:23 +10:00
|
|
|
for (std::size_t i = 0; i < S; ++i) \
|
2015-10-19 17:06:19 +11:00
|
|
|
out[i] = k[i] OP u; \
|
|
|
|
return out; \
|
2015-03-06 01:46:01 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
SCALAR_OP(+)
|
|
|
|
SCALAR_OP(-)
|
|
|
|
SCALAR_OP(*)
|
|
|
|
SCALAR_OP(/)
|
2015-07-21 01:39:01 +10:00
|
|
|
SCALAR_OP(%)
|
2015-03-06 01:46:01 +11:00
|
|
|
|
|
|
|
#undef SCALAR_OP
|
|
|
|
|
|
|
|
|
2015-03-07 03:18:32 +11:00
|
|
|
//-------------------------------------------------------------------------
|
2015-10-19 12:02:07 +11:00
|
|
|
// scalar assignment operators.
|
|
|
|
//
|
|
|
|
// we must check the operands/results do not need casting to store in the
|
|
|
|
// destination type to avoid silent errors accumulating.
|
|
|
|
#define SCALAR_OP(OP) \
|
|
|
|
template < \
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S, \
|
2015-10-19 12:02:07 +11:00
|
|
|
typename T, \
|
|
|
|
typename U, \
|
2017-08-28 12:25:23 +10:00
|
|
|
template <std::size_t,typename> class K, \
|
2016-03-14 19:54:25 +11:00
|
|
|
typename = std::enable_if_t< \
|
2016-09-22 16:20:32 +10:00
|
|
|
is_coord<K<S,T>>::value && \
|
2016-05-12 18:22:55 +10:00
|
|
|
std::is_arithmetic<T>::value && \
|
|
|
|
std::is_arithmetic<U>::value, \
|
2015-11-04 23:22:49 +11:00
|
|
|
void \
|
2016-03-14 19:54:25 +11:00
|
|
|
> \
|
2015-10-19 12:02:07 +11:00
|
|
|
> \
|
2016-03-14 19:54:25 +11:00
|
|
|
std::enable_if_t< \
|
2015-10-19 12:02:07 +11:00
|
|
|
std::is_same< \
|
|
|
|
T, \
|
2016-03-14 19:54:25 +11:00
|
|
|
std::common_type_t<T,U> \
|
|
|
|
>::value, \
|
2015-10-19 12:02:07 +11:00
|
|
|
K<S,T> \
|
2016-03-14 19:54:25 +11:00
|
|
|
>& \
|
2015-10-19 12:02:07 +11:00
|
|
|
operator OP (K<S,T> &k, U u) \
|
|
|
|
{ \
|
2017-08-28 12:25:23 +10:00
|
|
|
for (std::size_t i = 0; i < S; ++i) \
|
2015-10-19 12:02:07 +11:00
|
|
|
k[i] OP u; \
|
2015-11-04 23:22:49 +11:00
|
|
|
\
|
2015-10-19 12:02:07 +11:00
|
|
|
return k; \
|
2015-03-06 01:46:01 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
SCALAR_OP(+=)
|
|
|
|
SCALAR_OP(-=)
|
|
|
|
SCALAR_OP(*=)
|
|
|
|
SCALAR_OP(/=)
|
2015-07-21 01:39:01 +10:00
|
|
|
SCALAR_OP(%=)
|
2015-03-06 01:46:01 +11:00
|
|
|
#undef SCALAR_OP
|
|
|
|
|
|
|
|
|
2015-10-19 17:06:19 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// unary operators
|
|
|
|
|
|
|
|
#define UNARY_OP(OP) \
|
|
|
|
template < \
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S, \
|
2015-10-19 17:06:19 +11:00
|
|
|
typename T, \
|
2017-08-28 12:25:23 +10:00
|
|
|
template <std::size_t,typename> class K, \
|
2016-03-14 19:54:25 +11:00
|
|
|
typename = std::enable_if_t< \
|
2016-09-22 16:20:32 +10:00
|
|
|
is_coord_v<K<S,T>>, void \
|
2016-03-14 19:54:25 +11:00
|
|
|
> \
|
2015-10-19 17:06:19 +11:00
|
|
|
> \
|
2016-03-14 19:01:59 +11:00
|
|
|
constexpr \
|
2015-10-19 17:06:19 +11:00
|
|
|
auto \
|
|
|
|
operator OP (K<S,T> k) \
|
|
|
|
{ \
|
2016-03-14 19:01:59 +11:00
|
|
|
K<S,decltype(OP std::declval<T> ())> out{}; \
|
2015-10-19 17:06:19 +11:00
|
|
|
\
|
2017-08-28 12:25:23 +10:00
|
|
|
for (std::size_t i = 0; i < S; ++i) \
|
2015-10-19 17:06:19 +11:00
|
|
|
out[i] = OP k[i]; \
|
|
|
|
\
|
2016-03-14 18:56:36 +11:00
|
|
|
return out; \
|
2015-10-19 17:06:19 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
UNARY_OP(!)
|
|
|
|
UNARY_OP(~)
|
2016-03-11 12:48:19 +11:00
|
|
|
UNARY_OP(+)
|
|
|
|
UNARY_OP(-)
|
2015-10-19 17:06:19 +11:00
|
|
|
|
|
|
|
#undef UNARY_OP
|
|
|
|
|
2017-08-28 12:59:06 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
namespace detail {
|
|
|
|
template <
|
|
|
|
typename RetT,
|
|
|
|
typename ArgT,
|
|
|
|
typename FuncT,
|
|
|
|
std::size_t ...Indices,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
is_coord_v<ArgT> && is_coord_v<RetT>, void
|
|
|
|
>
|
|
|
|
>
|
|
|
|
constexpr auto
|
|
|
|
apply (const std::index_sequence<Indices...>,
|
|
|
|
FuncT &&func,
|
|
|
|
const ArgT &a,
|
|
|
|
const ArgT &b)
|
|
|
|
{
|
|
|
|
return RetT {
|
|
|
|
std::invoke (func, a[Indices], b[Indices])...
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <
|
|
|
|
typename RetT,
|
|
|
|
std::size_t S,
|
|
|
|
typename T,
|
|
|
|
template <std::size_t,typename> class ArgT,
|
|
|
|
typename FuncT,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
is_coord_v<RetT> && is_coord_v<ArgT>, void
|
|
|
|
>,
|
|
|
|
typename Indices = std::make_index_sequence<S>
|
|
|
|
>
|
|
|
|
constexpr auto
|
|
|
|
apply (FuncT &&func, const ArgT<S,T> &a, const ArgT<S,T> &b)
|
|
|
|
{
|
|
|
|
return detail::apply (Indices{}, std::forward<FuncT> (func), a, b);
|
|
|
|
}
|
|
|
|
|
2015-10-19 17:06:19 +11:00
|
|
|
|
2015-03-06 01:46:01 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// logic operators
|
2017-08-27 12:32:00 +10:00
|
|
|
namespace detail {
|
|
|
|
template <
|
|
|
|
std::size_t S,
|
|
|
|
typename T,
|
|
|
|
template <std::size_t,typename> class K,
|
|
|
|
typename FuncT,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
is_coord_v<K<S,T>>,
|
|
|
|
void
|
|
|
|
>,
|
|
|
|
std::size_t ...Indices
|
|
|
|
>
|
|
|
|
constexpr auto
|
|
|
|
compare (FuncT &&func, std::index_sequence<Indices...>, const K<S,T> a, const K<S,T> b)
|
|
|
|
{
|
|
|
|
return vector<S,bool> {
|
|
|
|
std::invoke (func, a[Indices], b[Indices])...
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <
|
|
|
|
std::size_t S,
|
|
|
|
typename T,
|
|
|
|
template <std::size_t,typename> class K,
|
|
|
|
typename FuncT,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
is_coord_v<K<S,T>>,
|
|
|
|
void
|
|
|
|
>,
|
|
|
|
typename Indices = std::make_index_sequence<S>
|
|
|
|
>
|
|
|
|
constexpr auto
|
|
|
|
compare (const K<S,T> a, const K<S,T> b, FuncT &&func)
|
|
|
|
{
|
|
|
|
return detail::compare (std::forward<FuncT> (func), Indices{}, a, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <
|
|
|
|
std::size_t S,
|
|
|
|
typename T,
|
|
|
|
template <std::size_t,typename> class K,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
is_coord_v<K<S,T>>,
|
|
|
|
void
|
|
|
|
>
|
|
|
|
>
|
|
|
|
constexpr auto
|
|
|
|
compare (const K<S,T> a, const K<S,T> b)
|
|
|
|
{
|
|
|
|
return compare (a, b, std::equal_to<T> {});
|
|
|
|
}
|
|
|
|
|
2015-03-07 03:18:32 +11:00
|
|
|
|
|
|
|
/// elementwise equality operator
|
2015-03-06 01:46:01 +11:00
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S,
|
2015-03-06 01:46:01 +11:00
|
|
|
typename T,
|
2017-08-28 12:25:23 +10:00
|
|
|
template <std::size_t,typename> class K,
|
2016-03-14 19:54:25 +11:00
|
|
|
typename = std::enable_if_t<
|
2016-09-22 16:20:32 +10:00
|
|
|
is_coord_v<K<S,T>>, void
|
2016-03-14 19:54:25 +11:00
|
|
|
>
|
2015-03-06 01:46:01 +11:00
|
|
|
>
|
2017-08-27 12:32:00 +10:00
|
|
|
constexpr bool
|
2016-03-14 19:54:25 +11:00
|
|
|
operator== (const K<S,T> a, const K<S,T> b)
|
2015-03-06 01:46:01 +11:00
|
|
|
{
|
2017-08-27 12:32:00 +10:00
|
|
|
return all (compare (a, b, std::equal_to<T> {}));
|
2015-03-06 01:46:01 +11:00
|
|
|
}
|
|
|
|
|
2015-03-07 03:18:32 +11:00
|
|
|
///------------------------------------------------------------------------
|
|
|
|
/// elementwise inquality operator
|
2015-03-06 01:46:01 +11:00
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S,
|
2015-03-06 01:46:01 +11:00
|
|
|
typename T,
|
2017-08-28 12:25:23 +10:00
|
|
|
template <std::size_t,typename> class K,
|
2016-03-14 19:54:25 +11:00
|
|
|
typename = std::enable_if_t<
|
2016-09-22 16:20:32 +10:00
|
|
|
is_coord_v<K<S,T>>, void
|
2016-03-14 19:54:25 +11:00
|
|
|
>
|
2015-03-06 01:46:01 +11:00
|
|
|
>
|
2017-08-27 12:32:00 +10:00
|
|
|
constexpr bool
|
|
|
|
operator!= (const K<S,T> a, const K<S,T> b)
|
2015-03-06 01:46:01 +11:00
|
|
|
{
|
2017-08-27 12:32:00 +10:00
|
|
|
return any (compare (a, b, std::not_equal_to<T> {}));
|
2015-03-06 01:46:01 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-11 16:32:48 +10:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S,
|
2016-08-11 16:32:48 +10:00
|
|
|
typename T,
|
2017-08-28 12:25:23 +10:00
|
|
|
template <std::size_t,typename> class K,
|
2016-08-11 16:32:48 +10:00
|
|
|
typename = std::enable_if_t<
|
2016-09-22 16:20:32 +10:00
|
|
|
is_coord_v<K<S,T>>, void
|
2016-08-11 16:32:48 +10:00
|
|
|
>
|
|
|
|
>
|
|
|
|
constexpr
|
|
|
|
bool
|
|
|
|
almost_zero (const K<S,T> &k)
|
|
|
|
{
|
|
|
|
return std::all_of (
|
|
|
|
std::cbegin (k),
|
|
|
|
std::cend (k),
|
|
|
|
[] (T t) { return almost_equal (t); }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-06 01:46:01 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// special operators
|
2015-03-07 03:18:32 +11:00
|
|
|
|
|
|
|
/// point-point subtraction giving a vector difference
|
2015-03-06 01:46:01 +11:00
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S,
|
2015-05-28 10:18:07 +10:00
|
|
|
typename T,
|
|
|
|
typename U
|
2015-03-06 01:46:01 +11:00
|
|
|
>
|
2016-03-14 19:01:59 +11:00
|
|
|
constexpr
|
2016-03-14 19:54:25 +11:00
|
|
|
vector<S,std::common_type_t<T,U>>
|
2015-05-28 10:18:07 +10:00
|
|
|
operator- (point<S,T> a, point<S,U> b)
|
2015-03-06 01:46:01 +11:00
|
|
|
{
|
2017-08-27 12:27:26 +10:00
|
|
|
return a.template as<vector> () - b.template as<vector> ();
|
2015-03-06 01:46:01 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-07 03:18:32 +11:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S,
|
2015-05-28 10:18:07 +10:00
|
|
|
typename T,
|
2016-05-12 18:22:55 +10:00
|
|
|
typename U,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
std::is_arithmetic<T>::value && std::is_arithmetic<U>::value,
|
|
|
|
void
|
|
|
|
>
|
2015-03-07 03:18:32 +11:00
|
|
|
>
|
2016-03-14 19:01:59 +11:00
|
|
|
constexpr
|
2016-03-14 19:54:25 +11:00
|
|
|
vector<S,std::common_type_t<T,U>>
|
2015-05-28 10:18:07 +10:00
|
|
|
operator- (U u, point<S,T> p)
|
2015-03-07 03:18:32 +11:00
|
|
|
{
|
2015-05-28 10:18:07 +10:00
|
|
|
return point<S,U> {u} - p;
|
2015-03-07 03:18:32 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-11 14:23:32 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2015-11-04 17:10:16 +11:00
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S,
|
2015-11-04 17:10:16 +11:00
|
|
|
typename T
|
|
|
|
>
|
2016-03-14 19:01:59 +11:00
|
|
|
constexpr
|
|
|
|
T
|
|
|
|
dot (const T (&a)[S], const T (&b)[S])
|
2015-11-04 17:10:16 +11:00
|
|
|
{
|
|
|
|
T sum = 0;
|
2017-08-28 12:25:23 +10:00
|
|
|
for (std::size_t i = 0; i < S; ++i)
|
2015-11-04 17:10:16 +11:00
|
|
|
sum += a[i] * b[i];
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
2016-08-11 14:23:32 +10:00
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
2015-03-06 01:46:01 +11:00
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S,
|
2015-03-06 01:46:01 +11:00
|
|
|
typename T,
|
2017-08-28 12:25:23 +10:00
|
|
|
template <std::size_t,typename> class A,
|
|
|
|
template <std::size_t,typename> class B,
|
2016-03-14 19:54:25 +11:00
|
|
|
typename = std::enable_if_t<
|
2016-09-22 16:20:32 +10:00
|
|
|
is_coord_v<A<S,T>> && is_coord_v<B<S,T>>, void
|
2016-03-14 19:54:25 +11:00
|
|
|
>
|
2015-03-06 01:46:01 +11:00
|
|
|
>
|
2016-03-14 19:01:59 +11:00
|
|
|
constexpr
|
2016-03-14 19:54:25 +11:00
|
|
|
T
|
2015-11-04 23:22:49 +11:00
|
|
|
dot (A<S,T> a, B<S,T> b)
|
2015-03-06 01:46:01 +11:00
|
|
|
{
|
2015-11-04 17:10:16 +11:00
|
|
|
return dot<S,T> (a.data, b.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-11 14:23:32 +10:00
|
|
|
//-------------------------------------------------------------------------
|
2015-11-04 17:10:16 +11:00
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S,
|
2015-11-04 17:10:16 +11:00
|
|
|
typename T,
|
2017-08-28 12:25:23 +10:00
|
|
|
template <std::size_t,typename> class K,
|
2016-03-14 19:54:25 +11:00
|
|
|
typename = std::enable_if_t<
|
2016-09-22 16:20:32 +10:00
|
|
|
is_coord_v<K<S,T>>, void
|
2016-03-14 19:54:25 +11:00
|
|
|
>
|
2015-11-04 17:10:16 +11:00
|
|
|
>
|
2016-03-14 19:01:59 +11:00
|
|
|
constexpr
|
2016-03-14 19:54:25 +11:00
|
|
|
T
|
2015-11-04 23:22:49 +11:00
|
|
|
dot (K<S,T> a, const T (&b)[S])
|
2015-11-04 17:10:16 +11:00
|
|
|
{
|
|
|
|
return dot<S,T> (a.data, b);
|
2015-03-06 01:46:01 +11:00
|
|
|
}
|
2015-03-06 17:49:35 +11:00
|
|
|
|
2016-03-14 19:01:59 +11:00
|
|
|
|
2016-08-11 14:23:32 +10:00
|
|
|
//-------------------------------------------------------------------------
|
2015-11-04 17:10:16 +11:00
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S,
|
2015-11-04 17:10:16 +11:00
|
|
|
typename T,
|
2017-08-28 12:25:23 +10:00
|
|
|
template <std::size_t,typename> class K,
|
2016-03-14 19:54:25 +11:00
|
|
|
typename = std::enable_if_t<
|
2016-09-22 16:20:32 +10:00
|
|
|
is_coord_v<K<S,T>>, void
|
2016-03-14 19:54:25 +11:00
|
|
|
>
|
2015-11-04 17:10:16 +11:00
|
|
|
>
|
2016-03-14 19:01:59 +11:00
|
|
|
constexpr
|
2016-03-14 19:54:25 +11:00
|
|
|
T
|
2015-11-04 23:22:49 +11:00
|
|
|
dot (const T (&a)[S], K<S,T> b)
|
2015-11-04 17:10:16 +11:00
|
|
|
{
|
|
|
|
return dot<S,T> (a, b.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-11 14:58:46 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S,
|
2016-08-11 14:58:46 +10:00
|
|
|
typename T,
|
2017-08-28 12:25:23 +10:00
|
|
|
template <std::size_t,typename> class K,
|
2016-08-11 14:58:46 +10:00
|
|
|
typename = std::enable_if_t<coord::has_norm_v<K>,void>
|
|
|
|
>
|
|
|
|
constexpr
|
|
|
|
T
|
|
|
|
norm2 (const K<S,T> &k)
|
|
|
|
{
|
2016-12-21 20:21:40 +11:00
|
|
|
return dot (k, k);
|
2016-08-11 14:58:46 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-29 15:51:08 +10:00
|
|
|
//-------------------------------------------------------------------------
|
2016-08-11 14:58:46 +10:00
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S,
|
2016-08-11 14:58:46 +10:00
|
|
|
typename T,
|
2017-08-28 12:25:23 +10:00
|
|
|
template <std::size_t,typename> class K,
|
2016-08-11 14:58:46 +10:00
|
|
|
typename = std::enable_if_t<
|
|
|
|
coord::has_norm_v<K>,
|
|
|
|
void
|
|
|
|
>
|
|
|
|
>
|
|
|
|
constexpr
|
|
|
|
T
|
|
|
|
norm (const K<S,T> &k)
|
|
|
|
{
|
|
|
|
return std::sqrt (norm2 (k));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S,
|
2016-08-11 14:58:46 +10:00
|
|
|
typename T,
|
2017-08-28 12:25:23 +10:00
|
|
|
template <std::size_t,typename> class K,
|
2016-08-11 14:58:46 +10:00
|
|
|
typename = std::enable_if_t<
|
|
|
|
coord::has_norm_v<K>,
|
|
|
|
void
|
|
|
|
>
|
|
|
|
>
|
|
|
|
constexpr
|
|
|
|
K<S,T>
|
|
|
|
normalised (const K<S,T> &k)
|
|
|
|
{
|
2016-10-12 22:58:13 +11:00
|
|
|
auto n = norm (k);
|
|
|
|
CHECK_NEZ (n);
|
|
|
|
|
|
|
|
return k / n;
|
2016-08-11 14:58:46 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S,
|
2016-08-11 14:58:46 +10:00
|
|
|
typename T,
|
2017-08-28 12:25:23 +10:00
|
|
|
template <std::size_t,typename> class K,
|
2016-08-11 14:58:46 +10:00
|
|
|
typename = std::enable_if_t<
|
|
|
|
coord::has_norm_v<K>,
|
|
|
|
void
|
|
|
|
>
|
|
|
|
>
|
|
|
|
constexpr
|
|
|
|
bool
|
|
|
|
is_normalised (const K<S,T> &k)
|
|
|
|
{
|
|
|
|
return almost_equal (norm2 (k), T{1});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2015-05-29 15:51:08 +10:00
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S,
|
2015-05-29 15:51:08 +10:00
|
|
|
typename T,
|
2017-08-28 12:25:23 +10:00
|
|
|
template <std::size_t,typename> class K,
|
2016-03-14 19:54:25 +11:00
|
|
|
typename = std::enable_if_t<
|
2016-09-22 16:20:32 +10:00
|
|
|
is_coord_v<K<S,T>>, void
|
2016-03-14 19:54:25 +11:00
|
|
|
>
|
2015-05-29 15:51:08 +10:00
|
|
|
>
|
2016-03-14 19:01:59 +11:00
|
|
|
constexpr
|
2015-05-29 15:51:08 +10:00
|
|
|
K<S,T>
|
|
|
|
abs (K<S,T> k)
|
|
|
|
{
|
|
|
|
for (auto &v: k)
|
|
|
|
v = std::abs (v);
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
|
2016-08-11 14:23:32 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2015-10-12 23:56:41 +11:00
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S,
|
2015-10-12 23:56:41 +11:00
|
|
|
typename T,
|
2017-08-28 12:25:23 +10:00
|
|
|
template <std::size_t,typename> class K,
|
2016-03-14 19:54:25 +11:00
|
|
|
typename = std::enable_if_t<
|
2016-09-22 16:20:32 +10:00
|
|
|
is_coord_v<K<S,T>>, void
|
2016-03-14 19:54:25 +11:00
|
|
|
>
|
2015-10-12 23:56:41 +11:00
|
|
|
>
|
2016-03-14 19:01:59 +11:00
|
|
|
constexpr
|
2015-10-12 23:56:41 +11:00
|
|
|
K<S,T>
|
|
|
|
pow (K<S,T> k)
|
|
|
|
{
|
|
|
|
for (auto &v: k)
|
|
|
|
v = pow (v);
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
|
2016-09-23 13:27:44 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// root of sum of squares
|
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S,
|
2016-09-23 13:27:44 +10:00
|
|
|
typename T,
|
2017-08-28 12:25:23 +10:00
|
|
|
template <std::size_t,typename> class K,
|
2016-09-23 13:27:44 +10:00
|
|
|
typename = std::enable_if_t<
|
|
|
|
is_coord_v<K<S,T>>, void
|
|
|
|
>
|
|
|
|
>
|
|
|
|
constexpr
|
|
|
|
T
|
|
|
|
hypot (K<S,T> k)
|
|
|
|
{
|
|
|
|
return std::sqrt (sum (k * k));
|
|
|
|
}
|
|
|
|
|
2015-10-12 23:56:41 +11:00
|
|
|
|
2016-09-14 17:38:30 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S,
|
2016-09-14 17:38:30 +10:00
|
|
|
typename T,
|
2017-08-28 12:25:23 +10:00
|
|
|
template <std::size_t,typename> class K,
|
2016-09-14 17:38:30 +10:00
|
|
|
typename = std::enable_if_t<
|
2016-09-22 16:20:32 +10:00
|
|
|
is_coord_v<K<S,T>>, void
|
2016-09-14 17:38:30 +10:00
|
|
|
>
|
|
|
|
>
|
|
|
|
constexpr
|
|
|
|
K<S,T>
|
|
|
|
mod (K<S,T> k, T t)
|
|
|
|
{
|
2016-09-14 17:42:11 +10:00
|
|
|
std::transform (
|
|
|
|
std::cbegin (k),
|
|
|
|
std::cend (k),
|
|
|
|
std::begin (k),
|
|
|
|
[t] (auto v) { return mod (v, t);
|
|
|
|
});
|
2016-09-14 17:38:30 +10:00
|
|
|
return k;
|
|
|
|
}
|
|
|
|
|
2016-09-14 17:39:32 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// trigonometric functions
|
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S,
|
2016-09-14 17:39:32 +10:00
|
|
|
typename T,
|
2017-08-28 12:25:23 +10:00
|
|
|
template <std::size_t,typename> class K,
|
2016-09-22 16:20:32 +10:00
|
|
|
typename = std::enable_if_t<is_coord_v<K<S,T>>,void>
|
2016-09-14 17:39:32 +10:00
|
|
|
>
|
|
|
|
constexpr
|
|
|
|
K<S,T>
|
|
|
|
sin (K<S,T> k)
|
|
|
|
{
|
|
|
|
std::transform (
|
|
|
|
std::cbegin (k),
|
|
|
|
std::cend (k),
|
|
|
|
std::begin (k),
|
|
|
|
[] (auto v) { return std::sin (v); }
|
|
|
|
);
|
|
|
|
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S,
|
2016-09-14 17:39:32 +10:00
|
|
|
typename T,
|
2017-08-28 12:25:23 +10:00
|
|
|
template <std::size_t,typename> class K,
|
2016-09-22 16:20:32 +10:00
|
|
|
typename = std::enable_if_t<is_coord_v<K<S,T>>,void>
|
2016-09-14 17:39:32 +10:00
|
|
|
>
|
|
|
|
constexpr
|
|
|
|
K<S,T>
|
|
|
|
cos (K<S,T> k)
|
|
|
|
{
|
|
|
|
std::transform (
|
|
|
|
std::cbegin (k),
|
|
|
|
std::cend (k),
|
|
|
|
std::begin (k),
|
|
|
|
[] (auto v) { return std::cos (v); }
|
|
|
|
);
|
|
|
|
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
|
2016-09-14 17:38:30 +10:00
|
|
|
|
2015-03-07 03:18:32 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// logical element operators
|
|
|
|
|
|
|
|
/// return a coord type containing the max element at each offset
|
2015-03-06 17:49:35 +11:00
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S,
|
2015-03-06 17:49:35 +11:00
|
|
|
typename T,
|
2017-08-28 12:25:23 +10:00
|
|
|
template <std::size_t,typename> class K,
|
2016-03-14 19:54:25 +11:00
|
|
|
typename = std::enable_if_t<
|
2016-09-22 16:20:32 +10:00
|
|
|
is_coord_v<K<S,T>>, void
|
2017-07-31 16:16:54 +10:00
|
|
|
>,
|
|
|
|
typename ...Args
|
2015-03-06 17:49:35 +11:00
|
|
|
>
|
2016-03-14 19:01:59 +11:00
|
|
|
constexpr
|
2015-03-06 17:49:35 +11:00
|
|
|
K<S,T>
|
2017-07-31 16:16:54 +10:00
|
|
|
min (K<S,T> a, K<S,T> b, Args &&...args)
|
2015-03-06 17:49:35 +11:00
|
|
|
{
|
2017-07-31 16:16:54 +10:00
|
|
|
static_assert ((... && std::is_same<K<S,T>, std::decay_t<Args>>::value));
|
|
|
|
|
2016-03-14 19:01:59 +11:00
|
|
|
K<S,T> out {};
|
2017-08-28 12:25:23 +10:00
|
|
|
for (std::size_t i = 0; i < S; ++i)
|
2017-07-31 16:16:54 +10:00
|
|
|
out[i] = min (a[i], b[i], args[i]...);
|
2015-03-06 17:49:35 +11:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-07 03:18:32 +11:00
|
|
|
///------------------------------------------------------------------------
|
|
|
|
// /return a coord type containing the max element at each offset
|
2015-03-06 17:49:35 +11:00
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S,
|
2015-03-06 17:49:35 +11:00
|
|
|
typename T,
|
2017-08-28 12:25:23 +10:00
|
|
|
template <std::size_t,typename> class K,
|
2016-03-14 19:54:25 +11:00
|
|
|
typename = std::enable_if_t<
|
2016-09-22 16:20:32 +10:00
|
|
|
is_coord_v<K<S,T>>, void
|
2017-07-31 16:16:54 +10:00
|
|
|
>,
|
|
|
|
typename ...Args
|
2015-03-06 17:49:35 +11:00
|
|
|
>
|
2016-03-14 19:01:59 +11:00
|
|
|
constexpr
|
2015-03-06 17:49:35 +11:00
|
|
|
K<S,T>
|
2017-07-31 16:16:54 +10:00
|
|
|
max (K<S,T> a, K<S,T> b, Args &&...args)
|
2015-03-06 17:49:35 +11:00
|
|
|
{
|
2017-07-31 16:16:54 +10:00
|
|
|
static_assert ((... && std::is_same<K<S,T>, std::decay_t<Args>>::value));
|
|
|
|
|
2016-03-14 19:01:59 +11:00
|
|
|
K<S,T> out {};
|
2017-08-28 12:25:23 +10:00
|
|
|
for (std::size_t i = 0; i < S; ++i)
|
2017-07-31 16:16:54 +10:00
|
|
|
out[i] = max (a[i], b[i], args[i]...);
|
2015-03-06 17:49:35 +11:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-16 17:26:16 +10:00
|
|
|
//-------------------------------------------------------------------------
|
2017-08-24 12:49:59 +10:00
|
|
|
/// returns a coordinate type where each element has been clamped to the
|
|
|
|
/// range [lo,hi].
|
|
|
|
///
|
|
|
|
/// we specifically do not allow different coordinate types for val, lo,
|
|
|
|
/// and hi because the min and max calls are ill definied for varying
|
|
|
|
/// types (not because varying types would not be useful).
|
2017-08-16 17:26:16 +10:00
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S,
|
2017-08-16 17:26:16 +10:00
|
|
|
typename T,
|
2017-08-28 12:25:23 +10:00
|
|
|
template<std::size_t,typename> class K,
|
2017-08-16 17:26:16 +10:00
|
|
|
typename = std::enable_if_t<
|
|
|
|
is_coord_v<K<S,T>>, void
|
|
|
|
>
|
|
|
|
>
|
|
|
|
constexpr
|
|
|
|
K<S,T>
|
|
|
|
limit (K<S,T> k, K<S,T> lo, K<S,T> hi)
|
|
|
|
{
|
|
|
|
assert (all (lo <= hi));
|
|
|
|
return max (min (k, hi), lo);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-24 12:49:59 +10:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S,
|
2017-08-24 12:49:59 +10:00
|
|
|
typename T,
|
2017-08-28 12:25:23 +10:00
|
|
|
template <std::size_t,typename> class K,
|
2017-08-24 12:49:59 +10:00
|
|
|
typename = std::enable_if_t<
|
|
|
|
is_coord_v<K<S,T>>, void
|
|
|
|
>
|
|
|
|
>
|
|
|
|
constexpr
|
|
|
|
K<S,T>
|
|
|
|
limit (K<S,T> k, T lo, K<S,T> hi)
|
|
|
|
{
|
|
|
|
return limit (k, K<S,T> {lo}, hi);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S,
|
2017-08-24 12:49:59 +10:00
|
|
|
typename T,
|
2017-08-28 12:25:23 +10:00
|
|
|
template <std::size_t,typename> class K,
|
2017-08-24 12:49:59 +10:00
|
|
|
typename = std::enable_if_t<
|
|
|
|
is_coord_v<K<S,T>>, void
|
|
|
|
>
|
|
|
|
>
|
|
|
|
constexpr
|
|
|
|
K<S,T>
|
|
|
|
limit (K<S,T> k, K<S,T> lo, T hi)
|
|
|
|
{
|
|
|
|
return limit (k, lo, K<S,T> {hi});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S,
|
2017-08-24 12:49:59 +10:00
|
|
|
typename T,
|
2017-08-28 12:25:23 +10:00
|
|
|
template <std::size_t,typename> class K,
|
2017-08-24 12:49:59 +10:00
|
|
|
typename = std::enable_if_t<
|
|
|
|
is_coord_v<K<S,T>>, void
|
|
|
|
>
|
|
|
|
>
|
|
|
|
constexpr K<S,T>
|
|
|
|
limit (K<S,T> k, T lo, T hi)
|
|
|
|
{
|
|
|
|
return limit (k, K<S,T> {lo}, K<S,T> {hi});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-07 03:18:32 +11:00
|
|
|
///------------------------------------------------------------------------
|
2016-03-14 19:54:25 +11:00
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S,
|
2016-03-14 19:54:25 +11:00
|
|
|
typename T,
|
2017-08-28 12:25:23 +10:00
|
|
|
template<std::size_t,typename> class K,
|
2016-03-14 19:54:25 +11:00
|
|
|
typename = std::enable_if_t<
|
2016-09-22 16:20:32 +10:00
|
|
|
is_coord_v<K<S,T>>, void
|
2016-03-14 19:54:25 +11:00
|
|
|
>
|
|
|
|
>
|
2016-03-14 19:01:59 +11:00
|
|
|
constexpr
|
2015-03-06 17:49:35 +11:00
|
|
|
T
|
2016-03-14 19:54:25 +11:00
|
|
|
min (const K<S,T> k)
|
|
|
|
{
|
|
|
|
return *std::min_element (std::cbegin (k), std::cend (k));
|
|
|
|
}
|
2015-03-06 17:49:35 +11:00
|
|
|
|
|
|
|
|
2016-03-14 19:54:25 +11:00
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S,
|
2016-03-14 19:54:25 +11:00
|
|
|
typename T,
|
2017-08-28 12:25:23 +10:00
|
|
|
template<std::size_t,typename> class K,
|
2016-03-14 19:54:25 +11:00
|
|
|
typename = std::enable_if_t<
|
2016-09-22 16:20:32 +10:00
|
|
|
is_coord_v<K<S,T>>, void
|
2016-03-14 19:54:25 +11:00
|
|
|
>
|
|
|
|
>
|
2016-03-14 19:01:59 +11:00
|
|
|
constexpr
|
2015-03-06 17:49:35 +11:00
|
|
|
T
|
2016-03-14 19:54:25 +11:00
|
|
|
max (const K<S,T> k)
|
|
|
|
{
|
|
|
|
return *std::max_element (std::cbegin (k), std::cend (k));
|
|
|
|
}
|
2015-05-26 16:25:41 +10:00
|
|
|
|
|
|
|
|
2016-08-15 18:46:04 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S,
|
2016-08-15 18:46:04 +10:00
|
|
|
typename T,
|
2017-08-28 12:25:23 +10:00
|
|
|
template <std::size_t,typename> class K,
|
2016-08-15 18:46:04 +10:00
|
|
|
typename = std::enable_if_t<
|
2016-09-22 16:20:32 +10:00
|
|
|
is_coord_v<K<S,T>>, void
|
2016-08-15 18:46:04 +10:00
|
|
|
>
|
|
|
|
>
|
|
|
|
constexpr
|
|
|
|
T
|
|
|
|
sum (const K<S,T> k)
|
|
|
|
{
|
2017-06-16 17:38:55 +10:00
|
|
|
// DO NOT USE util::sum(begin, end) from maths.hpp
|
|
|
|
//
|
|
|
|
// It would be nice to use kahan summation from maths.hpp but speed
|
|
|
|
// and simplicity is more important for these fixed sized
|
|
|
|
// coordinates. Infinities tend to crop up using these classes and
|
|
|
|
// they cause a few headaches in the kahan code.
|
|
|
|
//
|
|
|
|
// So, if the user wants kahan summation they can request it
|
|
|
|
// explicitly.
|
|
|
|
|
|
|
|
return std::accumulate (std::cbegin (k), std::cend (k), T{0});
|
2016-08-15 18:46:04 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2016-09-22 16:20:32 +10:00
|
|
|
#define VECTOR_OP(OP) \
|
|
|
|
template < \
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S, \
|
2016-09-22 16:20:32 +10:00
|
|
|
typename T, \
|
|
|
|
typename U, \
|
2017-08-28 12:25:23 +10:00
|
|
|
template <std::size_t,typename> class A, \
|
|
|
|
template <std::size_t,typename> class B, \
|
2016-09-22 16:20:32 +10:00
|
|
|
typename = std::enable_if_t< \
|
|
|
|
is_coord_v<A<S,T>> && is_coord_v<B<S,U>>, void \
|
|
|
|
> \
|
|
|
|
> \
|
|
|
|
constexpr \
|
|
|
|
vector<S,bool> \
|
|
|
|
operator OP (const A<S,T> a, const B<S,U> b) \
|
|
|
|
{ \
|
|
|
|
vector<S,bool> out {}; \
|
2017-08-28 12:25:23 +10:00
|
|
|
for (std::size_t i = 0; i < S; ++i) \
|
2016-09-22 16:20:32 +10:00
|
|
|
out[i] = a[i] OP b[i]; \
|
|
|
|
return out; \
|
2015-10-20 12:37:08 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
VECTOR_OP(<)
|
|
|
|
VECTOR_OP(>)
|
|
|
|
VECTOR_OP(<=)
|
|
|
|
VECTOR_OP(>=)
|
2016-09-23 13:39:13 +10:00
|
|
|
VECTOR_OP(&&)
|
|
|
|
VECTOR_OP(||)
|
2015-10-20 12:37:08 +11:00
|
|
|
|
|
|
|
#undef VECTOR_OP
|
|
|
|
|
|
|
|
|
2017-08-28 12:25:23 +10:00
|
|
|
#define SCALAR_OP(OP) \
|
|
|
|
template < \
|
|
|
|
std::size_t S, \
|
|
|
|
typename T, \
|
|
|
|
typename U, \
|
|
|
|
template <std::size_t,typename> class K, \
|
|
|
|
typename = std::enable_if_t< \
|
|
|
|
is_coord_v<K<S,T>>, void \
|
|
|
|
> \
|
|
|
|
> \
|
|
|
|
constexpr \
|
|
|
|
vector<S,bool> \
|
|
|
|
operator OP (const K<S,T> k, const U u) \
|
|
|
|
{ \
|
|
|
|
vector<S,bool> out {}; \
|
|
|
|
for (std::size_t i = 0; i < S; ++i) \
|
|
|
|
out[i] = k[i] OP u; \
|
|
|
|
return out; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
template < \
|
|
|
|
std::size_t S, \
|
|
|
|
typename T, \
|
|
|
|
typename U, \
|
|
|
|
template <std::size_t,typename> class K, \
|
|
|
|
typename = std::enable_if_t< \
|
|
|
|
is_coord_v<K<S,T>>, void \
|
|
|
|
> \
|
|
|
|
> \
|
|
|
|
constexpr \
|
|
|
|
vector<S,bool> \
|
|
|
|
operator OP (const U u, const K<S,T> k) \
|
|
|
|
{ \
|
|
|
|
vector<S,bool> out {}; \
|
|
|
|
for (std::size_t i = 0; i < S; ++i) \
|
|
|
|
out[i] = u OP k[i]; \
|
|
|
|
return out; \
|
2015-10-19 17:06:19 +11:00
|
|
|
}
|
2015-05-26 16:25:41 +10:00
|
|
|
|
2015-10-19 17:06:19 +11:00
|
|
|
SCALAR_OP(<)
|
|
|
|
SCALAR_OP(>)
|
|
|
|
SCALAR_OP(<=)
|
|
|
|
SCALAR_OP(>=)
|
2017-08-27 12:32:00 +10:00
|
|
|
SCALAR_OP(==)
|
2016-09-23 13:39:13 +10:00
|
|
|
SCALAR_OP(&&)
|
|
|
|
SCALAR_OP(||)
|
2015-05-26 16:25:41 +10:00
|
|
|
|
2015-10-19 17:06:19 +11:00
|
|
|
#undef SCALAR_OP
|
|
|
|
|
|
|
|
|
2017-08-24 14:32:58 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
namespace detail {
|
|
|
|
template <
|
|
|
|
std::size_t S,
|
|
|
|
template <std::size_t,typename> class K,
|
|
|
|
std::size_t ...I,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
is_coord_v<K<S,bool>>,
|
|
|
|
void
|
|
|
|
>
|
|
|
|
>
|
|
|
|
constexpr bool
|
|
|
|
any (const K<S,bool> k, std::index_sequence<I...>)
|
|
|
|
{
|
|
|
|
return (k[I] || ...);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
///---------------------------------------------------------------------------
|
|
|
|
/// returns true if any element is true.
|
|
|
|
///
|
|
|
|
/// this function must be suitable for use in static_assert, so it must remain
|
|
|
|
/// constexpr.
|
|
|
|
///
|
|
|
|
/// we would ideally use std::any_of, but it is not constexpr.
|
|
|
|
/// we would ideally use range-for, but cbegin is not constexpr.
|
|
|
|
/// so... moar templates.
|
2016-03-14 19:54:25 +11:00
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S,
|
|
|
|
template <std::size_t,typename> class K,
|
2016-03-14 19:54:25 +11:00
|
|
|
typename = std::enable_if_t<
|
2016-09-22 16:20:32 +10:00
|
|
|
is_coord_v<K<S,bool>>, void
|
2017-08-24 14:32:58 +10:00
|
|
|
>,
|
|
|
|
typename Indices = std::make_index_sequence<S>
|
2016-03-14 19:54:25 +11:00
|
|
|
>
|
2016-03-14 19:01:59 +11:00
|
|
|
constexpr
|
2015-05-26 16:25:41 +10:00
|
|
|
bool
|
2015-10-19 17:06:19 +11:00
|
|
|
any (const K<S,bool> k)
|
|
|
|
{
|
2017-08-24 14:32:58 +10:00
|
|
|
return detail::any (k, Indices{});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
namespace detail {
|
|
|
|
template <
|
|
|
|
std::size_t S,
|
2017-08-28 12:25:23 +10:00
|
|
|
template <std::size_t,typename> class K,
|
2017-08-24 14:32:58 +10:00
|
|
|
std::size_t ...I,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
is_coord_v<K<S,bool>>,
|
|
|
|
void
|
|
|
|
>
|
|
|
|
>
|
|
|
|
constexpr bool
|
|
|
|
all (const K<S,bool> k, std::index_sequence<I...>)
|
|
|
|
{
|
|
|
|
return (k[I] && ...);
|
|
|
|
}
|
2015-10-19 17:06:19 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
2017-08-24 14:32:58 +10:00
|
|
|
/// returns true if all elements are true.
|
|
|
|
///
|
|
|
|
/// this function must be suitable for use in static_assert, so it must be
|
|
|
|
/// constexpr.
|
|
|
|
///
|
|
|
|
/// we would ideally use std::all_of, but it is not constexpr.
|
|
|
|
/// we would ideally use range-for, but cbegin is not constexpr.
|
|
|
|
/// so... moar templates.
|
2016-03-14 19:54:25 +11:00
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S,
|
|
|
|
template <std::size_t,typename> class K,
|
2016-03-14 19:54:25 +11:00
|
|
|
typename = std::enable_if_t<
|
2016-09-22 16:20:32 +10:00
|
|
|
is_coord_v<K<S,bool>>, void
|
2017-08-24 14:32:58 +10:00
|
|
|
>,
|
|
|
|
typename Indices = std::make_index_sequence<S>
|
2016-03-14 19:54:25 +11:00
|
|
|
>
|
2016-03-14 19:01:59 +11:00
|
|
|
constexpr
|
2015-10-19 17:06:19 +11:00
|
|
|
bool
|
|
|
|
all (const K<S,bool> k)
|
|
|
|
{
|
2017-08-24 14:32:58 +10:00
|
|
|
return detail::all (k, Indices {});
|
2015-10-19 17:06:19 +11:00
|
|
|
}
|
2015-09-29 18:06:52 +10:00
|
|
|
|
|
|
|
|
2017-07-31 15:41:33 +10:00
|
|
|
///------------------------------------------------------------------------
|
|
|
|
/// returns an instance of K elementwise using a when s is true, and b
|
|
|
|
/// otherwise. ie, k[i] = s[i] ? a[i] : b[i];
|
|
|
|
///
|
|
|
|
/// corresponds to the function `select' from OpenCL.
|
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S,
|
2017-07-31 15:41:33 +10:00
|
|
|
typename T,
|
2017-08-28 12:25:23 +10:00
|
|
|
template <std::size_t,typename> class K,
|
2017-07-31 15:41:33 +10:00
|
|
|
typename = std::enable_if_t<
|
|
|
|
is_coord_v<K<S,T>>,
|
|
|
|
void
|
|
|
|
>
|
|
|
|
>
|
|
|
|
constexpr
|
|
|
|
K<S,T>
|
|
|
|
select (vector<S,bool> s, K<S,T> a, K<S,T> b)
|
|
|
|
{
|
|
|
|
K<S,T> k {};
|
2017-08-28 12:25:23 +10:00
|
|
|
for (std::size_t i = 0; i < S; ++i)
|
2017-07-31 15:41:33 +10:00
|
|
|
k[i] = s[i] ? a[i] : b[i];
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
|
2017-08-27 12:32:00 +10:00
|
|
|
|
2015-09-29 18:06:52 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2016-03-14 19:54:25 +11:00
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S,
|
2016-03-14 19:54:25 +11:00
|
|
|
typename T,
|
2017-08-28 12:25:23 +10:00
|
|
|
template<std::size_t,typename> class K,
|
2016-03-14 19:54:25 +11:00
|
|
|
typename = std::enable_if_t<
|
2016-09-22 16:20:32 +10:00
|
|
|
is_coord_v<K<S,T>> && std::is_floating_point<T>::value, void
|
2016-03-14 19:54:25 +11:00
|
|
|
>
|
|
|
|
>
|
2016-03-14 19:01:59 +11:00
|
|
|
constexpr
|
2016-03-14 19:54:25 +11:00
|
|
|
K<S,T>
|
2016-03-14 19:01:59 +11:00
|
|
|
floor (const K<S,T> k)
|
2015-09-29 18:06:52 +10:00
|
|
|
{
|
|
|
|
T (*floor_func)(T) = std::floor;
|
|
|
|
|
2016-03-14 19:01:59 +11:00
|
|
|
K<S,T> out {};
|
|
|
|
std::transform (std::cbegin (k),
|
|
|
|
std::cend (k),
|
|
|
|
std::begin (out),
|
|
|
|
floor_func);
|
|
|
|
return out;
|
2015-09-29 18:06:52 +10:00
|
|
|
}
|
2017-06-15 16:24:44 +10:00
|
|
|
|
2017-08-25 13:03:21 +10:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
/// shifts all elements `num' indices to the right, setting the left-most
|
|
|
|
/// `num' indices to the value `fill'.
|
|
|
|
///
|
|
|
|
/// num must be between 0 and S. when 0 it is equivalent to an ordinary
|
|
|
|
/// fill, when S it is equivalent to a noop.
|
|
|
|
template<
|
|
|
|
std::size_t S,
|
|
|
|
typename T,
|
2017-08-28 12:25:23 +10:00
|
|
|
template <std::size_t,typename> class K,
|
2017-08-25 13:03:21 +10:00
|
|
|
typename = std::enable_if_t<
|
|
|
|
is_coord_v<K<S,T>>, void
|
|
|
|
>
|
|
|
|
>
|
|
|
|
constexpr
|
|
|
|
K<S,T>
|
|
|
|
rshift (const K<S,T> k, const int num, const K<S,T> fill)
|
|
|
|
{
|
|
|
|
CHECK_LIMIT (num, 0, int (S));
|
|
|
|
|
|
|
|
K<S,T> res {};
|
|
|
|
|
|
|
|
std::copy_n (std::cbegin (k), S - num, std::begin (res) + num);
|
|
|
|
std::copy_n (std::cbegin (fill), num, std::begin (res));
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template<
|
|
|
|
std::size_t S,
|
|
|
|
typename T,
|
2017-08-28 12:25:23 +10:00
|
|
|
template <std::size_t,typename> class K,
|
2017-08-25 13:03:21 +10:00
|
|
|
typename = std::enable_if_t<
|
|
|
|
is_coord_v<K<S,T>>, void
|
|
|
|
>
|
|
|
|
>
|
|
|
|
constexpr
|
|
|
|
K<S,T>
|
|
|
|
rshift (const K<S,T> k, const int num, T fill)
|
|
|
|
{
|
|
|
|
return rshift (k, num, K<S,T> {fill});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-15 16:24:44 +10:00
|
|
|
/// returns the data at a templated index in a coordinate.
|
|
|
|
///
|
|
|
|
/// specifically required for structured bindings support.
|
|
|
|
///
|
|
|
|
/// \tparam I index of the requested data
|
|
|
|
/// \tparam S dimensionality of the coordinate
|
|
|
|
/// \tparam T underlying data type of the coordinate
|
|
|
|
/// \tparam K coordinate data type to operate on
|
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t I,
|
|
|
|
std::size_t S,
|
2017-06-15 16:24:44 +10:00
|
|
|
typename T,
|
|
|
|
template<
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t,
|
2017-06-15 16:24:44 +10:00
|
|
|
typename
|
|
|
|
> class K
|
|
|
|
>
|
|
|
|
const std::enable_if_t<
|
|
|
|
is_coord_v<K<S,T>>,
|
|
|
|
T
|
|
|
|
>&
|
|
|
|
get (const K<S,T> &k)
|
|
|
|
{
|
|
|
|
static_assert (I < S);
|
|
|
|
return k[I];
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// returns the data at a templated index in a coordinate.
|
|
|
|
///
|
|
|
|
/// specifically required for structured bindings support.
|
|
|
|
///
|
|
|
|
/// \tparam I index of the requested data
|
|
|
|
/// \tparam S dimensionality of the coordinate
|
|
|
|
/// \tparam T underlying data type of the coordinate
|
|
|
|
/// \tparam K coordinate data type to operate on
|
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t I,
|
|
|
|
std::size_t S,
|
2017-06-15 16:24:44 +10:00
|
|
|
typename T,
|
|
|
|
template<
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t,
|
2017-06-15 16:24:44 +10:00
|
|
|
typename
|
|
|
|
> class K
|
|
|
|
>
|
|
|
|
std::enable_if_t<
|
|
|
|
is_coord_v<K<S,T>>,
|
|
|
|
T
|
|
|
|
>&
|
|
|
|
get (K<S,T> &k)
|
|
|
|
{
|
|
|
|
static_assert (I < S);
|
|
|
|
return k[I];
|
|
|
|
};
|
2015-03-06 01:46:01 +11:00
|
|
|
}
|
|
|
|
|
2017-06-15 16:24:44 +10:00
|
|
|
|
2017-06-29 16:33:53 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2017-06-15 16:24:44 +10:00
|
|
|
#include <tuple>
|
|
|
|
|
|
|
|
namespace std {
|
|
|
|
/// returns the dimensions of a coordinate type.
|
|
|
|
///
|
|
|
|
/// specifically required for structured bindings support.
|
|
|
|
///
|
|
|
|
/// \tparam S dimensions
|
|
|
|
/// \tparam T data type
|
|
|
|
/// \tparam K coordinate class
|
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S,
|
2017-06-15 16:24:44 +10:00
|
|
|
typename T,
|
|
|
|
template<
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t,
|
2017-06-15 16:24:44 +10:00
|
|
|
typename
|
|
|
|
> class K
|
|
|
|
>
|
|
|
|
class tuple_size<K<S,T>> : public std::enable_if_t<
|
|
|
|
::util::is_coord_v<K<S,T>>,
|
|
|
|
std::integral_constant<decltype(S), S>
|
|
|
|
> { };
|
|
|
|
|
|
|
|
|
|
|
|
/// indicates the type at a given index of a coordinate type
|
|
|
|
///
|
|
|
|
/// specifically required for structured bindings support.
|
|
|
|
///
|
|
|
|
/// \tparam I data index
|
|
|
|
/// \tparam S dimensionality of the coordinate
|
|
|
|
/// \tparam T data type for the coordinate
|
|
|
|
/// \tparam K the underlying coordinate class
|
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t I,
|
|
|
|
std::size_t S,
|
2017-06-15 16:24:44 +10:00
|
|
|
typename T,
|
|
|
|
template<
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t,
|
2017-06-15 16:24:44 +10:00
|
|
|
typename
|
|
|
|
> class K
|
|
|
|
>
|
|
|
|
class tuple_element<
|
|
|
|
I, K<S,T>
|
|
|
|
> : public enable_if<
|
|
|
|
::util::is_coord_v<K<S,T>>,
|
|
|
|
T
|
|
|
|
> { };
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-29 16:33:53 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
#include "../hash.hpp"
|
|
|
|
|
|
|
|
namespace std {
|
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S,
|
2017-06-29 16:33:53 +10:00
|
|
|
typename T,
|
|
|
|
template <
|
|
|
|
std::size_t,typename
|
|
|
|
> class K
|
|
|
|
>
|
|
|
|
struct hash<
|
|
|
|
K<S,T>
|
|
|
|
> : public ::std::enable_if<
|
|
|
|
::util::is_coord_v<K<S,T>>
|
|
|
|
> {
|
|
|
|
std::size_t
|
|
|
|
operator() (K<S,T> k) const {
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t v = 0xdeadbeef;
|
2017-06-29 16:33:53 +10:00
|
|
|
|
|
|
|
for (auto t: k)
|
|
|
|
v = ::util::hash::mix (t, v);
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-06 01:46:01 +11:00
|
|
|
#endif
|