Visual Servoing Platform  version 3.3.0 under development (2020-02-17)
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 { return width * height; }
92 
97  inline double getBottom() const { return (this->top + this->height - 1.0); }
98 
105  {
106  vpImagePoint bottomLeft;
107  bottomLeft.set_u(getLeft());
108  bottomLeft.set_v(getBottom());
109  return bottomLeft;
110  }
111 
118  {
119  vpImagePoint bottomRight;
120  bottomRight.set_u(getRight());
121  bottomRight.set_v(getBottom());
122 
123  return bottomRight;
124  }
125 
136  inline void getCenter(double &x, double &y) const
137  {
138  x = this->left + this->width / 2.0 - 0.5;
139  y = this->top + this->height / 2.0 - 0.5;
140  }
141 
152  inline vpImagePoint getCenter() const
153  {
154  vpImagePoint center;
155  center.set_u(this->left + this->width / 2.0 - 0.5);
156  center.set_v(this->top + this->height / 2.0 - 0.5);
157  return center;
158  }
159 
166  inline double getHeight() const { return this->height; }
167 
173  inline double getLeft() const { return this->left; }
174 
179  inline double getRight() const { return (this->left + this->width - 1.0); }
180 
185  inline double getSize() const { return (this->width * this->height); }
186 
192  inline double getTop() const { return this->top; }
193 
199  inline vpImagePoint getTopLeft() const
200  {
201  vpImagePoint topLeft;
202  topLeft.set_u(getLeft());
203  topLeft.set_v(getTop());
204  return topLeft;
205  }
206 
212  inline vpImagePoint getTopRight() const
213  {
214  vpImagePoint topRight;
215  topRight.set_u(getRight());
216  topRight.set_v(getTop());
217  return topRight;
218  }
219 
227  inline double getWidth() const { return this->width; }
228 
232  bool isInside(const vpImagePoint &ip) const;
233 
234  bool operator==(const vpRect &r) const;
235  bool operator!=(const vpRect &r) const;
236  vpRect &operator&=(const vpRect &r);
237  vpRect &operator=(const vpRect &r);
238  vpRect operator&(const vpRect &r) const;
239 
240  friend VISP_EXPORT bool inRectangle(const vpImagePoint &ip, const vpRect &rect);
241  friend VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpRect &r);
242 
243  void set(double left, double top, double width, double height);
244  void set(const vpImagePoint &topLeft, double width, double height);
245  void set(const vpImagePoint &topLeft, const vpImagePoint &bottomRight);
246  void set(const vpRect &r);
247  void set(const std::vector<vpImagePoint> &ip);
248 
256  inline void setBottom(double pos) { this->height = pos - this->top + 1.0; }
257 
264  inline void setBottomRight(const vpImagePoint &bottomRight)
265  {
266  this->height = bottomRight.get_v() - this->top + 1.0;
267  this->width = bottomRight.get_u() - this->left + 1.0;
268  }
269 
276  inline void setHeight(double h)
277  {
278  assert(h > 0);
279  this->height = h;
280  }
281 
289  inline void setLeft(double pos) { this->left = pos; }
290 
301  inline void setRect(double l, double t, double w, double h)
302  {
303  this->left = l;
304  this->top = t;
305  this->width = w;
306  this->height = h;
307  }
308 
316  inline void setRight(double pos) { this->width = pos - this->left + 1.0; }
317 
325  inline void setTop(double pos) { this->top = pos; }
326 
334  inline void setTopLeft(const vpImagePoint &topLeft)
335  {
336  this->left = topLeft.get_u();
337  this->top = topLeft.get_v();
338  }
339 
346  inline void setWidth(double w)
347  {
348  assert(w > 0);
349  this->width = w;
350  }
351 
358  inline void moveCenter(double x, double y)
359  {
360  this->left = x - this->width / 2 + 0.5;
361  this->top = y - this->height / 2 + 0.5;
362  }
363 
370  inline void moveCenter(const vpImagePoint &center)
371  {
372  this->left = center.get_u() - this->width / 2 + 0.5;
373  this->top = center.get_v() - this->height / 2 + 0.5;
374  }
375 
376 private:
377  double left; // Upper left corner position along the columns axis
378  double top; // Upper left corner position along the rows axis
379  double width; // Rectangle width
380  double height; // Rectangle height
381 };
382 
383 #endif
double getTop() const
Definition: vpRect.h:192
vpImagePoint getTopRight() const
Definition: vpRect.h:212
void set_u(double u)
Definition: vpImagePoint.h:226
double getArea() const
Definition: vpRect.h:91
void setLeft(double pos)
Definition: vpRect.h:289
void getCenter(double &x, double &y) const
Definition: vpRect.h:136
vpImagePoint getTopLeft() const
Definition: vpRect.h:199
double getRight() const
Definition: vpRect.h:179
double get_u() const
Definition: vpImagePoint.h:263
void setTop(double pos)
Definition: vpRect.h:325
double getWidth() const
Definition: vpRect.h:227
void setHeight(double h)
Definition: vpRect.h:276
vpImagePoint getCenter() const
Definition: vpRect.h:152
void moveCenter(const vpImagePoint &center)
Definition: vpRect.h:370
void moveCenter(double x, double y)
Definition: vpRect.h:358
double getLeft() const
Definition: vpRect.h:173
void setTopLeft(const vpImagePoint &topLeft)
Definition: vpRect.h:334
double getSize() const
Definition: vpRect.h:185
void set_v(double v)
Definition: vpImagePoint.h:237
void setRight(double pos)
Definition: vpRect.h:316
double getHeight() const
Definition: vpRect.h:166
vpImagePoint getBottomRight() const
Definition: vpRect.h:117
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:301
double getBottom() const
Definition: vpRect.h:97
void setBottomRight(const vpImagePoint &bottomRight)
Definition: vpRect.h:264
void setBottom(double pos)
Definition: vpRect.h:256
double get_v() const
Definition: vpImagePoint.h:274
vpImagePoint getBottomLeft() const
Definition: vpRect.h:104
void setWidth(double w)
Definition: vpRect.h:346