/* SPDX-License-Identifier: LGPL-2.1-or-later */ /* * Copyright (C) 2024, Paul Elder * * Matrix and related operations */ #pragma once #include #include #include #include #include #include #include "libcamera/internal/yaml_parser.h" namespace libcamera { LOG_DECLARE_CATEGORY(Matrix) #ifndef __DOXYGEN__ template bool matrixInvert(Span dataIn, Span dataOut, unsigned int dim, Span scratchBuffer, Span swapBuffer); #endif /* __DOXYGEN__ */ template class Matrix { static_assert(std::is_arithmetic_v, "Matrix type must be arithmetic"); public: constexpr Matrix() { } Matrix(const std::array &data) { std::copy(data.begin(), data.end(), data_.begin()); } Matrix(const Span data) { std::copy(data.begin(), data.end(), data_.begin()); } static constexpr Matrix identity() { Matrix ret; for (size_t i = 0; i < std::min(Rows, Cols); i++) ret[i][i] = static_cast(1); return ret; } ~Matrix() = default; const std::string toString() const { std::stringstream out; out << "Matrix { "; for (unsigned int i = 0; i < Rows; i++) { out << "[ "; for (unsigned int j = 0; j < Cols; j++) { out << (*this)[i][j]; out << ((j + 1 < Cols) ? ", " : " "); } out << ((i + 1 < Rows) ? "], " : "]"); } out << " }"; return out.str(); } constexpr Span data() const { return data_; } constexpr Span operator[](size_t i) const { return Span{ &data_.data()[i * Cols], Cols }; } constexpr Span operator[](size_t i) { return Span{ &data_.data()[i * Cols], Cols }; } #ifndef __DOXYGEN__ template>> #else template #endif /* __DOXYGEN__ */ Matrix &operator*=(U d) { for (unsigned int i = 0; i < Rows * Cols; i++) data_[i] *= d; return *this; } Matrix inverse(bool *ok = nullptr) const { static_assert(Rows == Cols, "Matrix must be square"); Matrix inverse; std::array scratchBuffer; std::array swapBuffer; bool res = matrixInvert(Span(data_), Span(inverse.data_), Rows, Span(scratchBuffer), Span(swapBuffer)); if (ok) *ok = res; return inverse; } private: /* * \todo The initializer is only necessary for the constructor to be * constexpr in C++17. Remove the initializer as soon as we are on * C++20. */ std::array data_ = {}; }; #ifndef __DOXYGEN__ template> * = nullptr> #else template #endif /* __DOXYGEN__ */ Matrix operator*(T d, const Matrix &m) { Matrix result; for (unsigned int i = 0; i < Rows; i++) { for (unsigned int j = 0; j < Cols; j++) result[i][j] = d * m[i][j]; } return result; } #ifndef __DOXYGEN__ template> * = nullptr> #else template #endif /* __DOXYGEN__ */ Matrix operator*(const Matrix &m, T d) { return d * m; } template constexpr Matrix, R1, C2> operator*(const Matrix &m1, const Matrix &m2) { static_assert(C1 == R2, "Matrix dimensions must match for multiplication"); Matrix, R1, C2> result; for (unsigned int i = 0; i < R1; i++) { for (unsigned int j = 0; j < C2; j++) { std::common_type_t sum = 0; for (unsigned int k = 0; k < C1; k++) sum += m1[i][k] * m2[k][j]; result[i][j] = sum; } } return result; } template constexpr Matrix operator+(const Matrix &m1, const Matrix &m2) { Matrix result; for (unsigned int i = 0; i < Rows; i++) { for (unsigned int j = 0; j < Cols; j++) result[i][j] = m1[i][j] + m2[i][j]; } return result; } #ifndef __DOXYGEN__ bool matrixValidateYaml(const YamlObject &obj, unsigned int size); #endif /* __DOXYGEN__ */ #ifndef __DOXYGEN__ template std::ostream &operator<<(std::ostream &out, const Matrix &m) { out << m.toString(); return out; } template struct YamlObject::Getter> { std::optional> get(const YamlObject &obj) const { if (!matrixValidateYaml(obj, Rows * Cols)) return std::nullopt; Matrix matrix; T *data = &matrix[0][0]; unsigned int i = 0; for (const YamlObject &entry : obj.asList()) { const auto value = entry.get(); if (!value) return std::nullopt; data[i++] = *value; } return matrix; } }; #endif /* __DOXYGEN__ */ } /* namespace libcamera */