59 template<
typename T,
size_t rowCount,
size_t colCount>
62 static_assert(rowCount > 0 && colCount > 0,
"rowCount and colCount must be > 0");
65 using Storage = std::array<T, rowCount * colCount>;
86 static constexpr
size_t rows{ rowCount };
91 static constexpr
size_t cols{ colCount };
97 explicit Matrix(
const std::array<T, colCount * rowCount> &arrArr)
102 template<
typename Iterator>
105 if(std::distance(beginIt, endIt) !=
rows *
cols)
107 throw std::out_of_range(
"Input size does not match matrix size. Expected " + std::to_string(
rows *
cols)
108 +
" items, got " + std::to_string(std::distance(beginIt, endIt)) +
" items.");
111 std::copy(beginIt, endIt, m_mat.begin());
115 explicit Matrix(std::initializer_list<T> values)
120 explicit Matrix(std::initializer_list<std::initializer_list<T>> values)
122 if(values.size() !=
rows)
124 throw std::out_of_range(
"Input size does not match matrix size. Expected " + std::to_string(
rows)
125 +
"rows, got " + std::to_string(values.size()) +
" rows");
128 auto iterator =
begin();
129 for(
const auto &row : values)
131 if(row.size() !=
cols)
133 throw std::out_of_range(
"Input size does not match matrix size. Expected all rows to be of size "
134 + std::to_string(
cols) +
", got row with " + std::to_string(row.size())
137 for(
const auto &value : row)
139 *(iterator++) = value;
147 return m_mat.begin();
159 return m_mat.cbegin();
169 T &
at(
size_t row,
size_t col)
171 throwIfOutOfBounds(row, col);
172 return m_mat[row *
cols + col];
176 const T &
at(
size_t row,
size_t col)
const
178 throwIfOutOfBounds(row, col);
179 return m_mat[row *
cols + col];
185 return m_mat[row *
cols + col];
191 return m_mat[row *
cols + col];
210 std::stringstream ss;
212 for(
size_t row = 0; row < rowCount; row++)
214 ss << (row == 0 ?
"[" :
"\n [");
215 for(
size_t col = 0; col < colCount; col++)
217 const auto value{ m_mat[row * colCount + col] };
218 ss << (value >= 0 ?
" " :
"") << std::to_string(value);
219 ss << (col == colCount - 1 ?
"" :
", ");
221 ss << (row == rowCount - 1 ?
"]" :
"], ");
228 static void throwIfOutOfBounds(
size_t row,
size_t col)
232 throw std::out_of_range(
"Trying to access row with index " + std::to_string(row)
233 +
", but allowed range is [0, " + std::to_string(
rows - 1) +
"]");
237 throw std::out_of_range(
"Trying to access column with index " + std::to_string(col)
238 +
", but allowed range is [0, " + std::to_string(
cols - 1) +
"]");
246 template<
typename T,
size_t rowCount,
size_t colCount>
A fixed size matrix in row major order
Definition: Matrix.h:61
static constexpr size_t rows
The number of rows in the matrix
Definition: Matrix.h:86
T ValueType
The type stored in the matrix
Definition: Matrix.h:71
const T & operator()(size_t row, size_t col) const
Access specified element without bounds checking
Definition: Matrix.h:189
T * data()
Pointer to the underlying data
Definition: Matrix.h:195
typename Storage::iterator Iterator
The matrix iterator type for mutable access. It iterates over individual matrix elements in row major...
Definition: Matrix.h:76
Matrix(Iterator beginIt, Iterator endIt)
Constructor
Definition: Matrix.h:103
Matrix(std::initializer_list< T > values)
Constructor
Definition: Matrix.h:115
Matrix(const std::array< T, colCount *rowCount > &arrArr)
Constructor
Definition: Matrix.h:97
static constexpr size_t cols
The number of columns in the matrix
Definition: Matrix.h:91
Iterator begin()
Iterator to the beginning of the matrix
Definition: Matrix.h:145
Matrix()=default
Constructor
ConstIterator cbegin() const
Iterator to the beginning of the matrix
Definition: Matrix.h:157
std::string toString() const
Get string representation of the Matrix
Definition: Matrix.h:208
const T & at(size_t row, size_t col) const
Access specified element with bounds checking
Definition: Matrix.h:176
const T * data() const
Pointer to the underlying data
Definition: Matrix.h:201
Matrix(std::initializer_list< std::initializer_list< T >> values)
Constructor
Definition: Matrix.h:120
T & operator()(size_t row, size_t col)
Access specified element without bounds checking
Definition: Matrix.h:183
Iterator end()
Iterator to the end of the matrix
Definition: Matrix.h:151
ConstIterator cend() const
Iterator to the end of the matrix
Definition: Matrix.h:163
T & at(size_t row, size_t col)
Access specified element with bounds checking
Definition: Matrix.h:169
typename Storage::const_iterator ConstIterator
The matrix iterator type for immutable access. It iterates over individual matrix elements in row maj...
Definition: Matrix.h:81
The main Zivid namespace. All Zivid code is found here
Definition: Application.h:55
std::ostream & operator<<(std::ostream &stream, const Array2D< T > &array)
Serialize array information to a stream
Definition: Array2D.h:170