Visual Servoing Platform  version 3.6.1 under development (2024-12-12)
vpRect.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 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 VP_RECT_H
35 #define VP_RECT_H
36 
37 #include <algorithm>
38 #include <cassert>
39 #include <limits> // numeric_limits
40 #include <vector>
41 #include <visp3/core/vpConfig.h>
42 #include <visp3/core/vpException.h>
43 #include <visp3/core/vpImagePoint.h>
44 
45 BEGIN_VISP_NAMESPACE
46 
78 class VISP_EXPORT vpRect
79 {
80 public:
81  vpRect();
82  vpRect(double left, double top, double width, double height);
83  vpRect(const vpImagePoint &topLeft, double width, double height);
84  vpRect(const vpImagePoint &topLeft, const vpImagePoint &bottomRight);
85  vpRect(const vpRect &r);
86  VP_EXPLICIT vpRect(const std::vector<vpImagePoint> &ip);
87 
91  inline double getArea() const { return width * height; }
92 
97  inline double getBottom() const { return ((this->top + this->height) - 1.0); }
98 
105  {
106  vpImagePoint bottomLeft;
107  bottomLeft.set_u(getLeft());
108  bottomLeft.set_v(getBottom());
109  return bottomLeft;
110  }
111 
118  {
119  vpImagePoint bottomRight;
120  bottomRight.set_u(getRight());
121  bottomRight.set_v(getBottom());
122 
123  return bottomRight;
124  }
125 
136  inline void getCenter(double &x, double &y) const
137  {
138  x = (this->left + (this->width / 2.0)) - 0.5;
139  y = (this->top + (this->height / 2.0)) - 0.5;
140  }
141 
152  inline vpImagePoint getCenter() const
153  {
154  vpImagePoint center;
155  center.set_u((this->left + (this->width / 2.0)) - 0.5);
156  center.set_v((this->top + (this->height / 2.0)) - 0.5);
157  return center;
158  }
159 
166  inline double getHeight() const { return this->height; }
167 
173  inline double getLeft() const { return this->left; }
174 
179  inline double getRight() const { return ((this->left + this->width) - 1.0); }
180 
185  inline double getSize() const { return (this->width * this->height); }
186 
192  inline double getTop() const { return this->top; }
193 
199  inline vpImagePoint getTopLeft() const
200  {
201  vpImagePoint topLeft;
202  topLeft.set_u(getLeft());
203  topLeft.set_v(getTop());
204  return topLeft;
205  }
206 
212  inline vpImagePoint getTopRight() const
213  {
214  vpImagePoint topRight;
215  topRight.set_u(getRight());
216  topRight.set_v(getTop());
217  return topRight;
218  }
219 
227  inline double getWidth() const { return this->width; }
228 
232  bool isInside(const vpImagePoint &ip) const;
233 
234  bool operator==(const vpRect &r) const;
235  bool operator!=(const vpRect &r) const;
236 
243  inline vpRect &operator&=(const vpRect &r)
244  {
245  double x1 = std::max<double>(left, r.left);
246  double y1 = std::max<double>(top, r.top);
247  width = std::min<double>(left + width, r.left + r.width) - x1;
248  height = std::min<double>(top + height, r.top + r.height) - y1;
249  left = x1;
250  top = y1;
251 
252  if ((width <= 0) || (height <= 0)) {
253  *this = vpRect();
254  }
255 
256  return *this;
257  }
258 
259  vpRect &operator=(const vpRect &r);
260 
266  inline vpRect operator&(const vpRect &r) const
267  {
268  vpRect a = *this;
269  return a &= r;
270  }
271 
272  friend VISP_EXPORT bool inRectangle(const vpImagePoint &ip, const vpRect &rect);
273  friend VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpRect &r);
274 
275  void set(double left, double top, double width, double height);
276  void set(const vpImagePoint &topLeft, double width, double height);
277  void set(const vpImagePoint &topLeft, const vpImagePoint &bottomRight);
278  void set(const vpRect &r);
279  void set(const std::vector<vpImagePoint> &ip);
280 
288  inline void setBottom(double pos) { this->height = (pos - this->top) + 1.0; }
289 
296  inline void setBottomRight(const vpImagePoint &bottomRight)
297  {
298  this->height = (bottomRight.get_v() - this->top) + 1.0;
299  this->width = (bottomRight.get_u() - this->left) + 1.0;
300  }
301 
308  inline void setHeight(double h)
309  {
310  assert(h > 0);
311  this->height = h;
312  }
313 
321  inline void setLeft(double pos) { this->left = pos; }
322 
333  inline void setRect(double l, double t, double w, double h)
334  {
335  this->left = l;
336  this->top = t;
337  this->width = w;
338  this->height = h;
339  }
340 
348  inline void setRight(double pos) { this->width = (pos - this->left) + 1.0; }
349 
357  inline void setTop(double pos) { this->top = pos; }
358 
366  inline void setTopLeft(const vpImagePoint &topLeft)
367  {
368  this->left = topLeft.get_u();
369  this->top = topLeft.get_v();
370  }
371 
378  inline void setWidth(double w)
379  {
380  assert(w > 0);
381  this->width = w;
382  }
383 
390  inline void moveCenter(double x, double y)
391  {
392  const unsigned int magic_2 = 2;
393  this->left = (x - (this->width / magic_2)) + 0.5;
394  this->top = (y - (this->height / magic_2)) + 0.5;
395  }
396 
403  inline void moveCenter(const vpImagePoint &center)
404  {
405  const unsigned int magic_2 = 2;
406  this->left = (center.get_u() - (this->width / magic_2)) + 0.5;
407  this->top = (center.get_v() - (this->height / magic_2)) + 0.5;
408  }
409 
410 private:
411  double left; // Upper left corner position along the columns axis
412  double top; // Upper left corner position along the rows axis
413  double width; // Rectangle width
414  double height; // Rectangle height
415 };
416 END_VISP_NAMESPACE
417 #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:335
void set_v(double v)
Definition: vpImagePoint.h:346
double get_v() const
Definition: vpImagePoint.h:147
Defines a rectangle in the plane.
Definition: vpRect.h:79
void getCenter(double &x, double &y) const
Definition: vpRect.h:136
vpRect & operator&=(const vpRect &r)
Definition: vpRect.h:243
void setHeight(double h)
Definition: vpRect.h:308
double getWidth() const
Definition: vpRect.h:227
void moveCenter(const vpImagePoint &center)
Definition: vpRect.h:403
void moveCenter(double x, double y)
Definition: vpRect.h:390
vpImagePoint getBottomLeft() const
Definition: vpRect.h:104
void setBottomRight(const vpImagePoint &bottomRight)
Definition: vpRect.h:296
void setWidth(double w)
Definition: vpRect.h:378
void setTop(double pos)
Definition: vpRect.h:357
double getLeft() const
Definition: vpRect.h:173
double getSize() const
Definition: vpRect.h:185
void setTopLeft(const vpImagePoint &topLeft)
Definition: vpRect.h:366
vpImagePoint getTopLeft() const
Definition: vpRect.h:199
vpImagePoint getTopRight() const
Definition: vpRect.h:212
vpRect operator&(const vpRect &r) const
Definition: vpRect.h:266
void setRect(double l, double t, double w, double h)
Definition: vpRect.h:333
void setLeft(double pos)
Definition: vpRect.h:321
vpImagePoint getCenter() const
Definition: vpRect.h:152
void setRight(double pos)
Definition: vpRect.h:348
double getRight() const
Definition: vpRect.h:179
double getBottom() const
Definition: vpRect.h:97
double getArea() const
Definition: vpRect.h:91
double getHeight() const
Definition: vpRect.h:166
void setBottom(double pos)
Definition: vpRect.h:288
double getTop() const
Definition: vpRect.h:192
vpImagePoint getBottomRight() const
Definition: vpRect.h:117