MeshLib Documentation
Loading...
Searching...
No Matches
MRMatrix.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRRectIndexer.h"
4#include <vector>
5
6namespace MR
7{
8
11
14template <typename T>
16{
17public:
18 using ValueType = T;
19 constexpr Matrix() noexcept = default;
20
21 Matrix( size_t numRows, size_t numCols ) : RectIndexer( { (int)numCols, (int)numRows } )
22 {
23 data_.resize( size_ );
24 }
25
27 constexpr T& operator ()( size_t row, size_t col ) noexcept { return data_[toIndex( { (int)col, (int)row } )]; }
28 constexpr T& operator ()( size_t i ) noexcept { return data_[i]; }
29 constexpr const T& operator ()( size_t row, size_t col ) const noexcept { return data_[toIndex( { (int)col, (int)row } )]; }
30 constexpr const T& operator ()( size_t i ) const noexcept { return data_[i]; }
31
32 constexpr Matrix getSubMatrix( size_t startRow, size_t nRow, size_t startCol, size_t nCol )
33 {
34 Matrix res(nRow, nCol);
35 for( size_t r = 0; r < nRow; r++ )
36 {
37 for( size_t c = 0; c < nCol; c++ )
38 {
39 res(r, c) = data_[(startRow + r) * size_t(dims_.x) + startCol + c];
40 }
41 }
42 return res;
43 }
44
46 constexpr Matrix transposed() const
47 {
48 Matrix res( dims_.x, dims_.y );
49 for( size_t r = 0; r < dims_.y; r++ )
50 {
51 for( size_t c = 0; c < dims_.x; c++ )
52 {
53 res(c, r) = (*this)(r, c);
54 }
55 }
56 return res;
57 }
58
59 void fill( T val )
60 {
61 for( auto& elem : data_ )
62 {
63 elem = val;
64 }
65 }
66
67 void clear()
68 {
69 RectIndexer::resize( {0, 0} );
70 data_.clear();
71 }
72
73 size_t getRowsNum() const
74 {
75 return dims_.y;
76 }
77
78 size_t getColsNum() const
79 {
80 return dims_.x;
81 }
82
83 const std::vector<T> & data() const
84 {
85 return data_;
86 }
87
88private:
89 std::vector<T> data_;
90};
91
92} // namespace MR
a class for converting 2D integer coordinates into 1D linear coordinates and backward
Definition MRRectIndexer.h:38
Vector2i dims_
Definition MRRectIndexer.h:57
size_t size_
= dims_.x * dims_.y
Definition MRRectIndexer.h:58
void resize(const Vector2i &dims)
Definition MRRectIndexer.h:66
size_t toIndex(const Vector2i &pos) const
Definition MRRectIndexer.h:79
Definition MRCameraOrientationPlugin.h:8
Definition MRMatrix.h:16
constexpr Matrix() noexcept=default
constexpr Matrix transposed() const
computes transposed matrix
Definition MRMatrix.h:46
const std::vector< T > & data() const
Definition MRMatrix.h:83
constexpr Matrix getSubMatrix(size_t startRow, size_t nRow, size_t startCol, size_t nCol)
Definition MRMatrix.h:32
T ValueType
Definition MRMatrix.h:18
constexpr T & operator()(size_t row, size_t col) noexcept
main access method
Definition MRMatrix.h:27
void clear()
Definition MRMatrix.h:67
size_t getColsNum() const
Definition MRMatrix.h:78
size_t getRowsNum() const
Definition MRMatrix.h:73
void fill(T val)
Definition MRMatrix.h:59