15#include <libcamera/base/span.h>
25bool matrixInvert(Span<const T> dataIn, Span<T> dataOut,
unsigned int dim,
26 Span<T> scratchBuffer, Span<unsigned int> swapBuffer);
29template<
typename T,
unsigned int Rows,
unsigned int Cols>
32 static_assert(std::is_arithmetic_v<T>,
"Matrix type must be arithmetic");
41 std::copy(
data.begin(),
data.end(), data_.begin());
46 std::copy(
data.begin(),
data.end(), data_.begin());
52 for (
size_t i = 0; i < std::min(Rows, Cols); i++)
53 ret[i][i] =
static_cast<T
>(1);
61 std::stringstream out;
64 for (
unsigned int i = 0; i < Rows; i++) {
66 for (
unsigned int j = 0; j < Cols; j++) {
68 out << ((j + 1 < Cols) ?
", " :
" ");
70 out << ((i + 1 < Rows) ?
"], " :
"]");
77 constexpr Span<const T, Rows * Cols>
data()
const {
return data_; }
81 return Span<const T, Cols>{ &data_.data()[i * Cols], Cols };
86 return Span<T, Cols>{ &data_.data()[i * Cols], Cols };
90 template<
typename U, std::enable_if_t<std::is_arithmetic_v<U>>>
96 for (
unsigned int i = 0; i < Rows * Cols; i++)
103 static_assert(Rows == Cols,
"Matrix must be square");
106 std::array<T, Rows * Cols * 2> scratchBuffer;
107 std::array<unsigned int, Rows> swapBuffer;
108 bool res = matrixInvert(Span<const T>(data_),
111 Span<T>(scratchBuffer),
112 Span<unsigned int>(swapBuffer));
124 std::array<T, Rows * Cols> data_ = {};
128template<
typename T,
typename U,
unsigned int Rows,
unsigned int Cols,
129 std::enable_if_t<std::is_arithmetic_v<T>> * =
nullptr>
131template<
typename T,
typename U,
unsigned int Rows,
unsigned int Cols>
137 for (
unsigned int i = 0; i < Rows; i++) {
138 for (
unsigned int j = 0; j < Cols; j++)
139 result[i][j] = d * m[i][j];
146template<
typename T,
typename U,
unsigned int Rows,
unsigned int Cols,
147 std::enable_if_t<std::is_arithmetic_v<T>> * =
nullptr>
149template<
typename T,
typename U,
unsigned int Rows,
unsigned int Cols>
156template<
typename T1,
unsigned int R1,
unsigned int C1,
typename T2,
unsigned int R2,
unsigned int C2>
160 static_assert(C1 == R2,
"Matrix dimensions must match for multiplication");
163 for (
unsigned int i = 0; i < R1; i++) {
164 for (
unsigned int j = 0; j < C2; j++) {
165 std::common_type_t<T1, T2> sum = 0;
167 for (
unsigned int k = 0; k < C1; k++)
168 sum += m1[i][k] * m2[k][j];
177template<
typename T,
unsigned int Rows,
unsigned int Cols>
182 for (
unsigned int i = 0; i < Rows; i++) {
183 for (
unsigned int j = 0; j < Cols; j++)
184 result[i][j] = m1[i][j] + m2[i][j];
191bool matrixValidateYaml(
const YamlObject &obj,
unsigned int size);
195template<
typename T,
unsigned int Rows,
unsigned int Cols>
196std::ostream &
operator<<(std::ostream &out,
const Matrix<T, Rows, Cols> &m)
202template<
typename T,
unsigned int Rows,
unsigned int Cols>
203struct YamlObject::Getter<
Matrix<T, Rows, Cols>> {
204 std::optional<Matrix<T, Rows, Cols>>
get(
const YamlObject &obj)
const
206 if (!matrixValidateYaml(obj, Rows * Cols))
209 Matrix<T, Rows, Cols> matrix;
210 T *data = &matrix[0][0];
213 for (
const YamlObject &entry : obj.asList()) {
214 const auto value = entry.get<T>();
Matrix class.
Definition matrix.h:31
Matrix(const std::array< T, Rows *Cols > &data)
Construct a matrix from supplied data.
Definition matrix.h:39
Matrix< T, Rows, Cols > & operator*=(U d)
Multiply the matrix by a scalar in-place.
Definition matrix.h:94
constexpr Span< const T, Cols > operator[](size_t i) const
Index to a row in the matrix.
Definition matrix.h:79
static constexpr Matrix identity()
Construct an identity matrix.
Definition matrix.h:49
const std::string toString() const
Assemble and return a string describing the matrix.
Definition matrix.h:59
Matrix< T, Rows, Cols > inverse(bool *ok=nullptr) const
Compute the inverse of the matrix.
Definition matrix.h:101
constexpr Span< T, Cols > operator[](size_t i)
Index to a row in the matrix.
Definition matrix.h:84
constexpr Span< const T, Rows *Cols > data() const
Access the matrix data as a linear array.
Definition matrix.h:77
Matrix(const Span< const T, Rows *Cols > data)
Construct a matrix from supplied data.
Definition matrix.h:44
constexpr Matrix()
Construct a zero matrix.
Definition matrix.h:35
std::optional< T > get() const
Parse the YamlObject as a T value.
Definition yaml_parser.h:175
#define LOG_DECLARE_CATEGORY(name)
Declare a category of log messages.
Definition log.h:51
Top-level libcamera namespace.
Definition backtrace.h:17
std::ostream & operator<<(std::ostream &out, const Point &p)
Insert a text representation of a Point into an output stream.
Definition geometry.cpp:91
Matrix< U, Rows, Cols > operator*(T d, const Matrix< U, Rows, Cols > &m)
Multiply the matrix by a scalar.
Definition matrix.h:133
constexpr Matrix< T, Rows, Cols > operator+(const Matrix< T, Rows, Cols > &m1, const Matrix< T, Rows, Cols > &m2)
Matrix addition.
Definition matrix.h:178