Lugdunum  0.1.0
Matrix.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstdint>
4 #include <valarray>
5 #include <lug/Math/Export.hpp>
6 #include <lug/Math/ValArray.hpp>
7 #include <lug/System/Debug.hpp>
8 
9 namespace lug {
10 namespace Math {
11 
12 template <uint8_t Rows, uint8_t Columns, typename T = float>
13 class Matrix {
14 public:
15  // TODO: Use custom valarray with compile time size
17 
18 public:
19  constexpr Matrix() = default;
20 
21  explicit Matrix(T value);
22  Matrix(const Values& values);
23  Matrix(std::initializer_list<T> list);
24  Matrix(const Matrix<Rows, Columns, T>& matrix) = default;
25  Matrix(Matrix<Rows, Columns, T>&& matrix) = default;
26 
29 
30  ~Matrix() = default;
31 
32  constexpr uint8_t getRows() const;
33  constexpr uint8_t getColumns() const;
34 
35  Values& getValues();
36  constexpr const Values& getValues() const;
37 
38  T& operator()(uint8_t row, uint8_t col = 0);
39  constexpr const T& operator()(uint8_t row, uint8_t col = 0) const;
40 
41  // Matrix/Scalar operations
46 
47  // Matrix/Matrix operations
50 
51 #if defined(LUG_COMPILER_MSVC)
52 
53  template <typename = typename std::enable_if<(Rows == Columns)>::type>
55 
56  template <typename = typename std::enable_if<(Rows == Columns)>::type>
58 
59 #else
60 
61  template <bool EnableBool = true>
62  typename std::enable_if<(Rows == Columns) && EnableBool, Matrix<Rows, Columns, T>>::type& operator*=(const Matrix<Rows, Columns, T>& rhs);
63 
64  template <bool EnableBool = true>
65  typename std::enable_if<(Rows == Columns) && EnableBool, Matrix<Rows, Columns, T>>::type& operator/=(const Matrix<Rows, Columns, T>& rhs);
66 
67 #endif
68 
69 #if defined(LUG_COMPILER_MSVC)
70 
71  template <typename = typename std::enable_if<(Rows == 1)>::type>
73 
74  template <typename = typename std::enable_if<(Rows == 2)>::type, typename = void>
76 
77  template <typename = typename std::enable_if<(Rows == 3)>::type, typename = void, typename = void>
79 
80  template <typename = typename std::enable_if<(Rows == 4)>::type, typename = void, typename = void, typename = void>
82 
83 #else
84 
85  template <bool EnableBool = true>
86  typename std::enable_if<(Rows == 1) && EnableBool, Matrix<Rows, Columns, T>>::type inverse() const;
87 
88  template <bool EnableBool = true>
89  typename std::enable_if<(Rows == 2) && EnableBool, Matrix<Rows, Columns, T>>::type inverse() const;
90 
91  template <bool EnableBool = true>
92  typename std::enable_if<(Rows == 3) && EnableBool, Matrix<Rows, Columns, T>>::type inverse() const;
93 
94  template <bool EnableBool = true>
95  typename std::enable_if<(Rows == 4) && EnableBool, Matrix<Rows, Columns, T>>::type inverse() const;
96 
97 #endif
98 
100 
101 #if defined(LUG_COMPILER_MSVC)
102 
103  template <typename = typename std::enable_if<(Rows == 1)>::type>
104  T det() const;
105 
106  template <typename = typename std::enable_if<(Rows == 2)>::type, typename = void>
107  T det() const;
108 
109  template <typename = typename std::enable_if<(Rows == 3)>::type, typename = void, typename = void>
110  T det() const;
111 
112  template <typename = typename std::enable_if<(Rows == 4)>::type, typename = void, typename = void, typename = void>
113  T det() const;
114 
115  template <typename = typename std::enable_if<(Rows > 4)>::type, typename = void, typename = void, typename = void, typename = void>
116  T det() const;
117 
118 #else
119 
120  template <bool EnableBool = true>
121  typename std::enable_if<(Rows == 1) && EnableBool, T>::type det() const;
122 
123  template <bool EnableBool = true>
124  typename std::enable_if<(Rows == 2) && EnableBool, T>::type det() const;
125 
126  template <bool EnableBool = true>
127  typename std::enable_if<(Rows == 3) && EnableBool, T>::type det() const;
128 
129  template <bool EnableBool = true>
130  typename std::enable_if<(Rows == 4) && EnableBool, T>::type det() const;
131 
132  template <bool EnableBool = true>
133  typename std::enable_if<(Rows > 4) && EnableBool, T>::type det() const;
134 
135 #endif
136 
137 #if defined(LUG_COMPILER_MSVC)
138 
139  template <typename = typename std::enable_if<(Rows == Columns)>::type>
141 
142 #else
143 
144  template <bool EnableBool = true>
145  static typename std::enable_if<(Rows == Columns) && EnableBool, Matrix<Rows, Columns, T>>::type identity();
146 
147 #endif
148 
149 protected:
151 };
152 
153 #define DEFINE_LENGTH_MATRIX(rows, columns) \
154  template <typename T = float> \
155  using Mat##rows##x##columns= Matrix<rows, columns, T>; \
156  \
157  template class LUG_MATH_API Matrix<rows, columns, float>; \
158  using Mat##rows##x##columns##f = Mat##rows##x##columns<float>; \
159  \
160  template class LUG_MATH_API Matrix<rows, columns, double>; \
161  using Mat##rows##x##columns##d = Mat##rows##x##columns<double>; \
162  \
163  template class LUG_MATH_API Matrix<rows, columns, int32_t>; \
164  using Mat##rows##x##columns##i = Mat##rows##x##columns<int32_t>; \
165  \
166  template class LUG_MATH_API Matrix<rows, columns, uint32_t>; \
167  using Mat##rows##x##columns##u = Mat##rows##x##columns<uint32_t>;
168 
178 
179 #undef DEFINE_LENGTH_MATRIX
180 
181 // Unary operations
182 template <uint8_t Rows, uint8_t Columns, typename T>
183 Matrix<Rows, Columns, T> operator-(const Matrix<Rows, Columns, T>& lhs);
184 
185 // Matrix/Scalar operations
186 template <uint8_t Rows, uint8_t Columns, typename T>
187 Matrix<Rows, Columns, T> operator+(const Matrix<Rows, Columns, T>& lhs, T rhs);
188 
189 template <uint8_t Rows, uint8_t Columns, typename T>
190 Matrix<Rows, Columns, T> operator-(const Matrix<Rows, Columns, T>& lhs, T rhs);
191 
192 template <uint8_t Rows, uint8_t Columns, typename T>
193 Matrix<Rows, Columns, T> operator*(const Matrix<Rows, Columns, T>& lhs, T rhs);
194 
195 template <uint8_t Rows, uint8_t Columns, typename T>
196 Matrix<Rows, Columns, T> operator/(const Matrix<Rows, Columns, T>& lhs, T rhs);
197 
198 template <uint8_t Rows, uint8_t Columns, typename T>
199 Matrix<Rows, Columns, T> operator+(T lhs, const Matrix<Rows, Columns, T>& rhs);
200 
201 template <uint8_t Rows, uint8_t Columns, typename T>
202 Matrix<Rows, Columns, T> operator-(T lhs, const Matrix<Rows, Columns, T>& rhs);
203 
204 template <uint8_t Rows, uint8_t Columns, typename T>
205 Matrix<Rows, Columns, T> operator*(T lhs, const Matrix<Rows, Columns, T>& rhs);
206 
207 template <uint8_t Rows, uint8_t Columns, typename T>
208 Matrix<Rows, Columns, T> operator/(T lhs, const Matrix<Rows, Columns, T>& rhs);
209 
210 // Matrix/Matrix operation
211 template <uint8_t Rows, uint8_t Columns, typename T>
212 Matrix<Rows, Columns, T> operator+(const Matrix<Rows, Columns, T>& lhs, const Matrix<Rows, Columns, T>& rhs);
213 
214 template <uint8_t Rows, uint8_t Columns, typename T>
215 Matrix<Rows, Columns, T> operator-(const Matrix<Rows, Columns, T>& lhs, const Matrix<Rows, Columns, T>& rhs);
216 
217 template <uint8_t RowsLeft, uint8_t ColumnsLeft, uint8_t RowsRight, uint8_t ColumnsRight, typename T>
218 Matrix<RowsLeft, ColumnsRight, T> operator*(const Matrix<RowsLeft, ColumnsLeft, T>& lhs, const Matrix<RowsRight, ColumnsRight, T>& rhs);
219 
220 template <uint8_t RowsLeft, uint8_t ColumnsLeft, uint8_t RowsRight, uint8_t ColumnsRight, typename T>
221 Matrix<RowsLeft, ColumnsRight, T> operator/(const Matrix<RowsLeft, ColumnsLeft, T>& lhs, const Matrix<RowsRight, ColumnsRight, T>& rhs);
222 
223 // Comparaison operators
224 template <uint8_t Rows, uint8_t Columns, typename T>
225 bool operator==(const Matrix<Rows, Columns, T>& lhs, const Matrix<Rows, Columns, T>& rhs);
226 
227 template <uint8_t Rows, uint8_t Columns, typename T>
228 bool operator!=(const Matrix<Rows, Columns, T>& lhs, const Matrix<Rows, Columns, T>& rhs);
229 
230 template <uint8_t Rows, uint8_t Columns, typename T>
231 std::ostream& operator<<(std::ostream& os, const Matrix<Rows, Columns, T>& matrix);
232 
233 #include <lug/Math/Matrix.inl>
234 
235 } // Math
236 } // lug
bool operator!=(const Matrix< Rows, Columns, T > &lhs, const Matrix< Rows, Columns, T > &rhs)
bool operator==(const Matrix< Rows, Columns, T > &lhs, const Matrix< Rows, Columns, T > &rhs)
#define DEFINE_LENGTH_MATRIX(rows, columns)
Definition: Matrix.hpp:153
Matrix< Columns, Rows, T > transpose() const
Definition: Matrix.inl:331
Matrix< Rows, Columns, T > & operator+=(T rhs)
Definition: Matrix.inl:64
Matrix< Rows, Columns, T > operator-(const Matrix< Rows, Columns, T > &lhs)
constexpr uint8_t getRows() const
Definition: Matrix.inl:32
Values & getValues()
Definition: Matrix.inl:42
static std::enable_if<(Rows==Columns) &&EnableBool, Matrix< Rows, Columns, T > >::type identity()
constexpr Matrix()=default
Matrix< Rows, Columns, T > operator+(const Matrix< Rows, Columns, T > &lhs, T rhs)
Matrix< Rows, Columns, T > & operator=(const Matrix< Rows, Columns, T > &rhs)=default
std::enable_if<(Rows==1) &&EnableBool, Matrix< Rows, Columns, T > >::type inverse() const
T & operator()(uint8_t row, uint8_t col=0)
constexpr uint8_t getColumns() const
Definition: Matrix.inl:37
Matrix< Rows, Columns, T > operator/(const Matrix< Rows, Columns, T > &lhs, T rhs)
Matrix< Rows, Columns, T > & operator*=(T rhs)
Definition: Matrix.inl:76
Matrix< Rows, Columns, T > & operator/=(T rhs)
Definition: Matrix.inl:82
std::enable_if<(Rows==1) &&EnableBool, T >::type det() const
Matrix< Rows, Columns, T > operator*(const Matrix< Rows, Columns, T > &lhs, T rhs)
Matrix< Rows, Columns, T > & operator-=(T rhs)
Definition: Matrix.inl:70