Visual Servoing Platform  version 3.0.0
vpRect.h
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 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 <visp3/core/vpException.h>
78 #include <visp3/core/vpImagePoint.h>
79 
80 
81 class VISP_EXPORT vpRect
82 {
83 public:
84 
85  vpRect();
86  vpRect(double left, double top, double width, double height);
87  vpRect(const vpImagePoint &topLeft, double width, double height);
88  vpRect(const vpImagePoint &topLeft, const vpImagePoint &bottomRight);
89  vpRect(const vpRect& r);
90  vpRect(const std::vector<vpImagePoint> &ip);
91 
92 
93  vpRect &operator=(const vpRect& r);
94 
99  inline double getBottom() const { return (this->top + this->height - 1.0); };
104  inline vpImagePoint getBottomRight() const {
105  vpImagePoint bottomRight;
106  bottomRight.set_u( getRight() );
107  bottomRight.set_v( getBottom() );
108 
109  return bottomRight;
110  };
122  inline void getCenter(double & x, double & y) const {
123  x = this->left + this->width / 2.0 - 0.5;
124  y = this->top + this->height / 2.0 - 0.5;
125  };
137  inline vpImagePoint getCenter() const {
138  vpImagePoint center;
139  center.set_u( this->left + this->width / 2.0 - 0.5 );
140  center.set_v( this->top + this->height / 2.0 - 0.5 );
141  return center;
142  };
151  inline double getHeight() const { return this->height; };
157  inline double getLeft() const { return this->left; };
158 
163  inline double getRight() const { return (this->left + this->width - 1.0); };
164 
169  inline double getSize() const { return (this->width * this->height); };
170 
176  inline double getTop() const { return this->top; };
182  inline vpImagePoint getTopLeft() const {
183  vpImagePoint topLeft;
184  topLeft.set_u( this->left );
185  topLeft.set_v( this->top );
186  return topLeft;
187  };
195  inline double getWidth() const { return this->width; };
196 
197  friend VISP_EXPORT bool inRectangle( const vpImagePoint &ip, const vpRect &rect );
198  friend VISP_EXPORT std::ostream& operator<< (std::ostream &os, const vpRect& r);
199  void set(double left, double top, double width, double height);
200  void set(const vpImagePoint &topLeft, double width, double height);
201  void set(const vpImagePoint &topLeft, const vpImagePoint &bottomRight);
202  void set(const vpRect& r);
203  void set(const std::vector<vpImagePoint> &ip);
204 
213  inline void setBottom(double pos) { this->height = pos - this->top + 1.0; };
221  inline void setBottomRight(const vpImagePoint &bottomRight) {
222  this->height = bottomRight.get_v() - this->top + 1.0;
223  this->width = bottomRight.get_u() - this->left + 1.0;
224  };
232  inline void setHeight(double h) { this->height = h; };
242  inline void setLeft(double pos) { this->left = pos; };
252  inline void setRect(double l, double t, double w, double h) {
253  this->left = l;
254  this->top = t;
255  this->width = w;
256  this->height = h;
257  };
267  inline void setRight(double pos) { this->width = pos - this->left + 1.0; };
276  inline void setTop(double pos) { this->top = pos; };
285  inline void setTopLeft(const vpImagePoint &topLeft) {
286  this->left = topLeft.get_u();
287  this->top = topLeft.get_v();
288  };
296  inline void setWidth(double w) { this->width = w; }
297 
306  inline void moveCenter(double x, double y) {
307  this->left = x - this->width/2 + 0.5;
308  this->top = y - this->height/2 + 0.5;
309  };
310 
319  inline void moveCenter(const vpImagePoint &center) {
320  this->left = center.get_u() - this->width/2 + 0.5;
321  this->top = center.get_v() - this->height/2 + 0.5;
322  };
323 
324 private:
325  double left; // Upper left corner position along the columns axis
326  double top; // Upper left corner position along the rows axis
327  double width; // Rectangle width
328  double height; // Rectangle height
329 };
330 
331 #endif
double getTop() const
Definition: vpRect.h:176
double get_v() const
Definition: vpImagePoint.h:259
void getCenter(double &x, double &y) const
Definition: vpRect.h:122
double getSize() const
Definition: vpRect.h:169
void setLeft(double pos)
Definition: vpRect.h:242
double get_u() const
Definition: vpImagePoint.h:248
double getHeight() const
Definition: vpRect.h:151
double getRight() const
Definition: vpRect.h:163
vpImagePoint getTopLeft() const
Definition: vpRect.h:182
double getBottom() const
Definition: vpRect.h:99
double getWidth() const
Definition: vpRect.h:195
void setTop(double pos)
Definition: vpRect.h:276
void setHeight(double h)
Definition: vpRect.h:232
void set_u(const double u)
Definition: vpImagePoint.h:212
vpImagePoint getBottomRight() const
Definition: vpRect.h:104
void moveCenter(const vpImagePoint &center)
Definition: vpRect.h:319
void set_v(const double v)
Definition: vpImagePoint.h:223
void moveCenter(double x, double y)
Definition: vpRect.h:306
void setTopLeft(const vpImagePoint &topLeft)
Definition: vpRect.h:285
void setRight(double pos)
Definition: vpRect.h:267
Defines a rectangle in the plane.
Definition: vpRect.h:81
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:137
void setRect(double l, double t, double w, double h)
Definition: vpRect.h:252
double getLeft() const
Definition: vpRect.h:157
void setBottomRight(const vpImagePoint &bottomRight)
Definition: vpRect.h:221
void setBottom(double pos)
Definition: vpRect.h:213
void setWidth(double w)
Definition: vpRect.h:296