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

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

Commit Message

Jacopo Mondi Sept. 30, 2024, 7:59 p.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>
---
 include/libcamera/geometry.h |  7 +++++
 src/libcamera/geometry.cpp   | 58 ++++++++++++++++++++++++++++++++++++
 test/geometry.cpp            | 14 +++++++++
 3 files changed, 79 insertions(+)

Comments

Barnabás Pőcze Sept. 30, 2024, 10:50 p.m. UTC | #1
Hi


2024. szeptember 30., hétfő 21:59 keltezéssel, Jacopo Mondi <jacopo.mondi@ideasonboard.com> írta:

> 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>
> ---
>  include/libcamera/geometry.h |  7 +++++
>  src/libcamera/geometry.cpp   | 58 ++++++++++++++++++++++++++++++++++++
>  test/geometry.cpp            | 14 +++++++++
>  3 files changed, 79 insertions(+)
> 
> diff --git a/include/libcamera/geometry.h b/include/libcamera/geometry.h
> index 3e6f0f5d7fab..2cc25f1facd9 100644
> --- a/include/libcamera/geometry.h
> +++ b/include/libcamera/geometry.h
> @@ -262,6 +262,13 @@ public:
>  	{
>  	}
> 
> +	constexpr Rectangle(const Point &point1, const Point &point2)
> +		: Rectangle(std::min(point1.x, point2.x), std::min(point1.y, point2.y),
> +			    std::max(point1.x, point2.x) - std::min(point1.x, point2.x),
> +			    std::max(point1.y, point2.y) - std::min(point1.y, point2.y))

The subtraction can lead to under/overflow. I believe simply casting to
`unsigned` avoids that, e.g.:

  unsigned(std::max(point1.x, point2.x)) - unsigned(std::min(point1.x, point2.x))

Although I don't foresee it causing any issues, since the fix is easy,
I think it's worth doing.


Regards,
Barnabás Pőcze

> +	{
> +	}
> +
>  	int x;
>  	int y;
>  	unsigned int width;
> [...]
Harvey Yang Oct. 1, 2024, 6:43 a.m. UTC | #2
Thank you Jacopo for the rebase and fix.

