[v10,2/5] libcamera: geometry: Add two-point Rectangle constructor
diff mbox series

Message ID 20241002055002.6271-2-jacopo.mondi@ideasonboard.com
State Accepted
Headers show
Series
  • Add Face Detection Controls
Related show

Commit Message

Jacopo Mondi Oct. 2, 2024, 5:49 a.m. UTC
From: Yudhistira Erlandinata <yerlandinata@chromium.org>

Add a constructor to the Rectangle class that accepts two points.

The constructed Rectangle spans all the space between the two given
points.

Signed-off-by: Yudhistira Erlandinata <yerlandinata@chromium.org>
Co-developed-by: Harvey Yang <chenghaoyang@chromium.org>
Reviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
Reviewed-by: Harvey Yang <chenghaoyang@chromium.org>
---
 include/libcamera/geometry.h |  9 +++++++++
 src/libcamera/geometry.cpp   |  7 +++++++
 test/geometry.cpp            | 14 ++++++++++++++
 3 files changed, 30 insertions(+)

Comments

Laurent Pinchart Oct. 2, 2024, 11:45 a.m. UTC | #1
On Wed, Oct 02, 2024 at 07:49:59AM +0200, Jacopo Mondi wrote:
> From: Yudhistira Erlandinata <yerlandinata@chromium.org>
> 
> Add a constructor to the Rectangle class that accepts two points.
> 
> The constructed Rectangle spans all the space between the two given
> points.
> 
> Signed-off-by: Yudhistira Erlandinata <yerlandinata@chromium.org>
> Co-developed-by: Harvey Yang <chenghaoyang@chromium.org>
> Reviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
> Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
> Reviewed-by: Harvey Yang <chenghaoyang@chromium.org>

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

> ---
>  include/libcamera/geometry.h |  9 +++++++++
>  src/libcamera/geometry.cpp   |  7 +++++++
>  test/geometry.cpp            | 14 ++++++++++++++
>  3 files changed, 30 insertions(+)
> 
> diff --git a/include/libcamera/geometry.h b/include/libcamera/geometry.h
> index 3e6f0f5d7fab..9ca5865a3d0d 100644
> --- a/include/libcamera/geometry.h
> +++ b/include/libcamera/geometry.h
> @@ -262,6 +262,15 @@ public:
>  	{
>  	}
>  
> +	constexpr Rectangle(const Point &point1, const Point &point2)
> +		: Rectangle(std::min(point1.x, point2.x), std::min(point1.y, point2.y),
> +			    static_cast<unsigned int>(std::max(point1.x, point2.x)) -
> +			    static_cast<unsigned int>(std::min(point1.x, point2.x)),
> +			    static_cast<unsigned int>(std::max(point1.y, point2.y)) -
> +			    static_cast<unsigned int>(std::min(point1.y, point2.y)))
> +	{
> +	}
> +
>  	int x;
>  	int y;
>  	unsigned int width;
> diff --git a/src/libcamera/geometry.cpp b/src/libcamera/geometry.cpp
> index 85a7f53a6f6c..90ccf8c19f97 100644
> --- a/src/libcamera/geometry.cpp
> +++ b/src/libcamera/geometry.cpp
> @@ -635,6 +635,13 @@ std::ostream &operator<<(std::ostream &out, const SizeRange &sr)
>   * \param[in] size The desired Rectangle size
>   */
>  
> +/**
> + * \fn Rectangle::Rectangle(const Point &point1, const Point &point2)
> + * \brief Construct a Rectangle from two opposite corners
> + * \param[in] point1 One of corners of the rectangle
> + * \param[in] point2 The opposite corner of \a point1
> + */
> +
>  /**
>   * \var Rectangle::x
>   * \brief The horizontal coordinate of the rectangle's top-left corner
> diff --git a/test/geometry.cpp b/test/geometry.cpp
> index 64169206ad16..5760fa3c885a 100644
> --- a/test/geometry.cpp
> +++ b/test/geometry.cpp
> @@ -481,6 +481,20 @@ protected:
>  			return TestFail;
>  		}
>  
> +		Point topLeft(3, 3);
> +		Point bottomRight(30, 30);
> +		Point topRight(30, 3);
> +		Point bottomLeft(3, 30);
> +		Rectangle rect1(topLeft, bottomRight);
> +		Rectangle rect2(topRight, bottomLeft);
> +		Rectangle rect3(bottomRight, topLeft);
> +		Rectangle rect4(bottomLeft, topRight);
> +
> +		if (rect1 != rect2 || rect1 != rect3 || rect1 != rect4) {
> +			cout << "Point-to-point construction failed" << endl;
> +			return TestFail;
> +		}
> +
>  		return TestPass;
>  	}
>  };

Patch
diff mbox series

diff --git a/include/libcamera/geometry.h b/include/libcamera/geometry.h
index 3e6f0f5d7fab..9ca5865a3d0d 100644
--- a/include/libcamera/geometry.h
+++ b/include/libcamera/geometry.h
@@ -262,6 +262,15 @@  public:
 	{
 	}
 
+	constexpr Rectangle(const Point &point1, const Point &point2)
+		: Rectangle(std::min(point1.x, point2.x), std::min(point1.y, point2.y),
+			    static_cast<unsigned int>(std::max(point1.x, point2.x)) -
+			    static_cast<unsigned int>(std::min(point1.x, point2.x)),
+			    static_cast<unsigned int>(std::max(point1.y, point2.y)) -
+			    static_cast<unsigned int>(std::min(point1.y, point2.y)))
+	{
+	}
+
 	int x;
 	int y;
 	unsigned int width;
diff --git a/src/libcamera/geometry.cpp b/src/libcamera/geometry.cpp
index 85a7f53a6f6c..90ccf8c19f97 100644
--- a/src/libcamera/geometry.cpp
+++ b/src/libcamera/geometry.cpp
@@ -635,6 +635,13 @@  std::ostream &operator<<(std::ostream &out, const SizeRange &sr)
  * \param[in] size The desired Rectangle size
  */
 
+/**
+ * \fn Rectangle::Rectangle(const Point &point1, const Point &point2)
+ * \brief Construct a Rectangle from two opposite corners
+ * \param[in] point1 One of corners of the rectangle
+ * \param[in] point2 The opposite corner of \a point1
+ */
+
 /**
  * \var Rectangle::x
  * \brief The horizontal coordinate of the rectangle's top-left corner
diff --git a/test/geometry.cpp b/test/geometry.cpp
index 64169206ad16..5760fa3c885a 100644
--- a/test/geometry.cpp
+++ b/test/geometry.cpp
@@ -481,6 +481,20 @@  protected:
 			return TestFail;
 		}
 
+		Point topLeft(3, 3);
+		Point bottomRight(30, 30);
+		Point topRight(30, 3);
+		Point bottomLeft(3, 30);
+		Rectangle rect1(topLeft, bottomRight);
+		Rectangle rect2(topRight, bottomLeft);
+		Rectangle rect3(bottomRight, topLeft);
+		Rectangle rect4(bottomLeft, topRight);
+
+		if (rect1 != rect2 || rect1 != rect3 || rect1 != rect4) {
+			cout << "Point-to-point construction failed" << endl;
+			return TestFail;
+		}
+
 		return TestPass;
 	}
 };