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-11-22 16:49:37 +11:00
|
|
|
* Copyright 2012-2017 Danny Robson <danny@nerdcruft.net>
|
2015-03-06 01:46:01 +11:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __UTIL_COORDS_OPS
|
|
|
|
#define __UTIL_COORDS_OPS
|
|
|
|
|
2017-11-22 16:49:37 +11:00
|
|
|
#include "fwd.hpp"
|
2017-11-22 17:03:00 +11:00
|
|
|
#include "traits.hpp"
|
2016-08-11 14:22:29 +10:00
|
|
|
|
2016-10-12 22:58:13 +11:00
|
|
|
#include "../debug.hpp"
|
2015-03-06 17:49:35 +11:00
|
|
|
#include "../maths.hpp"
|
2016-10-12 22:58:13 +11:00
|
|
|
#include "../preprocessor.hpp"
|
2015-05-29 15:51:08 +10:00
|
|
|
#include "../types/bits.hpp"
|
2015-03-06 01:46:01 +11:00
|
|
|
|
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-11-22 17:03:00 +11:00
|
|
|
#include <functional>
|
2015-03-06 01:46:01 +11:00
|
|
|
|
|
|
|
namespace util {
|
2017-11-22 17:03:00 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// a templated functor that exposes arithmetic and assignment maths
|
|
|
|
// functions for vector-vector or vector-scalar operations.
|
|
|
|
//
|
|
|
|
// we implement the operations this way because it (somewhat) simplifies
|
|
|
|
// ambiguity resolution in the various operators we need to provide.
|
|
|
|
// eg, operator+(vec,vec) vs operator+(vec,int).
|
|
|
|
//
|
|
|
|
// it used to be directly implemented with a series of templated free
|
|
|
|
// functions when we could restrict the arguments more easily with quite
|
|
|
|
// specific template template parameters. but the introduction of
|
|
|
|
// coordinate types that do not expose size or type information as template
|
|
|
|
// parameters we can't rely on this mechanism anymore.
|
|
|
|
template <typename, typename,typename=void>
|
|
|
|
struct ops { };
|
2015-03-06 01:46:01 +11:00
|
|
|
|
2016-08-11 14:23:32 +10:00
|
|
|
|
2017-11-22 17:03:00 +11:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// vector operators
|
|
|
|
template <typename ValueA, typename ValueB>
|
|
|
|
struct ops<
|
|
|
|
ValueA,
|
|
|
|
ValueB,
|
|
|
|
std::enable_if_t<
|
|
|
|
is_coord_v<ValueA> &&
|
|
|
|
is_coord_v<ValueB> &&
|
|
|
|
arity<ValueA>::value == arity<ValueB>::value &&
|
|
|
|
std::is_same_v<
|
|
|
|
typename ValueA::value_type,
|
|
|
|
typename ValueB::value_type
|
|
|
|
>,
|
|
|
|
void
|
2016-08-11 14:23:32 +10:00
|
|
|
>
|
2017-11-22 17:03:00 +11:00
|
|
|
> {
|
|
|
|
template <typename OpT>
|
|
|
|
static constexpr auto
|
|
|
|
arithmetic (OpT op, ValueA a, ValueB b)
|
|
|
|
{
|
|
|
|
result_t<ValueA,ValueB> out {};
|
|
|
|
for (std::size_t i = 0; i < ValueA::elements; ++i)
|
|
|
|
out[i] = op (a[i], b[i]);
|
|
|
|
return out;
|
|
|
|
}
|
2016-08-11 14:58:04 +10:00
|
|
|
|
2017-11-22 17:03:00 +11:00
|
|
|
template <typename OpT>
|
|
|
|
static constexpr ValueA&
|
|
|
|
assignment (OpT op, ValueA &a, ValueB b)
|
|
|
|
{
|
|
|
|
for (std::size_t i = 0; i < ValueA::elements; ++i)
|
|
|
|
a[i] = op (a[i], b[i]);
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
};
|
2016-08-11 14:58:04 +10:00
|
|
|
|
2016-08-11 14:58:46 +10:00
|
|
|
|
2017-11-22 17:03:00 +11:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// vector-scalar operations
|
|
|
|
//
|
|
|
|
// we allow scalar types which can be naturally promoted to the vector's
|
|
|
|
// value_type
|
|
|
|
template <
|
|
|
|
typename CoordT,
|
|
|
|
typename ScalarT
|
|
|
|
>
|
|
|
|
struct ops<
|
|
|
|
CoordT,
|
|
|
|
ScalarT,
|
|
|
|
std::enable_if_t<
|
|
|
|
has_scalar_op_v<CoordT> &&
|
|
|
|
is_coord_v<CoordT> &&
|
|
|
|
std::is_same_v<
|
|
|
|
typename CoordT::value_type,
|
|
|
|
std::common_type_t<
|
|
|
|
typename CoordT::value_type,
|
|
|
|
ScalarT
|
|
|
|
>
|
|
|
|
>,
|
|
|
|
void
|
|
|
|
>
|
|
|
|
> {
|
|
|
|
template <typename OpT>
|
|
|
|
static constexpr CoordT&
|
|
|
|
assignment (OpT &&op, CoordT &c, ScalarT s)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < CoordT::elements; ++i)
|
|
|
|
c[i] = op (c[i], s);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2016-08-11 14:58:46 +10:00
|
|
|
|
2017-11-22 17:03:00 +11:00
|
|
|
template <typename OpT>
|
|
|
|
static constexpr CoordT
|
|
|
|
arithmetic (OpT &&op, CoordT c, ScalarT s)
|
|
|
|
{
|
|
|
|
CoordT out {};
|
|
|
|
for (size_t i = 0; i < CoordT::elements; ++i)
|
|
|
|
out[i] = op (c[i], s);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
};
|
2016-08-11 14:58:46 +10:00
|
|
|
|
|
|
|
|
2017-11-22 17:03:00 +11:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// scalar-vector operations
|
|
|
|
template <typename ScalarT, typename CoordT>
|
|
|
|
struct ops<
|
|
|
|
ScalarT,
|
|
|
|
CoordT,
|
|
|
|
std::enable_if_t<
|
|
|
|
has_scalar_op_v<CoordT> &&
|
|
|
|
std::is_arithmetic_v<ScalarT> &&
|
|
|
|
std::is_same_v<ScalarT, typename CoordT::value_type>,
|
|
|
|
void
|
|
|
|
>
|
|
|
|
> {
|
|
|
|
template <typename OpT>
|
|
|
|
static constexpr CoordT
|
|
|
|
arithmetic (OpT op, ScalarT s, CoordT c)
|
|
|
|
{
|
|
|
|
CoordT out {};
|
|
|
|
for (size_t i = 0; i < CoordT::elements; ++i)
|
|
|
|
out[i] = op (s, c[i]);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
};
|
2016-08-11 14:58:04 +10:00
|
|
|
|
|
|
|
|
2017-11-22 17:03:00 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
template <
|
|
|
|
typename A,
|
|
|
|
typename B,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
(is_coord_v<std::decay_t<A>> || is_coord_v<std::decay_t<B>>) &&
|
|
|
|
(is_coord_v<std::decay_t<A>> || std::is_arithmetic_v<std::decay_t<A>>) &&
|
|
|
|
(is_coord_v<std::decay_t<B>> || std::is_arithmetic_v<std::decay_t<B>>)
|
|
|
|
>
|
|
|
|
>
|
|
|
|
constexpr auto
|
|
|
|
operator+ (A &&a, B &&b)
|
|
|
|
{
|
|
|
|
return ops<std::decay_t<A>,std::decay_t<B>>::template arithmetic (std::plus{}, a, b);
|
2015-03-06 01:46:01 +11:00
|
|
|
}
|
|
|
|
|
2015-11-04 23:22:49 +11:00
|
|
|
|
2017-11-22 17:03:00 +11:00
|
|
|
template <
|
|
|
|
typename A,
|
|
|
|
typename B,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
(is_coord_v<std::decay_t<A>> || is_coord_v<std::decay_t<B>>) &&
|
|
|
|
(is_coord_v<std::decay_t<A>> || std::is_arithmetic_v<std::decay_t<A>>) &&
|
|
|
|
(is_coord_v<std::decay_t<B>> || std::is_arithmetic_v<std::decay_t<B>>)
|
|
|
|
>
|
|
|
|
>
|
|
|
|
constexpr auto
|
|
|
|
operator- (A &&a, B &&b)
|
|
|
|
{
|
|
|
|
return ops<std::decay_t<A>,std::decay_t<B>>::template arithmetic (std::minus{}, a, b);
|
|
|
|
}
|
|
|
|
|
2015-11-04 23:22:49 +11:00
|
|
|
|
2017-11-22 17:03:00 +11:00
|
|
|
template <
|
|
|
|
typename A,
|
|
|
|
typename B,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
(is_coord_v<std::decay_t<A>> || is_coord_v<std::decay_t<B>>) &&
|
|
|
|
(is_coord_v<std::decay_t<A>> || std::is_arithmetic_v<std::decay_t<A>>) &&
|
|
|
|
(is_coord_v<std::decay_t<B>> || std::is_arithmetic_v<std::decay_t<B>>)
|
|
|
|
>
|
|
|
|
>
|
|
|
|
constexpr auto
|
|
|
|
operator* (A &&a, B &&b)
|
|
|
|
{
|
|
|
|
return ops<std::decay_t<A>,std::decay_t<B>>::template arithmetic (std::multiplies{}, a, b);
|
|
|
|
}
|
2016-03-14 19:54:25 +11:00
|
|
|
|
2017-03-23 14:36:39 +11:00
|
|
|
|
2017-11-22 17:03:00 +11:00
|
|
|
template <
|
|
|
|
typename A,
|
|
|
|
typename B,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
(is_coord_v<std::decay_t<A>> || is_coord_v<std::decay_t<B>>) &&
|
|
|
|
(is_coord_v<std::decay_t<A>> || std::is_arithmetic_v<std::decay_t<A>>) &&
|
|
|
|
(is_coord_v<std::decay_t<B>> || std::is_arithmetic_v<std::decay_t<B>>)
|
|
|
|
>
|
|
|
|
>
|
|
|
|
constexpr auto
|
|
|
|
operator/ (A &&a, B &&b)
|
2017-03-23 14:36:39 +11:00
|
|
|
{
|
2017-11-22 17:03:00 +11:00
|
|
|
return ops<std::decay_t<A>,std::decay_t<B>>::template arithmetic (std::divides{}, a, b);
|
2017-03-23 14:36:39 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
2017-11-22 17:03:00 +11:00
|
|
|
template <
|
|
|
|
typename A,
|
|
|
|
typename B,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
(is_coord_v<std::decay_t<A>> || is_coord_v<std::decay_t<B>>) &&
|
|
|
|
(is_coord_v<std::decay_t<A>> || std::is_arithmetic_v<std::decay_t<A>>) &&
|
|
|
|
(is_coord_v<std::decay_t<B>> || std::is_arithmetic_v<std::decay_t<B>>)
|
|
|
|
>
|
|
|
|
>
|
|
|
|
constexpr auto
|
|
|
|
operator += (A &&a, B &&b)
|
2017-03-23 14:36:39 +11:00
|
|
|
{
|
2017-11-22 17:03:00 +11:00
|
|
|
return ops<std::decay_t<A>, std::decay_t<B>>::template assignment (std::plus{}, a, b);
|
2017-03-23 14:36:39 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-22 17:03:00 +11:00
|
|
|
template <
|
|
|
|
typename A,
|
|
|
|
typename B,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
(is_coord_v<std::decay_t<A>> || is_coord_v<std::decay_t<B>>) &&
|
|
|
|
(is_coord_v<std::decay_t<A>> || std::is_arithmetic_v<std::decay_t<A>>) &&
|
|
|
|
(is_coord_v<std::decay_t<B>> || std::is_arithmetic_v<std::decay_t<B>>)
|
|
|
|
>
|
|
|
|
>
|
|
|
|
constexpr auto
|
|
|
|
operator -= (A &&a, B &&b)
|
|
|
|
{
|
|
|
|
return ops<std::decay_t<A>, std::decay_t<B>>::template assignment (std::plus{}, a, b);
|
|
|
|
}
|
2015-03-06 01:46:01 +11:00
|
|
|
|
|
|
|
|
2017-11-22 17:03:00 +11:00
|
|
|
template <
|
|
|
|
typename A,
|
|
|
|
typename B,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
(is_coord_v<std::decay_t<A>> || is_coord_v<std::decay_t<B>>) &&
|
|
|
|
(is_coord_v<std::decay_t<A>> || std::is_arithmetic_v<std::decay_t<A>>) &&
|
|
|
|
(is_coord_v<std::decay_t<B>> || std::is_arithmetic_v<std::decay_t<B>>)
|
|
|
|
>
|
|
|
|
>
|
|
|
|
constexpr auto
|
|
|
|
operator *= (A &&a, B &&b)
|
|
|
|
{
|
|
|
|
return ops<std::decay_t<A>, std::decay_t<B>>::template assignment (std::plus{}, a, b);
|
|
|
|
}
|
2015-03-06 01:46:01 +11:00
|
|
|
|
|
|
|
|
2017-11-22 17:03:00 +11:00
|
|
|
template <
|
|
|
|
typename A,
|
|
|
|
typename B,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
(is_coord_v<std::decay_t<A>> || is_coord_v<std::decay_t<B>>) &&
|
|
|
|
(is_coord_v<std::decay_t<A>> || std::is_arithmetic_v<std::decay_t<A>>) &&
|
|
|
|
(is_coord_v<std::decay_t<B>> || std::is_arithmetic_v<std::decay_t<B>>)
|
|
|
|
>
|
|
|
|
>
|
|
|
|
constexpr auto
|
|
|
|
operator /= (A &&a, B &&b)
|
|
|
|
{
|
|
|
|
return ops<std::decay_t<A>, std::decay_t<B>>::template assignment (std::plus{}, a, b);
|
2015-03-06 01:46:01 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-19 17:06:19 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// unary operators
|
|
|
|
#define UNARY_OP(OP) \
|
|
|
|
template < \
|
2017-11-22 17:03:00 +11:00
|
|
|
std::size_t S, \
|
2015-10-19 17:06:19 +11:00
|
|
|
typename T, \
|
2017-11-22 17:03:00 +11: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-11-22 17:03:00 +11: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
|
|
|
|
|
|
|
|
|
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 <
|
2017-11-22 17:03:00 +11:00
|
|
|
typename ValueT,
|
2016-03-14 19:54:25 +11:00
|
|
|
typename = std::enable_if_t<
|
2017-11-22 17:03:00 +11:00
|
|
|
is_coord_v<ValueT>, 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
|
2015-03-06 01:46:01 +11:00
|
|
|
bool
|
2017-11-22 17:03:00 +11:00
|
|
|
operator== (const ValueT a, const ValueT b)
|
2015-03-06 01:46:01 +11:00
|
|
|
{
|
2017-11-22 17:03:00 +11:00
|
|
|
using value_type = typename ValueT::value_type;
|
|
|
|
bool (*predicate)(const value_type&, const value_type&) = almost_equal;
|
2015-07-21 01:39:14 +10:00
|
|
|
|
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 <
|
2017-11-22 17:03:00 +11:00
|
|
|
std::size_t S,
|
2015-03-06 01:46:01 +11:00
|
|
|
typename T,
|
2017-11-22 17:03:00 +11: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
|
|
|
>
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-11 16:32:48 +10:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <
|
2017-11-22 17:03:00 +11:00
|
|
|
std::size_t S,
|
2016-08-11 16:32:48 +10:00
|
|
|
typename T,
|
2017-11-22 17:03:00 +11: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-11-22 17:03:00 +11: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
|
|
|
{
|
2016-03-14 19:54:25 +11:00
|
|
|
vector<S,std::common_type_t<T,U>> out {};
|
2017-11-22 17:03:00 +11:00
|
|
|
for (std::size_t i = 0; i < S; ++i)
|
2015-03-06 01:46:01 +11:00
|
|
|
out[i] = a[i] - b[i];
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-07 03:18:32 +11:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <
|
2017-11-22 17:03:00 +11: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-11-22 17:03:00 +11: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-11-22 17:03:00 +11: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
|
|
|
|
2017-11-22 17:03:00 +11:00
|
|
|
template <
|
|
|
|
std::size_t S,
|
|
|
|
typename T,
|
|
|
|
typename K,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
is_coord_v<K> && std::is_same_v<typename K::value_type, T> && K::elements == S,
|
|
|
|
void
|
|
|
|
>
|
|
|
|
>
|
|
|
|
constexpr
|
|
|
|
T
|
|
|
|
dot (const T (&a)[S], K k)
|
|
|
|
{
|
|
|
|
return dot (a, k.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <
|
|
|
|
std::size_t S,
|
|
|
|
typename T,
|
|
|
|
typename K,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
is_coord_v<K> && std::is_same_v<typename K::value_type, T> && K::elements == S,
|
|
|
|
void
|
|
|
|
>
|
|
|
|
>
|
|
|
|
constexpr
|
|
|
|
T
|
|
|
|
dot (K k, const T (&a)[S])
|
|
|
|
{
|
|
|
|
return dot (k.data, a);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-11 14:23:32 +10:00
|
|
|
//-------------------------------------------------------------------------
|
2015-03-06 01:46:01 +11:00
|
|
|
template <
|
2017-11-22 17:03:00 +11:00
|
|
|
std::size_t S,
|
2015-03-06 01:46:01 +11:00
|
|
|
typename T,
|
2017-11-22 17:03:00 +11: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-11-22 17:03:00 +11:00
|
|
|
std::size_t S,
|
2015-11-04 17:10:16 +11:00
|
|
|
typename T,
|
2017-11-22 17:03:00 +11: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-11-22 17:03:00 +11:00
|
|
|
std::size_t S,
|
2015-11-04 17:10:16 +11:00
|
|
|
typename T,
|
2017-11-22 17:03:00 +11: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-11-22 17:03:00 +11:00
|
|
|
typename K,
|
|
|
|
typename = std::enable_if_t<has_norm_v<K>,void>
|
2016-08-11 14:58:46 +10:00
|
|
|
>
|
|
|
|
constexpr
|
2017-11-22 17:03:00 +11:00
|
|
|
auto
|
|
|
|
norm2 (const K &k)
|
2016-08-11 14:58:46 +10:00
|
|
|
{
|
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-11-22 17:03:00 +11:00
|
|
|
typename K,
|
2016-08-11 14:58:46 +10:00
|
|
|
typename = std::enable_if_t<
|
2017-11-22 17:03:00 +11:00
|
|
|
has_norm_v<K>,
|
2016-08-11 14:58:46 +10:00
|
|
|
void
|
|
|
|
>
|
|
|
|
>
|
|
|
|
constexpr
|
2017-11-22 17:03:00 +11:00
|
|
|
auto
|
|
|
|
norm (const K &k)
|
2016-08-11 14:58:46 +10:00
|
|
|
{
|
|
|
|
return std::sqrt (norm2 (k));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <
|
2017-11-22 17:03:00 +11:00
|
|
|
typename K,
|
2016-08-11 14:58:46 +10:00
|
|
|
typename = std::enable_if_t<
|
2017-11-22 17:03:00 +11:00
|
|
|
has_norm_v<K>,
|
2016-08-11 14:58:46 +10:00
|
|
|
void
|
|
|
|
>
|
|
|
|
>
|
|
|
|
constexpr
|
2017-11-22 17:03:00 +11:00
|
|
|
auto
|
|
|
|
normalised (const K &k)
|
2016-08-11 14:58:46 +10:00
|
|
|
{
|
2017-11-22 17:03:00 +11:00
|
|
|
CHECK_NEZ (norm (k));
|
|
|
|
return k / norm (k);
|
2016-08-11 14:58:46 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <
|
2017-11-22 17:03:00 +11:00
|
|
|
typename K,
|
2016-08-11 14:58:46 +10:00
|
|
|
typename = std::enable_if_t<
|
2017-11-22 17:03:00 +11:00
|
|
|
has_norm_v<K>,
|
2016-08-11 14:58:46 +10:00
|
|
|
void
|
|
|
|
>
|
|
|
|
>
|
|
|
|
constexpr
|
|
|
|
bool
|
2017-11-22 17:03:00 +11:00
|
|
|
is_normalised (const K &k)
|
2016-08-11 14:58:46 +10:00
|
|
|
{
|
2017-11-22 17:03:00 +11:00
|
|
|
return almost_equal (norm2 (k), 1);
|
2016-08-11 14:58:46 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2015-05-29 15:51:08 +10:00
|
|
|
template <
|
2017-11-22 17:03:00 +11:00
|
|
|
typename K,
|
2016-03-14 19:54:25 +11:00
|
|
|
typename = std::enable_if_t<
|
2017-11-22 17:03:00 +11:00
|
|
|
is_coord_v<K>, 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
|
2017-11-22 17:03:00 +11:00
|
|
|
K
|
|
|
|
abs (K k)
|
2015-05-29 15:51:08 +10:00
|
|
|
{
|
|
|
|
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-11-22 17:03:00 +11:00
|
|
|
typename K,
|
2016-03-14 19:54:25 +11:00
|
|
|
typename = std::enable_if_t<
|
2017-11-22 17:03:00 +11:00
|
|
|
is_coord_v<K>, 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
|
2017-11-22 17:03:00 +11:00
|
|
|
K
|
|
|
|
pow (K k)
|
2015-10-12 23:56:41 +11:00
|
|
|
{
|
|
|
|
for (auto &v: k)
|
|
|
|
v = pow (v);
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
|
2016-09-23 13:27:44 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// root of sum of squares
|
|
|
|
template <
|
2017-11-22 17:03:00 +11:00
|
|
|
std::size_t S,
|
2016-09-23 13:27:44 +10:00
|
|
|
typename T,
|
2017-11-22 17:03:00 +11: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-11-22 17:03:00 +11:00
|
|
|
std::size_t S,
|
2016-09-14 17:38:30 +10:00
|
|
|
typename T,
|
2017-11-22 17:03:00 +11: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-11-22 17:03:00 +11:00
|
|
|
std::size_t S,
|
2016-09-14 17:39:32 +10:00
|
|
|
typename T,
|
2017-11-22 17:03:00 +11: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-11-22 17:03:00 +11:00
|
|
|
std::size_t S,
|
2016-09-14 17:39:32 +10:00
|
|
|
typename T,
|
2017-11-22 17:03:00 +11: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-11-22 17:03:00 +11:00
|
|
|
std::size_t S,
|
2015-03-06 17:49:35 +11:00
|
|
|
typename T,
|
2017-11-22 17:03:00 +11: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 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 {};
|
2017-11-22 17:03:00 +11:00
|
|
|
for (std::size_t i = 0; i < S; ++i)
|
2015-03-06 17:49:35 +11:00
|
|
|
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 <
|
2017-11-22 17:03:00 +11:00
|
|
|
std::size_t S,
|
2015-03-06 17:49:35 +11:00
|
|
|
typename T,
|
2017-11-22 17:03:00 +11: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 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 {};
|
2017-11-22 17:03:00 +11:00
|
|
|
for (std::size_t i = 0; i < S; ++i)
|
2015-03-06 17:49:35 +11:00
|
|
|
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 <
|
2017-11-22 17:03:00 +11:00
|
|
|
std::size_t S,
|
2016-03-14 19:54:25 +11:00
|
|
|
typename T,
|
2017-11-22 17:03:00 +11: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-11-22 17:03:00 +11:00
|
|
|
std::size_t S,
|
2016-03-14 19:54:25 +11:00
|
|
|
typename T,
|
2017-11-22 17:03:00 +11: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-11-22 17:03:00 +11:00
|
|
|
std::size_t S,
|
2016-08-15 18:46:04 +10:00
|
|
|
typename T,
|
2017-11-22 17:03:00 +11: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-11-22 17:03:00 +11:00
|
|
|
std::size_t S, \
|
2016-09-22 16:20:32 +10:00
|
|
|
typename T, \
|
|
|
|
typename U, \
|
2017-11-22 17:03:00 +11: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-11-22 17:03:00 +11: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-11-22 17:03:00 +11: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; \
|
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(>=)
|
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
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
2016-03-14 19:54:25 +11:00
|
|
|
template <
|
2017-11-22 17:03:00 +11: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
|
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)
|
|
|
|
{
|
2016-03-14 19:54:25 +11:00
|
|
|
return std::any_of (std::cbegin (k),
|
2016-09-21 22:22:52 +10:00
|
|
|
std::cend (k),
|
2016-03-14 19:54:25 +11:00
|
|
|
identity<bool>);
|
2015-10-19 17:06:19 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
2016-03-14 19:54:25 +11:00
|
|
|
template <
|
2017-11-22 17:03:00 +11: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
|
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)
|
|
|
|
{
|
2016-03-14 19:54:25 +11:00
|
|
|
return std::all_of (std::cbegin (k),
|
2016-09-21 22:22:52 +10:00
|
|
|
std::cend (k),
|
2016-03-14 19:54:25 +11:00
|
|
|
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 <
|
2017-11-22 17:03:00 +11:00
|
|
|
std::size_t S,
|
2016-03-14 19:54:25 +11:00
|
|
|
typename T,
|
2017-11-22 17:03:00 +11: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
|
|
|
|
|
|
|
/// 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-11-22 17:03:00 +11:00
|
|
|
std::size_t I,
|
|
|
|
std::size_t S,
|
2017-06-15 16:24:44 +10:00
|
|
|
typename T,
|
|
|
|
template<
|
2017-11-22 17:03:00 +11: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-11-22 17:03:00 +11:00
|
|
|
std::size_t I,
|
|
|
|
std::size_t S,
|
2017-06-15 16:24:44 +10:00
|
|
|
typename T,
|
|
|
|
template<
|
2017-11-22 17:03:00 +11: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-11-22 17:03:00 +11:00
|
|
|
std::size_t S,
|
2017-06-15 16:24:44 +10:00
|
|
|
typename T,
|
|
|
|
template<
|
2017-11-22 17:03:00 +11: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-11-22 17:03:00 +11:00
|
|
|
std::size_t I,
|
|
|
|
std::size_t S,
|
2017-06-15 16:24:44 +10:00
|
|
|
typename T,
|
|
|
|
template<
|
2017-11-22 17:03:00 +11: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-11-22 17:03:00 +11: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>>
|
|
|
|
> {
|
2017-11-22 17:03:00 +11:00
|
|
|
uint32_t
|
2017-06-29 16:33:53 +10:00
|
|
|
operator() (K<S,T> k) const {
|
2017-11-22 17:03:00 +11:00
|
|
|
uint32_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
|