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
|
|
|
*
|
|
|
|
* Copyright 2012-2015 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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-03-06 01:46:01 +11:00
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
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 <
|
|
|
|
size_t S,
|
|
|
|
typename T,
|
2015-05-28 10:18:07 +10:00
|
|
|
typename U,
|
2015-03-06 01:46:01 +11:00
|
|
|
template <size_t,typename> class A,
|
|
|
|
template <size_t,typename> class B
|
|
|
|
>
|
|
|
|
struct traits { };
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
2015-05-28 10:18:07 +10:00
|
|
|
template <size_t S, typename T, typename U> struct traits<S,T,U,colour,colour> { typedef colour<S,typename std::common_type<T,U>::type> result; };
|
|
|
|
template <size_t S, typename T, typename U> struct traits<S,T,U,extent,extent> { typedef extent<S,typename std::common_type<T,U>::type> result; };
|
|
|
|
template <size_t S, typename T, typename U> struct traits<S,T,U,extent,vector> { typedef extent<S,typename std::common_type<T,U>::type> result; };
|
|
|
|
template <size_t S, typename T, typename U> struct traits<S,T,U,point,extent> { typedef point <S,typename std::common_type<T,U>::type> result; };
|
|
|
|
template <size_t S, typename T, typename U> struct traits<S,T,U,point,vector> { typedef point <S,typename std::common_type<T,U>::type> result; };
|
|
|
|
template <size_t S, typename T, typename U> struct traits<S,T,U,vector,point> { typedef point <S,typename std::common_type<T,U>::type> result; };
|
|
|
|
template <size_t S, typename T, typename U> struct traits<S,T,U,vector,vector> { typedef vector<S,typename std::common_type<T,U>::type> result; };
|
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, \
|
|
|
|
template <size_t,typename> class B \
|
|
|
|
> \
|
2015-05-28 10:18:07 +10:00
|
|
|
typename coord::traits<S,T,U,A,B>::result \
|
|
|
|
operator OP (A<S,T> a, B<S,U> b) \
|
2015-04-09 18:00:40 +10:00
|
|
|
{ \
|
2015-05-28 10:18:07 +10:00
|
|
|
typename coord::traits<S,T,U,A,B>::result 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, \
|
|
|
|
template <size_t,typename> class A, \
|
|
|
|
template <size_t,typename> class B \
|
|
|
|
> \
|
2015-05-28 10:18:07 +10:00
|
|
|
typename coord::traits<S,T,T,A,B>::result& \
|
2015-04-09 18:00:40 +10:00
|
|
|
operator PASTE(OP,=) (A<S,T>& a, B<S,T> b) \
|
|
|
|
{ \
|
|
|
|
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(/)
|
|
|
|
#undef ELEMENT_OP
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// scalar operators
|
2015-04-09 18:00:40 +10:00
|
|
|
#define SCALAR_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 K \
|
|
|
|
> \
|
2015-05-28 10:18:07 +10:00
|
|
|
typename std::enable_if<std::is_fundamental<U>::value, K<S,T>>::type \
|
|
|
|
operator OP (U u, K<S,T> k) \
|
2015-04-09 18:00:40 +10:00
|
|
|
{ \
|
|
|
|
K<S,T> out; \
|
|
|
|
for (size_t i = 0; i < S; ++i) \
|
2015-05-28 10:18:07 +10:00
|
|
|
out[i] = u OP k[i]; \
|
2015-04-09 18:00:40 +10:00
|
|
|
return out; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
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 K \
|
|
|
|
> \
|
2015-05-28 10:18:07 +10:00
|
|
|
typename std::enable_if<std::is_fundamental<U>::value, K<S,T>>::type \
|
|
|
|
operator OP (K<S,T> k, U u) \
|
2015-04-09 18:00:40 +10:00
|
|
|
{ \
|
|
|
|
K<S,T> out; \
|
|
|
|
for (size_t i = 0; i < S; ++i) \
|
2015-05-28 10:18:07 +10:00
|
|
|
out[i] = k[i] OP u; \
|
2015-04-09 18:00:40 +10:00
|
|
|
return out; \
|
2015-03-06 01:46:01 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
SCALAR_OP(+)
|
|
|
|
SCALAR_OP(-)
|
|
|
|
SCALAR_OP(*)
|
|
|
|
SCALAR_OP(/)
|
|
|
|
|
|
|
|
#undef SCALAR_OP
|
|
|
|
|
|
|
|
|
2015-03-07 03:18:32 +11:00
|
|
|
//-------------------------------------------------------------------------
|
2015-03-06 01:46:01 +11:00
|
|
|
#define SCALAR_OP(OP) \
|
|
|
|
template < \
|
|
|
|
size_t S, \
|
|
|
|
typename T, \
|
|
|
|
template <size_t,typename> class K \
|
|
|
|
> \
|
|
|
|
K<S,T>& \
|
|
|
|
operator OP (K<S,T> &k, T t) \
|
|
|
|
{ \
|
|
|
|
for (size_t i = 0; i < S; ++i) \
|
|
|
|
k[i] OP t; \
|
|
|
|
return k; \
|
|
|
|
}
|
|
|
|
|
|
|
|
SCALAR_OP(+=)
|
|
|
|
SCALAR_OP(-=)
|
|
|
|
SCALAR_OP(*=)
|
|
|
|
SCALAR_OP(/=)
|
|
|
|
#undef SCALAR_OP
|
|
|
|
|
2015-03-07 03:18:13 +11:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// negation
|
|
|
|
template <size_t S, typename T, template<size_t,typename> class K>
|
|
|
|
K<S,T> operator- (K<S,T> k)
|
|
|
|
{
|
2015-04-08 14:53:55 +10:00
|
|
|
return k * T{-1};
|
2015-03-07 03:18:13 +11:00
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
template <size_t,typename> class K
|
|
|
|
>
|
|
|
|
bool
|
|
|
|
operator== (K<S,T> a, K<S,T> b)
|
|
|
|
{
|
|
|
|
return std::equal (std::begin (a),
|
|
|
|
std::end (a),
|
|
|
|
std::begin (b));
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
template <size_t,typename> class K
|
|
|
|
>
|
|
|
|
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
|
|
|
>
|
2015-05-28 10:18:07 +10:00
|
|
|
vector<S,typename std::common_type<T,U>::type>
|
|
|
|
operator- (point<S,T> a, point<S,U> b)
|
2015-03-06 01:46:01 +11:00
|
|
|
{
|
2015-05-28 10:18:07 +10:00
|
|
|
vector<S,typename std::common_type<T,U>::type> 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
|
|
|
>
|
2015-05-28 10:18:07 +10:00
|
|
|
vector<S,typename std::common_type<T,U>::type>
|
|
|
|
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
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <
|
|
|
|
size_t S,
|
|
|
|
typename T,
|
|
|
|
template<size_t,typename> class A,
|
|
|
|
template <size_t,typename> class B
|
|
|
|
>
|
|
|
|
T dot (A<S,T> a, B<S,T> b)
|
|
|
|
{
|
|
|
|
T sum { 0 };
|
|
|
|
for (size_t i = 0; i < S; ++i)
|
|
|
|
sum += a[i] * b[i];
|
|
|
|
return sum;
|
|
|
|
}
|
2015-03-06 17:49:35 +11: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 <
|
|
|
|
size_t S,
|
|
|
|
typename T,
|
|
|
|
template <size_t,typename> class K
|
|
|
|
>
|
|
|
|
K<S,T>
|
|
|
|
min (K<S,T> a, K<S,T> b)
|
|
|
|
{
|
|
|
|
K<S,T> out;
|
|
|
|
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,
|
|
|
|
template <size_t,typename> class K
|
|
|
|
>
|
|
|
|
K<S,T>
|
|
|
|
max (K<S,T> a, K<S,T> b)
|
|
|
|
{
|
|
|
|
K<S,T> out;
|
|
|
|
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
|
|
|
///------------------------------------------------------------------------
|
|
|
|
/// return the minimum element of the coordinate type
|
2015-03-06 17:49:35 +11:00
|
|
|
template <size_t S, typename T, template<size_t,typename> class K>
|
|
|
|
T
|
|
|
|
min (K<S,T> k)
|
|
|
|
{ return *std::min_element (k.begin (), k.end ()); }
|
|
|
|
|
|
|
|
|
2015-03-07 03:18:32 +11:00
|
|
|
///------------------------------------------------------------------------
|
|
|
|
/// return the maximum element of the coordinate type
|
2015-03-06 17:49:35 +11:00
|
|
|
template <size_t S, typename T, template<size_t,typename> class K>
|
|
|
|
T
|
|
|
|
max (K<S,T> k)
|
|
|
|
{ return *std::max_element (k.begin (), k.end ()); }
|
2015-05-26 16:25:41 +10:00
|
|
|
|
|
|
|
|
|
|
|
template <size_t S, typename T, template<size_t,typename> class K>
|
|
|
|
bool
|
|
|
|
operator>= (K<S,T> k, T t)
|
|
|
|
{ return min (k) >= t; }
|
|
|
|
|
|
|
|
|
|
|
|
template <size_t S, typename T, template<size_t,typename> class K>
|
|
|
|
bool
|
|
|
|
operator<= (K<S,T> k, T t)
|
|
|
|
{ return max (k) <= t; }
|
2015-03-06 01:46:01 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|