From patchwork Sun Nov 17 22:17:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 21939 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 AC06FC32DD for ; Sun, 17 Nov 2024 22:17:50 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 410FF658C3; Sun, 17 Nov 2024 23:17:50 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="DVb2nMUc"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 679B7658B7 for ; Sun, 17 Nov 2024 23:17:34 +0100 (CET) Received: from pendragon.ideasonboard.com (81-175-209-231.bb.dnainternet.fi [81.175.209.231]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 16E2CA06 for ; Sun, 17 Nov 2024 23:17:18 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1731881838; bh=N4kymlvsO1kcNz1aPnJKStzldZ5qZz+nrHimPlV+m0s=; h=From:To:Subject:Date:In-Reply-To:References:From; b=DVb2nMUcdCScR0VPpWwSNBNoeLWIvPfBJN5qiGfhghOY2CBeSShVyUHqCnnYvZrFz YP9WCHNMhvEZrVC716HmJz0PYGB76zkzfpRCB5bUWx1KL4MGLhFHmz2IbJi3OEsr9E UfyLe3rSLtP7aOFcTKea30E7nyx+Elulhvsk+fCI= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Subject: [RFC PATCH 09/11] ipa: libipa: vector: Add sum() and normalize() functions Date: Mon, 18 Nov 2024 00:17:10 +0200 Message-ID: <20241117221712.29616-10-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241117221712.29616-1-laurent.pinchart@ideasonboard.com> References: <20241117221712.29616-1-laurent.pinchart@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" Add functions to calculate the sum of a vector, and to normalize it. They are useful for algorithms. Signed-off-by: Laurent Pinchart --- src/ipa/libipa/vector.cpp | 17 +++++++++++++++++ src/ipa/libipa/vector.h | 12 ++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/ipa/libipa/vector.cpp b/src/ipa/libipa/vector.cpp index 14816cdb5d09..0662e598c086 100644 --- a/src/ipa/libipa/vector.cpp +++ b/src/ipa/libipa/vector.cpp @@ -296,6 +296,23 @@ namespace ipa { * \return The length of the vector */ +/** + * \fn Vector::normalize() + * \brief Normalize the vector + */ + +/** + * \fn Vector::sum() const + * \brief Calculate the sum of all the vector elements + * \tparam R The type of the sum + * + * The type R of the sum defaults to the type T of the elements, but can be set + * explicitly to use a different type in case the type T would risk + * overflowing. + * + * \return The sum of all the vector elements + */ + /** * \fn Vector operator*(const Matrix &m, const Vector &v) * \brief Multiply a matrix by a vector diff --git a/src/ipa/libipa/vector.h b/src/ipa/libipa/vector.h index 949dc4650aa4..b3a6412497c9 100644 --- a/src/ipa/libipa/vector.h +++ b/src/ipa/libipa/vector.h @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -245,6 +246,17 @@ public: return std::sqrt(length2()); } + void normalize() + { + *this /= sum(); + } + + template + constexpr R sum() const + { + return std::accumulate(data_.begin(), data_.end(), R{}); + } + private: static constexpr Vector apply(const Vector &lhs, const Vector &rhs, std::function func) {