From patchwork Mon Feb 17 10:01:44 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Klug X-Patchwork-Id: 22789 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id CBC62C327D for ; Mon, 17 Feb 2025 10:02:21 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 2A4976866C; Mon, 17 Feb 2025 11:02:21 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="sH+plZlR"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 6A58D68661 for ; Mon, 17 Feb 2025 11:02:16 +0100 (CET) Received: from ideasonboard.com (unknown [IPv6:2a00:6020:448c:6c00:b47f:e20a:c4c7:ece1]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 275FDE29; Mon, 17 Feb 2025 11:00:55 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1739786455; bh=Prka8/qby0xM9wSgS3e77LXT7u3uW8O5pulkE87B6A8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sH+plZlRUQK5aOQ4lAQf0M2oyEqqByjiJQuuWHoKR9LbMrKIrtmcBn81QaSBzEN15 CxyZ6ukWsDHL41o3ABkbTpqetokb1831rPgjsCJn6w3ECjkp2daA7JPBCs0sPXHZhJ wzoJR5JZumi/zzq+S1+FOnv6K4xbH6LyOM9C0fVo= From: Stefan Klug To: libcamera-devel@lists.libcamera.org Cc: Stefan Klug Subject: [PATCH 03/10] libcamera: matrix: Add inverse() function Date: Mon, 17 Feb 2025 11:01:44 +0100 Message-ID: <20250217100203.297894-4-stefan.klug@ideasonboard.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250217100203.297894-1-stefan.klug@ideasonboard.com> References: <20250217100203.297894-1-stefan.klug@ideasonboard.com> MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" For calculations in upcoming algorithm patches, the inverse of a matrix is required. Add an implementation of the inverse() function for 3x3 matrices. Signed-off-by: Stefan Klug --- include/libcamera/internal/matrix.h | 24 ++++++++++++++++++++++++ src/libcamera/matrix.cpp | 10 ++++++++++ 2 files changed, 34 insertions(+) diff --git a/include/libcamera/internal/matrix.h b/include/libcamera/internal/matrix.h index ca81dcda93c4..053336e9cfa4 100644 --- a/include/libcamera/internal/matrix.h +++ b/include/libcamera/internal/matrix.h @@ -90,6 +90,30 @@ public: return *this; } + Matrix inverse() const + { + static_assert(Rows == 3 && Cols == 3, "Matrix must be 3x3"); + + const auto &m = *this; + double det = m[0][0] * (m[1][1] * m[2][2] - m[2][1] * m[1][2]) - + m[0][1] * (m[1][0] * m[2][2] - m[1][2] * m[2][0]) + + m[0][2] * (m[1][0] * m[2][1] - m[1][1] * m[2][0]); + + double invdet = 1 / det; + + Matrix ret; + ret[0][0] = (m[1][1] * m[2][2] - m[2][1] * m[1][2]) * invdet; + ret[0][1] = (m[0][2] * m[2][1] - m[0][1] * m[2][2]) * invdet; + ret[0][2] = (m[0][1] * m[1][2] - m[0][2] * m[1][1]) * invdet; + ret[1][0] = (m[1][2] * m[2][0] - m[1][0] * m[2][2]) * invdet; + ret[1][1] = (m[0][0] * m[2][2] - m[0][2] * m[2][0]) * invdet; + ret[1][2] = (m[1][0] * m[0][2] - m[0][0] * m[1][2]) * invdet; + ret[2][0] = (m[1][0] * m[2][1] - m[2][0] * m[1][1]) * invdet; + ret[2][1] = (m[2][0] * m[0][1] - m[0][0] * m[2][1]) * invdet; + ret[2][2] = (m[0][0] * m[1][1] - m[1][0] * m[0][1]) * invdet; + return ret; + } + template Matrix cast() const { diff --git a/src/libcamera/matrix.cpp b/src/libcamera/matrix.cpp index 91a3f16405a3..b17a1938f1ba 100644 --- a/src/libcamera/matrix.cpp +++ b/src/libcamera/matrix.cpp @@ -77,6 +77,16 @@ LOG_DEFINE_CATEGORY(Matrix) * \return Row \a i from the matrix, as a Span */ +/** + * \fn Matrix::inverse() const + * \brief Compute the inverse of the matrix + * + * This function computes the inverse of the matrix. It is only implemented for + * 3x3 matrices. + * + * \return The inverse of the matrix + */ + /** * \fn template Matrix Matrix::cast() const * \brief Cast the matrix to a different type