From 6287ceff5aba1e8207aafdae9f967c70d9aad19f Mon Sep 17 00:00:00 2001 From: Stefan Klug Date: Thu, 3 Apr 2025 17:49:10 +0200 Subject: libcamera: matrix: Add inverse() function For calculations in upcoming algorithm patches, the inverse of a matrix is required. Add an implementation of the inverse() function for square matrices. Signed-off-by: Stefan Klug Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham Reviewed-by: Paul Elder --- include/libcamera/internal/matrix.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'include') diff --git a/include/libcamera/internal/matrix.h b/include/libcamera/internal/matrix.h index 6d40567a..a07a4770 100644 --- a/include/libcamera/internal/matrix.h +++ b/include/libcamera/internal/matrix.h @@ -19,6 +19,12 @@ 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 { @@ -91,6 +97,23 @@ public: 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 -- cgit v1.2.1