Visual Servoing Platform  version 3.2.1 under development (2019-08-19)
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 <visp3/core/vpException.h>
76 #include <visp3/core/vpImagePoint.h>
77 
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  explicit vpRect(const std::vector<vpImagePoint> &ip);
87 
91  inline double getArea() const
92  {
93  return width*height;
94  }
95 
100  inline double getBottom() const { return (this->top + this->height - 1.0); }
101 
107  {
108  vpImagePoint bottomRight;
109  bottomRight.set_u(getRight());
110  bottomRight.set_v(getBottom());
111 
112  return bottomRight;
113  }
114 
125  inline void getCenter(double &x, double &y) const
126  {
127  x = this->left + this->width / 2.0 - 0.5;
128  y = this->top + this->height / 2.0 - 0.5;
129  }
130 
141  inline vpImagePoint getCenter() const
142  {
143  vpImagePoint center;
144  center.set_u(this->left + this->width / 2.0 - 0.5);
145  center.set_v(this->top + this->height / 2.0 - 0.5);
146  return center;
147  }
148 
155  inline double getHeight() const { return this->height; }
156 
162  inline double getLeft() const { return this->left; }
163 
168  inline double getRight() const { return (this->left + this->width - 1.0); }
169 
174  inline double getSize() const { return (this->width * this->height); }
175 
181  inline double getTop() const { return this->top; }
182 
188  inline vpImagePoint getTopLeft() const
189  {
190  vpImagePoint topLeft;
191  topLeft.set_u(this->left);
192  topLeft.set_v(this->top);
193  return topLeft;
194  }
195 
203  inline double getWidth() const { return this->width; }
204 
208  bool isInside(const vpImagePoint &ip) const;
209 
210  bool operator==(const vpRect &r) const;
211  bool operator!=(const vpRect &r) const;
212  vpRect &operator&=(const vpRect &r);
213  vpRect &operator=(const vpRect &r);
214  vpRect operator&(const vpRect &r) const;
215 
216  friend VISP_EXPORT bool inRectangle(const vpImagePoint &ip, const vpRect &rect);
217  friend VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpRect &r);
218 
219  void set(double left, double top, double width, double height);
220  void set(const vpImagePoint &topLeft, double width, double height);
221  void set(const vpImagePoint &topLeft, const vpImagePoint &bottomRight);
222  void set(const vpRect &r);
223  void set(const std::vector<vpImagePoint> &ip);
224 
232  inline void setBottom(double pos) { this->height = pos - this->top + 1.0; }
233 
240  inline void setBottomRight(const vpImagePoint &bottomRight)
241  {
242  this->height = bottomRight.get_v() - this->top + 1.0;
243  this->width = bottomRight.get_u() - this->left + 1.0;
244  }
245 
252  inline void setHeight(double h)
253  {
254  assert(h > 0);
255  this->height = h;
256  }
257 
265  inline void setLeft(double pos) { this->left = pos; }
266 
276  inline void setRect(double l, double t, double w, double h)
277  {
278  this->left = l;
279  this->top = t;
280  this->width = w;
281  this->height = h;
282  }
283 
291  inline void setRight(double pos) { this->width = pos - this->left + 1.0; }
292 
300  inline void setTop(double pos) { this->top = pos; }
301 
309  inline void setTopLeft(const vpImagePoint &topLeft)
310  {
311  this->left = topLeft.get_u();
312  this->top = topLeft.get_v();
313  }
314 
321  inline void setWidth(double w)
322  {
323  assert(w > 0);
324  this->width = w;
325  }
326 
333  inline void moveCenter(double x, double y)
334  {
335  this->left = x - this->width / 2 + 0.5;
336  this->top = y - this->height / 2 + 0.5;
337  }
338 
345  inline void moveCenter(const vpImagePoint &center)
346  {
347  this->left = center.get_u() - this->width / 2 + 0.5;
348  this->top = center.get_v() - this->height / 2 + 0.5;
349  }
350 
351 private:
352  double left; // Upper left corner position along the columns axis
353  double top; // Upper left corner position along the rows axis
354  double width; // Rectangle width
355  double height; // Rectangle height
356 };
357 
358 #endif
double getTop() const
Definition: vpRect.h:181
double getArea() const
Definition: vpRect.h:91
void setLeft(double pos)
Definition: vpRect.h:265
void getCenter(double &x, double &y) const
Definition: vpRect.h:125
vpImagePoint getTopLeft() const
Definition: vpRect.h:188
double getRight() const
Definition: vpRect.h:168
double get_u() const
Definition: vpImagePoint.h:263
void setTop(double pos)
Definition: vpRect.h:300
double getWidth() const
Definition: vpRect.h:203
void setHeight(double h)
Definition: vpRect.h:252
vpImagePoint getCenter() const
Definition: vpRect.h:141
void set_u(const double u)
Definition: vpImagePoint.h:226
void moveCenter(const vpImagePoint &center)
Definition: vpRect.h:345
void set_v(const double v)
Definition: vpImagePoint.h:237
void moveCenter(double x, double y)
Definition: vpRect.h:333
double getLeft() const
Definition: vpRect.h:162
void setTopLeft(const vpImagePoint &topLeft)
Definition: vpRect.h:309
double getSize() const
Definition: vpRect.h:174
void setRight(double pos)
Definition: vpRect.h:291
double getHeight() const
Definition: vpRect.h:155
vpImagePoint getBottomRight() const
Definition: vpRect.h:106
Defines a rectangle in the plane.
Definition: vpRect.h:78
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
void setRect(double l, double t, double w, double h)
Definition: vpRect.h:276
double getBottom() const
Definition: vpRect.h:100
void setBottomRight(const vpImagePoint &bottomRight)
Definition: vpRect.h:240
void setBottom(double pos)
Definition: vpRect.h:232
double get_v() const
Definition: vpImagePoint.h:274
void setWidth(double w)
Definition: vpRect.h:321