Visual Servoing Platform  version 3.5.1 under development (2023-09-22)
vpRect.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 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 https://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  *
35 *****************************************************************************/
36 
43 #include <visp3/core/vpDebug.h>
44 #include <visp3/core/vpRect.h>
45 
51 vpRect::vpRect() : left(0), top(0), width(0), height(0) {}
52 
62 vpRect::vpRect(double l, double t, double w, double h) : left(l), top(t), width(w), height(h) {}
63 
72 vpRect::vpRect(const vpImagePoint &topLeft, double w, double h)
73  : left(topLeft.get_u()), top(topLeft.get_v()), width(w), height(h)
74 {
75 }
76 
81 vpRect::vpRect(const vpImagePoint &topLeft, const vpImagePoint &bottomRight)
82  : left(topLeft.get_u()), top(topLeft.get_v()), width(0), height(0)
83 {
84  this->left = topLeft.get_u();
85  this->top = topLeft.get_v();
86 
87  setBottom(bottomRight.get_v());
88  setRight(bottomRight.get_u());
89 }
90 
94 vpRect::vpRect(const vpRect &r) : left(0), top(0), width(0), height(0) { *this = r; }
95 
101 {
102  this->left = r.left;
103  this->top = r.top;
104  this->width = r.width;
105  this->height = r.height;
106  return *this;
107 }
108 
114 vpRect::vpRect(const std::vector<vpImagePoint> &ip) : left(0), top(0), width(0), height(0) { set(ip); }
115 
123 bool vpRect::isInside(const vpImagePoint &ip) const
124 {
125  return (ip.get_i() <= this->getBottom() && ip.get_i() >= this->getTop() && ip.get_j() <= this->getRight() &&
126  ip.get_j() >= this->getLeft());
127 }
128 
137 void vpRect::set(double l, double t, double w, double h)
138 {
139  left = l;
140  top = t;
141  width = w;
142  height = h;
143 }
144 
153 void vpRect::set(const vpImagePoint &topLeft, double w, double h)
154 {
155  left = topLeft.get_u();
156  top = topLeft.get_v();
157  width = w;
158  height = h;
159 }
160 
166 void vpRect::set(const std::vector<vpImagePoint> &ip)
167 {
168  if (ip.size() < 1)
169  throw(vpException(vpException::dimensionError, "At least 1 point is requested to build a rectangle"));
170  double minu, maxu;
171  double minv, maxv;
172  minu = maxu = ip[0].get_u();
173  minv = maxv = ip[0].get_v();
174 
175  for (size_t i = 1; i < ip.size(); i++) {
176  double u = ip[i].get_u();
177  double v = ip[i].get_v();
178  if (u < minu)
179  minu = u;
180  else if (u > maxu)
181  maxu = u;
182  if (v < minv)
183  minv = v;
184  else if (v > maxv)
185  maxv = v;
186  }
187 
188  setLeft(minu);
189  setTop(minv);
190  setRight(maxu);
191  setBottom(maxv);
192 }
193 
201 void vpRect::set(const vpImagePoint &topLeft, const vpImagePoint &bottomRight)
202 {
203  this->left = topLeft.get_u();
204  this->top = topLeft.get_v();
205 
206  setBottom(bottomRight.get_v());
207  setRight(bottomRight.get_u());
208 }
209 
213 void vpRect::set(const vpRect &r) { *this = r; }
214 
219 bool vpRect::operator==(const vpRect &r) const
220 {
221  // return (top == r.top && left == r.left && width == r.width && height ==
222  // r.height);
223  return (std::fabs(top - r.top) <= std::fabs(top) * std::numeric_limits<double>::epsilon() &&
224  std::fabs(left - r.left) <= std::fabs(left) * std::numeric_limits<double>::epsilon() &&
225  std::fabs(width - r.width) <= std::fabs(width) * std::numeric_limits<double>::epsilon() &&
226  std::fabs(height - r.height) <= std::fabs(height) * std::numeric_limits<double>::epsilon());
227 }
228 
233 bool vpRect::operator!=(const vpRect &r) const
234 {
235  // return (top != r.top || left != r.left || width != r.width || height !=
236  // r.height);
237  // return (std::fabs(top-r.top) >
238  // std::fabs(top)*std::numeric_limits<double>::epsilon()
239  // || std::fabs(left-r.left) >
240  // std::fabs(left)*std::numeric_limits<double>::epsilon()
241  // || std::fabs(width-r.width) >
242  // std::fabs(width)*std::numeric_limits<double>::epsilon()
243  // || std::fabs(height-r.height) >
244  // std::fabs(height)*std::numeric_limits<double>::epsilon());
245  return !(*this == r);
246 }
247 
256 VISP_EXPORT bool inRectangle(const vpImagePoint &ip, const vpRect &rect)
257 {
258  return (ip.get_i() <= rect.getBottom() && ip.get_i() >= rect.getTop() && ip.get_j() <= rect.getRight() &&
259  ip.get_j() >= rect.getLeft());
260 }
261 
262 VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpRect &r)
263 {
264  os << r.getLeft() << ", " << r.getTop() << ", " << r.getWidth() << ", " << r.getHeight();
265  return os;
266 }
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:529
error that can be emitted by ViSP classes.
Definition: vpException.h:59
@ dimensionError
Bad dimension.
Definition: vpException.h:83
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
double get_j() const
Definition: vpImagePoint.h:125
double get_u() const
Definition: vpImagePoint.h:136
double get_i() const
Definition: vpImagePoint.h:114
double get_v() const
Definition: vpImagePoint.h:147
Defines a rectangle in the plane.
Definition: vpRect.h:76
vpRect()
Definition: vpRect.cpp:51
double getWidth() const
Definition: vpRect.h:224
bool isInside(const vpImagePoint &ip) const
Definition: vpRect.cpp:123
void set(double left, double top, double width, double height)
Definition: vpRect.cpp:137
void setTop(double pos)
Definition: vpRect.h:354
double getLeft() const
Definition: vpRect.h:170
vpRect & operator=(const vpRect &r)
Definition: vpRect.cpp:100
bool operator==(const vpRect &r) const
Definition: vpRect.cpp:219
bool operator!=(const vpRect &r) const
Definition: vpRect.cpp:233
void setLeft(double pos)
Definition: vpRect.h:318
void setRight(double pos)
Definition: vpRect.h:345
double getRight() const
Definition: vpRect.h:176
double getBottom() const
Definition: vpRect.h:94
double getHeight() const
Definition: vpRect.h:163
void setBottom(double pos)
Definition: vpRect.h:285
double getTop() const
Definition: vpRect.h:189