{"id":15663,"url":"https://patchwork.libcamera.org/api/patches/15663/?format=json","web_url":"https://patchwork.libcamera.org/patch/15663/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20220412132614.317745-1-kieran.bingham@ideasonboard.com>","date":"2022-04-12T13:26:14","name":"[libcamera-devel,v2] libcamera: geometry: Add Size members to clamp to a min/max","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"c28d738293dccf723ae08f34ad1ba925b73743bc","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/?format=json","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"delegate":{"id":11,"url":"https://patchwork.libcamera.org/api/users/11/?format=json","username":"kbingham","first_name":"Kieran","last_name":"Bingham","email":"kieran.bingham@ideasonboard.com"},"mbox":"https://patchwork.libcamera.org/patch/15663/mbox/","series":[{"id":3048,"url":"https://patchwork.libcamera.org/api/series/3048/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3048","date":"2022-04-12T13:26:14","name":"[libcamera-devel,v2] libcamera: geometry: Add Size members to clamp to a min/max","version":2,"mbox":"https://patchwork.libcamera.org/series/3048/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/15663/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/15663/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 56AC8C3256\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 12 Apr 2022 13:26:00 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id B476D6563F;\n\tTue, 12 Apr 2022 15:25:59 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id C1968604B7\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 12 Apr 2022 15:25:57 +0200 (CEST)","from Q.ksquared.org.uk.beta.tailscale.net (unknown\n\t[178.237.134.231])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id F1F72486;\n\tTue, 12 Apr 2022 15:25:56 +0200 (CEST)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1649769959;\n\tbh=sYNK6Gfwefxw6d4bLHDLSqAPk/orYRM8WKTQPpQxYGI=;\n\th=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post:\n\tList-Help:List-Subscribe:From:Reply-To:From;\n\tb=0H579I/VQGpgm7yLtEVgDp2lQwXzfOq35Gp+8oVH1Nofl66DUNGX3utPC+OrXgiBb\n\t/2s90sxmFRp1WuW4PRUu4RH1H+YNutsSK4IjBK3X4O4346XJVfIikJIEBZV5QD7gJZ\n\teblOw8tBWYfxDxCpck1gMdSDTrwNQnucaFzNEfWpMcvcRmAz5cITgmI/AOODZtPs+r\n\ttfe94EqefTF/PQdzVkNzePl6sciViEBpo99Inmv2WY/zAXa1DOpakg7WBQmwVX2wnM\n\toQZkGM4exeCkVb14UD/j1lNO5OMJabIZ4odePJ9Qpy5Znfyp253kTssj1IqHP47d1n\n\tb6QuQDdKlGx1w==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1649769957;\n\tbh=sYNK6Gfwefxw6d4bLHDLSqAPk/orYRM8WKTQPpQxYGI=;\n\th=From:To:Cc:Subject:Date:From;\n\tb=Es/BiKWyOPU9SHTKHLG6TG41BCnJ+qeptYRograIE6ZAuVLyFF71DR/mZoRVzhLJ4\n\tY31QE1lLV8MGQOaF+kCXUCZoRi46bo8UiSOxnZK4Iai5g8hBng0H2Siavyd0L2wmji\n\txOhOInfGpdRhT2kyxIFhTkHE3HYZAv9UL1TU4P+Q="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"Es/BiKWy\"; dkim-atps=neutral","To":"libcamera devel <libcamera-devel@lists.libcamera.org>","Date":"Tue, 12 Apr 2022 15:26:14 +0200","Message-Id":"<20220412132614.317745-1-kieran.bingham@ideasonboard.com>","X-Mailer":"git-send-email 2.25.1","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v2] libcamera: geometry: Add Size members\n\tto clamp to a min/max","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","From":"Kieran Bingham via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Provide two new operations to support clamping a Size to a given\nminimum or maximum Size, or returning a const variant of the same\nrestriction.\n\nReviewed-by: Umang Jain <umang.jain@ideasonboard.com>\nSigned-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n---\n include/libcamera/geometry.h | 16 ++++++++++++++++\n src/libcamera/geometry.cpp   | 23 +++++++++++++++++++++++\n test/geometry.cpp            | 24 ++++++++++++++++++++++--\n 3 files changed, 61 insertions(+), 2 deletions(-)","diff":"diff --git a/include/libcamera/geometry.h b/include/libcamera/geometry.h\nindex 7838b6793617..a447beb55965 100644\n--- a/include/libcamera/geometry.h\n+++ b/include/libcamera/geometry.h\n@@ -93,6 +93,13 @@ public:\n \t\treturn *this;\n \t}\n \n+\tSize &clamp(const Size &minimum, const Size &maximum)\n+\t{\n+\t\twidth = std::clamp(width, minimum.width, maximum.width);\n+\t\theight = std::clamp(height, minimum.height, maximum.height);\n+\t\treturn *this;\n+\t}\n+\n \tSize &growBy(const Size &margins)\n \t{\n \t\twidth += margins.width;\n@@ -141,6 +148,15 @@ public:\n \t\t};\n \t}\n \n+\t__nodiscard constexpr Size clampedTo(const Size &minimum,\n+\t\t\t\t\t     const Size &maximum) const\n+\t{\n+\t\treturn {\n+\t\t\tstd::clamp(width, minimum.width, maximum.width),\n+\t\t\tstd::clamp(height, minimum.height, maximum.height)\n+\t\t};\n+\t}\n+\n \t__nodiscard constexpr Size grownBy(const Size &margins) const\n \t{\n \t\treturn {\ndiff --git a/src/libcamera/geometry.cpp b/src/libcamera/geometry.cpp\nindex cb3c2de18d5e..6689b3996741 100644\n--- a/src/libcamera/geometry.cpp\n+++ b/src/libcamera/geometry.cpp\n@@ -173,6 +173,19 @@ const std::string Size::toString() const\n  * \\return A reference to this object\n  */\n \n+/**\n+ * \\fn Size::clamp(const Size &minimum, const Size &maximum)\n+ * \\brief Clamp the size to be within the dimensions of both the \\a minimum and\n+ * \\a maximum values.\n+ * \\param[in] minimum The minimum size\n+ * \\param[in] maximum The maximum size\n+ *\n+ * This function constrains and clamps the width and height of the size to be\n+ * within the limits of the \\a minimum and \\a maximum sizes given.\n+ *\n+ * \\return A reference to this object\n+ */\n+\n /**\n  * \\fn Size::growBy(const Size &margins)\n  * \\brief Grow the size by \\a margins in place\n@@ -231,6 +244,16 @@ const std::string Size::toString() const\n  * height of this size and the \\a expand size\n  */\n \n+/**\n+ * \\fn Size::clampedTo(const Size &minimum, const Size &maximum)\n+ * \\brief Clamp the size to be within the dimensions of both the \\a minimum and\n+ * \\a maximum values\n+ * \\param[in] minimum The minimum size\n+ * \\param[in] maximum The maximum size\n+ * \\return A Size whose width and height are clamped to be within the limits of\n+ * the \\a minimum and \\a maximum sizes given.\n+ */\n+\n /**\n  * \\fn Size::grownBy(const Size &margins)\n  * \\brief Grow the size by \\a margins\ndiff --git a/test/geometry.cpp b/test/geometry.cpp\nindex 5125692496b3..8d29245b2568 100644\n--- a/test/geometry.cpp\n+++ b/test/geometry.cpp\n@@ -106,7 +106,7 @@ protected:\n \n \t\t/*\n \t\t * Test alignDownTo(), alignUpTo(), boundTo(), expandTo(),\n-\t\t * growBy() and shrinkBy()\n+\t\t * clamp(), growBy() and shrinkBy()\n \t\t */\n \t\tSize s(50, 50);\n \n@@ -134,6 +134,18 @@ protected:\n \t\t\treturn TestFail;\n \t\t}\n \n+\t\ts.clamp({ 80, 120 }, { 160, 240 });\n+\t\tif (s != Size(80, 120)) {\n+\t\t\tcout << \"Size::clamp (minium) test failed\" << endl;\n+\t\t\treturn TestFail;\n+\t\t}\n+\n+\t\ts.clamp({ 20, 30 }, { 50, 50 });\n+\t\tif (s != Size(50, 50)) {\n+\t\t\tcout << \"Size::clamp (maximum) test failed\" << endl;\n+\t\t\treturn TestFail;\n+\t\t}\n+\n \t\ts.growBy({ 10, 20 });\n \t\tif (s != Size(60, 70)) {\n \t\t\tcout << \"Size::growBy() test failed\" << endl;\n@@ -162,7 +174,7 @@ protected:\n \n \t\t/*\n \t\t * Test alignedDownTo(), alignedUpTo(), boundedTo(),\n-\t\t * expandedTo(), grownBy() and shrunkBy()\n+\t\t * expandedTo(), clampedTo(), grownBy() and shrunkBy()\n \t\t */\n \t\tif (Size(0, 0).alignedDownTo(16, 8) != Size(0, 0) ||\n \t\t    Size(1, 1).alignedDownTo(16, 8) != Size(0, 0) ||\n@@ -192,6 +204,14 @@ protected:\n \t\t\treturn TestFail;\n \t\t}\n \n+\t\tif (Size(0, 0).clampedTo({ 60, 40 }, { 80, 90 }) != Size(60, 40) ||\n+\t\t    Size(100, 100).clampedTo({ 60, 40 }, { 80, 90 }) != Size(80, 90) ||\n+\t\t    Size(30, 100).clampedTo({ 60, 40 }, { 80, 90 }) != Size(60, 90) ||\n+\t\t    Size(100, 30).clampedTo({ 60, 40 }, { 80, 90 }) != Size(80, 40)) {\n+\t\t\tcout << \"Size::clampedTo() test failed\" << endl;\n+\t\t\treturn TestFail;\n+\t\t}\n+\n \t\tif (Size(0, 0).grownBy({ 10, 20 }) != Size(10, 20) ||\n \t\t    Size(200, 50).grownBy({ 10, 20 }) != Size(210, 70)) {\n \t\t\tcout << \"Size::grownBy() test failed\" << endl;\n","prefixes":["libcamera-devel","v2"]}