Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpRect.cpp
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 
45 #include <visp3/core/vpRect.h>
46 #include <visp3/core/vpDebug.h>
47 
53 vpRect::vpRect() : left(0), top(0), width(0), height(0) {}
54 
64 vpRect::vpRect(double l, double t, double w, double h)
65  : left(l), top(t), width(w), height(h)
66 {
67 }
68 
77 vpRect::vpRect(const vpImagePoint &topLeft, double w, double h)
78  : left(topLeft.get_u()), top(topLeft.get_v()), width(w), height(h)
79 {
80 }
81 
86 vpRect::vpRect(const vpImagePoint &topLeft, const vpImagePoint &bottomRight)
87  : left(topLeft.get_u()), top(topLeft.get_v()), width(0), height(0)
88 {
89  this->left = topLeft.get_u();
90  this->top = topLeft.get_v();
91 
92  setBottom( bottomRight.get_v() );
93  setRight( bottomRight.get_u() );
94 }
95 
100  : left(0), top(0), width(0), height(0)
101 {
102  *this = r;
103 }
104 
110 {
111  this->left = r.left;
112  this->top = r.top;
113  this->width = r.width;
114  this->height = r.height;
115  return *this;
116 }
117 
123 vpRect::vpRect(const std::vector<vpImagePoint> &ip)
124  : left(0), top(0), width(0), height(0)
125 {
126  set(ip);
127 }
128 
136 bool vpRect::isInside( const vpImagePoint &ip ) const{
137  return ( ip.get_i() <= this->getBottom() && ip.get_i() >= this->getTop() && ip.get_j() <= this->getRight() && ip.get_j() >= this->getLeft());
138 }
139 
148 void vpRect::set(double l, double t, double w, double h)
149 {
150  left = l;
151  top = t;
152  width = w;
153  height = h;
154 }
155 
164 void vpRect::set(const vpImagePoint &topLeft, double w, double h)
165 {
166  left = topLeft.get_u();
167  top = topLeft.get_v();
168  width = w;
169  height = h;
170 }
171 
177 void vpRect::set(const std::vector<vpImagePoint> &ip)
178 {
179  if (ip.size() < 1)
181  "At least 1 point is requested to build a rectangle"));
182  double minu, maxu;
183  double minv, maxv;
184  minu = maxu = ip[0].get_u();
185  minv = maxv = ip[0].get_v();
186 
187  for(size_t i=1; i<ip.size(); i++) {
188  double u = ip[i].get_u();
189  double v = ip[i].get_v();
190  if ( u < minu ) minu = u;
191  else if (u > maxu) maxu = u;
192  if ( v < minv ) minv = v;
193  else if (v > maxv) maxv = v;
194  }
195 
196  setLeft (minu);
197  setTop (minv);
198  setRight (maxu);
199  setBottom(maxv);
200 }
201 
209 void vpRect::set(const vpImagePoint &topLeft, const vpImagePoint &bottomRight)
210 {
211  this->left = topLeft.get_u();
212  this->top = topLeft.get_v();
213 
214  setBottom( bottomRight.get_v() );
215  setRight( bottomRight.get_u() );
216 }
217 
221 void vpRect::set(const vpRect& r)
222 {
223  *this = r;
224 }
225 
230 bool vpRect::operator==(const vpRect &r) const
231 {
232  //return (top == r.top && left == r.left && width == r.width && height == r.height);
233  return (std::fabs(top-r.top) <= std::fabs(top)*std::numeric_limits<double>::epsilon()
234  && std::fabs(left-r.left) <= std::fabs(left)*std::numeric_limits<double>::epsilon()
235  && std::fabs(width-r.width) <= std::fabs(width)*std::numeric_limits<double>::epsilon()
236  && std::fabs(height-r.height) <= std::fabs(height)*std::numeric_limits<double>::epsilon());
237 }
238 
243 bool vpRect::operator!=(const vpRect &r) const
244 {
245  //return (top != r.top || left != r.left || width != r.width || height != r.height);
246 // return (std::fabs(top-r.top) > std::fabs(top)*std::numeric_limits<double>::epsilon()
247 // || std::fabs(left-r.left) > std::fabs(left)*std::numeric_limits<double>::epsilon()
248 // || std::fabs(width-r.width) > std::fabs(width)*std::numeric_limits<double>::epsilon()
249 // || std::fabs(height-r.height) > std::fabs(height)*std::numeric_limits<double>::epsilon());
250  return !(*this == r);
251 }
252 
258  double x1 = std::max(left, r.left);
259  double y1 = std::max(top, r.top);
260  width = std::min(left + width, r.left + r.width) - x1;
261  height = std::min(top + height, r.top + r.height) - y1;
262  left = x1;
263  top = y1;
264 
265  if (width <= 0 || height <= 0) {
266  *this = vpRect();
267  }
268 
269  return *this;
270 }
271 
276 vpRect vpRect::operator&(const vpRect &r) const {
277  vpRect a = *this;
278  return a &= r;
279 }
280 
289 VISP_EXPORT bool inRectangle( const vpImagePoint &ip, const vpRect &rect ) {
290  return ( ip.get_i() <= rect.getBottom() && ip.get_i() >= rect.getTop() && ip.get_j() <= rect.getRight() && ip.get_j() >= rect.getLeft());
291 }
292 
293 VISP_EXPORT std::ostream& operator<< (std::ostream &os, const vpRect& r)
294  {
295  os << r.getLeft() << ", " << r.getTop() << ", " << r.getWidth() << ", " << r.getHeight();
296  return os;
297 }
double getTop() const
Definition: vpRect.h:178
double get_v() const
Definition: vpImagePoint.h:268
bool operator==(const vpRect &r) const
Definition: vpRect.cpp:230
double get_i() const
Definition: vpImagePoint.h:199
void setLeft(double pos)
Definition: vpRect.h:257
double get_u() const
Definition: vpImagePoint.h:257
error that can be emited by ViSP classes.
Definition: vpException.h:73
double getHeight() const
Definition: vpRect.h:152
double getRight() const
Definition: vpRect.h:165
double get_j() const
Definition: vpImagePoint.h:210
double getBottom() const
Definition: vpRect.h:100
double getWidth() const
Definition: vpRect.h:199
void setTop(double pos)
Definition: vpRect.h:292
void set(double left, double top, double width, double height)
Definition: vpRect.cpp:148
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:267
vpRect()
Definition: vpRect.cpp:53
void setRight(double pos)
Definition: vpRect.h:283
Defines a rectangle in the plane.
Definition: vpRect.h:82
bool isInside(const vpImagePoint &ip) const
Definition: vpRect.cpp:136
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
vpRect & operator&=(const vpRect &r)
Definition: vpRect.cpp:257
bool operator!=(const vpRect &r) const
Definition: vpRect.cpp:243
vpRect & operator=(const vpRect &r)
Definition: vpRect.cpp:109
double getLeft() const
Definition: vpRect.h:159
void setBottom(double pos)
Definition: vpRect.h:226
vpRect operator&(const vpRect &r) const
Definition: vpRect.cpp:276