From 63624bc85a5f765dc3899d4b3b72ad875894ca04 Mon Sep 17 00:00:00 2001 From: David Plowman Date: Mon, 26 Oct 2020 17:19:06 +0000 Subject: libcamera: Add geometry helper functions These functions are aimed at making it easier to calculate cropping rectangles, particularly in order to implement digital zoom. Signed-off-by: David Plowman Reviewed-by: Jacopo Mondi Reviewed-by: Laurent Pinchart Signed-off-by: Laurent Pinchart --- include/libcamera/geometry.h | 68 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) (limited to 'include/libcamera/geometry.h') diff --git a/include/libcamera/geometry.h b/include/libcamera/geometry.h index 02fb63c0..2f3a82e2 100644 --- a/include/libcamera/geometry.h +++ b/include/libcamera/geometry.h @@ -13,6 +13,38 @@ namespace libcamera { +class Rectangle; + +class Point +{ +public: + constexpr Point() + : x(0), y(0) + { + } + + constexpr Point(int xpos, int ypos) + : x(xpos), y(ypos) + { + } + + int x; + int y; + + const std::string toString() const; + + constexpr Point operator-() const + { + return { -x, -y }; + } +}; + +bool operator==(const Point &lhs, const Point &rhs); +static inline bool operator!=(const Point &lhs, const Point &rhs) +{ + return !(lhs == rhs); +} + class Size { public: @@ -93,6 +125,17 @@ public: std::max(height, expand.height) }; } + + Size boundedToAspectRatio(const Size &ratio) const; + Size expandedToAspectRatio(const Size &ratio) const; + + Rectangle centeredTo(const Point ¢er) const; + + Size operator*(float factor) const; + Size operator/(float factor) const; + + Size &operator*=(float factor); + Size &operator/=(float factor); }; bool operator==(const Size &lhs, const Size &rhs); @@ -176,6 +219,11 @@ public: { } + constexpr explicit Rectangle(const Size &size) + : x(0), y(0), width(size.width), height(size.height) + { + } + int x; int y; unsigned int width; @@ -183,6 +231,26 @@ public: bool isNull() const { return !width && !height; } const std::string toString() const; + + Point center() const; + + Size size() const + { + return { width, height }; + } + + Point topLeft() const + { + return { x, y }; + } + + Rectangle &scaleBy(const Size &numerator, const Size &denominator); + Rectangle &translateBy(const Point &point); + + Rectangle boundedTo(const Rectangle &bound) const; + Rectangle enclosedIn(const Rectangle &boundary) const; + Rectangle scaledBy(const Size &numerator, const Size &denominator) const; + Rectangle translatedBy(const Point &point) const; }; bool operator==(const Rectangle &lhs, const Rectangle &rhs); -- cgit v1.2.1