Loading...
Searching...
No Matches
Matrix.hpp
Go to the documentation of this file.
1// This code is based on Jet framework.
2// Copyright (c) 2018 Doyub Kim
3// CubbyFlow is voxel-based fluid simulation engine for computer games.
4// Copyright (c) 2020 CubbyFlow Team
5// Core Part: Chris Ohk, Junwoo Hwang, Jihong Sin, Seungwoo Yoo
6// AI Part: Dongheon Cho, Minseo Kim
7// We are making my contributions/submissions to this project solely in our
8// personal capacity and are not conveying any rights to any intellectual
9// property of any third parties.
10
11#ifndef CUBBYFLOW_MATRIX_HPP
12#define CUBBYFLOW_MATRIX_HPP
13
16#include <Core/Utils/Macros.hpp>
19
20#include <array>
21#include <cstdint>
22#include <vector>
23
24namespace CubbyFlow
25{
26template <typename T, size_t Rows, size_t Cols>
28 : public MatrixExpression<T, Rows, Cols, Matrix<T, Rows, Cols>>,
29 public MatrixDenseBase<T, Rows, Cols, Matrix<T, Rows, Cols>>
30{
31 public:
32 static_assert(IsMatrixSizeStatic<Rows, Cols>(),
33 "This class should be a static-sized matrix.");
34
36 using Base::CopyFrom;
37 using Base::operator();
38
39 using ValueType = T;
40 using Reference = T&;
41 using ConstReference = const T&;
42 using Pointer = T*;
43 using ConstPointer = const T*;
46
47 constexpr Matrix() : m_elements{}
48 {
49 // Do nothing
50 }
51
53
54 template <typename... Args>
56 : m_elements{ { first, static_cast<ValueType>(rest)... } }
57 {
58 // Do nothing
59 }
60
61 template <size_t R, size_t C, typename E>
63
65
66 explicit Matrix(ConstPointer ptr);
67
68 ~Matrix() = default;
69
70 constexpr Matrix(const Matrix&) = default;
71
72 constexpr Matrix(Matrix&&) noexcept = default;
73
75
77
78 void Fill(const T& val);
79
80 void Fill(const std::function<T(size_t i)>& func);
81
82 void Fill(const std::function<T(size_t i, size_t j)>& func);
83
84 void Swap(Matrix& other);
85
86 [[nodiscard]] constexpr size_t GetRows() const;
87
88 [[nodiscard]] constexpr size_t GetCols() const;
89
90 Iterator begin();
91
93
94 Iterator end();
95
97
98 Pointer data();
99
101
102 Reference operator[](size_t i);
103
104 ConstReference operator[](size_t i) const;
105
106 private:
107 std::array<T, Rows * Cols> m_elements;
108};
109
112 public MatrixDenseBase<T, 1, 1, Matrix<T, 1, 1>>
113{
114 public:
116 using Base::operator();
117
118 using ValueType = T;
119 using Reference = T&;
120 using ConstReference = const T&;
121 using Pointer = T*;
122 using ConstPointer = const T*;
125
126 constexpr Matrix() : x(T{})
127 {
128 // Do nothing
129 }
130
131 constexpr Matrix(const T& _x) : x(_x)
132 {
133 // Do nothing
134 }
135
136 template <size_t R, size_t C, typename E>
138
139 Matrix(const std::initializer_list<T>& lst);
140
141 ~Matrix() = default;
142
143 constexpr Matrix(const Matrix&) = default;
144
146
148
150
151 void Fill(const T& val);
152
153 void Fill(const std::function<T(size_t i)>& func);
154
155 void Fill(const std::function<T(size_t i, size_t j)>& func);
156
157 void Swap(Matrix& other);
158
159 [[nodiscard]] constexpr size_t GetRows() const;
160
161 [[nodiscard]] constexpr size_t GetCols() const;
162
163 [[nodiscard]] Iterator begin();
164
166
167 [[nodiscard]] Iterator end();
168
170
171 [[nodiscard]] Pointer data();
172
174
175 Reference operator[](size_t i);
176
177 ConstReference operator[](size_t i) const;
178
179 constexpr static Matrix MakeUnitX();
180
181 constexpr static Matrix MakeUnit(size_t i);
182
184};
185
188 public MatrixDenseBase<T, 2, 1, Matrix<T, 2, 1>>
189{
190 public:
192 using Base::operator();
193
194 using ValueType = T;
195 using Reference = T&;
196 using ConstReference = const T&;
197 using Pointer = T*;
198 using ConstPointer = const T*;
201
202 constexpr Matrix() : x(T{}), y(T{})
203 {
204 // Do nothing
205 }
206
207 constexpr Matrix(const T& _x, const T& _y) : x(_x), y(_y)
208 {
209 // Do nothing
210 }
211
212 template <size_t R, size_t C, typename E>
214
215 Matrix(const std::initializer_list<T>& lst);
216
217 ~Matrix() = default;
218
219 constexpr Matrix(const Matrix&) = default;
220
222
224
226
227 void Fill(const T& val);
228
229 void Fill(const std::function<T(size_t i)>& func);
230
231 void Fill(const std::function<T(size_t i, size_t j)>& func);
232
233 void Swap(Matrix& other);
234
235 [[nodiscard]] constexpr size_t GetRows() const;
236
237 [[nodiscard]] constexpr size_t GetCols() const;
238
239 [[nodiscard]] Iterator begin();
240
242
243 [[nodiscard]] Iterator end();
244
246
247 [[nodiscard]] Pointer data();
248
250
251 Reference operator[](size_t i);
252
253 ConstReference operator[](size_t i) const;
254
255 constexpr static Matrix MakeUnitX();
256
257 constexpr static Matrix MakeUnitY();
258
259 constexpr static Matrix MakeUnit(size_t i);
260
263};
264
267 public MatrixDenseBase<T, 3, 1, Matrix<T, 3, 1>>
268{
269 public:
271 using Base::operator();
272
273 using ValueType = T;
274 using Reference = T&;
275 using ConstReference = const T&;
276 using Pointer = T*;
277 using ConstPointer = const T*;
280
281 constexpr Matrix() : x(T{}), y(T{}), z(T{})
282 {
283 // Do nothing
284 }
285
286 constexpr Matrix(const Matrix<T, 2, 1>& _xy, const T& _z)
287 : x(_xy.x), y(_xy.y), z(_z)
288 {
289 // Do nothing
290 }
291
292 constexpr Matrix(const T& _x, const T& _y, const T& _z)
293 : x(_x), y(_y), z(_z)
294 {
295 // Do nothing
296 }
297
298 template <size_t R, size_t C, typename E>
300
301 Matrix(const std::initializer_list<T>& lst);
302
303 ~Matrix() = default;
304
305 constexpr Matrix(const Matrix&) = default;
306
308
310
312
313 void Fill(const T& val);
314
315 void Fill(const std::function<T(size_t i)>& func);
316
317 void Fill(const std::function<T(size_t i, size_t j)>& func);
318
319 void Swap(Matrix& other);
320
321 [[nodiscard]] constexpr size_t GetRows() const;
322
323 [[nodiscard]] constexpr size_t GetCols() const;
324
325 [[nodiscard]] Iterator begin();
326
328
329 [[nodiscard]] Iterator end();
330
332
333 [[nodiscard]] Pointer data();
334
336
337 Reference operator[](size_t i);
338
339 ConstReference operator[](size_t i) const;
340
341 constexpr static Matrix MakeUnitX();
342
343 constexpr static Matrix MakeUnitY();
344
345 constexpr static Matrix MakeUnitZ();
346
347 constexpr static Matrix MakeUnit(size_t i);
348
352};
353
356 public MatrixDenseBase<T, 4, 1, Matrix<T, 4, 1>>
357{
358 public:
360 using Base::operator();
361
362 using ValueType = T;
363 using Reference = T&;
364 using ConstReference = const T&;
365 using Pointer = T*;
366 using ConstPointer = const T*;
369
370 constexpr Matrix() : x(T{}), y(T{}), z(T{}), w(T{})
371 {
372 // Do nothing
373 }
374
375 constexpr Matrix(const T& _x, const T& _y, const T& _z, const T& _w)
376 : x(_x), y(_y), z(_z), w(_w)
377 {
378 // Do nothing
379 }
380
381 template <size_t R, size_t C, typename E>
383
384 Matrix(const std::initializer_list<T>& lst);
385
386 ~Matrix() = default;
387
388 constexpr Matrix(const Matrix&) = default;
389
391
393
395
396 void Fill(const T& val);
397
398 void Fill(const std::function<T(size_t i)>& func);
399
400 void Fill(const std::function<T(size_t i, size_t j)>& func);
401
402 void Swap(Matrix& other);
403
404 [[nodiscard]] constexpr size_t GetRows() const;
405
406 [[nodiscard]] constexpr size_t GetCols() const;
407
408 [[nodiscard]] Iterator begin();
409
411
412 [[nodiscard]] Iterator end();
413
415
416 [[nodiscard]] Pointer data();
417
419
420 Reference operator[](size_t i);
421
422 ConstReference operator[](size_t i) const;
423
424 constexpr static Matrix MakeUnitX();
425
426 constexpr static Matrix MakeUnitY();
427
428 constexpr static Matrix MakeUnitZ();
429
430 constexpr static Matrix MakeUnitW();
431
432 constexpr static Matrix MakeUnit(size_t i);
433
438};
439
441class Matrix<T, MATRIX_SIZE_DYNAMIC, MATRIX_SIZE_DYNAMIC> final
443 T, MATRIX_SIZE_DYNAMIC, MATRIX_SIZE_DYNAMIC,
444 Matrix<T, MATRIX_SIZE_DYNAMIC, MATRIX_SIZE_DYNAMIC>>,
446 T, MATRIX_SIZE_DYNAMIC, MATRIX_SIZE_DYNAMIC,
447 Matrix<T, MATRIX_SIZE_DYNAMIC, MATRIX_SIZE_DYNAMIC>>
448{
449 public:
450 using ValueType = T;
451 using Reference = T&;
452 using ConstReference = const T&;
453 using Pointer = T*;
454 using ConstPointer = const T*;
457 using MatrixDenseBase<
458 T, MATRIX_SIZE_DYNAMIC, MATRIX_SIZE_DYNAMIC,
460
462
463 Matrix(size_t rows, size_t cols, ConstReference value = ValueType{});
464
465 template <size_t R, size_t C, typename E>
467
469
470 explicit Matrix(size_t rows, size_t cols, ConstPointer ptr);
471
472 ~Matrix() = default;
473
475
476 Matrix(Matrix&& other) noexcept;
477
479
480 Matrix& operator=(Matrix&& other) noexcept;
481
482 void Fill(const T& val);
483
484 void Fill(const std::function<T(size_t i)>& func);
485
486 void Fill(const std::function<T(size_t i, size_t j)>& func);
487
488 void Swap(Matrix& other);
489
490 void Resize(size_t rows, size_t cols, ConstReference val = ValueType{});
491
492 void Clear();
493
494 [[nodiscard]] size_t GetRows() const;
495
496 [[nodiscard]] size_t GetCols() const;
497
498 [[nodiscard]] Iterator begin();
499
500 [[nodiscard]] ConstIterator begin() const;
501
502 [[nodiscard]] Iterator end();
503
504 [[nodiscard]] ConstIterator end() const;
505
506 [[nodiscard]] Pointer data();
507
508 [[nodiscard]] ConstPointer data() const;
509
510 Reference operator[](size_t i);
511
512 ConstReference operator[](size_t i) const;
513
514 private:
515 std::vector<T> m_elements;
516 size_t m_rows = 0;
517 size_t m_cols = 0;
518};
519
520template <typename T>
521class Matrix<T, MATRIX_SIZE_DYNAMIC, 1> final
522 : public MatrixExpression<T, MATRIX_SIZE_DYNAMIC, 1,
523 Matrix<T, MATRIX_SIZE_DYNAMIC, 1>>,
524 public MatrixDenseBase<T, MATRIX_SIZE_DYNAMIC, 1,
525 Matrix<T, MATRIX_SIZE_DYNAMIC, 1>>
526{
527 public:
528 using ValueType = T;
529 using Reference = T&;
530 using ConstReference = const T&;
531 using Pointer = T*;
532 using ConstPointer = const T*;
535 using MatrixDenseBase<T, MATRIX_SIZE_DYNAMIC, 1,
537
539
540 explicit Matrix(size_t rows, ConstReference value = ValueType{});
541
542 template <size_t R, size_t C, typename E>
544
545 Matrix(const std::initializer_list<T>& lst);
546
547 explicit Matrix(size_t rows, ConstPointer ptr);
548
549 ~Matrix() = default;
550
552
553 Matrix(Matrix&& other) noexcept;
554
556
558
559 void Fill(const T& val);
560
561 void Fill(const std::function<T(size_t i)>& func);
562
563 void Fill(const std::function<T(size_t i, size_t j)>& func);
564
565 void Swap(Matrix& other);
566
567 void Resize(size_t rows, ConstReference val = ValueType{});
568
569 void AddElement(ConstReference newElem);
570
571 void AddElement(const Matrix& newElems);
572
573 void Clear();
574
575 [[nodiscard]] size_t GetRows() const;
576
577 [[nodiscard]] constexpr size_t GetCols() const;
578
580
581 [[nodiscard]] ConstIterator begin() const;
582
583 [[nodiscard]] Iterator end();
584
585 [[nodiscard]] ConstIterator end() const;
586
587 [[nodiscard]] Pointer data();
588
589 [[nodiscard]] ConstPointer data() const;
590
591 Reference operator[](size_t i);
592
593 ConstReference operator[](size_t i) const;
594
595 private:
596 std::vector<T> m_elements;
597};
598
599template <typename T>
601
602template <typename T>
604
605template <typename T>
607
620
633
646
647template <typename T, size_t Rows>
649
650template <typename T>
652
653template <typename T>
655
656template <typename T>
658
659template <typename T>
661
674
687
700
713
714template <typename T>
716
729
730template <typename T>
732
745
746template <typename T, size_t Rows, size_t Cols>
748{
749 using value = T;
750};
751
752template <typename T, size_t R1, size_t C1, size_t R2, size_t C2, typename M2>
754
755template <typename T, size_t Rows, size_t Cols>
756void operator+=(Matrix<T, Rows, Cols>& a, const T& b);
757
758template <typename T, size_t R1, size_t C1, size_t R2, size_t C2, typename M2>
760
761template <typename T, size_t Rows, size_t Cols>
762void operator-=(Matrix<T, Rows, Cols>& a, const T& b);
763
764template <typename T, size_t R1, size_t C1, size_t R2, size_t C2, typename M2>
766
767template <typename T, size_t R1, size_t C1, size_t R2, size_t C2, typename M2>
769
770template <typename T, size_t Rows, size_t Cols>
771void operator*=(Matrix<T, Rows, Cols>& a, const T& b);
772
773template <typename T, size_t R1, size_t C1, size_t R2, size_t C2, typename M2>
775
776template <typename T, size_t Rows, size_t Cols>
777void operator/=(Matrix<T, Rows, Cols>& a, const T& b);
778
779template <typename T, size_t Rows, size_t Cols, typename M1, typename M2>
780constexpr std::enable_if_t<IsMatrixSizeStatic<Rows, Cols>(), bool> operator==(
783
784template <typename T, size_t R1, size_t C1, size_t R2, size_t C2, typename M1,
785 typename M2>
788
789template <typename T, size_t R1, size_t C1, size_t R2, size_t C2, typename M1,
790 typename M2>
793
794template <typename T, size_t Rows, size_t Cols, typename M1,
795 typename BinaryOperation>
796constexpr std::enable_if_t<TraitIsMatrixSizeStatic<Rows, Cols>::value, T>
799
800template <typename T, size_t Rows, size_t Cols, typename M1>
801constexpr std::enable_if_t<TraitIsMatrixSizeStatic<Rows, Cols>::value, T>
803
804template <typename T, size_t Rows, size_t Cols, typename M1>
805constexpr std::enable_if_t<TraitIsMatrixSizeStatic<Rows, Cols>::value, T>
807
808template <typename T, size_t Rows, size_t Cols, typename M1,
809 typename BinaryOperation>
810constexpr std::enable_if_t<TraitIsMatrixSizeDynamic<Rows, Cols>::value, T>
813
814template <typename T, size_t Rows, size_t Cols, typename M1>
815constexpr std::enable_if_t<TraitIsMatrixSizeDynamic<Rows, Cols>::value, T>
817
818template <typename T, size_t Rows, size_t Cols, typename M1>
819constexpr std::enable_if_t<TraitIsMatrixSizeDynamic<Rows, Cols>::value, T>
821
822template <typename T, size_t Rows, size_t Cols, typename M1>
824 const T& init);
825
826template <typename T, size_t Rows, size_t Cols, typename M1, typename M2,
827 typename M3, typename M4>
828std::enable_if_t<IsMatrixSizeStatic<Rows, Cols>(), Matrix<T, Rows, Cols>>
833} // namespace CubbyFlow
834
836
837#endif
constexpr Matrix(const Matrix &)=default
constexpr Matrix(Matrix &&) noexcept=default
constexpr Matrix(const T &_x)
Definition Matrix.hpp:131
constexpr Matrix()
Definition Matrix.hpp:126
constexpr Matrix(const Matrix &)=default
constexpr Matrix(Matrix &&) noexcept=default
constexpr Matrix()
Definition Matrix.hpp:202
constexpr Matrix(const T &_x, const T &_y)
Definition Matrix.hpp:207
constexpr Matrix()
Definition Matrix.hpp:281
constexpr Matrix(const T &_x, const T &_y, const T &_z)
Definition Matrix.hpp:292
constexpr Matrix(const Matrix &)=default
constexpr Matrix(const Matrix< T, 2, 1 > &_xy, const T &_z)
Definition Matrix.hpp:286
constexpr Matrix(Matrix &&) noexcept=default
constexpr Matrix(const T &_x, const T &_y, const T &_z, const T &_w)
Definition Matrix.hpp:375
constexpr Matrix()
Definition Matrix.hpp:370
constexpr Matrix(const Matrix &)=default
constexpr Matrix(Matrix &&) noexcept=default
Matrix & operator=(Matrix &&other) noexcept
Matrix & operator=(const Matrix &other)
const T & ConstReference
Definition Matrix.hpp:530
const T * ConstPointer
Definition Matrix.hpp:532
Definition MatrixDenseBase.hpp:21
void CopyFrom(const MatrixExpression< T, R, C, E > &expression)
Copies from generic expression.
Definition MatrixDenseBase-Impl.hpp:21
Base class for matrix expression.
Definition MatrixExpression.hpp:94
Definition Matrix.hpp:30
T & Reference
Definition Matrix.hpp:40
ConstPointer ConstIterator
Definition Matrix.hpp:45
constexpr Matrix(Matrix &&) noexcept=default
void Fill(const T &val)
Definition Matrix-Impl.hpp:226
const T * ConstPointer
Definition Matrix.hpp:43
constexpr size_t GetCols() const
Definition Matrix-Impl.hpp:266
Iterator begin()
Definition Matrix-Impl.hpp:272
void Swap(Matrix &other)
Definition Matrix-Impl.hpp:254
T * Pointer
Definition Matrix.hpp:42
Pointer Iterator
Definition Matrix.hpp:44
constexpr size_t GetRows() const
Definition Matrix-Impl.hpp:260
Pointer data()
Definition Matrix-Impl.hpp:298
constexpr Matrix()
Definition Matrix.hpp:47
constexpr Matrix(ConstReference first, Args... rest)
Definition Matrix.hpp:55
Iterator end()
Definition Matrix-Impl.hpp:285
constexpr Matrix(const Matrix &)=default
Reference operator[](size_t i)
Definition Matrix-Impl.hpp:311
const T & ConstReference
Definition Matrix.hpp:41
Definition pybind11Utils.hpp:22
void ElemIDiv(Matrix< T, R1, C1 > &a, const MatrixExpression< T, R2, C2, M2 > &b)
Definition Matrix-Impl.hpp:1356
void Fill(ArrayView< T, N > a, const Vector< size_t, N > &begin, const Vector< size_t, N > &end, const T &val)
Definition ArrayUtils-Impl.hpp:132
void ElemIMul(Matrix< T, R1, C1 > &a, const MatrixExpression< T, R2, C2, M2 > &b)
Definition Matrix-Impl.hpp:1338
constexpr std::enable_if_t< IsMatrixSizeStatic< Rows, Cols >(), bool > operator==(const MatrixExpression< T, Rows, Cols, M1 > &a, const MatrixExpression< T, Rows, Cols, M2 > &b)
Definition Matrix-Impl.hpp:1371
constexpr T Product(const MatrixExpression< T, Rows, Cols, M1 > &a, const T &init)
Definition Matrix-Impl.hpp:1468
std::enable_if_t< std::is_arithmetic< T >::value, T > MonotonicCatmullRom(const T &f0, const T &f1, const T &f2, const T &f3, T t)
Computes monotonic Catmull-Rom interpolation.
Definition MathUtils-Impl.hpp:336
Matrix< T, Rows, 1 > Vector
Definition Matrix.hpp:648
bool operator!=(const MatrixExpression< T, R1, C1, M1 > &a, const MatrixExpression< T, R2, C2, M2 > &b)
Definition Matrix-Impl.hpp:1406
void operator+=(Matrix< T, R1, C1 > &a, const MatrixExpression< T, R2, C2, M2 > &b)
Definition Matrix-Impl.hpp:1305
constexpr std::enable_if_t< TraitIsMatrixSizeStatic< Rows, Cols >::value, T > Accumulate(const MatrixExpression< T, Rows, Cols, M1 > &a, const T &init, BinaryOperation op)
Definition Matrix-Impl.hpp:1415
void operator*=(Matrix< T, R1, C1 > &a, const MatrixExpression< T, R2, C2, M2 > &b)
Definition Matrix-Impl.hpp:1329
void operator/=(Matrix< T, Rows, Cols > &a, const T &b)
Definition Matrix-Impl.hpp:1363
void operator-=(Matrix< T, R1, C1 > &a, const MatrixExpression< T, R2, C2, M2 > &b)
Definition Matrix-Impl.hpp:1317
Returns the type of the value itself.
Definition TypeHelpers.hpp:19