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>;
67 static constexpr bool invertSupported = ((rowCount == 4 && colCount == 4) || (rowCount == 3 && colCount == 3))
68 && (std::is_same<T, float>::value || std::is_same<T, double>::value);
70 static constexpr bool identitySupported = (rowCount == colCount) && (std::is_arithmetic<T>::value);
91 static constexpr size_t rows{ rowCount };
96 static constexpr size_t cols{ colCount };
108 explicit Matrix(
const std::array<T, colCount * rowCount> &arrArr)
119 template<
typename Iterator>
122 if(std::distance(beginIt, endIt) !=
rows *
cols)
124 throw std::out_of_range(
125 "Matrix constructor got input that does not match matrix size. Expected "
126 + std::to_string(
rows *
cols) +
" items, got " + std::to_string(std::distance(beginIt, endIt))
130 std::copy(beginIt, endIt, m_mat.begin());
134 explicit Matrix(std::initializer_list<T> values)
139 explicit Matrix(std::initializer_list<std::initializer_list<T>> values)
141 if(values.size() !=
rows)
143 throw std::out_of_range(
144 "Matrix constructor got input that does not match matrix size. Expected " + std::to_string(
rows)
145 +
" rows, got " + std::to_string(values.size()) +
" rows");
148 auto iterator =
begin();
149 for(
const auto &row : values)
151 if(row.size() !=
cols)
153 throw std::out_of_range(
154 "Matrix constructor got input that does not match matrix size. Expected all rows to be of size "
155 + std::to_string(
cols) +
", got row with " + std::to_string(row.size()) +
" values");
157 std::copy(std::begin(row), std::end(row), iterator);
158 std::advance(iterator,
cols);
164 template<
typename Q = T,
typename =
typename std::enable_if_t<
identitySupported, Q>>
170 matrix(i, i) = T{ 1 };
178 return m_mat.begin();
190 return m_mat.begin();
202 return m_mat.cbegin();
212 T &
at(
size_t row,
size_t col)
214 throwIfOutOfBounds(row, col);
215 return m_mat[(row *
cols) + col];
219 const T &
at(
size_t row,
size_t col)
const
221 throwIfOutOfBounds(row, col);
222 return m_mat[(row *
cols) + col];
228 return m_mat[(row *
cols) + col];
234 return m_mat[(row *
cols) + col];
253 void save(
const std::string &fileName)
const;
260 void load(
const std::string &fileName);
267 template<
typename Q = T,
typename =
typename std::enable_if_t<invertSupported, Q>>
274 std::stringstream ss;
276 for(
size_t row = 0; row < rowCount; row++)
278 ss << (row == 0 ?
"[" :
"\n [");
279 for(
size_t col = 0; col < colCount; col++)
281 const auto value{ m_mat[(row * colCount) + col] };
282 ss << (value >= 0 ?
" " :
"") << std::to_string(value);
283 ss << (col == colCount - 1 ?
"" :
", ");
285 ss << (row == rowCount - 1 ?
"]" :
"], ");
292 static void throwIfOutOfBounds(
size_t row,
size_t col)
296 throw std::out_of_range{
"Trying to access row with index " + std::to_string(row)
297 +
", but allowed range is [0, " + std::to_string(
rows - 1) +
"]" };
301 throw std::out_of_range{
"Trying to access column with index " + std::to_string(col)
302 +
", but allowed range is [0, " + std::to_string(
cols - 1) +
"]" };
310 template<
typename T,
size_t rowCount,
size_t colCount>
341 saveFloatMatrix(
const float *data,
size_t rowCount,
size_t colCount,
const std::string &fileName);
344 loadFloatMatrix(
float *data,
size_t rowCount,
size_t colCount,
const std::string &fileName);
357 template<
typename T,
size_t rowCount,
size_t colCount>
360 static_assert(std::is_same<T, float>::value,
"Saving matrices of this type is not supported");
362 Detail::saveFloatMatrix(data(), rowCount, colCount, fileName);
365 template<
typename T,
size_t rowCount,
size_t colCount>
368 static_assert(std::is_same<T, float>::value,
"Loading matrices of this type is not supported");
370 Detail::loadFloatMatrix(data(), rowCount, colCount, fileName);
#define ZIVID_UTILS_EXPORT
Definition UtilsExport.h:56
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:91
T ValueType
The type stored in the matrix.
Definition Matrix.h:76
T * data()
Pointer to the underlying data.
Definition Matrix.h:238
ZIVID_UTILS_EXPORT Matrix inverse() const
Get the inverse of this matrix.
void load(const std::string &fileName)
Load the matrix from the given file.
ConstIterator begin() const
Iterator to the beginning of the matrix.
Definition Matrix.h:188
void save(const std::string &fileName) const
Save the matrix to the given file.
typename Storage::iterator Iterator
The matrix iterator type for mutable access. It iterates over individual matrix elements in row major...
Definition Matrix.h:81
static Matrix identity()
Get the identity matrix of given type and size.
Definition Matrix.h:165
Matrix(Iterator beginIt, Iterator endIt)
Constructor.
Definition Matrix.h:120
Matrix(std::initializer_list< T > values)
Constructor.
Definition Matrix.h:134
Matrix(const std::array< T, colCount *rowCount > &arrArr)
Constructor.
Definition Matrix.h:108
Matrix(const std::string &fileName)
Load the matrix from a file.
Definition Matrix.h:102
const T * data() const
Pointer to the underlying data.
Definition Matrix.h:244
static constexpr size_t cols
The number of columns in the matrix.
Definition Matrix.h:96
Iterator begin()
Iterator to the beginning of the matrix.
Definition Matrix.h:176
Matrix()=default
Constructor.
ConstIterator cbegin() const
Iterator to the beginning of the matrix.
Definition Matrix.h:200
T & at(size_t row, size_t col)
Access specified element with bounds checking.
Definition Matrix.h:212
std::string toString() const
Get string representation of the Matrix.
Definition Matrix.h:272
ConstIterator end() const
Iterator to the end of the matrix.
Definition Matrix.h:194
Matrix(std::initializer_list< std::initializer_list< T > > values)
Constructor.
Definition Matrix.h:139
const T & at(size_t row, size_t col) const
Access specified element with bounds checking.
Definition Matrix.h:219
const T & operator()(size_t row, size_t col) const
Access specified element without bounds checking.
Definition Matrix.h:232
Iterator end()
Iterator to the end of the matrix.
Definition Matrix.h:182
Matrix(const Matrix< U, rowCount, colCount > &sourceMatrix)
Constructor.
Definition Matrix.h:114
ConstIterator cend() const
Iterator to the end of the matrix.
Definition Matrix.h:206
T & operator()(size_t row, size_t col)
Access specified element without bounds checking.
Definition Matrix.h:226
typename Storage::const_iterator ConstIterator
The matrix iterator type for immutable access. It iterates over individual matrix elements in row maj...
Definition Matrix.h:86
The main Zivid namespace. All Zivid code is found here.
Definition Application.h:84
std::ostream & operator<<(std::ostream &stream, const Array2D< T > &array)
Serialize array information to a stream.
Definition Array2D.h:283