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
|
|
|
*
|
2016-03-11 12:48:19 +11:00
|
|
|
* Copyright 2012-2016 Danny Robson <danny@nerdcruft.net>
|
2015-03-06 01:46:01 +11:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __UTIL_COORDS_OPS
|
|
|
|
#define __UTIL_COORDS_OPS
|
|
|
|
|
2015-03-06 02:01:50 +11:00
|
|
|
#include "../preprocessor.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
|
|
|
|
|
|
|
#include <cstdlib>
|
2015-09-29 18:06:52 +10:00
|
|
|
#include <cmath>
|
2015-03-06 01:46:01 +11:00
|
|
|
|
|
|
|
namespace util {
|
|
|
|
// forward declerations for traits
|
2015-03-06 02:01:32 +11:00
|
|
|
template <size_t,typename> struct point;
|
|
|
|
template <size_t,typename> struct extent;
|
|
|
|
template <size_t,typename> struct vector;
|
2015-04-09 18:00:40 +10:00
|
|
|
template <size_t,typename> struct colour;
|
2015-03-06 01:46:01 +11:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
// operation traits
|
|
|
|
namespace coord {
|
|
|
|
template <
|
|
|
|
template <size_t,typename> class A,
|
|
|
|
template <size_t,typename> class B
|
|
|
|
>
|
|
|
|
struct traits { };
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
2015-10-19 17:06:19 +11:00
|
|
|
template <> struct traits<colour,colour> { template <size_t S, typename T> using result = colour<S,T>; };
|
|
|
|
template <> struct traits<extent,extent> { template <size_t S, typename T> using result = extent<S,T>; };
|
|
|
|
template <> struct traits<extent,vector> { template <size_t S, typename T> using result = extent<S,T>; };
|
|
|
|
template <> struct traits<point,extent> { template <size_t S, typename T> using result = point <S,T>; };
|
|
|
|
template <> struct traits<point,vector> { template <size_t S, typename T> using result = point <S,T>; };
|
|
|
|
template <> struct traits<vector,point> { template <size_t S, typename T> using result = point <S,T>; };
|
|
|
|
template <> struct traits<vector,vector> { template <size_t S, typename T> using result = vector<S,T>; };
|
2015-03-06 01:46:01 +11:00
|
|
|
}
|
|
|
|
|
2015-11-04 23:22:49 +11:00
|
|
|
template <template <size_t,typename> class> struct is_coord : std::false_type { };
|
|
|
|
|
|
|
|
template <> struct is_coord<point> : std::true_type { };
|
|
|
|
template <> struct is_coord<extent> : std::true_type { };
|
|
|
|
template <> struct is_coord<vector> : std::true_type { };
|
|
|
|
template <> struct is_coord<colour> : std::true_type { };
|
|
|
|
|
2016-03-14 19:54:25 +11:00
|
|
|
template <template <size_t,typename> class K>
|
|
|
|
constexpr bool
|
|
|
|
is_coord_v = is_coord<K>::value;
|
|
|
|
|
2015-03-06 01:46:01 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// vector operators
|
2015-04-09 18:00:40 +10:00
|
|
|
#define ELEMENT_OP(OP) \
|
|
|
|
template < \
|
|
|
|
size_t S, \
|
|
|
|
typename T, \
|
2015-05-28 10:18:07 +10:00
|
|
|
typename U, \
|
2015-04-09 18:00:40 +10:00
|
|
|
template <size_t,typename> class A, \
|
2015-11-04 23:22:49 +11:00
|
|
|
template <size_t,typename> class B, \
|
2016-03-14 19:54:25 +11:00
|
|
|
typename = std::enable_if_t< \
|
|
|
|
is_coord_v<A> && is_coord_v<B>, \
|
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
|
|
|
{ \
|
2015-10-19 17:06:19 +11:00
|
|
|
typename coord::traits<A,B>::template result< \
|
2016-03-14 19:54:25 +11:00
|
|
|
S,std::common_type_t<T,U> \
|
2016-03-14 19:01:59 +11:00
|
|
|
> out {}; \
|
2015-04-09 18:00:40 +10:00
|
|
|
for (size_t i = 0; i < S; ++i) \
|
|
|
|
out[i] = a[i] OP b[i]; \
|
|
|
|
return out; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
template < \
|
|
|
|
size_t S, \
|
|
|
|
typename T, \
|
2015-10-19 17:06:19 +11:00
|
|
|
typename U, \
|
2015-04-09 18:00:40 +10:00
|
|
|
template <size_t,typename> class A, \
|
2015-11-04 23:22:49 +11:00
|
|
|
template <size_t,typename> class B, \
|
2016-03-14 19:54:25 +11:00
|
|
|
typename = std::enable_if_t< \
|
|
|
|
is_coord_v<A> && \
|
|
|
|
is_coord_v<B> && \
|
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
|
|
|
{ \
|
|
|
|
for (size_t i = 0; i < S; ++i) \
|
|
|
|
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 < \
|
|
|
|
size_t S, \
|
|
|
|
typename T, \
|
|
|
|
typename U, \
|
2015-11-04 23:22:49 +11:00
|
|
|
template <size_t,typename> class K, \
|
2016-03-14 19:54:25 +11:00
|
|
|
typename = std::enable_if_t< \
|
|
|
|
is_coord_v<K>,void \
|
|
|
|
> \
|
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
|
|
|
\
|
2015-10-19 17:06:19 +11:00
|
|
|
for (size_t i = 0; i < S; ++i) \
|
|
|
|
out[i] = u OP k[i]; \
|
|
|
|
return out; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
template < \
|
|
|
|
size_t S, \
|
|
|
|
typename T, \
|
|
|
|
typename U, \
|
2015-11-04 23:22:49 +11:00
|
|
|
template <size_t,typename> class K, \
|
2016-03-14 19:54:25 +11:00
|
|
|
typename = std::enable_if_t< \
|
2015-11-04 23:22:49 +11:00
|
|
|
is_coord<K>::value,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
|
|
|
\
|
2015-10-19 17:06:19 +11:00
|
|
|
for (size_t i = 0; i < S; ++i) \
|
|
|
|
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 < \
|
|
|
|
size_t S, \
|
|
|
|
typename T, \
|
|
|
|
typename U, \
|
2015-11-04 23:22:49 +11:00
|
|
|
template <size_t,typename> class K, \
|
2016-03-14 19:54:25 +11:00
|
|
|
typename = std::enable_if_t< \
|
2015-11-04 23:22:49 +11:00
|
|
|
is_coord<K>::value, \
|
|
|
|
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) \
|
|
|
|
{ \
|
|
|
|
for (size_t i = 0; i < S; ++i) \
|
|
|
|
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 < \
|
|
|
|
size_t S, \
|
|
|
|
typename T, \
|
2016-03-14 19:54:25 +11:00
|
|
|
template <size_t,typename> class K, \
|
|
|
|
typename = std::enable_if_t< \
|
|
|
|
is_coord_v<K>, void \
|
|
|
|
> \
|
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
|
|
|
\
|
|
|
|
for (size_t i = 0; i < S; ++i) \
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2015-03-06 01:46:01 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// logic operators
|
2015-03-07 03:18:32 +11:00
|
|
|
|
|
|
|
/// elementwise equality operator
|
2015-03-06 01:46:01 +11:00
|
|
|
template <
|
|
|
|
size_t S,
|
|
|
|
typename T,
|
2016-03-14 19:54:25 +11:00
|
|
|
template <size_t,typename> class K,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
is_coord_v<K>, void
|
|
|
|
>
|
2015-03-06 01:46:01 +11:00
|
|
|
>
|
2016-03-14 19:01:59 +11:00
|
|
|
constexpr
|
2015-03-06 01:46:01 +11:00
|
|
|
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
|
|
|
{
|
2015-07-21 01:39:14 +10:00
|
|
|
bool (*predicate)(const T&, const T&) = almost_equal;
|
|
|
|
|
2016-03-14 19:54:25 +11:00
|
|
|
return std::equal (std::cbegin (a),
|
|
|
|
std::cend (a),
|
|
|
|
std::cbegin (b),
|
2015-07-21 01:39:14 +10:00
|
|
|
predicate);
|
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 <
|
|
|
|
size_t S,
|
|
|
|
typename T,
|
2016-03-14 19:54:25 +11:00
|
|
|
template <size_t,typename> class K,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
is_coord_v<K>, void
|
|
|
|
>
|
2015-03-06 01:46:01 +11:00
|
|
|
>
|
2016-03-14 19:01:59 +11:00
|
|
|
constexpr
|
2015-03-06 01:46:01 +11:00
|
|
|
bool
|
|
|
|
operator!= (K<S,T> a, K<S,T> b)
|
|
|
|
{
|
|
|
|
return !(a == b);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// 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 <
|
|
|
|
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
|
|
|
{
|
2016-03-14 19:54:25 +11:00
|
|
|
vector<S,std::common_type_t<T,U>> out {};
|
2015-03-06 01:46:01 +11:00
|
|
|
for (size_t i = 0; i < S; ++i)
|
|
|
|
out[i] = a[i] - b[i];
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-07 03:18:32 +11:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <
|
|
|
|
size_t S,
|
2015-05-28 10:18:07 +10:00
|
|
|
typename T,
|
|
|
|
typename U
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-06 01:46:01 +11:00
|
|
|
//-------------------------------------------------------------------------
|
2015-11-04 17:10:16 +11:00
|
|
|
template <
|
|
|
|
size_t S,
|
|
|
|
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;
|
|
|
|
for (size_t i = 0; i < S; ++i)
|
|
|
|
sum += a[i] * b[i];
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
2015-03-06 01:46:01 +11:00
|
|
|
template <
|
|
|
|
size_t S,
|
|
|
|
typename T,
|
2015-05-29 15:51:08 +10:00
|
|
|
template <size_t,typename> class A,
|
2016-03-14 19:54:25 +11:00
|
|
|
template <size_t,typename> class B,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
is_coord_v<A> && is_coord_v<B>, void
|
|
|
|
>
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <
|
|
|
|
size_t S,
|
|
|
|
typename T,
|
2016-03-14 19:54:25 +11:00
|
|
|
template <size_t,typename> class K,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
is_coord_v<K>, void
|
|
|
|
>
|
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
|
|
|
|
2015-11-04 17:10:16 +11:00
|
|
|
template <
|
|
|
|
size_t S,
|
|
|
|
typename T,
|
2016-03-14 19:54:25 +11:00
|
|
|
template <size_t,typename> class K,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
is_coord_v<K>, void
|
|
|
|
>
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-29 15:51:08 +10:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <
|
|
|
|
size_t S,
|
|
|
|
typename T,
|
2016-03-14 19:54:25 +11:00
|
|
|
template <size_t,typename> class K,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
is_coord_v<K>, void
|
|
|
|
>
|
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;
|
|
|
|
}
|
|
|
|
|
2015-10-12 23:56:41 +11:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <
|
|
|
|
size_t S,
|
|
|
|
typename T,
|
2016-03-14 19:54:25 +11:00
|
|
|
template <size_t,typename> class K,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
is_coord_v<K>, void
|
|
|
|
>
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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 <
|
|
|
|
size_t S,
|
|
|
|
typename T,
|
2016-03-14 19:54:25 +11:00
|
|
|
template <size_t,typename> class K,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
is_coord_v<K>, void
|
|
|
|
>
|
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>
|
|
|
|
min (K<S,T> a, K<S,T> b)
|
|
|
|
{
|
2016-03-14 19:01:59 +11:00
|
|
|
K<S,T> out {};
|
2015-03-06 17:49:35 +11:00
|
|
|
for (size_t i = 0; i < S; ++i)
|
|
|
|
out[i] = min (a[i], b[i]);
|
|
|
|
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 <
|
|
|
|
size_t S,
|
|
|
|
typename T,
|
2016-03-14 19:54:25 +11:00
|
|
|
template <size_t,typename> class K,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
is_coord_v<K>, void
|
|
|
|
>
|
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>
|
|
|
|
max (K<S,T> a, K<S,T> b)
|
|
|
|
{
|
2016-03-14 19:01:59 +11:00
|
|
|
K<S,T> out {};
|
2015-03-06 17:49:35 +11:00
|
|
|
for (size_t i = 0; i < S; ++i)
|
|
|
|
out[i] = max (a[i], b[i]);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-07 03:18:32 +11:00
|
|
|
///------------------------------------------------------------------------
|
2016-03-14 19:54:25 +11:00
|
|
|
template <
|
|
|
|
size_t S,
|
|
|
|
typename T,
|
|
|
|
template<size_t,typename> class K,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
is_coord_v<K>, void
|
|
|
|
>
|
|
|
|
>
|
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 <
|
|
|
|
size_t S,
|
|
|
|
typename T,
|
|
|
|
template<size_t,typename> class K,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
is_coord_v<K>, void
|
|
|
|
>
|
|
|
|
>
|
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
|
|
|
|
|
|
|
|
2015-10-19 17:06:19 +11:00
|
|
|
//-------------------------------------------------------------------------
|
2015-10-20 12:37:08 +11:00
|
|
|
#define VECTOR_OP(OP) \
|
|
|
|
template < \
|
|
|
|
size_t S, \
|
|
|
|
typename T, \
|
|
|
|
typename U, \
|
|
|
|
template <size_t,typename> class A, \
|
2016-03-14 19:54:25 +11:00
|
|
|
template <size_t,typename> class B, \
|
|
|
|
typename = std::enable_if_t< \
|
|
|
|
is_coord_v<A> && is_coord_v<B>, void \
|
|
|
|
> \
|
2015-10-20 12:37:08 +11:00
|
|
|
> \
|
2016-03-14 19:01:59 +11:00
|
|
|
constexpr \
|
2015-10-20 12:37:08 +11:00
|
|
|
vector<S,bool> \
|
|
|
|
operator OP (const A<S,T> a, const B<S,U> b) \
|
|
|
|
{ \
|
2016-03-14 19:01:59 +11:00
|
|
|
vector<S,bool> out {}; \
|
2015-10-20 12:37:08 +11:00
|
|
|
for (size_t i = 0; i < S; ++i) \
|
|
|
|
out[i] = a[i] OP b[i]; \
|
|
|
|
return out; \
|
|
|
|
}
|
|
|
|
|
|
|
|
VECTOR_OP(<)
|
|
|
|
VECTOR_OP(>)
|
|
|
|
VECTOR_OP(<=)
|
|
|
|
VECTOR_OP(>=)
|
|
|
|
|
|
|
|
#undef VECTOR_OP
|
|
|
|
|
|
|
|
|
2015-10-19 17:06:19 +11:00
|
|
|
#define SCALAR_OP(OP) \
|
|
|
|
template < \
|
|
|
|
size_t S, \
|
|
|
|
typename T, \
|
|
|
|
typename U, \
|
2016-03-14 19:54:25 +11:00
|
|
|
template <size_t,typename> class K, \
|
|
|
|
typename = std::enable_if_t< \
|
|
|
|
is_coord_v<K>, void \
|
|
|
|
> \
|
2015-10-19 17:06:19 +11:00
|
|
|
> \
|
2016-03-14 19:01:59 +11:00
|
|
|
constexpr \
|
2015-10-20 12:37:08 +11:00
|
|
|
vector<S,bool> \
|
2015-10-19 17:06:19 +11:00
|
|
|
operator OP (const K<S,T> k, const U u) \
|
|
|
|
{ \
|
2016-03-14 19:01:59 +11:00
|
|
|
vector<S,bool> out {}; \
|
2015-10-19 17:06:19 +11:00
|
|
|
for (size_t i = 0; i < S; ++i) \
|
|
|
|
out[i] = k[i] OP u; \
|
|
|
|
return out; \
|
|
|
|
}
|
2015-05-26 16:25:41 +10:00
|
|
|
|
2015-10-19 17:06:19 +11:00
|
|
|
SCALAR_OP(<)
|
|
|
|
SCALAR_OP(>)
|
|
|
|
SCALAR_OP(<=)
|
|
|
|
SCALAR_OP(>=)
|
2015-05-26 16:25:41 +10:00
|
|
|
|
2015-10-19 17:06:19 +11:00
|
|
|
#undef SCALAR_OP
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
2016-03-14 19:54:25 +11:00
|
|
|
template <
|
|
|
|
size_t S,
|
|
|
|
template <size_t,typename> class K,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
is_coord_v<K>, void
|
|
|
|
>
|
|
|
|
>
|
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)
|
|
|
|
{
|
2016-03-14 19:54:25 +11:00
|
|
|
return std::any_of (std::cbegin (k),
|
|
|
|
std::cbegin (k),
|
|
|
|
identity<bool>);
|
2015-10-19 17:06:19 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
2016-03-14 19:54:25 +11:00
|
|
|
template <
|
|
|
|
size_t S,
|
|
|
|
template <size_t,typename> class K,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
is_coord_v<K>, void
|
|
|
|
>
|
|
|
|
>
|
2016-03-14 19:01:59 +11:00
|
|
|
constexpr
|
2015-10-19 17:06:19 +11:00
|
|
|
bool
|
|
|
|
all (const K<S,bool> k)
|
|
|
|
{
|
2016-03-14 19:54:25 +11:00
|
|
|
return std::all_of (std::cbegin (k),
|
|
|
|
std::cbegin (k),
|
|
|
|
identity<bool>);
|
2015-10-19 17:06:19 +11:00
|
|
|
}
|
2015-09-29 18:06:52 +10:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2016-03-14 19:54:25 +11:00
|
|
|
template <
|
|
|
|
size_t S,
|
|
|
|
typename T,
|
|
|
|
template<size_t,typename> class K,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
is_coord_v<K> && std::is_floating_point<T>::value, void
|
|
|
|
>
|
|
|
|
>
|
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
|
|
|
}
|
2015-03-06 01:46:01 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|