ViSP  2.10.0
vpRect.cpp
1 /****************************************************************************
2  *
3  * $Id: vpRect.cpp 4917 2014-10-06 08:35:22Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Defines a rectangle in the plane.
36  *
37  * Author:
38  * Fabien Spindler
39  *
40  *****************************************************************************/
41 
49 #include <visp/vpRect.h>
50 #include <visp/vpDebug.h>
51 
59 vpRect::vpRect() : left(0), top(0), width(0), height(0) {};
60 
72 vpRect::vpRect(double l, double t, double w, double h)
73  : left(l), top(t), width(w), height(h)
74 {
75 };
76 
87 vpRect::vpRect(const vpImagePoint &topLeft, double w, double h)
88  : left(topLeft.get_u()), top(topLeft.get_v()), width(w), height(h)
89 {
90 };
91 
98 vpRect::vpRect(const vpImagePoint &topLeft, const vpImagePoint &bottomRight)
99  : left(topLeft.get_u()), top(topLeft.get_v()), width(0), height(0)
100 {
101  this->left = topLeft.get_u();
102  this->top = topLeft.get_v();
103 
104  setBottom( bottomRight.get_v() );
105  setRight( bottomRight.get_u() );
106 };
107 
114  : left(0), top(0), width(0), height(0)
115 {
116  *this = r;
117 };
118 
124 {
125  this->left = r.left;
126  this->top = r.top;
127  this->width = r.width;
128  this->height = r.height;
129  return *this;
130 };
131 
138 vpRect::vpRect(const std::vector<vpImagePoint> &ip)
139  : left(0), top(0), width(0), height(0)
140 {
141  set(ip);
142 }
143 
154 void vpRect::set(double l, double t, double w, double h)
155 {
156  left = l;
157  top = t;
158  width = w;
159  height = h;
160 };
161 
171 void vpRect::set(const vpImagePoint &topLeft, double w, double h)
172 {
173  left = topLeft.get_u();
174  top = topLeft.get_v();
175  width = w;
176  height = h;
177 };
178 
185 void vpRect::set(const std::vector<vpImagePoint> &ip)
186 {
187  if (ip.size() < 1)
189  "At least 1 point is requested to build a rectangle"));
190  double minu, maxu;
191  double minv, maxv;
192  minu = maxu = ip[0].get_u();
193  minv = maxv = ip[0].get_v();
194 
195  for(size_t i=1; i<ip.size(); i++) {
196  double u = ip[i].get_u();
197  double v = ip[i].get_v();
198  if ( u < minu ) minu = u;
199  else if (u > maxu) maxu = u;
200  if ( v < minv ) minv = v;
201  else if (v > maxv) maxv = v;
202  }
203 
204  setLeft (minu);
205  setTop (minv);
206  setRight (maxu);
207  setBottom(maxv);
208 };
209 
219 void vpRect::set(const vpImagePoint &topLeft, const vpImagePoint &bottomRight)
220 {
221  this->left = topLeft.get_u();
222  this->top = topLeft.get_v();
223 
224  setBottom( bottomRight.get_v() );
225  setRight( bottomRight.get_u() );
226 };
227 
233 void vpRect::set(const vpRect& r)
234 {
235  *this = r;
236 };
237 
248 VISP_EXPORT bool inRectangle( const vpImagePoint &ip, const vpRect &rect ) {
249  return ( ip.get_i() <= rect.getBottom() && ip.get_i() >= rect.getTop() && ip.get_j() <= rect.getRight() && ip.get_j() >= rect.getLeft());
250 }
251 
252 VISP_EXPORT std::ostream& operator<< (std::ostream &os, const vpRect& r)
253  {
254  os << r.getLeft() << ", " << r.getTop() << ", " << r.getWidth() << ", " << r.getHeight();
255  return os;
256 }
double getTop() const
Definition: vpRect.h:180
double get_v() const
Definition: vpImagePoint.h:264
double get_i() const
Definition: vpImagePoint.h:195
void setLeft(double pos)
Definition: vpRect.h:246
double get_u() const
Definition: vpImagePoint.h:253
error that can be emited by ViSP classes.
Definition: vpException.h:76
double getHeight() const
Definition: vpRect.h:155
double getRight() const
Definition: vpRect.h:167
double get_j() const
Definition: vpImagePoint.h:206
double getBottom() const
Definition: vpRect.h:103
double getWidth() const
Definition: vpRect.h:199
void setTop(double pos)
Definition: vpRect.h:280
void set(double left, double top, double width, double height)
Definition: vpRect.cpp:154
vpRect()
Definition: vpRect.cpp:59
void setRight(double pos)
Definition: vpRect.h:271
Defines a rectangle in the plane.
Definition: vpRect.h:85
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:93
vpRect & operator=(const vpRect &r)
Definition: vpRect.cpp:123
double getLeft() const
Definition: vpRect.h:161
void setBottom(double pos)
Definition: vpRect.h:217