105 template<
typename T,
size_t rowCount,
size_t colCount>
108 static_assert(rowCount > 0 && colCount > 0,
"rowCount and colCount must be > 0");
111 using Storage = std::array<T, rowCount * colCount>;
113 static constexpr bool invertSupported = ((rowCount == 4 && colCount == 4) || (rowCount == 3 && colCount == 3))
114 && (std::is_same<T, float>::value || std::is_same<T, double>::value);
135 static constexpr size_t rows{ rowCount };
140 static constexpr size_t cols{ colCount };
146 explicit Matrix(
const std::array<T, colCount * rowCount> &arrArr)
151 template<
typename Iterator>
154 if(std::distance(beginIt, endIt) !=
rows *
cols)
156 throw std::out_of_range(
157 "Input size does not match matrix size. Expected " + std::to_string(
rows *
cols) +
" items, got "
158 + std::to_string(std::distance(beginIt, endIt)) +
" items.");
161 std::copy(beginIt, endIt, m_mat.begin());
165 explicit Matrix(std::initializer_list<T> values)
170 explicit Matrix(std::initializer_list<std::initializer_list<T>> values)
172 if(values.size() !=
rows)
174 throw std::out_of_range(
175 "Input size does not match matrix size. Expected " + std::to_string(
rows) +
"rows, got "
176 + std::to_string(values.size()) +
" rows");
179 auto iterator =
begin();
180 for(
const auto &row : values)
182 if(row.size() !=
cols)
184 throw std::out_of_range(
185 "Input size does not match matrix size. Expected all rows to be of size " + std::to_string(
cols)
186 +
", got row with " + std::to_string(row.size()) +
" values");
188 for(
const auto &value : row)
190 *(iterator++) = value;
198 return m_mat.begin();
210 return m_mat.begin();
222 return m_mat.cbegin();
232 T &
at(
size_t row,
size_t col)
234 throwIfOutOfBounds(row, col);
235 return m_mat[row *
cols + col];
239 const T &
at(
size_t row,
size_t col)
const
241 throwIfOutOfBounds(row, col);
242 return m_mat[row *
cols + col];
248 return m_mat[row *
cols + col];
254 return m_mat[row *
cols + col];
274 template<typename Q = T, typename = typename std::enable_if<invertSupported, Q>::type>
281 std::stringstream ss;
283 for(
size_t row = 0; row < rowCount; row++)
285 ss << (row == 0 ?
"[" :
"\n [");
286 for(
size_t col = 0; col < colCount; col++)
288 const auto value{ m_mat[row * colCount + col] };
289 ss << (value >= 0 ?
" " :
"") << std::to_string(value);
290 ss << (col == colCount - 1 ?
"" :
", ");
292 ss << (row == rowCount - 1 ?
"]" :
"], ");
299 static void throwIfOutOfBounds(
size_t row,
size_t col)
303 throw std::out_of_range(
304 "Trying to access row with index " + std::to_string(row) +
", but allowed range is [0, "
305 + std::to_string(
rows - 1) +
"]");
309 throw std::out_of_range(
310 "Trying to access column with index " + std::to_string(col) +
", but allowed range is [0, "
311 + std::to_string(
cols - 1) +
"]");
319 template<
typename T,
size_t rowCount,
size_t colCount>
#define ZIVID_CORE_EXPORT
Definition: CoreExport.h:101
A fixed size matrix in row major order
Definition: Matrix.h:107
static constexpr size_t rows
The number of rows in the matrix
Definition: Matrix.h:135
T ValueType
The type stored in the matrix
Definition: Matrix.h:120
T * data()
Pointer to the underlying data
Definition: Matrix.h:258
ConstIterator begin() const
Iterator to the beginning of the matrix
Definition: Matrix.h:208
typename Storage::iterator Iterator
The matrix iterator type for mutable access. It iterates over individual matrix elements in row major...
Definition: Matrix.h:125
Matrix(Iterator beginIt, Iterator endIt)
Constructor
Definition: Matrix.h:152
Matrix(std::initializer_list< T > values)
Constructor
Definition: Matrix.h:165
Matrix(const std::array< T, colCount *rowCount > &arrArr)
Constructor
Definition: Matrix.h:146
const T * data() const
Pointer to the underlying data
Definition: Matrix.h:264
static constexpr size_t cols
The number of columns in the matrix
Definition: Matrix.h:140
Iterator begin()
Iterator to the beginning of the matrix
Definition: Matrix.h:196
Matrix()=default
Constructor
ConstIterator cbegin() const
Iterator to the beginning of the matrix
Definition: Matrix.h:220
T & at(size_t row, size_t col)
Access specified element with bounds checking
Definition: Matrix.h:232
std::string toString() const
Get string representation of the Matrix
Definition: Matrix.h:279
ConstIterator end() const
Iterator to the end of the matrix
Definition: Matrix.h:214
Matrix(std::initializer_list< std::initializer_list< T > > values)
Constructor
Definition: Matrix.h:170
const T & at(size_t row, size_t col) const
Access specified element with bounds checking
Definition: Matrix.h:239
const T & operator()(size_t row, size_t col) const
Access specified element without bounds checking
Definition: Matrix.h:252
Iterator end()
Iterator to the end of the matrix
Definition: Matrix.h:202
ZIVID_CORE_EXPORT Matrix inverse() const
Get the inverse of this matrix
ConstIterator cend() const
Iterator to the end of the matrix
Definition: Matrix.h:226
T & operator()(size_t row, size_t col)
Access specified element without bounds checking
Definition: Matrix.h:246
typename Storage::const_iterator ConstIterator
The matrix iterator type for immutable access. It iterates over individual matrix elements in row maj...
Definition: Matrix.h:130
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