Visual Servoing Platform  version 3.5.0 under development (2022-02-15)
vpRect.h
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Defines a rectangle in the plane.
33  *
34  * Author:
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
39 #ifndef vpRect_h
40 #define vpRect_h
41 
73 #include <cassert>
74 #include <vector>
75 #include <algorithm>
76 #include <visp3/core/vpException.h>
77 #include <visp3/core/vpImagePoint.h>
78 
79 class VISP_EXPORT vpRect
80 {
81 public:
82  vpRect();
83  vpRect(double left, double top, double width, double height);
84  vpRect(const vpImagePoint &topLeft, double width, double height);
85  vpRect(const vpImagePoint &topLeft, const vpImagePoint &bottomRight);
86  vpRect(const vpRect &r);
87  explicit vpRect(const std::vector<vpImagePoint> &ip);
88 
92  inline double getArea() const { return width * height; }
93 
98  inline double getBottom() const { return (this->top + this->height - 1.0); }
99 
106  {
107  vpImagePoint bottomLeft;
108  bottomLeft.set_u(getLeft());
109  bottomLeft.set_v(getBottom());
110  return bottomLeft;
111  }
112 
119  {
120  vpImagePoint bottomRight;
121  bottomRight.set_u(getRight());
122  bottomRight.set_v(getBottom());
123 
124  return bottomRight;
125  }
126 
137  inline void getCenter(double &x, double &y) const
138  {
139  x = this->left + this->width / 2.0 - 0.5;
140  y = this->top + this->height / 2.0 - 0.5;
141  }
142 
153  inline vpImagePoint getCenter() const
154  {
155  vpImagePoint center;
156  center.set_u(this->left + this->width / 2.0 - 0.5);
157  center.set_v(this->top + this->height / 2.0 - 0.5);
158  return center;
159  }
160 
167  inline double getHeight() const { return this->height; }
168 
174  inline double getLeft() const { return this->left; }
175 
180  inline double getRight() const { return (this->left + this->width - 1.0); }
181 
186  inline double getSize() const { return (this->width * this->height); }
187 
193  inline double getTop() const { return this->top; }
194 
200  inline vpImagePoint getTopLeft() const
201  {
202  vpImagePoint topLeft;
203  topLeft.set_u(getLeft());
204  topLeft.set_v(getTop());
205  return topLeft;
206  }
207 
213  inline vpImagePoint getTopRight() const
214  {
215  vpImagePoint topRight;
216  topRight.set_u(getRight());
217  topRight.set_v(getTop());
218  return topRight;
219  }
220 
228  inline double getWidth() const { return this->width; }
229 
233  bool isInside(const vpImagePoint &ip) const;
234 
235  bool operator==(const vpRect &r) const;
236  bool operator!=(const vpRect &r) const;
237 
244  inline vpRect & operator &=(const vpRect &r)
245  {
246  double x1 = (std::max)(left, r.left);
247  double y1 = (std::max)(top, r.top);
248  width = (std::min)(left + width, r.left + r.width) - x1;
249  height = (std::min)(top + height, r.top + r.height) - y1;
250  left = x1;
251  top = y1;
252 
253  if (width <= 0 || height <= 0) {
254  *this = vpRect();
255  }
256 
257  return *this;
258  }
259 
260  vpRect &operator=(const vpRect &r);
261 
267  inline vpRect operator &(const vpRect &r) const
268  {
269  vpRect a = *this;
270  return a &= r;
271  }
272 
273  friend VISP_EXPORT bool inRectangle(const vpImagePoint &ip, const vpRect &rect);
274  friend VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpRect &r);
275 
276  void set(double left, double top, double width, double height);
277  void set(const vpImagePoint &topLeft, double width, double height);
278  void set(const vpImagePoint &topLeft, const vpImagePoint &bottomRight);
279  void set(const vpRect &r);
280  void set(const std::vector<vpImagePoint> &ip);
281 
289  inline void setBottom(double pos) { this->height = pos - this->top + 1.0; }
290 
297  inline void setBottomRight(const vpImagePoint &bottomRight)
298  {
299  this->height = bottomRight.get_v() - this->top + 1.0;
300  this->width = bottomRight.get_u() - this->left + 1.0;
301  }
302 
309  inline void setHeight(double h)
310  {
311  assert(h > 0);
312  this->height = h;
313  }
314 
322  inline void setLeft(double pos) { this->left = pos; }
323 
334  inline void setRect(double l, double t, double w, double h)
335  {
336  this->left = l;
337  this->top = t;
338  this->width = w;
339  this->height = h;
340  }
341 
349  inline void setRight(double pos) { this->width = pos - this->left + 1.0; }
350 
358  inline void setTop(double pos) { this->top = pos; }
359 
367  inline void setTopLeft(const vpImagePoint &topLeft)
368  {
369  this->left = topLeft.get_u();
370  this->top = topLeft.get_v();
371  }
372 
379  inline void setWidth(double w)
380  {
381  assert(w > 0);
382  this->width = w;
383  }
384 
391  inline void moveCenter(double x, double y)
392  {
393  this->left = x - this->width / 2 + 0.5;
394  this->top = y - this->height / 2 + 0.5;
395  }
396 
403  inline void moveCenter(const vpImagePoint &center)
404  {
405  this->left = center.get_u() - this->width / 2 + 0.5;
406  this->top = center.get_v() - this->height / 2 + 0.5;
407  }
408 
409 private:
410  double left; // Upper left corner position along the columns axis
411  double top; // Upper left corner position along the rows axis
412  double width; // Rectangle width
413  double height; // Rectangle height
414 };
415 
416 #endif
double getTop() const
Definition: vpRect.h:193
vpImagePoint getTopRight() const
Definition: vpRect.h:213
void set_u(double u)
Definition: vpImagePoint.h:225
double getArea() const
Definition: vpRect.h:92
void setLeft(double pos)
Definition: vpRect.h:322
void getCenter(double &x, double &y) const
Definition: vpRect.h:137
vpImagePoint getTopLeft() const
Definition: vpRect.h:200
double getRight() const
Definition: vpRect.h:180
double get_u() const
Definition: vpImagePoint.h:262
void setTop(double pos)
Definition: vpRect.h:358
double getWidth() const
Definition: vpRect.h:228
void setHeight(double h)
Definition: vpRect.h:309
vpImagePoint getCenter() const
Definition: vpRect.h:153
void moveCenter(const vpImagePoint &center)
Definition: vpRect.h:403
void moveCenter(double x, double y)
Definition: vpRect.h:391
double getLeft() const
Definition: vpRect.h:174
void setTopLeft(const vpImagePoint &topLeft)
Definition: vpRect.h:367
double getSize() const
Definition: vpRect.h:186
void set_v(double v)
Definition: vpImagePoint.h:236
void setRight(double pos)
Definition: vpRect.h:349
double getHeight() const
Definition: vpRect.h:167
vpImagePoint getBottomRight() const
Definition: vpRect.h:118
Defines a rectangle in the plane.
Definition: vpRect.h:79
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:87
void setRect(double l, double t, double w, double h)
Definition: vpRect.h:334
double getBottom() const
Definition: vpRect.h:98
void setBottomRight(const vpImagePoint &bottomRight)
Definition: vpRect.h:297
void setBottom(double pos)
Definition: vpRect.h:289
double get_v() const
Definition: vpImagePoint.h:273
vpImagePoint getBottomLeft() const
Definition: vpRect.h:105
void setWidth(double w)
Definition: vpRect.h:379