Visual Servoing Platform  version 3.6.1 under development (2024-04-24)
vpRect.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Defines a rectangle in the plane.
32  */
33 
34 #ifndef vpRect_h
35 #define vpRect_h
36 
69 #include <algorithm>
70 #include <cassert>
71 #include <vector>
72 #include <visp3/core/vpException.h>
73 #include <visp3/core/vpImagePoint.h>
74 
75 class VISP_EXPORT vpRect
76 {
77 public:
78  vpRect();
79  vpRect(double left, double top, double width, double height);
80  vpRect(const vpImagePoint &topLeft, double width, double height);
81  vpRect(const vpImagePoint &topLeft, const vpImagePoint &bottomRight);
82  vpRect(const vpRect &r);
83  explicit vpRect(const std::vector<vpImagePoint> &ip);
84 
88  inline double getArea() const { return width * height; }
89 
94  inline double getBottom() const { return (this->top + this->height - 1.0); }
95 
102  {
103  vpImagePoint bottomLeft;
104  bottomLeft.set_u(getLeft());
105  bottomLeft.set_v(getBottom());
106  return bottomLeft;
107  }
108 
115  {
116  vpImagePoint bottomRight;
117  bottomRight.set_u(getRight());
118  bottomRight.set_v(getBottom());
119 
120  return bottomRight;
121  }
122 
133  inline void getCenter(double &x, double &y) const
134  {
135  x = this->left + this->width / 2.0 - 0.5;
136  y = this->top + this->height / 2.0 - 0.5;
137  }
138 
149  inline vpImagePoint getCenter() const
150  {
151  vpImagePoint center;
152  center.set_u(this->left + this->width / 2.0 - 0.5);
153  center.set_v(this->top + this->height / 2.0 - 0.5);
154  return center;
155  }
156 
163  inline double getHeight() const { return this->height; }
164 
170  inline double getLeft() const { return this->left; }
171 
176  inline double getRight() const { return (this->left + this->width - 1.0); }
177 
182  inline double getSize() const { return (this->width * this->height); }
183 
189  inline double getTop() const { return this->top; }
190 
196  inline vpImagePoint getTopLeft() const
197  {
198  vpImagePoint topLeft;
199  topLeft.set_u(getLeft());
200  topLeft.set_v(getTop());
201  return topLeft;
202  }
203 
209  inline vpImagePoint getTopRight() const
210  {
211  vpImagePoint topRight;
212  topRight.set_u(getRight());
213  topRight.set_v(getTop());
214  return topRight;
215  }
216 
224  inline double getWidth() const { return this->width; }
225 
229  bool isInside(const vpImagePoint &ip) const;
230 
231  bool operator==(const vpRect &r) const;
232  bool operator!=(const vpRect &r) const;
233 
240  inline vpRect &operator&=(const vpRect &r)
241  {
242  double x1 = std::max<double>(left, r.left);
243  double y1 = std::max<double>(top, r.top);
244  width = std::min<double>(left + width, r.left + r.width) - x1;
245  height = std::min<double>(top + height, r.top + r.height) - y1;
246  left = x1;
247  top = y1;
248 
249  if (width <= 0 || height <= 0) {
250  *this = vpRect();
251  }
252 
253  return *this;
254  }
255 
256  vpRect &operator=(const vpRect &r);
257 
263  inline vpRect operator&(const vpRect &r) const
264  {
265  vpRect a = *this;
266  return a &= r;
267  }
268 
269  friend VISP_EXPORT bool inRectangle(const vpImagePoint &ip, const vpRect &rect);
270  friend VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpRect &r);
271 
272  void set(double left, double top, double width, double height);
273  void set(const vpImagePoint &topLeft, double width, double height);
274  void set(const vpImagePoint &topLeft, const vpImagePoint &bottomRight);
275  void set(const vpRect &r);
276  void set(const std::vector<vpImagePoint> &ip);
277 
285  inline void setBottom(double pos) { this->height = pos - this->top + 1.0; }
286 
293  inline void setBottomRight(const vpImagePoint &bottomRight)
294  {
295  this->height = bottomRight.get_v() - this->top + 1.0;
296  this->width = bottomRight.get_u() - this->left + 1.0;
297  }
298 
305  inline void setHeight(double h)
306  {
307  assert(h > 0);
308  this->height = h;
309  }
310 
318  inline void setLeft(double pos) { this->left = pos; }
319 
330  inline void setRect(double l, double t, double w, double h)
331  {
332  this->left = l;
333  this->top = t;
334  this->width = w;
335  this->height = h;
336  }
337 
345  inline void setRight(double pos) { this->width = pos - this->left + 1.0; }
346 
354  inline void setTop(double pos) { this->top = pos; }
355 
363  inline void setTopLeft(const vpImagePoint &topLeft)
364  {
365  this->left = topLeft.get_u();
366  this->top = topLeft.get_v();
367  }
368 
375  inline void setWidth(double w)
376  {
377  assert(w > 0);
378  this->width = w;
379  }
380 
387  inline void moveCenter(double x, double y)
388  {
389  this->left = x - this->width / 2 + 0.5;
390  this->top = y - this->height / 2 + 0.5;
391  }
392 
399  inline void moveCenter(const vpImagePoint &center)
400  {
401  this->left = center.get_u() - this->width / 2 + 0.5;
402  this->top = center.get_v() - this->height / 2 + 0.5;
403  }
404 
405 private:
406  double left; // Upper left corner position along the columns axis
407  double top; // Upper left corner position along the rows axis
408  double width; // Rectangle width
409  double height; // Rectangle height
410 };
411 
412 #endif
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
double get_u() const
Definition: vpImagePoint.h:136
void set_u(double u)
Definition: vpImagePoint.h:330
void set_v(double v)
Definition: vpImagePoint.h:341
double get_v() const
Definition: vpImagePoint.h:147
Defines a rectangle in the plane.
Definition: vpRect.h:76
void getCenter(double &x, double &y) const
Definition: vpRect.h:133
vpRect & operator&=(const vpRect &r)
Definition: vpRect.h:240
void setHeight(double h)
Definition: vpRect.h:305
double getWidth() const
Definition: vpRect.h:224
void moveCenter(const vpImagePoint &center)
Definition: vpRect.h:399
void moveCenter(double x, double y)
Definition: vpRect.h:387
vpImagePoint getBottomLeft() const
Definition: vpRect.h:101
void setBottomRight(const vpImagePoint &bottomRight)
Definition: vpRect.h:293
void setWidth(double w)
Definition: vpRect.h:375
void setTop(double pos)
Definition: vpRect.h:354
double getLeft() const
Definition: vpRect.h:170
double getSize() const
Definition: vpRect.h:182
void setTopLeft(const vpImagePoint &topLeft)
Definition: vpRect.h:363
vpImagePoint getTopLeft() const
Definition: vpRect.h:196
vpImagePoint getTopRight() const
Definition: vpRect.h:209
vpRect operator&(const vpRect &r) const
Definition: vpRect.h:263
void setRect(double l, double t, double w, double h)
Definition: vpRect.h:330
void setLeft(double pos)
Definition: vpRect.h:318
vpImagePoint getCenter() const
Definition: vpRect.h:149
void setRight(double pos)
Definition: vpRect.h:345
double getRight() const
Definition: vpRect.h:176
double getBottom() const
Definition: vpRect.h:94
double getArea() const
Definition: vpRect.h:88
double getHeight() const
Definition: vpRect.h:163
void setBottom(double pos)
Definition: vpRect.h:285
double getTop() const
Definition: vpRect.h:189
vpImagePoint getBottomRight() const
Definition: vpRect.h:114