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 };
152 explicit Matrix(
const std::array<T, colCount * rowCount> &arrArr)
157 template<
typename Iterator>
160 if(std::distance(beginIt, endIt) !=
rows *
cols)
162 throw std::out_of_range(
163 "Matrix constructor got input that does not match matrix size. Expected "
164 + std::to_string(
rows *
cols) +
" items, got " + std::to_string(std::distance(beginIt, endIt))
168 std::copy(beginIt, endIt, m_mat.begin());
172 explicit Matrix(std::initializer_list<T> values)
177 explicit Matrix(std::initializer_list<std::initializer_list<T>> values)
179 if(values.size() !=
rows)
181 throw std::out_of_range(
182 "Matrix constructor got input that does not match matrix size. Expected " + std::to_string(
rows)
183 +
" rows, got " + std::to_string(values.size()) +
" rows");
186 auto iterator =
begin();
187 for(
const auto &row : values)
189 if(row.size() !=
cols)
191 throw std::out_of_range(
192 "Matrix constructor got input that does not match matrix size. Expected all rows to be of size "
193 + std::to_string(
cols) +
", got row with " + std::to_string(row.size()) +
" values");
195 for(
const auto &value : row)
197 *(iterator++) = value;
205 return m_mat.begin();
217 return m_mat.begin();
229 return m_mat.cbegin();
239 T &
at(
size_t row,
size_t col)
241 throwIfOutOfBounds(row, col);
242 return m_mat[row *
cols + col];
246 const T &
at(
size_t row,
size_t col)
const
248 throwIfOutOfBounds(row, col);
249 return m_mat[row *
cols + col];
255 return m_mat[row *
cols + col];
261 return m_mat[row *
cols + col];
280 void save(
const std::string &fileName)
const;
287 void load(
const std::string &fileName);
294 template<typename Q = T, typename = typename std::enable_if<invertSupported, Q>::type>
301 std::stringstream ss;
303 for(
size_t row = 0; row < rowCount; row++)
305 ss << (row == 0 ?
"[" :
"\n [");
306 for(
size_t col = 0; col < colCount; col++)
308 const auto value{ m_mat[row * colCount + col] };
309 ss << (value >= 0 ?
" " :
"") << std::to_string(value);
310 ss << (col == colCount - 1 ?
"" :
", ");
312 ss << (row == rowCount - 1 ?
"]" :
"], ");
319 static void throwIfOutOfBounds(
size_t row,
size_t col)
323 throw std::out_of_range{
"Trying to access row with index " + std::to_string(row)
324 +
", but allowed range is [0, " + std::to_string(
rows - 1) +
"]" };
328 throw std::out_of_range{
"Trying to access column with index " + std::to_string(col)
329 +
", but allowed range is [0, " + std::to_string(
cols - 1) +
"]" };
337 template<
typename T,
size_t rowCount,
size_t colCount>
368 saveFloatMatrix(
const float *data,
size_t rowCount,
size_t colCount,
const std::string &fileName);
371 loadFloatMatrix(
float *data,
size_t rowCount,
size_t colCount,
const std::string &fileName);
376 extern template ZIVID_CORE_EXPORT Matrix<float, 4, 4> Matrix<float, 4, 4>::inverse<float, float>()
const;
378 extern template ZIVID_CORE_EXPORT Matrix<double, 4, 4> Matrix<double, 4, 4>::inverse<double, double>()
const;
380 extern template ZIVID_CORE_EXPORT Matrix<float, 3, 3> Matrix<float, 3, 3>::inverse<float, float>()
const;
382 extern template ZIVID_CORE_EXPORT Matrix<double, 3, 3> Matrix<double, 3, 3>::inverse<double, double>()
const;
384 template<
typename T,
size_t rowCount,
size_t colCount>
387 static_assert(std::is_same<T, float>::value,
"Saving matrices of this type is not supported");
389 Detail::saveFloatMatrix(data(), rowCount, colCount, fileName);
392 template<
typename T,
size_t rowCount,
size_t colCount>
395 static_assert(std::is_same<T, float>::value,
"Loading matrices of this type is not supported");
397 Detail::loadFloatMatrix(data(), rowCount, colCount, fileName);
#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:265
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:215
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:125
Matrix(Iterator beginIt, Iterator endIt)
Constructor
Definition: Matrix.h:158
Matrix(std::initializer_list< T > values)
Constructor
Definition: Matrix.h:172
Matrix(const std::array< T, colCount *rowCount > &arrArr)
Constructor
Definition: Matrix.h:152
Matrix(const std::string &fileName)
Load the matrix from a file
Definition: Matrix.h:146
const T * data() const
Pointer to the underlying data
Definition: Matrix.h:271
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:203
Matrix()=default
Constructor
ConstIterator cbegin() const
Iterator to the beginning of the matrix
Definition: Matrix.h:227
T & at(size_t row, size_t col)
Access specified element with bounds checking
Definition: Matrix.h:239
std::string toString() const
Get string representation of the Matrix
Definition: Matrix.h:299
ConstIterator end() const
Iterator to the end of the matrix
Definition: Matrix.h:221
Matrix(std::initializer_list< std::initializer_list< T > > values)
Constructor
Definition: Matrix.h:177
const T & at(size_t row, size_t col) const
Access specified element with bounds checking
Definition: Matrix.h:246
const T & operator()(size_t row, size_t col) const
Access specified element without bounds checking
Definition: Matrix.h:259
Iterator end()
Iterator to the end of the matrix
Definition: Matrix.h:209
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:233
T & operator()(size_t row, size_t col)
Access specified element without bounds checking
Definition: Matrix.h:253
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:243