2015-03-06 01:46:01 +11:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2015-03-06 01:46:01 +11:00
|
|
|
*
|
2019-01-03 15:48:34 +11:00
|
|
|
* Copyright 2012-2019 Danny Robson <danny@nerdcruft.net>
|
2015-03-06 01:46:01 +11:00
|
|
|
*/
|
|
|
|
|
2019-01-03 15:48:34 +11:00
|
|
|
#pragma once
|
2015-03-06 01:46:01 +11:00
|
|
|
|
2017-11-22 17:03:00 +11:00
|
|
|
#include "fwd.hpp"
|
2017-08-24 14:34:46 +10:00
|
|
|
|
2017-11-23 17:24:11 +11:00
|
|
|
#include "ops.hpp"
|
2015-03-06 01:46:01 +11:00
|
|
|
#include "init.hpp"
|
2017-11-22 17:03:00 +11:00
|
|
|
#include "traits.hpp"
|
2015-04-09 17:58:47 +10:00
|
|
|
#include "../maths.hpp"
|
2015-03-06 01:46:01 +11:00
|
|
|
|
|
|
|
#include <algorithm>
|
2018-06-12 10:54:08 +10:00
|
|
|
#include <array>
|
2015-03-06 01:46:01 +11:00
|
|
|
#include <cstdlib>
|
2016-10-25 19:57:37 +11:00
|
|
|
#include <type_traits>
|
2015-03-06 01:46:01 +11:00
|
|
|
|
2017-11-22 17:03:00 +11:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
namespace cruft::coord {
|
2015-03-06 01:46:01 +11:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2017-11-22 17:03:00 +11:00
|
|
|
// the base class for all coordinate-like types.
|
|
|
|
//
|
|
|
|
// SelfT should not be exposed as a template template directly because
|
|
|
|
// some types (eg, XYZ colours) do not conform to the same template
|
|
|
|
// parameters are others (eg, vector2f). ie, it does not make sense to
|
|
|
|
// allow redim, or type changing on some types so they just aren't exposed.
|
2015-10-19 12:01:29 +11:00
|
|
|
template <
|
2017-08-28 12:25:23 +10:00
|
|
|
std::size_t S,
|
2015-10-19 12:01:29 +11:00
|
|
|
typename T,
|
2017-11-22 17:03:00 +11:00
|
|
|
typename SelfT
|
2015-10-19 12:01:29 +11:00
|
|
|
>
|
2017-11-22 17:03:00 +11:00
|
|
|
struct base : public init<S,T,SelfT> {
|
2016-10-25 19:57:37 +11:00
|
|
|
static_assert (S > 0);
|
|
|
|
static_assert (std::is_arithmetic<T>::value);
|
2017-11-22 17:03:00 +11:00
|
|
|
static_assert (sizeof (init<S,T,SelfT>) == S * sizeof (T));
|
2015-03-06 01:46:01 +11:00
|
|
|
|
2018-01-10 17:19:39 +11:00
|
|
|
using self_t = SelfT;
|
2016-03-11 12:48:19 +11:00
|
|
|
using value_type = T;
|
2017-08-28 12:25:23 +10:00
|
|
|
static constexpr std::size_t dimension = S;
|
|
|
|
static constexpr std::size_t elements = S;
|
2015-03-06 01:46:01 +11:00
|
|
|
|
2017-08-24 14:34:22 +10:00
|
|
|
/// returns the number of elements we contain
|
2017-11-22 17:03:00 +11:00
|
|
|
static constexpr auto size (void) { return S; }
|
2015-03-06 01:46:01 +11:00
|
|
|
|
|
|
|
// constructors
|
2017-11-22 17:03:00 +11:00
|
|
|
using init<S,T,SelfT>::init;
|
2017-08-24 14:34:22 +10:00
|
|
|
|
|
|
|
/// constructs, but does not initialise, the data.
|
|
|
|
///
|
|
|
|
/// used to avoid unnecessary initialisation in many situations where
|
|
|
|
/// we have arrays of these types that are about to be overwritten. it
|
|
|
|
/// is a very important performance optimisation.
|
2015-03-06 01:46:01 +11:00
|
|
|
base () = default;
|
|
|
|
|
2017-08-24 14:34:22 +10:00
|
|
|
/// constructs an instance where all elements are initialised to `val'.
|
2018-04-16 15:51:33 +10:00
|
|
|
constexpr
|
2017-08-27 11:37:18 +10:00
|
|
|
base (T fill)
|
|
|
|
{
|
|
|
|
for (decltype(S) i = 0; i < S; ++i)
|
|
|
|
this->data[i] = fill;
|
|
|
|
}
|
2015-03-06 01:46:01 +11:00
|
|
|
|
2017-11-22 17:03:00 +11:00
|
|
|
constexpr base (const base<S,T,SelfT> &rhs) = default;
|
2017-11-24 13:08:56 +11:00
|
|
|
base& operator= (const base<S,T,SelfT> &rhs)& = default;
|
|
|
|
base& operator= (const T t)&
|
|
|
|
{
|
2018-04-11 18:22:25 +10:00
|
|
|
for (auto &v: *this)
|
2017-11-24 13:08:56 +11:00
|
|
|
v = t;
|
|
|
|
return *this;
|
|
|
|
}
|
2015-03-06 01:46:01 +11:00
|
|
|
|
|
|
|
// element accessors
|
2024-10-29 15:02:59 +11:00
|
|
|
template <typename IdxT>
|
|
|
|
constexpr T&
|
|
|
|
operator[] (IdxT i)& noexcept
|
|
|
|
{ return this->data[i]; }
|
2017-11-24 13:08:56 +11:00
|
|
|
|
2024-10-29 15:02:59 +11:00
|
|
|
template <typename IdxT>
|
|
|
|
constexpr const T&
|
|
|
|
operator[] (IdxT i) const& noexcept
|
|
|
|
{ return this->data[i]; }
|
2015-03-06 01:46:01 +11:00
|
|
|
|
2016-09-21 22:23:20 +10:00
|
|
|
auto cbegin (void) const { return std::cbegin (this->data); }
|
|
|
|
auto cend (void) const { return std::cend (this->data); }
|
|
|
|
|
2015-10-19 12:01:29 +11:00
|
|
|
auto begin (void) const { return std::begin (this->data); }
|
|
|
|
auto end (void) const { return std::end (this->data); }
|
2015-03-06 01:46:01 +11:00
|
|
|
|
2015-10-19 12:01:29 +11:00
|
|
|
auto begin (void) { return std::begin (this->data); }
|
|
|
|
auto end (void) { return std::end (this->data); }
|
2015-03-11 22:58:14 +11:00
|
|
|
|
2016-08-10 18:26:40 +10:00
|
|
|
const T& front (void) const { return this->data[0]; }
|
|
|
|
T& front (void) { return this->data[0]; }
|
|
|
|
|
|
|
|
const T& back (void) const { return this->data[S-1]; }
|
|
|
|
T& back (void) { return this->data[S-1]; }
|
|
|
|
|
2018-06-12 10:54:08 +10:00
|
|
|
|
2015-04-09 17:47:35 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
2015-03-11 22:58:14 +11:00
|
|
|
// conversions
|
2017-08-28 12:25:23 +10:00
|
|
|
template <template <std::size_t, typename> class K>
|
2015-03-11 22:58:14 +11:00
|
|
|
K<S,T> as (void) const
|
|
|
|
{
|
|
|
|
K<S,T> k;
|
|
|
|
std::copy (begin (), end (), k.begin ());
|
|
|
|
return k;
|
|
|
|
}
|
2015-04-09 17:58:47 +10:00
|
|
|
|
2017-11-22 17:03:00 +11:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
template <
|
|
|
|
typename K,
|
|
|
|
typename = std::enable_if_t<is_coord_v<K>,void>
|
|
|
|
>
|
|
|
|
K as (void) const
|
|
|
|
{
|
|
|
|
static_assert (K::elements == elements);
|
|
|
|
K k;
|
|
|
|
std::copy (begin (), end (), k.begin ());
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-21 15:34:46 +10:00
|
|
|
//---------------------------------
|
2015-04-09 17:58:47 +10:00
|
|
|
template <typename U>
|
2017-11-22 17:03:00 +11:00
|
|
|
auto
|
2015-04-09 17:58:47 +10:00
|
|
|
cast (void) const
|
|
|
|
{
|
2017-11-22 17:03:00 +11:00
|
|
|
typename revalue_type<SelfT>::template type<U> out;
|
|
|
|
|
2017-06-13 14:18:00 +10:00
|
|
|
std::copy (std::cbegin (this->data),
|
|
|
|
std::cend (this->data),
|
|
|
|
std::begin (out.data));
|
2015-04-09 17:58:47 +10:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2018-06-12 10:54:08 +10:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
operator std::array<value_type,elements> () const
|
|
|
|
{
|
|
|
|
std::array<value_type,elements> res;
|
|
|
|
std::copy (begin (), end (), std::begin (res));
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-09 17:58:47 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
2017-08-24 14:34:46 +10:00
|
|
|
/// returns an instance with the same data, but truncated to `D'
|
|
|
|
/// elements
|
|
|
|
///
|
|
|
|
/// explicitly does not allow a fill parameter given it can't be used
|
|
|
|
/// when reducing dimensions.
|
2018-01-17 13:27:57 +11:00
|
|
|
///
|
|
|
|
/// HACK,gcc#,gcc-7.x: This function _must not_ use std::copy or
|
|
|
|
/// similar algorithms to move the data as it produces an ICE on some
|
|
|
|
/// code fragements if it does. Namely:
|
|
|
|
/// "internal compiler error: in trunc_int_for_mode, at explow.c:55"
|
2017-11-22 17:03:00 +11:00
|
|
|
template <
|
|
|
|
size_t D,
|
2018-01-17 13:27:57 +11:00
|
|
|
typename = std::enable_if_t<
|
|
|
|
has_redim_v<SelfT> && S >= D,
|
|
|
|
void
|
|
|
|
>
|
2017-08-24 14:34:46 +10:00
|
|
|
>
|
2018-01-17 13:27:57 +11:00
|
|
|
auto
|
2015-04-09 17:58:47 +10:00
|
|
|
redim (void) const
|
|
|
|
{
|
2017-11-22 17:03:00 +11:00
|
|
|
redim_t<SelfT,D> out;
|
2018-01-17 13:27:57 +11:00
|
|
|
for (size_t i = 0; i < D; ++i)
|
|
|
|
out[i] = this->data[i];
|
2015-04-09 17:58:47 +10:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
2017-08-24 14:34:46 +10:00
|
|
|
/// returns an instance with the same data, but more elements, where
|
|
|
|
/// the new elements are initialised with values with the same index
|
|
|
|
/// in the coordinate `fill'.
|
|
|
|
///
|
|
|
|
/// explicitly requires a fill parameter so that we avoid undefined
|
|
|
|
/// values.
|
2018-01-17 13:27:57 +11:00
|
|
|
///
|
|
|
|
/// HACK,gcc#,gcc-7.x: This function _must not_ use std::copy or
|
|
|
|
/// similar algorithms to move the data as it produces an ICE on some
|
|
|
|
/// code fragements if it does. Namely:
|
|
|
|
/// "internal compiler error: in trunc_int_for_mode, at explow.c:55"
|
2017-11-22 17:03:00 +11:00
|
|
|
template<size_t D,typename _sfinae = SelfT>
|
2017-08-24 14:34:46 +10:00
|
|
|
std::enable_if_t<
|
2018-01-17 13:27:57 +11:00
|
|
|
has_redim_v<_sfinae> && S <= D,
|
2017-11-22 17:03:00 +11:00
|
|
|
redim_t<_sfinae,D>
|
2017-08-24 14:34:46 +10:00
|
|
|
>
|
2017-11-22 17:03:00 +11:00
|
|
|
redim (const redim_t<_sfinae,D> fill) const
|
2015-04-09 17:58:47 +10:00
|
|
|
{
|
2017-11-22 17:03:00 +11:00
|
|
|
redim_t<SelfT,D> out;
|
2015-04-09 17:58:47 +10:00
|
|
|
|
2018-01-17 13:27:57 +11:00
|
|
|
for (size_t i = 0; i < S; ++i)
|
|
|
|
out[i] = this->data[i];
|
|
|
|
for (size_t i = S; i < D; ++i)
|
|
|
|
out[i] = fill[i];
|
2015-04-09 17:58:47 +10:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
2017-08-24 14:34:46 +10:00
|
|
|
/// returns an instance with the same data, but more elements, where
|
|
|
|
/// all the new elemenst are initialised with the scalar `fill'.
|
|
|
|
///
|
|
|
|
/// explicitly requires a fill parameter so that we avoid undefined
|
|
|
|
/// values.
|
2018-01-17 13:27:57 +11:00
|
|
|
///
|
|
|
|
/// HACK,gcc#,gcc-7.x: This function _must not_ use std::copy or
|
|
|
|
/// similar algorithms to move the data as it produces an ICE on some
|
|
|
|
/// code fragements if it does. Namely:
|
|
|
|
/// "internal compiler error: in trunc_int_for_mode, at explow.c:55"
|
2017-11-22 17:03:00 +11:00
|
|
|
template <
|
|
|
|
size_t D,
|
|
|
|
typename _sfinae = SelfT
|
|
|
|
>
|
2017-08-24 14:34:46 +10:00
|
|
|
std::enable_if_t<
|
2018-01-17 13:27:57 +11:00
|
|
|
has_redim_v<_sfinae> && S <= D,
|
2017-11-22 17:03:00 +11:00
|
|
|
redim_t<_sfinae,D>
|
2017-08-24 14:34:46 +10:00
|
|
|
>
|
2015-04-09 17:58:47 +10:00
|
|
|
redim (T fill) const
|
|
|
|
{
|
2017-11-22 17:03:00 +11:00
|
|
|
redim_t<SelfT,D> out;
|
2018-01-17 13:27:57 +11:00
|
|
|
for (size_t i = 0; i < S; ++i)
|
|
|
|
out[i] = this->data[i];
|
|
|
|
for (size_t i = S; i < D; ++i)
|
|
|
|
out[i] = fill;
|
2015-04-09 17:58:47 +10:00
|
|
|
return out;
|
|
|
|
}
|
2017-08-24 14:39:06 +10:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
2019-03-22 11:54:29 +11:00
|
|
|
/// Returns an instance with elements specified by the Indices
|
2017-08-24 14:39:06 +10:00
|
|
|
/// parameter. eg, point2f p{}.indices<0,2> would return {p.x, p.z}.
|
|
|
|
///
|
2019-03-22 11:54:29 +11:00
|
|
|
/// Given we don't take any arguments all indices must be valid for
|
|
|
|
/// the current object.
|
|
|
|
template <
|
|
|
|
std::size_t ...Indices,
|
|
|
|
typename = std::enable_if_t<cruft::max (Indices...) < S>
|
|
|
|
>
|
2017-11-23 17:24:11 +11:00
|
|
|
constexpr auto
|
2017-08-24 14:39:06 +10:00
|
|
|
indices (void) const
|
2019-03-22 11:54:29 +11:00
|
|
|
{
|
|
|
|
return redim_t<SelfT,sizeof...(Indices)> {
|
|
|
|
this->data[Indices]...
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Return an instance of the same type where the elements of the new
|
|
|
|
/// value area specified by the Indices... template parameter.
|
|
|
|
///
|
|
|
|
/// If the index is greater than the maximum index of this type then
|
|
|
|
/// the value is taken from a pack of supplementary values.
|
|
|
|
///
|
|
|
|
/// eg, vector2f{0,1}.indices<0,2,1> (2) would result in {0,2,1}
|
|
|
|
template <
|
|
|
|
std::size_t ...Indices,
|
|
|
|
typename ...U,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
// At least one index doesn't address our current data
|
|
|
|
cruft::max (Indices...) >= S &&
|
|
|
|
// The new data type must be the old data type
|
|
|
|
(std::is_same_v<T,U> && ...)
|
|
|
|
>
|
|
|
|
>
|
|
|
|
constexpr auto
|
2019-03-22 15:09:19 +11:00
|
|
|
indices (U const &...supplementary) const
|
2017-08-24 14:39:06 +10:00
|
|
|
{
|
2017-08-27 11:37:52 +10:00
|
|
|
static_assert (
|
2019-03-22 11:54:29 +11:00
|
|
|
cruft::max (Indices...) < S + sizeof...(supplementary),
|
2017-08-27 11:37:52 +10:00
|
|
|
"indices must fall within the defined range for the type"
|
|
|
|
);
|
|
|
|
|
2019-03-22 11:54:29 +11:00
|
|
|
// Expand the pack using either:
|
|
|
|
// * data from ourselves,
|
|
|
|
// * or rebasing the index into a tuple of the supplementary values
|
|
|
|
//
|
|
|
|
// The index to std::get _must_ be valid even if we don't use the
|
|
|
|
// result (otherwise we tend to encounter static assertions). Thus
|
|
|
|
// we conditionally specify a zero index if we know it won't get
|
|
|
|
// used.
|
2017-11-23 17:24:11 +11:00
|
|
|
return redim_t<SelfT,sizeof...(Indices)> {
|
2019-03-22 11:54:29 +11:00
|
|
|
Indices < S
|
|
|
|
? this->data[Indices]
|
|
|
|
: std::get<
|
|
|
|
(Indices > S) ? Indices - S : 0
|
|
|
|
> (std::tuple (supplementary...))...
|
2017-11-23 17:24:11 +11:00
|
|
|
};
|
2017-08-24 14:39:06 +10:00
|
|
|
}
|
2015-03-06 01:46:01 +11:00
|
|
|
};
|
2017-01-05 15:06:49 +11:00
|
|
|
}
|
2015-03-06 01:46:01 +11:00
|
|
|
|
2019-01-03 15:48:34 +11:00
|
|
|
|
|
|
|
/// Invoke a macro across all typical coordinate parameter combinations.
|
|
|
|
///
|
|
|
|
/// ie, each of the types i16, i32, i64, f32, and f64; and arities of 1, 2, 3,
|
|
|
|
/// and 4.
|
|
|
|
///
|
|
|
|
/// This is useful for exhaustive delayed instantiation in .cpp files.
|
|
|
|
|
|
|
|
#define MAP_CRUFT_COORD_PARAMS(F) \
|
|
|
|
F(1,i16) F(2,i16) F(3,i16) F(4,i16) \
|
|
|
|
F(1,i32) F(2,i32) F(3,i32) F(4,i32) \
|
|
|
|
F(1,i64) F(2,i64) F(3,i64) F(4,i64) \
|
|
|
|
F(1,u16) F(2,u16) F(3,u16) F(4,u16) \
|
|
|
|
F(1,u32) F(2,u32) F(3,u32) F(4,u32) \
|
|
|
|
F(1,u64) F(2,u64) F(3,u64) F(4,u64) \
|
|
|
|
F(1,f32) F(2,f32) F(3,f32) F(4,f32) \
|
|
|
|
F(1,f64) F(2,f64) F(3,f64) F(4,f64)
|