Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
vpRect.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
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 https://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  *
34 *****************************************************************************/
35 
41 #include <visp3/core/vpRect.h>
42 
43 BEGIN_VISP_NAMESPACE
49 vpRect::vpRect() : left(0), top(0), width(0), height(0) { }
50 
60 vpRect::vpRect(double l, double t, double w, double h) : left(l), top(t), width(w), height(h) { }
61 
70 vpRect::vpRect(const vpImagePoint &topLeft, double w, double h)
71  : left(topLeft.get_u()), top(topLeft.get_v()), width(w), height(h)
72 { }
73 
78 vpRect::vpRect(const vpImagePoint &topLeft, const vpImagePoint &bottomRight)
79  : left(topLeft.get_u()), top(topLeft.get_v()), width(0), height(0)
80 {
81  this->left = topLeft.get_u();
82  this->top = topLeft.get_v();
83 
84  setBottom(bottomRight.get_v());
85  setRight(bottomRight.get_u());
86 }
87 
91 vpRect::vpRect(const vpRect &r) : left(0), top(0), width(0), height(0) { *this = r; }
92 
98 {
99  this->left = r.left;
100  this->top = r.top;
101  this->width = r.width;
102  this->height = r.height;
103  return *this;
104 }
105 
111 vpRect::vpRect(const std::vector<vpImagePoint> &ip) : left(0), top(0), width(0), height(0) { set(ip); }
112 
120 bool vpRect::isInside(const vpImagePoint &ip) const
121 {
122  return ((ip.get_i() <= this->getBottom()) && (ip.get_i() >= this->getTop()) && (ip.get_j() <= this->getRight()) &&
123  (ip.get_j() >= this->getLeft()));
124 }
125 
134 void vpRect::set(double l, double t, double w, double h)
135 {
136  left = l;
137  top = t;
138  width = w;
139  height = h;
140 }
141 
150 void vpRect::set(const vpImagePoint &topLeft, double w, double h)
151 {
152  left = topLeft.get_u();
153  top = topLeft.get_v();
154  width = w;
155  height = h;
156 }
157 
163 void vpRect::set(const std::vector<vpImagePoint> &ip)
164 {
165  if (ip.size() < 1) {
166  throw(vpException(vpException::dimensionError, "At least 1 point is requested to build a rectangle"));
167  }
168  double minu, maxu;
169  double minv, maxv;
170  maxu = ip[0].get_u();
171  maxv = ip[0].get_v();
172  minu = maxu;
173  minv = maxv;
174 
175  size_t ip_size = ip.size();
176  for (size_t i = 1; i < ip_size; ++i) {
177  double u = ip[i].get_u();
178  double v = ip[i].get_v();
179  if (u < minu) {
180  minu = u;
181  }
182  else if (u > maxu) {
183  maxu = u;
184  }
185  if (v < minv) {
186  minv = v;
187  }
188  else if (v > maxv) {
189  maxv = v;
190  }
191  }
192 
193  setLeft(minu);
194  setTop(minv);
195  setRight(maxu);
196  setBottom(maxv);
197 }
198 
206 void vpRect::set(const vpImagePoint &topLeft, const vpImagePoint &bottomRight)
207 {
208  this->left = topLeft.get_u();
209  this->top = topLeft.get_v();
210 
211  setBottom(bottomRight.get_v());
212  setRight(bottomRight.get_u());
213 }
214 
218 void vpRect::set(const vpRect &r) { *this = r; }
219 
224 bool vpRect::operator==(const vpRect &r) const
225 {
226  // --comment: return top == r.top and left == r.left and width == r.width and height == r.height
227  return ((std::fabs(top - r.top) <= (std::fabs(top) * std::numeric_limits<double>::epsilon())) &&
228  (std::fabs(left - r.left) <= (std::fabs(left) * std::numeric_limits<double>::epsilon())) &&
229  (std::fabs(width - r.width) <= (std::fabs(width) * std::numeric_limits<double>::epsilon())) &&
230  (std::fabs(height - r.height) <= (std::fabs(height) * std::numeric_limits<double>::epsilon())));
231 }
232 
237 bool vpRect::operator!=(const vpRect &r) const
238 {
239  /*
240  // --comment: return (top != r.top || left != r.left || width != r.width || height !=
241  // --comment: r.height);
242  // --comment: return (std::fabs(top-r.top) >
243  // --comment: std::fabs(top)*std::numeric_limits<double>::epsilon()
244  // --comment: || std::fabs(left-r.left) >
245  // --comment: std::fabs(left)*std::numeric_limits<double>::epsilon()
246  // --comment: || std::fabs(width-r.width) >
247  // --comment: std::fabs(width)*std::numeric_limits<double>::epsilon()
248  // --comment: || std::fabs(height-r.height) >
249  // --comment: std::fabs(height)*std::numeric_limits<double>::epsilon()
250  */
251  return !(*this == r);
252 }
253 
262 VISP_EXPORT bool inRectangle(const vpImagePoint &ip, const vpRect &rect)
263 {
264  return ((ip.get_i() <= rect.getBottom()) && (ip.get_i() >= rect.getTop()) && (ip.get_j() <= rect.getRight()) &&
265  (ip.get_j() >= rect.getLeft()));
266 }
267 
268 VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpRect &r)
269 {
270  os << r.getLeft() << ", " << r.getTop() << ", " << r.getWidth() << ", " << r.getHeight();
271  return os;
272 }
273 END_VISP_NAMESPACE
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:614
error that can be emitted by ViSP classes.
Definition: vpException.h:60
@ dimensionError
Bad dimension.
Definition: vpException.h:71
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:79
double getWidth() const
Definition: vpRect.h:227
bool isInside(const vpImagePoint &ip) const
Definition: vpRect.cpp:120
void set(double left, double top, double width, double height)
Definition: vpRect.cpp:134
void setTop(double pos)
Definition: vpRect.h:357
double getLeft() const
Definition: vpRect.h:173
vpRect()
Definition: vpRect.cpp:49
vpRect & operator=(const vpRect &r)
Definition: vpRect.cpp:97
bool operator==(const vpRect &r) const
Definition: vpRect.cpp:224
bool operator!=(const vpRect &r) const
Definition: vpRect.cpp:237
void setLeft(double pos)
Definition: vpRect.h:321
void setRight(double pos)
Definition: vpRect.h:348
double getRight() const
Definition: vpRect.h:179
double getBottom() const
Definition: vpRect.h:97
double getHeight() const
Definition: vpRect.h:166
void setBottom(double pos)
Definition: vpRect.h:288
double getTop() const
Definition: vpRect.h:192