On Tue, Oct 1, 2024 at 3:59 AM Jacopo Mondi
<jacopo.mondi@ideasonboard.com> 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>
> ---
>  include/libcamera/geometry.h |  7 +++++
>  src/libcamera/geometry.cpp   | 58 ++++++++++++++++++++++++++++++++++++
>  test/geometry.cpp            | 14 +++++++++
>  3 files changed, 79 insertions(+)
>
> diff --git a/include/libcamera/geometry.h b/include/libcamera/geometry.h
> index 3e6f0f5d7fab..2cc25f1facd9 100644
> --- a/include/libcamera/geometry.h
> +++ b/include/libcamera/geometry.h
> @@ -262,6 +262,13 @@ public:
>         {
>         }
>
> +       constexpr Rectangle(const Point &point1, const Point &point2)
> +               : Rectangle(std::min(point1.x, point2.x), std::min(point1.y, point2.y),
> +                           std::max(point1.x, point2.x) - std::min(point1.x, point2.x),
> +                           std::max(point1.y, point2.y) - 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 6eb432e5d803..5e5e72e15d67 100644
> --- a/src/libcamera/geometry.cpp
> +++ b/src/libcamera/geometry.cpp
> @@ -715,6 +715,64 @@ 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 with the two given points
> + * \param[in] point1 One of corners of the rectangle
> + * \param[in] point2 The opposite diagonal corner point of \a point1
> + *
> + * Contruct a rectangle that spans the space between two given points. The
> + * position of the two given points is not relevant for the rectangle's
> + * contruction.
> + *
> + * \verbatim
> +
> +  p1 = point1
> +  p2 = point2
> +
> +          ^
> +          |
> +          |      ----------------p2
> +          |     |                 |
> +          |     |                 |
> +          |     p1----------------
> +          |
> +          o------------------------------->
> +         (0,0)
> +
> +          ^
> +          |
> +          |     p1----------------
> +          |     |                 |
> +          |     |                 |
> +          |      ----------------p2
> +          |
> +          o------------------------------->
> +         (0,0)
> +
> +          ^
> +          |
> +          |      ----------------p1
> +          |     |                 |
> +          |     |                 |
> +          |     p2----------------
> +          |
> +          o------------------------------->
> +         (0,0)
> +
> +          ^
> +          |
> +          |     p2----------------
> +          |     |                 |
> +          |     |                 |
> +          |      ----------------p1
> +          |
> +          o------------------------------->
> +         (0,0)
> +
> +   \endverbatim
> + */
> +
>  /**
>   * \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);

Sorry, I think I did it wrong in v8:
Maybe we should swap the points of topRight and bottomLeft.

Otherwise LGTM.

Reviewed-by: Harvey Yang <chenghaoyang@chromium.org>

BR,
Harvey

> +               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;
>         }
>  };
> --
> 2.46.1
>
Jacopo Mondi Oct. 1, 2024, 7:50 a.m. UTC | #3
Hi Barnabás

On Mon, Sep 30, 2024 at 10:50:30PM GMT, Barnabás Pőcze wrote:
> Hi
>
>
> 2024. szeptember 30., hétfő 21:59 keltezéssel, Jacopo Mondi <jacopo.mondi@ideasonboard.com> írta:
>
> > 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>
> > ---
> >  include/libcamera/geometry.h |  7 +++++
> >  src/libcamera/geometry.cpp   | 58 ++++++++++++++++++++++++++++++++++++
> >  test/geometry.cpp            | 14 +++++++++
> >  3 files changed, 79 insertions(+)
> >
> > diff --git a/include/libcamera/geometry.h b/include/libcamera/geometry.h
> > index 3e6f0f5d7fab..2cc25f1facd9 100644
> > --- a/include/libcamera/geometry.h
> > +++ b/include/libcamera/geometry.h
> > @@ -262,6 +262,13 @@ public:
> >  	{
> >  	}
> >
> > +	constexpr Rectangle(const Point &point1, const Point &point2)
> > +		: Rectangle(std::min(point1.x, point2.x), std::min(point1.y, point2.y),
> > +			    std::max(point1.x, point2.x) - std::min(point1.x, point2.x),
> > +			    std::max(point1.y, point2.y) - std::min(point1.y, point2.y))
>
> The subtraction can lead to under/overflow. I believe simply casting to
> `unsigned` avoids that, e.g.:
>
>   unsigned(std::max(point1.x, point2.x)) - unsigned(std::min(point1.x, point2.x))
>
> Although I don't foresee it causing any issues, since the fix is easy,
> I think it's worth doing.

Is this because we're subtracting two signed values and assign the
result to an unsigned ?

It shouldn't be an issue, but better safe than sorry, so I'll happily
take your suggestion in.

Thanks
  j

>
>
> Regards,
> Barnabás Pőcze
>
> > +	{
> > +	}
> > +
> >  	int x;
> >  	int y;
> >  	unsigned int width;
> > [...]
Jacopo Mondi Oct. 1, 2024, 7:55 a.m. UTC | #4
Hi Harvey

On Tue, Oct 01, 2024 at 02:43:20PM GMT, Cheng-Hao Yang wrote:
> Thank you Jacopo for the rebase and fix.
>
> On Tue, Oct 1, 2024 at 3:59 AM Jacopo Mondi
> <jacopo.mondi@ideasonboard.com> 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>
> > ---
> >  include/libcamera/geometry.h |  7 +++++
> >  src/libcamera/geometry.cpp   | 58 ++++++++++++++++++++++++++++++++++++
> >  test/geometry.cpp            | 14 +++++++++
> >  3 files changed, 79 insertions(+)
> >
> > diff --git a/include/libcamera/geometry.h b/include/libcamera/geometry.h
> > index 3e6f0f5d7fab..2cc25f1facd9 100644
> > --- a/include/libcamera/geometry.h
> > +++ b/include/libcamera/geometry.h
> > @@ -262,6 +262,13 @@ public:
> >         {
> >         }
> >
> > +       constexpr Rectangle(const Point &point1, const Point &point2)
> > +               : Rectangle(std::min(point1.x, point2.x), std::min(point1.y, point2.y),
> > +                           std::max(point1.x, point2.x) - std::min(point1.x, point2.x),
> > +                           std::max(point1.y, point2.y) - 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 6eb432e5d803..5e5e72e15d67 100644
> > --- a/src/libcamera/geometry.cpp
> > +++ b/src/libcamera/geometry.cpp
> > @@ -715,6 +715,64 @@ 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 with the two given points
> > + * \param[in] point1 One of corners of the rectangle
> > + * \param[in] point2 The opposite diagonal corner point of \a point1
> > + *
> > + * Contruct a rectangle that spans the space between two given points. The
> > + * position of the two given points is not relevant for the rectangle's
> > + * contruction.
> > + *
> > + * \verbatim
> > +
> > +  p1 = point1
> > +  p2 = point2
> > +
> > +          ^
> > +          |
> > +          |      ----------------p2
> > +          |     |                 |
> > +          |     |                 |
> > +          |     p1----------------
> > +          |
> > +          o------------------------------->
> > +         (0,0)
> > +
> > +          ^
> > +          |
> > +          |     p1----------------
> > +          |     |                 |
> > +          |     |                 |
> > +          |      ----------------p2
> > +          |
> > +          o------------------------------->
> > +         (0,0)
> > +
> > +          ^
> > +          |
> > +          |      ----------------p1
> > +          |     |                 |
> > +          |     |                 |
> > +          |     p2----------------
> > +          |
> > +          o------------------------------->
> > +         (0,0)
> > +
> > +          ^
> > +          |
> > +          |     p2----------------
> > +          |     |                 |
> > +          |     |                 |
> > +          |      ----------------p1
> > +          |
> > +          o------------------------------->
> > +         (0,0)
> > +
> > +   \endverbatim
> > + */
> > +
> >  /**
> >   * \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);
>
> Sorry, I think I did it wrong in v8:
> Maybe we should swap the points of topRight and bottomLeft.

I don't think it's an issue, the "visual" naming again depends on the
reference system orientation, doesn't it ?



 -------------------------------------->
 |
 |     TL                      TR
 |
 |
 |
 |
 |
 |
 |     BL                       BR
 |
 |
 V


 ^
 |
 |     BL                      BR
 |
 |
 |
 |
 |
 |
 |     TL                       TR
 |
 |
 -------------------------------------->

As long as they're opposite points, I think it's fine!

>
> Otherwise LGTM.
>
> Reviewed-by: Harvey Yang <chenghaoyang@chromium.org>

Thanks
  j

>
> BR,
> Harvey
>
> > +               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;
> >         }
> >  };
> > --
> > 2.46.1
> >
Harvey Yang Oct. 1, 2024, 8:17 a.m. UTC | #5
On Tue, Oct 1, 2024 at 3:55 PM Jacopo Mondi
<jacopo.mondi@ideasonboard.com> wrote:
>
> Hi Harvey
>
> On Tue, Oct 01, 2024 at 02:43:20PM GMT, Cheng-Hao Yang wrote:
> > Thank you Jacopo for the rebase and fix.
> >
> > On Tue, Oct 1, 2024 at 3:59 AM Jacopo Mondi
> > <jacopo.mondi@ideasonboard.com> 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>
> > > ---
> > >  include/libcamera/geometry.h |  7 +++++
> > >  src/libcamera/geometry.cpp   | 58 ++++++++++++++++++++++++++++++++++++
> > >  test/geometry.cpp            | 14 +++++++++
> > >  3 files changed, 79 insertions(+)
> > >
> > > diff --git a/include/libcamera/geometry.h b/include/libcamera/geometry.h
> > > index 3e6f0f5d7fab..2cc25f1facd9 100644
> > > --- a/include/libcamera/geometry.h
> > > +++ b/include/libcamera/geometry.h
> > > @@ -262,6 +262,13 @@ public:
> > >         {
> > >         }
> > >
> > > +       constexpr Rectangle(const Point &point1, const Point &point2)
> > > +               : Rectangle(std::min(point1.x, point2.x), std::min(point1.y, point2.y),
> > > +                           std::max(point1.x, point2.x) - std::min(point1.x, point2.x),
> > > +                           std::max(point1.y, point2.y) - 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 6eb432e5d803..5e5e72e15d67 100644
> > > --- a/src/libcamera/geometry.cpp
> > > +++ b/src/libcamera/geometry.cpp
> > > @@ -715,6 +715,64 @@ 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 with the two given points
> > > + * \param[in] point1 One of corners of the rectangle
> > > + * \param[in] point2 The opposite diagonal corner point of \a point1
> > > + *
> > > + * Contruct a rectangle that spans the space between two given points. The
> > > + * position of the two given points is not relevant for the rectangle's
> > > + * contruction.
> > > + *
> > > + * \verbatim
> > > +
> > > +  p1 = point1
> > > +  p2 = point2
> > > +
> > > +          ^
> > > +          |
> > > +          |      ----------------p2
> > > +          |     |                 |
> > > +          |     |                 |
> > > +          |     p1----------------
> > > +          |
> > > +          o------------------------------->
> > > +         (0,0)
> > > +
> > > +          ^
> > > +          |
> > > +          |     p1----------------
> > > +          |     |                 |
> > > +          |     |                 |
> > > +          |      ----------------p2
> > > +          |
> > > +          o------------------------------->
> > > +         (0,0)
> > > +
> > > +          ^
> > > +          |
> > > +          |      ----------------p1
> > > +          |     |                 |
> > > +          |     |                 |
> > > +          |     p2----------------
> > > +          |
> > > +          o------------------------------->
> > > +         (0,0)
> > > +
> > > +          ^
> > > +          |
> > > +          |     p2----------------
> > > +          |     |                 |
> > > +          |     |                 |
> > > +          |      ----------------p1
> > > +          |
> > > +          o------------------------------->
> > > +         (0,0)
> > > +
> > > +   \endverbatim
> > > + */
> > > +
> > >  /**
> > >   * \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);
> >
> > Sorry, I think I did it wrong in v8:
> > Maybe we should swap the points of topRight and bottomLeft.
>
> I don't think it's an issue, the "visual" naming again depends on the
> reference system orientation, doesn't it ?

Ah right, and the naming should fit better with the first example below.
Thanks!
Still LGTM.

BR,
Harvey

>
>
>
>  -------------------------------------->
>  |
>  |     TL                      TR
>  |
>  |
>  |
>  |
>  |
>  |
>  |     BL                       BR
>  |
>  |
>  V
>
>
>  ^
>  |
>  |     BL                      BR
>  |
>  |
>  |
>  |
>  |
>  |
>  |     TL                       TR
>  |
>  |
>  -------------------------------------->
>
> As long as they're opposite points, I think it's fine!
>
> >
> > Otherwise LGTM.
> >
> > Reviewed-by: Harvey Yang <chenghaoyang@chromium.org>
>
> Thanks
>   j
>
> >
> > BR,
> > Harvey
> >
> > > +               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;
> > >         }
> > >  };
> > > --
> > > 2.46.1
> > >
Barnabás Pőcze Oct. 1, 2024, 3:31 p.m. UTC | #6
Hi


2024. október 1., kedd 9:50 keltezéssel, Jacopo Mondi <jacopo.mondi@ideasonboard.com> írta:

> Hi Barnabás
> 
> On Mon, Sep 30, 2024 at 10:50:30PM GMT, Barnabás Pőcze wrote:
> > Hi
> >
> >
> > 2024. szeptember 30., hétfő 21:59 keltezéssel, Jacopo Mondi <jacopo.mondi@ideasonboard.com> írta:
> >
> > > 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>
> > > ---
> > >  include/libcamera/geometry.h |  7 +++++
> > >  src/libcamera/geometry.cpp   | 58 ++++++++++++++++++++++++++++++++++++
> > >  test/geometry.cpp            | 14 +++++++++
> > >  3 files changed, 79 insertions(+)
> > >
> > > diff --git a/include/libcamera/geometry.h b/include/libcamera/geometry.h
> > > index 3e6f0f5d7fab..2cc25f1facd9 100644
> > > --- a/include/libcamera/geometry.h
> > > +++ b/include/libcamera/geometry.h
> > > @@ -262,6 +262,13 @@ public:
> > >  	{
> > >  	}
> > >
> > > +	constexpr Rectangle(const Point &point1, const Point &point2)
> > > +		: Rectangle(std::min(point1.x, point2.x), std::min(point1.y, point2.y),
> > > +			    std::max(point1.x, point2.x) - std::min(point1.x, point2.x),
> > > +			    std::max(point1.y, point2.y) - std::min(point1.y, point2.y))
> >
> > The subtraction can lead to under/overflow. I believe simply casting to
> > `unsigned` avoids that, e.g.:
> >
> >   unsigned(std::max(point1.x, point2.x)) - unsigned(std::min(point1.x, point2.x))
> >
> > Although I don't foresee it causing any issues, since the fix is easy,
> > I think it's worth doing.
> 
> Is this because we're subtracting two signed values and assign the
> result to an unsigned ?

Simply subtracting two signed integers can be problematic. E.g.

  std::numeric_limits<int>::max() - std::numeric_limits<int>::min()

The result is not representable as an `int`. But after casting to `unsigned`,
the subtraction is valid, and result is `std::numeric_limits<unsigned>::max()`
as expected.


Regards,
Barnabás Pőcze


> 
> It shouldn't be an issue, but better safe than sorry, so I'll happily
> take your suggestion in.
> 
> Thanks
>   j
> 
> >
> >
> > Regards,
> > Barnabás Pőcze
> >
> > > +	{
> > > +	}
> > > +
> > >  	int x;
> > >  	int y;
> > >  	unsigned int width;
> > > [...]
>
Laurent Pinchart Oct. 1, 2024, 6:08 p.m. UTC | #7
On Mon, Sep 30, 2024 at 09:59:10PM +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>
> ---
>  include/libcamera/geometry.h |  7 +++++
>  src/libcamera/geometry.cpp   | 58 ++++++++++++++++++++++++++++++++++++
>  test/geometry.cpp            | 14 +++++++++
>  3 files changed, 79 insertions(+)
> 
> diff --git a/include/libcamera/geometry.h b/include/libcamera/geometry.h
> index 3e6f0f5d7fab..2cc25f1facd9 100644
> --- a/include/libcamera/geometry.h
> +++ b/include/libcamera/geometry.h
> @@ -262,6 +262,13 @@ public:
>  	{
>  	}
>  
> +	constexpr Rectangle(const Point &point1, const Point &point2)
> +		: Rectangle(std::min(point1.x, point2.x), std::min(point1.y, point2.y),
> +			    std::max(point1.x, point2.x) - std::min(point1.x, point2.x),
> +			    std::max(point1.y, point2.y) - 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 6eb432e5d803..5e5e72e15d67 100644
> --- a/src/libcamera/geometry.cpp
> +++ b/src/libcamera/geometry.cpp
> @@ -715,6 +715,64 @@ 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 with the two given points
> + * \param[in] point1 One of corners of the rectangle
> + * \param[in] point2 The opposite diagonal corner point of \a point1
> + *
> + * Contruct a rectangle that spans the space between two given points. The
> + * position of the two given points is not relevant for the rectangle's
> + * contruction.
> + *
> + * \verbatim
> +
> +  p1 = point1
> +  p2 = point2
> +
> +          ^
> +          |
> +          |      ----------------p2
> +          |     |                 |
> +          |     |                 |
> +          |     p1----------------
> +          |
> +          o------------------------------->
> +         (0,0)
> +
> +          ^
> +          |
> +          |     p1----------------
> +          |     |                 |
> +          |     |                 |
> +          |      ----------------p2
> +          |
> +          o------------------------------->
> +         (0,0)
> +
> +          ^
> +          |
> +          |      ----------------p1
> +          |     |                 |
> +          |     |                 |
> +          |     p2----------------
> +          |
> +          o------------------------------->
> +         (0,0)
> +
> +          ^
> +          |
> +          |     p2----------------
> +          |     |                 |
> +          |     |                 |
> +          |      ----------------p1
> +          |
> +          o------------------------------->
> +         (0,0)
> +
> +   \endverbatim
> + */

Following the comment on 1/5, I think this is also unnecessarily
complicated.

/**
 * \fn Rectangle::Rectangle(const Point &point1, const Point &point2)
 * \brief Construct a Rectangle from two opposite corners
 * \param[in] point1 One of the corners of the rectangle
 * \param[in] point2 The corner opposite to \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..2cc25f1facd9 100644
--- a/include/libcamera/geometry.h
+++ b/include/libcamera/geometry.h
@@ -262,6 +262,13 @@  public:
 	{
 	}
 
+	constexpr Rectangle(const Point &point1, const Point &point2)
+		: Rectangle(std::min(point1.x, point2.x), std::min(point1.y, point2.y),
+			    std::max(point1.x, point2.x) - std::min(point1.x, point2.x),
+			    std::max(point1.y, point2.y) - 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 6eb432e5d803..5e5e72e15d67 100644
--- a/src/libcamera/geometry.cpp
+++ b/src/libcamera/geometry.cpp
@@ -715,6 +715,64 @@  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 with the two given points
+ * \param[in] point1 One of corners of the rectangle
+ * \param[in] point2 The opposite diagonal corner point of \a point1
+ *
+ * Contruct a rectangle that spans the space between two given points. The
+ * position of the two given points is not relevant for the rectangle's
+ * contruction.
+ *
+ * \verbatim
+
+  p1 = point1
+  p2 = point2
+
+          ^
+          |
+          |      ----------------p2
+          |     |                 |
+          |     |                 |
+          |     p1----------------
+          |
+          o------------------------------->
+         (0,0)
+
+          ^
+          |
+          |     p1----------------
+          |     |                 |
+          |     |                 |
+          |      ----------------p2
+          |
+          o------------------------------->
+         (0,0)
+
+          ^
+          |
+          |      ----------------p1
+          |     |                 |
+          |     |                 |
+          |     p2----------------
+          |
+          o------------------------------->
+         (0,0)
+
+          ^
+          |
+          |     p2----------------
+          |     |                 |
+          |     |                 |
+          |      ----------------p1
+          |
+          o------------------------------->
+         (0,0)
+
+   \endverbatim
+ */
+
 /**
  * \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;
 	}
 };