Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpRect.h
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
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 http://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  * Author:
34  * Fabien Spindler
35  *
36  *****************************************************************************/
37 
38 
39 #ifndef vpRect_h
40 #define vpRect_h
41 
42 
43 
44 
76 #include <vector>
77 #include <cassert>
78 #include <visp3/core/vpException.h>
79 #include <visp3/core/vpImagePoint.h>
80 
81 
82 class VISP_EXPORT vpRect
83 {
84 public:
85 
86  vpRect();
87  vpRect(double left, double top, double width, double height);
88  vpRect(const vpImagePoint &topLeft, double width, double height);
89  vpRect(const vpImagePoint &topLeft, const vpImagePoint &bottomRight);
90  vpRect(const vpRect& r);
91  vpRect(const std::vector<vpImagePoint> &ip);
92 
93 
94  vpRect &operator=(const vpRect& r);
95 
100  inline double getBottom() const { return (this->top + this->height - 1.0); }
105  inline vpImagePoint getBottomRight() const {
106  vpImagePoint bottomRight;
107  bottomRight.set_u( getRight() );
108  bottomRight.set_v( getBottom() );
109 
110  return bottomRight;
111  }
112 
123  inline void getCenter(double & x, double & y) const {
124  x = this->left + this->width / 2.0 - 0.5;
125  y = this->top + this->height / 2.0 - 0.5;
126  }
127 
138  inline vpImagePoint getCenter() const {
139  vpImagePoint center;
140  center.set_u( this->left + this->width / 2.0 - 0.5 );
141  center.set_v( this->top + this->height / 2.0 - 0.5 );
142  return center;
143  }
144 
152  inline double getHeight() const { return this->height; }
153 
159  inline double getLeft() const { return this->left; }
160 
165  inline double getRight() const { return (this->left + this->width - 1.0); }
166 
171  inline double getSize() const { return (this->width * this->height); }
172 
178  inline double getTop() const { return this->top; }
179 
185  inline vpImagePoint getTopLeft() const {
186  vpImagePoint topLeft;
187  topLeft.set_u( this->left );
188  topLeft.set_v( this->top );
189  return topLeft;
190  }
191 
199  inline double getWidth() const { return this->width; }
200 
204  bool isInside( const vpImagePoint &ip ) const;
205 
206  bool operator==(const vpRect &r) const;
207  bool operator!=(const vpRect &r) const;
208  vpRect& operator&=(const vpRect &r);
209  vpRect operator&(const vpRect &r) const;
210 
211  friend VISP_EXPORT bool inRectangle( const vpImagePoint &ip, const vpRect &rect );
212  friend VISP_EXPORT std::ostream& operator<< (std::ostream &os, const vpRect& r);
213  void set(double left, double top, double width, double height);
214  void set(const vpImagePoint &topLeft, double width, double height);
215  void set(const vpImagePoint &topLeft, const vpImagePoint &bottomRight);
216  void set(const vpRect& r);
217  void set(const std::vector<vpImagePoint> &ip);
218 
226  inline void setBottom(double pos) { this->height = pos - this->top + 1.0; }
227 
234  inline void setBottomRight(const vpImagePoint &bottomRight) {
235  this->height = bottomRight.get_v() - this->top + 1.0;
236  this->width = bottomRight.get_u() - this->left + 1.0;
237  }
238 
245  inline void setHeight(double h) {
246  assert(h > 0);
247  this->height = h;
248  }
249 
257  inline void setLeft(double pos) { this->left = pos; }
258 
269  inline void setRect(double l, double t, double w, double h) {
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  this->left = topLeft.get_u();
303  this->top = topLeft.get_v();
304  }
305 
312  inline void setWidth(double w) {
313  assert(w > 0);
314  this->width = w;
315  }
316 
323  inline void moveCenter(double x, double y) {
324  this->left = x - this->width/2 + 0.5;
325  this->top = y - this->height/2 + 0.5;
326  }
327 
334  inline void moveCenter(const vpImagePoint &center) {
335  this->left = center.get_u() - this->width/2 + 0.5;
336  this->top = center.get_v() - this->height/2 + 0.5;
337  }
338 
339 private:
340  double left; // Upper left corner position along the columns axis
341  double top; // Upper left corner position along the rows axis
342  double width; // Rectangle width
343  double height; // Rectangle height
344 };
345 
346 #endif
double getTop() const
Definition: vpRect.h:178
double get_v() const
Definition: vpImagePoint.h:268
void getCenter(double &x, double &y) const
Definition: vpRect.h:123
double getSize() const
Definition: vpRect.h:171
void setLeft(double pos)
Definition: vpRect.h:257
double get_u() const
Definition: vpImagePoint.h:257
double getHeight() const
Definition: vpRect.h:152
double getRight() const
Definition: vpRect.h:165
vpImagePoint getTopLeft() const
Definition: vpRect.h:185
double getBottom() const
Definition: vpRect.h:100
double getWidth() const
Definition: vpRect.h:199
void setTop(double pos)
Definition: vpRect.h:292
void setHeight(double h)
Definition: vpRect.h:245
void set_u(const double u)
Definition: vpImagePoint.h:221
vpImagePoint getBottomRight() const
Definition: vpRect.h:105
void moveCenter(const vpImagePoint &center)
Definition: vpRect.h:334
void set_v(const double v)
Definition: vpImagePoint.h:232
void moveCenter(double x, double y)
Definition: vpRect.h:323
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:82
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:138
void setRect(double l, double t, double w, double h)
Definition: vpRect.h:269
double getLeft() const
Definition: vpRect.h:159
void setBottomRight(const vpImagePoint &bottomRight)
Definition: vpRect.h:234
void setBottom(double pos)
Definition: vpRect.h:226
void setWidth(double w)
Definition: vpRect.h:312