25bool matrixInvert(std::span<const T> dataIn, std::span<T> dataOut,
unsigned int dim,
26 std::span<T> scratchBuffer, std::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 std::span<const T, Rows * Cols>
data()
const {
return data_; }
79 constexpr std::span<T, Rows * Cols>
data() {
return data_; }
81 constexpr std::span<const T, Cols>
operator[](
size_t i)
const
83 return std::span<const T, Cols>{ &data_.data()[i * Cols], Cols };
88 return std::span<T, Cols>{ &data_.data()[i * Cols], Cols };
92 template<
typename U, std::enable_if_t<std::is_arithmetic_v<U>>>
98 for (
unsigned int i = 0; i < Rows * Cols; i++)
105 static_assert(Rows == Cols,
"Matrix must be square");
108 std::array<T, Rows * Cols * 2> scratchBuffer;
109 std::array<unsigned int, Rows> swapBuffer;
110 bool res = matrixInvert(std::span<const T>(data_),
113 std::span<T>(scratchBuffer),
114 std::span<unsigned int>(swapBuffer));
120 template<
typename U = T>
124 static_assert(std::is_convertible_v<T, U>);
127 std::span<U, Rows * Cols>
data = transposed.
data();
129 for (
unsigned int r = 0; r < Rows; ++r) {
130 for (
unsigned int c = 0; c < Cols; ++c)
131 data[c * Rows + r] = data_[r * Cols + c];
143 std::array<T, Rows * Cols> data_ = {};
147template<
typename T,
typename U,
unsigned int Rows,
unsigned int Cols,
148 std::enable_if_t<std::is_arithmetic_v<T>> * =
nullptr>
150template<
typename T,
typename U,
unsigned int Rows,
unsigned int Cols>
156 for (
unsigned int i = 0; i < Rows; i++) {
157 for (
unsigned int j = 0; j < Cols; j++)
158 result[i][j] = d * m[i][j];
165template<
typename T,
typename U,
unsigned int Rows,
unsigned int Cols,
166 std::enable_if_t<std::is_arithmetic_v<T>> * =
nullptr>
168template<
typename T,
typename U,
unsigned int Rows,
unsigned int Cols>
175template<
typename T1,
unsigned int R1,
unsigned int C1,
typename T2,
unsigned int R2,
unsigned int C2>
179 static_assert(C1 == R2,
"Matrix dimensions must match for multiplication");
182 for (
unsigned int i = 0; i < R1; i++) {
183 for (
unsigned int j = 0; j < C2; j++) {
184 std::common_type_t<T1, T2> sum = 0;
186 for (
unsigned int k = 0; k < C1; k++)
187 sum += m1[i][k] * m2[k][j];
196template<
typename T,
unsigned int Rows,
unsigned int Cols>
201 for (
unsigned int i = 0; i < Rows; i++) {
202 for (
unsigned int j = 0; j < Cols; j++)
203 result[i][j] = m1[i][j] + m2[i][j];
210bool matrixValidateValueNode(
const ValueNode &obj,
unsigned int size);
212template<
typename T,
unsigned int Rows,
unsigned int Cols>
213std::ostream &
operator<<(std::ostream &out,
const Matrix<T, Rows, Cols> &m)
219template<
typename T,
unsigned int Rows,
unsigned int Cols>
220struct ValueNode::Accessor<Matrix<T, Rows, Cols>> {
221 std::optional<Matrix<T, Rows, Cols>>
get(
const ValueNode &obj)
const
223 if (!matrixValidateValueNode(obj, Rows * Cols))
226 Matrix<T, Rows, Cols> matrix;
227 T *data = &matrix[0][0];
230 for (
const ValueNode &entry : obj.
asList()) {
231 const auto value = entry.get<T>();
Matrix class.
Definition matrix.h:31
static constexpr Matrix identity()
Construct an identity matrix.
Definition matrix.h:49
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:96
Matrix< T, Rows, Cols > inverse(bool *ok=nullptr) const
Compute the inverse of the matrix.
Definition matrix.h:103
constexpr Matrix()
Construct a zero matrix.
Definition matrix.h:35
constexpr Matrix< U, Cols, Rows > transpose() const
Compute the transpose of the matrix.
Definition matrix.h:122
constexpr std::span< T, Cols > operator[](size_t i)
Index to a row in the matrix.
Definition matrix.h:86
std::string toString() const
Assemble and return a string describing the matrix.
Definition matrix.h:59
constexpr std::span< const T, Cols > operator[](size_t i) const
Index to a row in the matrix.
Definition matrix.h:81
constexpr std::span< T, Rows *Cols > data()
Access the matrix data as a linear array.
Definition matrix.h:79
constexpr std::span< const T, Rows *Cols > data() const
Access the matrix data as a linear array.
Definition matrix.h:77
Matrix(std::span< const T, Rows *Cols > data)
Construct a matrix from supplied data.
Definition matrix.h:44
std::optional< T > get() const
Parse the ValueNode as a T value.
Definition value_node.h:211
ListAdapter asList()
Wrap a list ValueNode in an adapter that exposes iterators.
Definition value_node.h:230
#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:93
Matrix< U, Rows, Cols > operator*(T d, const Matrix< U, Rows, Cols > &m)
Multiply the matrix by a scalar.
Definition matrix.h:152
constexpr Matrix< T, Rows, Cols > operator+(const Matrix< T, Rows, Cols > &m1, const Matrix< T, Rows, Cols > &m2)
Matrix addition.
Definition matrix.h:197
Data structure to manage tree of values.