From patchwork Sun Jun 21 00:23:05 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 27008 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 5EB84C3307 for ; Sun, 21 Jun 2026 00:23:18 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id D6439656F4; Sun, 21 Jun 2026 02:23:17 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="ULA9vr5u"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id E4329656E5 for ; Sun, 21 Jun 2026 02:23:12 +0200 (CEST) Received: from killaraus.ideasonboard.com (2001-14ba-70f3-e800--a06.rev.dnainternet.fi [IPv6:2001:14ba:70f3:e800::a06]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 1056E268; Sun, 21 Jun 2026 02:22:36 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1782001356; bh=sw9YqKgDYcqB85sXmuOJ+W8a6HWiz4GDP0cDtZxX6t8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ULA9vr5u9S6CSuuzzwq9DbeMtUzgQ1qQF1DmUaTbBn64AmL/TotseYhl6oR0W3SeX 2HAsHjEmhasWXnAZl9ZS6erfcGb1YTi7tEN2nZ2/eUdjrtmKNbuZI9EXW+XsDS6xxW zbmmhBSCGGRYqrHRoozZK6oGItGef8WkaZYqv5Qk= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Cc: Kieran Bingham Subject: [PATCH 4/4] libcamera: vector: Add clamp() member function Date: Sun, 21 Jun 2026 03:23:05 +0300 Message-ID: <20260621002305.3763752-5-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260621002305.3763752-1-laurent.pinchart@ideasonboard.com> References: <20260621002305.3763752-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" From: Kieran Bingham Provide a member function that performs a std::clamp() given a scalar high and low values to clamp to. Suggested-by: Laurent Pinchart Reviewed-by: Laurent Pinchart Signed-off-by: Kieran Bingham Signed-off-by: Laurent Pinchart --- Changes since Kieran's version: - Use apply() - Fix typos in commit message --- include/libcamera/internal/vector.h | 7 +++++++ src/libcamera/vector.cpp | 8 ++++++++ test/vector.cpp | 1 + 3 files changed, 16 insertions(+) diff --git a/include/libcamera/internal/vector.h b/include/libcamera/internal/vector.h index 7c90d6542352..0270651a0ef7 100644 --- a/include/libcamera/internal/vector.h +++ b/include/libcamera/internal/vector.h @@ -185,6 +185,13 @@ public: return apply(*this, [](T a, T b) -> T { return std::max(a, b); }, scalar); } + constexpr Vector clamp(T low, T high) const + { + return apply(*this, + [](T v, T lo, T hi) -> T { return std::clamp(v, lo, hi); }, + low, high); + } + constexpr T dot(const Vector &other) const { T ret = 0; diff --git a/src/libcamera/vector.cpp b/src/libcamera/vector.cpp index 47b3d1af36d9..a135ab498e17 100644 --- a/src/libcamera/vector.cpp +++ b/src/libcamera/vector.cpp @@ -224,6 +224,14 @@ LOG_DEFINE_CATEGORY(Vector) * \return The element-wise maximum of this vector and \a scalar */ +/** + * \fn Vector::clamp(T low, T high) const + * \brief Clamp the vector element-wise between \a low and \a high + * \param[in] low The lower limit + * \param[in] high The upper limit + * \return A vector with each element clamped between \a low and \a high + */ + /** * \fn Vector::dot(const Vector &other) const * \brief Compute the dot product diff --git a/test/vector.cpp b/test/vector.cpp index 4ff908e8f682..2f3d97d93dcc 100644 --- a/test/vector.cpp +++ b/test/vector.cpp @@ -72,6 +72,7 @@ protected: ASSERT_EQ(v2.min(4.0), (Vector{{ 1.0, 4.0, 4.0 }})); ASSERT_EQ(v2.max(v3), (Vector{{ 4.0, 4.0, 8.0 }})); ASSERT_EQ(v2.max(4.0), (Vector{{ 4.0, 4.0, 8.0 }})); + ASSERT_EQ(v2.clamp(2.0, 6.0), (Vector{{ 2.0, 4.0, 6.0 }})); ASSERT_EQ(v2.dot(v3), 52.0);