Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
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 
88  vpRect &operator=(const vpRect &r);
89 
94  inline double getBottom() const { return (this->top + this->height - 1.0); }
100  {
101  vpImagePoint bottomRight;
102  bottomRight.set_u(getRight());
103  bottomRight.set_v(getBottom());
104 
105  return bottomRight;
106  }
107 
118  inline void getCenter(double &x, double &y) const
119  {
120  x = this->left + this->width / 2.0 - 0.5;
121  y = this->top + this->height / 2.0 - 0.5;
122  }
123 
134  inline vpImagePoint getCenter() const
135  {
136  vpImagePoint center;
137  center.set_u(this->left + this->width / 2.0 - 0.5);
138  center.set_v(this->top + this->height / 2.0 - 0.5);
139  return center;
140  }
141 
149  inline double getHeight() const { return this->height; }
150 
156  inline double getLeft() const { return this->left; }
157 
162  inline double getRight() const { return (this->left + this->width - 1.0); }
163 
168  inline double getSize() const { return (this->width * this->height); }
169 
175  inline double getTop() const { return this->top; }
176 
182  inline vpImagePoint getTopLeft() const
183  {
184  vpImagePoint topLeft;
185  topLeft.set_u(this->left);
186  topLeft.set_v(this->top);
187  return topLeft;
188  }
189 
197  inline double getWidth() const { return this->width; }
198 
202  bool isInside(const vpImagePoint &ip) const;
203 
204  bool operator==(const vpRect &r) const;
205  bool operator!=(const vpRect &r) const;
206  vpRect &operator&=(const vpRect &r);
207  vpRect operator&(const vpRect &r) const;
208 
209  friend VISP_EXPORT bool inRectangle(const vpImagePoint &ip, const vpRect &rect);
210  friend VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpRect &r);
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:175
double get_v() const
Definition: vpImagePoint.h:274
void getCenter(double &x, double &y) const
Definition: vpRect.h:118
double getSize() const
Definition: vpRect.h:168
void setLeft(double pos)
Definition: vpRect.h:257
double get_u() const
Definition: vpImagePoint.h:263
double getHeight() const
Definition: vpRect.h:149
double getRight() const
Definition: vpRect.h:162
vpImagePoint getTopLeft() const
Definition: vpRect.h:182
double getBottom() const
Definition: vpRect.h:94
double getWidth() const
Definition: vpRect.h:197
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:99
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:134
void setRect(double l, double t, double w, double h)
Definition: vpRect.h:268
double getLeft() const
Definition: vpRect.h:156
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