Visual Servoing Platform  version 3.0.0
vpRect.cpp
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 
45 #include <visp3/core/vpRect.h>
46 #include <visp3/core/vpDebug.h>
47 
55 vpRect::vpRect() : left(0), top(0), width(0), height(0) {};
56 
68 vpRect::vpRect(double l, double t, double w, double h)
69  : left(l), top(t), width(w), height(h)
70 {
71 };
72 
83 vpRect::vpRect(const vpImagePoint &topLeft, double w, double h)
84  : left(topLeft.get_u()), top(topLeft.get_v()), width(w), height(h)
85 {
86 };
87 
94 vpRect::vpRect(const vpImagePoint &topLeft, const vpImagePoint &bottomRight)
95  : left(topLeft.get_u()), top(topLeft.get_v()), width(0), height(0)
96 {
97  this->left = topLeft.get_u();
98  this->top = topLeft.get_v();
99 
100  setBottom( bottomRight.get_v() );
101  setRight( bottomRight.get_u() );
102 };
103 
110  : left(0), top(0), width(0), height(0)
111 {
112  *this = r;
113 };
114 
120 {
121  this->left = r.left;
122  this->top = r.top;
123  this->width = r.width;
124  this->height = r.height;
125  return *this;
126 };
127 
134 vpRect::vpRect(const std::vector<vpImagePoint> &ip)
135  : left(0), top(0), width(0), height(0)
136 {
137  set(ip);
138 }
139 
150 void vpRect::set(double l, double t, double w, double h)
151 {
152  left = l;
153  top = t;
154  width = w;
155  height = h;
156 };
157 
167 void vpRect::set(const vpImagePoint &topLeft, double w, double h)
168 {
169  left = topLeft.get_u();
170  top = topLeft.get_v();
171  width = w;
172  height = h;
173 };
174 
181 void vpRect::set(const std::vector<vpImagePoint> &ip)
182 {
183  if (ip.size() < 1)
185  "At least 1 point is requested to build a rectangle"));
186  double minu, maxu;
187  double minv, maxv;
188  minu = maxu = ip[0].get_u();
189  minv = maxv = ip[0].get_v();
190 
191  for(size_t i=1; i<ip.size(); i++) {
192  double u = ip[i].get_u();
193  double v = ip[i].get_v();
194  if ( u < minu ) minu = u;
195  else if (u > maxu) maxu = u;
196  if ( v < minv ) minv = v;
197  else if (v > maxv) maxv = v;
198  }
199 
200  setLeft (minu);
201  setTop (minv);
202  setRight (maxu);
203  setBottom(maxv);
204 };
205 
215 void vpRect::set(const vpImagePoint &topLeft, const vpImagePoint &bottomRight)
216 {
217  this->left = topLeft.get_u();
218  this->top = topLeft.get_v();
219 
220  setBottom( bottomRight.get_v() );
221  setRight( bottomRight.get_u() );
222 };
223 
229 void vpRect::set(const vpRect& r)
230 {
231  *this = r;
232 };
233 
244 VISP_EXPORT bool inRectangle( const vpImagePoint &ip, const vpRect &rect ) {
245  return ( ip.get_i() <= rect.getBottom() && ip.get_i() >= rect.getTop() && ip.get_j() <= rect.getRight() && ip.get_j() >= rect.getLeft());
246 }
247 
248 VISP_EXPORT std::ostream& operator<< (std::ostream &os, const vpRect& r)
249  {
250  os << r.getLeft() << ", " << r.getTop() << ", " << r.getWidth() << ", " << r.getHeight();
251  return os;
252 }
double getTop() const
Definition: vpRect.h:176
double get_v() const
Definition: vpImagePoint.h:259
double get_i() const
Definition: vpImagePoint.h:190
void setLeft(double pos)
Definition: vpRect.h:242
double get_u() const
Definition: vpImagePoint.h:248
error that can be emited by ViSP classes.
Definition: vpException.h:73
double getHeight() const
Definition: vpRect.h:151
double getRight() const
Definition: vpRect.h:163
double get_j() const
Definition: vpImagePoint.h:201
double getBottom() const
Definition: vpRect.h:99
double getWidth() const
Definition: vpRect.h:195
void setTop(double pos)
Definition: vpRect.h:276
void set(double left, double top, double width, double height)
Definition: vpRect.cpp:150
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:267
vpRect()
Definition: vpRect.cpp:55
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
vpRect & operator=(const vpRect &r)
Definition: vpRect.cpp:119
double getLeft() const
Definition: vpRect.h:157
void setBottom(double pos)
Definition: vpRect.h:213