Visual Servoing Platform  version 3.2.1 under development (2019-05-26)
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 
92  inline double getBottom() const { return (this->top + this->height - 1.0); }
98  {
99  vpImagePoint bottomRight;
100  bottomRight.set_u(getRight());
101  bottomRight.set_v(getBottom());
102 
103  return bottomRight;
104  }
105 
116  inline void getCenter(double &x, double &y) const
117  {
118  x = this->left + this->width / 2.0 - 0.5;
119  y = this->top + this->height / 2.0 - 0.5;
120  }
121 
132  inline vpImagePoint getCenter() const
133  {
134  vpImagePoint center;
135  center.set_u(this->left + this->width / 2.0 - 0.5);
136  center.set_v(this->top + this->height / 2.0 - 0.5);
137  return center;
138  }
139 
147  inline double getHeight() const { return this->height; }
148 
154  inline double getLeft() const { return this->left; }
155 
160  inline double getRight() const { return (this->left + this->width - 1.0); }
161 
166  inline double getSize() const { return (this->width * this->height); }
167 
173  inline double getTop() const { return this->top; }
174 
180  inline vpImagePoint getTopLeft() const
181  {
182  vpImagePoint topLeft;
183  topLeft.set_u(this->left);
184  topLeft.set_v(this->top);
185  return topLeft;
186  }
187 
195  inline double getWidth() const { return this->width; }
196 
200  bool isInside(const vpImagePoint &ip) const;
201 
202  bool operator==(const vpRect &r) const;
203  bool operator!=(const vpRect &r) const;
204  vpRect &operator&=(const vpRect &r);
205  vpRect &operator=(const vpRect &r);
206  vpRect operator&(const vpRect &r) const;
207 
208  friend VISP_EXPORT bool inRectangle(const vpImagePoint &ip, const vpRect &rect);
209  friend VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpRect &r);
210 
211  void set(double left, double top, double width, double height);
212  void set(const vpImagePoint &topLeft, double width, double height);
213  void set(const vpImagePoint &topLeft, const vpImagePoint &bottomRight);
214  void set(const vpRect &r);
215  void set(const std::vector<vpImagePoint> &ip);
216 
224  inline void setBottom(double pos) { this->height = pos - this->top + 1.0; }
225 
232  inline void setBottomRight(const vpImagePoint &bottomRight)
233  {
234  this->height = bottomRight.get_v() - this->top + 1.0;
235  this->width = bottomRight.get_u() - this->left + 1.0;
236  }
237 
244  inline void setHeight(double h)
245  {
246  assert(h > 0);
247  this->height = h;
248  }
249 
257  inline void setLeft(double pos) { this->left = pos; }
258 
268  inline void setRect(double l, double t, double w, double h)
269  {
270  this->left = l;
271  this->top = t;
272  this->width = w;
273  this->height = h;
274  }
275 
283  inline void setRight(double pos) { this->width = pos - this->left + 1.0; }
284 
292  inline void setTop(double pos) { this->top = pos; }
293 
301  inline void setTopLeft(const vpImagePoint &topLeft)
302  {
303  this->left = topLeft.get_u();
304  this->top = topLeft.get_v();
305  }
306 
313  inline void setWidth(double w)
314  {
315  assert(w > 0);
316  this->width = w;
317  }
318 
325  inline void moveCenter(double x, double y)
326  {
327  this->left = x - this->width / 2 + 0.5;
328  this->top = y - this->height / 2 + 0.5;
329  }
330 
337  inline void moveCenter(const vpImagePoint &center)
338  {
339  this->left = center.get_u() - this->width / 2 + 0.5;
340  this->top = center.get_v() - this->height / 2 + 0.5;
341  }
342 
343 private:
344  double left; // Upper left corner position along the columns axis
345  double top; // Upper left corner position along the rows axis
346  double width; // Rectangle width
347  double height; // Rectangle height
348 };
349 
350 #endif
double getTop() const
Definition: vpRect.h:173
double get_v() const
Definition: vpImagePoint.h:274
void getCenter(double &x, double &y) const
Definition: vpRect.h:116
double getSize() const
Definition: vpRect.h:166
void setLeft(double pos)
Definition: vpRect.h:257
double get_u() const
Definition: vpImagePoint.h:263
double getHeight() const
Definition: vpRect.h:147
double getRight() const
Definition: vpRect.h:160
vpImagePoint getTopLeft() const
Definition: vpRect.h:180
double getBottom() const
Definition: vpRect.h:92
double getWidth() const
Definition: vpRect.h:195
void setTop(double pos)
Definition: vpRect.h:292
void setHeight(double h)
Definition: vpRect.h:244
void set_u(const double u)
Definition: vpImagePoint.h:226
vpImagePoint getBottomRight() const
Definition: vpRect.h:97
void moveCenter(const vpImagePoint &center)
Definition: vpRect.h:337
void set_v(const double v)
Definition: vpImagePoint.h:237
void moveCenter(double x, double y)
Definition: vpRect.h:325
void setTopLeft(const vpImagePoint &topLeft)
Definition: vpRect.h:301
void setRight(double pos)
Definition: vpRect.h:283
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
vpImagePoint getCenter() const
Definition: vpRect.h:132
void setRect(double l, double t, double w, double h)
Definition: vpRect.h:268
double getLeft() const
Definition: vpRect.h:154
void setBottomRight(const vpImagePoint &bottomRight)
Definition: vpRect.h:232
void setBottom(double pos)
Definition: vpRect.h:224
void setWidth(double w)
Definition: vpRect.h:313