From patchwork Mon Apr 15 16:56:55 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 987 Return-Path: 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 77BEE60DC1 for ; Mon, 15 Apr 2019 18:57:16 +0200 (CEST) Received: from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi [81.175.216.236]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id E76DB333 for ; Mon, 15 Apr 2019 18:57:15 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1555347436; bh=ovhaqq34QoTOi/qg9DDjQZfuPQbPoYVh6USwqqUEwR0=; h=From:To:Subject:Date:In-Reply-To:References:From; b=Rr7NQFCoqL6vdGkHpvd4oe1rkKfs3Gh+/lPseB6+Y9G6LgLtBVk9tpR1vxwV5D7Dq sC6dxu5ZbIQbEpRKik7Uws7NJ6msDxQvBpvkfzeD+tvjpie9uB4Hz7N7IVcF6+reY5 peFEGRd3M27UVZvNGjdOWtDPv6LHnuVWdBGpIuXI= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Mon, 15 Apr 2019 19:56:55 +0300 Message-Id: <20190415165700.22970-7-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190415165700.22970-1-laurent.pinchart@ideasonboard.com> References: <20190415165700.22970-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 06/11] libcamera: geometry: Add comparison operators to geometry classes X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 15 Apr 2019 16:57:17 -0000 Add equality and inequality comparison operators for the Rectangle, Size and SizeRange classes. For the Size class, also add ordering operators. Sizes are first compared on combined width and height, then on area, and finally on width only to achieve a stable ordering. Signed-off-by: Laurent Pinchart Reviewed-by: Niklas Söderlund Reviewed-by: Jacopo Mondi --- include/libcamera/geometry.h | 35 ++++++++++++ src/libcamera/geometry.cpp | 102 +++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+) diff --git a/include/libcamera/geometry.h b/include/libcamera/geometry.h index 80f79c6ba2d3..3dbbced5c76f 100644 --- a/include/libcamera/geometry.h +++ b/include/libcamera/geometry.h @@ -21,6 +21,12 @@ struct Rectangle { const std::string toString() const; }; +bool operator==(const Rectangle &lhs, const Rectangle &rhs); +static inline bool operator!=(const Rectangle &lhs, const Rectangle &rhs) +{ + return !(lhs == rhs); +} + struct Size { Size() : Size(0, 0) @@ -36,6 +42,29 @@ struct Size { unsigned int height; }; +bool operator==(const Size &lhs, const Size &rhs); +bool operator<(const Size &lhs, const Size &rhs); + +static inline bool operator!=(const Size &lhs, const Size &rhs) +{ + return !(lhs == rhs); +} + +static inline bool operator<=(const Size &lhs, const Size &rhs) +{ + return lhs < rhs || lhs == rhs; +} + +static inline bool operator>(const Size &lhs, const Size &rhs) +{ + return !(lhs <= rhs); +} + +static inline bool operator>=(const Size &lhs, const Size &rhs) +{ + return !(lhs < rhs); +} + struct SizeRange { SizeRange() { @@ -51,6 +80,12 @@ struct SizeRange { Size max; }; +bool operator==(const SizeRange &lhs, const SizeRange &rhs); +static inline bool operator!=(const SizeRange &lhs, const SizeRange &rhs) +{ + return !(lhs == rhs); +} + } /* namespace libcamera */ #endif /* __LIBCAMERA_GEOMETRY_H__ */ diff --git a/src/libcamera/geometry.cpp b/src/libcamera/geometry.cpp index c1c7daed7259..7b65e63f2ebd 100644 --- a/src/libcamera/geometry.cpp +++ b/src/libcamera/geometry.cpp @@ -6,6 +6,7 @@ */ #include +#include #include @@ -62,6 +63,22 @@ const std::string Rectangle::toString() const return ss.str(); } +/** + * \brief Compare rectangles for equality + * \return True if the two rectangles are equal, false otherwise + */ +bool operator==(const Rectangle &lhs, const Rectangle &rhs) +{ + return lhs.x == rhs.x && lhs.y == rhs.y && + lhs.w == rhs.w && lhs.h == rhs.h; +} + +/** + * \fn bool operator!=(const Rectangle &lhs, const Rectangle &rhs) + * \brief Compare rectangles for inequality + * \return True if the two rectangles are not equal, false otherwise + */ + /** * \struct Size * \brief Describe a two-dimensional size @@ -91,6 +108,76 @@ const std::string Rectangle::toString() const * \brief The Size height */ +/** + * \brief Compare sizes for equality + * \return True if the two sizes are equal, false otherwise + */ +bool operator==(const Size &lhs, const Size &rhs) +{ + return lhs.width == rhs.width && lhs.height == rhs.height; +} + +/** + * \brief Compare sizes for smaller than order + * + * Sizes are compared on three criteria, in the following order. + * + * - A size with smaller width and smaller height is smaller. + * - A size with smaller area is smaller. + * - A size with smaller width is smaller. + * + * \return True if the left hand size is smaller than the right hand size, false + * otherwise + */ +bool operator<(const Size &lhs, const Size &rhs) +{ + if (lhs.width < rhs.width && lhs.height < rhs.height) + return true; + else if (lhs.width >= rhs.width && lhs.height >= rhs.height) + return false; + + uint64_t larea = static_cast(lhs.width) * + static_cast(lhs.height); + uint64_t rarea = static_cast(rhs.width) * + static_cast(rhs.height); + if (larea < rarea) + return true; + else if (larea > rarea) + return false; + + return lhs.width < rhs.width; +} + +/** + * \fn bool operator!=(const Size &lhs, const Size &rhs) + * \brief Compare sizes for inequality + * \return True if the two sizes are not equal, false otherwise + */ + +/** + * \fn bool operator<=(const Size &lhs, const Size &rhs) + * \brief Compare sizes for smaller than or equal to order + * \return True if the left hand size is smaller than or equal to the right + * hand size, false otherwise + * \sa bool operator<(const Size &lhs, const Size &rhs) + */ + +/** + * \fn bool operator>(const Size &lhs, const Size &rhs) + * \brief Compare sizes for greater than order + * \return True if the left hand size is greater than the right hand size, + * false otherwise + * \sa bool operator<(const Size &lhs, const Size &rhs) + */ + +/** + * \fn bool operator>=(const Size &lhs, const Size &rhs) + * \brief Compare sizes for greater than or equal to order + * \return True if the left hand size is greater than or equal to the right + * hand size, false otherwise + * \sa bool operator<(const Size &lhs, const Size &rhs) + */ + /** * \struct SizeRange * \brief Describe a range of sizes @@ -124,4 +211,19 @@ const std::string Rectangle::toString() const * \brief The maximum size */ +/** + * \brief Compare size ranges for equality + * \return True if the two size ranges are equal, false otherwise + */ +bool operator==(const SizeRange &lhs, const SizeRange &rhs) +{ + return lhs.min == rhs.min && lhs.max == rhs.max; +} + +/** + * \fn bool operator!=(const SizeRange &lhs, const SizeRange &rhs) + * \brief Compare size ranges for inequality + * \return True if the two size ranges are not equal, false otherwise + */ + } /* namespace libcamera */