ViSP  2.9.0
vpRect.cpp
1 /****************************************************************************
2  *
3  * $Id: vpRect.cpp 4649 2014-02-07 14:57:11Z 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 
67 vpRect::vpRect(double l, double t, double w, double h)
68  : left(l), top(t), width(w), height(h)
69 {
70 };
71 
78 vpRect::vpRect(const vpImagePoint &topLeft, double w, double h)
79  : left(topLeft.get_u()), top(topLeft.get_v()), width(w), height(h)
80 {
81 };
82 
89 vpRect::vpRect(const vpImagePoint &topLeft, const vpImagePoint &bottomRight)
90  : left(topLeft.get_u()), top(topLeft.get_v()), width(0), height(0)
91 {
92  this->left = topLeft.get_u();
93  this->top = topLeft.get_v();
94 
95  setBottom( bottomRight.get_v() );
96  setRight( bottomRight.get_u() );
97 };
98 
105  : left(0), top(0), width(0), height(0)
106 {
107  *this = r;
108 };
109 
115 {
116  this->left = r.left;
117  this->top = r.top;
118  this->width = r.width;
119  this->height = r.height;
120  return *this;
121 };
122 
129 vpRect::vpRect(const std::vector<vpImagePoint> &ip)
130  : left(0), top(0), width(0), height(0)
131 {
132  set(ip);
133 }
134 
141 void vpRect::set(const std::vector<vpImagePoint> &ip)
142 {
143  if (ip.size() < 1)
145  "At least 1 point is requested to build a rectangle"));
146  double minu, maxu;
147  double minv, maxv;
148  minu = maxu = ip[0].get_u();
149  minv = maxv = ip[0].get_v();
150 
151  for(size_t i=1; i<ip.size(); i++) {
152  double u = ip[i].get_u();
153  double v = ip[i].get_v();
154  if ( u < minu ) minu = u;
155  else if (u > maxu) maxu = u;
156  if ( v < minv ) minv = v;
157  else if (v > maxv) maxv = v;
158  }
159 
160  setLeft (minu);
161  setTop (minv);
162  setRight (maxu);
163  setBottom(maxv);
164 };
165 
176 VISP_EXPORT bool inRectangle( const vpImagePoint &ip, const vpRect &rect ) {
177  return ( ip.get_i() <= rect.getBottom() && ip.get_i() >= rect.getTop() && ip.get_j() <= rect.getRight() && ip.get_j() >= rect.getLeft());
178 }
179 
180 VISP_EXPORT std::ostream& operator<< (std::ostream &os, const vpRect& r)
181  {
182  os << r.getLeft() << ", " << r.getTop() << ", " << r.getWidth() << ", " << r.getHeight();
183  return os;
184 }
double getTop() const
Definition: vpRect.h:174
double get_v() const
Definition: vpImagePoint.h:263
double get_i() const
Definition: vpImagePoint.h:194
void setLeft(double pos)
Definition: vpRect.h:236
double get_u() const
Definition: vpImagePoint.h:252
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:205
double getBottom() const
Definition: vpRect.h:103
double getWidth() const
Definition: vpRect.h:193
void setTop(double pos)
Definition: vpRect.h:270
vpRect()
Definition: vpRect.cpp:59
void setRight(double pos)
Definition: vpRect.h:261
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:92
vpRect & operator=(const vpRect &r)
Definition: vpRect.cpp:114
double getLeft() const
Definition: vpRect.h:161
void setBottom(double pos)
Definition: vpRect.h:207
void set(const std::vector< vpImagePoint > &ip)
Definition: vpRect.cpp:141