Mathter
A configurable 3D math library for game developers.
Traits.hpp
Go to the documentation of this file.
1 //==============================================================================
2 // This software is distributed under The Unlicense.
3 // For more information, please refer to <http://unlicense.org/>
4 //==============================================================================
5 
6 #pragma once
7 
8 #include "Definitions.hpp"
9 #include <cmath>
10 #include <type_traits>
11 
12 
13 namespace mathter::traits {
14 
15 
16  // Vector properties
17  template <class VectorT>
19 
20  template <class T_, int Dim_, bool Packed_>
21  class VectorTraitsHelper<Vector<T_, Dim_, Packed_>> {
22  public:
23  using Type = T_;
24  static constexpr int Dim = Dim_;
25  static constexpr bool Packed = Packed_;
26  };
27 
28  template <class VectorT>
29  class VectorTraits : public VectorTraitsHelper<typename std::decay<VectorT>::type> {};
30 
31 
32  // Matrix properties
33  template <class MatrixT>
35 
36  template <class T_, int Rows_, int Columns_, eMatrixOrder Order_, eMatrixLayout Layout_, bool Packed_>
37  class MatrixTraitsHelper<Matrix<T_, Rows_, Columns_, Order_, Layout_, Packed_>> {
38  public:
39  using Type = T_;
40  static constexpr int Rows = Rows_;
41  static constexpr int Columns = Columns_;
42  static constexpr eMatrixOrder Order = Order_;
43  static constexpr eMatrixLayout Layout = Layout_;
44  static constexpr bool Packed = Packed_;
45  };
46 
47  template <class MatrixT>
48  class MatrixTraits : public MatrixTraitsHelper<typename std::decay<MatrixT>::type> {};
49 
50 
51  template <eMatrixLayout Layout>
53  public:
55  };
56 
57 
58  // Common utility
59  template <class T, class U>
60  using MatMulElemT = decltype(T() * U() + T() + U());
61 
62 
63 
64  // Template metaprogramming utilities
65  template <template <class> class Cond, class... T>
66  struct All;
67 
68  template <template <class> class Cond, class Head, class... Rest>
69  struct All<Cond, Head, Rest...> {
70  static constexpr bool value = Cond<Head>::value && All<Cond, Rest...>::value;
71  };
72 
73  template <template <class> class Cond>
74  struct All<Cond> {
75  static constexpr bool value = true;
76  };
77 
78 
79  template <template <class> class Cond, class... T>
80  struct Any;
81 
82  template <template <class> class Cond, class Head, class... Rest>
83  struct Any<Cond, Head, Rest...> {
84  static constexpr bool value = Cond<Head>::value || Any<Cond, Rest...>::value;
85  };
86 
87  template <template <class> class Cond>
88  struct Any<Cond> {
89  static constexpr bool value = false;
90  };
91 
92 
93 
94  template <class... T>
95  struct TypeList {};
96 
97  template <class Tl1, class Tl2>
99 
100  template <class... T, class... U>
101  struct ConcatTypeList<TypeList<T...>, TypeList<U...>> {
102  using type = TypeList<T..., U...>;
103  };
104 
105  template <class T, int N>
106  struct RepeatType {
107  using type = typename std::conditional<N <= 0, TypeList<>, typename ConcatTypeList<TypeList<T>, typename RepeatType<T, N - 1>::type>::type>::type;
108  };
109 
110 
111  // Decide if type is Scalar, Vector or Matrix.
112  template <class Arg>
113  struct IsVector {
114  static constexpr bool value = false;
115  };
116  template <class T, int Dim, bool Packed>
117  struct IsVector<Vector<T, Dim, Packed>> {
118  static constexpr bool value = true;
119  };
120  template <class Arg>
121  struct NotVector {
122  static constexpr bool value = !IsVector<Arg>::value;
123  };
124 
125  template <class Arg>
126  struct IsSwizzle {
127  static constexpr bool value = false;
128  };
129  template <class T, int... Indices>
130  struct IsSwizzle<Swizzle<T, Indices...>> {
131  static constexpr bool value = true;
132  };
133  template <class Arg>
134  struct NotSwizzle {
135  static constexpr bool value = !IsSwizzle<Arg>::value;
136  };
137 
138  template <class Arg>
140  static constexpr bool value = IsVector<Arg>::value || IsSwizzle<Arg>::value;
141  };
142 
143  template <class T>
144  struct IsMatrix {
145  static constexpr bool value = false;
146  };
147  template <class T, int Rows, int Columns, eMatrixOrder Order, eMatrixLayout Layout, bool Packed>
148  struct IsMatrix<Matrix<T, Rows, Columns, Order, Layout, Packed>> {
149  static constexpr bool value = true;
150  };
151  template <class T>
152  struct NotMatrix {
153  static constexpr bool value = !IsMatrix<T>::value;
154  };
155 
156  template <class T>
157  struct IsSubmatrix {
158  static constexpr bool value = false;
159  };
160  template <class M, int Rows, int Columns>
161  struct IsSubmatrix<SubmatrixHelper<M, Rows, Columns>> {
162  static constexpr bool value = true;
163  };
164  template <class T>
165  struct NotSubmatrix {
166  static constexpr bool value = !IsSubmatrix<T>::value;
167  };
168 
169  template <class Arg>
170  struct IsQuaternion {
171  static constexpr bool value = false;
172  };
173  template <class T, bool Packed>
174  struct IsQuaternion<Quaternion<T, Packed>> {
175  static constexpr bool value = true;
176  };
177  template <class Arg>
178  struct NotQuaternion {
179  static constexpr bool value = !IsQuaternion<Arg>::value;
180  };
181 
182 
183  template <class T>
184  struct IsScalar {
186  };
187 
188  // Dimension of an argument (add dynamically sized vectors later).
189  template <class U, int Along = 0>
190  struct DimensionOf {
191  static constexpr int value = 1;
192  };
193  template <class T, int Dim, bool Packed>
194  struct DimensionOf<Vector<T, Dim, Packed>, 0> {
195  static constexpr int value = Dim;
196  };
197  template <class T, int... Indices>
198  struct DimensionOf<Swizzle<T, Indices...>> {
199  static constexpr int value = sizeof...(Indices);
200  };
201 
202  // Sum dimensions of arguments.
203  template <class... Rest>
205 
206  template <class Head, class... Rest>
207  struct SumDimensions<Head, Rest...> {
208  static constexpr int value = DimensionOf<Head>::value > 0 ? DimensionOf<Head>::value + SumDimensions<Rest...>::value : DYNAMIC;
209  };
210 
211  template <>
212  struct SumDimensions<> {
213  static constexpr int value = 0;
214  };
215 
216  // Weather vector uses SIDM.
217  template <class VectorDataT>
218  struct HasSimd {
219  template <class U>
220  static std::false_type test(...) { return {}; }
221 
222  template <class U>
223  static decltype(U::simd) test(int) { return {}; }
224 
225 
226  static constexpr bool value = !std::is_same<std::false_type, decltype(test<VectorDataT>(0))>::value;
227  };
228 }
Definition: Traits.hpp:13
Definition: Traits.hpp:106
Definition: Traits.hpp:165
typename std::conditional< N<=0, TypeList<>, typename ConcatTypeList< TypeList< T >, typename RepeatType< T, N - 1 >::type >::type >::type type
Definition: Traits.hpp:107
Allows you to do quaternion math and represent rotation in a compact way.
Definition: Definitions.hpp:69
Definition: Traits.hpp:113
Definition: Traits.hpp:190
Definition: Traits.hpp:18
Definition: Traits.hpp:144
Definition: Traits.hpp:29
Definition: Traits.hpp:98
Definition: Traits.hpp:157
static std::false_type test(...)
Definition: Traits.hpp:220
Represents a vector in N-dimensional space.
Definition: Definitions.hpp:57
Enables element swizzling (reordering elements) for vectors.
Definition: Definitions.hpp:60
Definition: Traits.hpp:48
Definition: Traits.hpp:121
Definition: Traits.hpp:66
constexpr int DYNAMIC
Specify this as Vector or Matrix dimension template parameter to set size at runtime. PLEASE NOTE THAT DYNAMICALLY SIZED VECTORS AND MATRICES ARE NOT SUPPORTED YET.
Definition: Definitions.hpp:49
Definition: Traits.hpp:139
Definition: Traits.hpp:152
Definition: Traits.hpp:134
Definition: Traits.hpp:95
Definition: Definitions.hpp:66
Definition: Traits.hpp:184
Definition: Traits.hpp:204
Definition: Traits.hpp:80
static decltype(U::simd) test(int)
Definition: Traits.hpp:223
Definition: Definitions.hpp:63
Definition: Traits.hpp:178
Definition: Traits.hpp:34
Definition: Traits.hpp:126
Definition: Traits.hpp:52
Definition: Traits.hpp:170
Definition: Traits.hpp:218
eMatrixOrder
Determines if you want to left- or right-multiply your matrices with vectors.
Definition: Definitions.hpp:22
decltype(T() *U()+T()+U()) MatMulElemT
Definition: Traits.hpp:60
eMatrixLayout
Determines the memory layout of matrices.
Definition: Definitions.hpp:36