103 template<
typename T,
size_t rowCount,
size_t colCount>
106 static_assert(rowCount > 0 && colCount > 0,
"rowCount and colCount must be > 0");
109 using Storage = std::array<T, rowCount * colCount>;
130 static constexpr size_t rows{ rowCount };
135 static constexpr size_t cols{ colCount };
141 explicit Matrix(
const std::array<T, colCount * rowCount> &arrArr)
146 template<
typename Iterator>
149 if(std::distance(beginIt, endIt) !=
rows *
cols)
151 throw std::out_of_range(
152 "Input size does not match matrix size. Expected " + std::to_string(
rows *
cols) +
" items, got "
153 + std::to_string(std::distance(beginIt, endIt)) +
" items.");
156 std::copy(beginIt, endIt, m_mat.begin());
160 explicit Matrix(std::initializer_list<T> values)
165 explicit Matrix(std::initializer_list<std::initializer_list<T>> values)
167 if(values.size() !=
rows)
169 throw std::out_of_range(
170 "Input size does not match matrix size. Expected " + std::to_string(
rows) +
"rows, got "
171 + std::to_string(values.size()) +
" rows");
174 auto iterator =
begin();
175 for(
const auto &row : values)
177 if(row.size() !=
cols)
179 throw std::out_of_range(
180 "Input size does not match matrix size. Expected all rows to be of size " + std::to_string(
cols)
181 +
", got row with " + std::to_string(row.size()) +
" values");
183 for(
const auto &value : row)
185 *(iterator++) = value;
193 return m_mat.begin();
205 return m_mat.cbegin();
215 T &
at(
size_t row,
size_t col)
217 throwIfOutOfBounds(row, col);
218 return m_mat[row *
cols + col];
222 const T &
at(
size_t row,
size_t col)
const
224 throwIfOutOfBounds(row, col);
225 return m_mat[row *
cols + col];
231 return m_mat[row *
cols + col];
237 return m_mat[row *
cols + col];
256 std::stringstream ss;
258 for(
size_t row = 0; row < rowCount; row++)
260 ss << (row == 0 ?
"[" :
"\n [");
261 for(
size_t col = 0; col < colCount; col++)
263 const auto value{ m_mat[row * colCount + col] };
264 ss << (value >= 0 ?
" " :
"") << std::to_string(value);
265 ss << (col == colCount - 1 ?
"" :
", ");
267 ss << (row == rowCount - 1 ?
"]" :
"], ");
274 static void throwIfOutOfBounds(
size_t row,
size_t col)
278 throw std::out_of_range(
279 "Trying to access row with index " + std::to_string(row) +
", but allowed range is [0, "
280 + std::to_string(
rows - 1) +
"]");
284 throw std::out_of_range(
285 "Trying to access column with index " + std::to_string(col) +
", but allowed range is [0, "
286 + std::to_string(
cols - 1) +
"]");
294 template<
typename T,
size_t rowCount,
size_t colCount>
A fixed size matrix in row major order
Definition: Matrix.h:105
static constexpr size_t rows
The number of rows in the matrix
Definition: Matrix.h:130
T ValueType
The type stored in the matrix
Definition: Matrix.h:115
T * data()
Pointer to the underlying data
Definition: Matrix.h:241
typename Storage::iterator Iterator
The matrix iterator type for mutable access. It iterates over individual matrix elements in row major...
Definition: Matrix.h:120
Matrix(Iterator beginIt, Iterator endIt)
Constructor
Definition: Matrix.h:147
Matrix(std::initializer_list< T > values)
Constructor
Definition: Matrix.h:160
Matrix(const std::array< T, colCount *rowCount > &arrArr)
Constructor
Definition: Matrix.h:141
const T * data() const
Pointer to the underlying data
Definition: Matrix.h:247
static constexpr size_t cols
The number of columns in the matrix
Definition: Matrix.h:135
Iterator begin()
Iterator to the beginning of the matrix
Definition: Matrix.h:191
Matrix()=default
Constructor
ConstIterator cbegin() const
Iterator to the beginning of the matrix
Definition: Matrix.h:203
T & at(size_t row, size_t col)
Access specified element with bounds checking
Definition: Matrix.h:215
std::string toString() const
Get string representation of the Matrix
Definition: Matrix.h:254
Matrix(std::initializer_list< std::initializer_list< T > > values)
Constructor
Definition: Matrix.h:165
const T & at(size_t row, size_t col) const
Access specified element with bounds checking
Definition: Matrix.h:222
const T & operator()(size_t row, size_t col) const
Access specified element without bounds checking
Definition: Matrix.h:235
Iterator end()
Iterator to the end of the matrix
Definition: Matrix.h:197
ConstIterator cend() const
Iterator to the end of the matrix
Definition: Matrix.h:209
T & operator()(size_t row, size_t col)
Access specified element without bounds checking
Definition: Matrix.h:229
typename Storage::const_iterator ConstIterator
The matrix iterator type for immutable access. It iterates over individual matrix elements in row maj...
Definition: Matrix.h:125
The main Zivid namespace. All Zivid code is found here
Definition: Application.h:99
std::ostream & operator<<(std::ostream &stream, const Array2D< T > &array)
Serialize array information to a stream
Definition: Array2D.h:214