Visual Servoing Platform  version 3.6.1 under development (2024-04-19)
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 
42 #include <visp3/core/vpDebug.h>
43 #include <visp3/core/vpRect.h>
44 
50 vpRect::vpRect() : left(0), top(0), width(0), height(0) { }
51 
61 vpRect::vpRect(double l, double t, double w, double h) : left(l), top(t), width(w), height(h) { }
62 
71 vpRect::vpRect(const vpImagePoint &topLeft, double w, double h)
72  : left(topLeft.get_u()), top(topLeft.get_v()), width(w), height(h)
73 { }
74 
79 vpRect::vpRect(const vpImagePoint &topLeft, const vpImagePoint &bottomRight)
80  : left(topLeft.get_u()), top(topLeft.get_v()), width(0), height(0)
81 {
82  this->left = topLeft.get_u();
83  this->top = topLeft.get_v();
84 
85  setBottom(bottomRight.get_v());
86  setRight(bottomRight.get_u());
87 }
88 
92 vpRect::vpRect(const vpRect &r) : left(0), top(0), width(0), height(0) { *this = r; }
93 
99 {
100  this->left = r.left;
101  this->top = r.top;
102  this->width = r.width;
103  this->height = r.height;
104  return *this;
105 }
106 
112 vpRect::vpRect(const std::vector<vpImagePoint> &ip) : left(0), top(0), width(0), height(0) { set(ip); }
113 
121 bool vpRect::isInside(const vpImagePoint &ip) const
122 {
123  return ((ip.get_i() <= this->getBottom()) && (ip.get_i() >= this->getTop()) && (ip.get_j() <= this->getRight()) &&
124  (ip.get_j() >= this->getLeft()));
125 }
126 
135 void vpRect::set(double l, double t, double w, double h)
136 {
137  left = l;
138  top = t;
139  width = w;
140  height = h;
141 }
142 
151 void vpRect::set(const vpImagePoint &topLeft, double w, double h)
152 {
153  left = topLeft.get_u();
154  top = topLeft.get_v();
155  width = w;
156  height = h;
157 }
158 
164 void vpRect::set(const std::vector<vpImagePoint> &ip)
165 {
166  if (ip.size() < 1) {
167  throw(vpException(vpException::dimensionError, "At least 1 point is requested to build a rectangle"));
168  }
169  double minu, maxu;
170  double minv, maxv;
171  maxu = ip[0].get_u();
172  maxv = ip[0].get_v();
173  minu = maxu;
174  minv = maxv;
175 
176  size_t ip_size = ip.size();
177  for (size_t i = 1; i < ip_size; ++i) {
178  double u = ip[i].get_u();
179  double v = ip[i].get_v();
180  if (u < minu) {
181  minu = u;
182  }
183  else if (u > maxu) {
184  maxu = u;
185  }
186  if (v < minv) {
187  minv = v;
188  }
189  else if (v > maxv) {
190  maxv = v;
191  }
192  }
193 
194  setLeft(minu);
195  setTop(minv);
196  setRight(maxu);
197  setBottom(maxv);
198 }
199 
207 void vpRect::set(const vpImagePoint &topLeft, const vpImagePoint &bottomRight)
208 {
209  this->left = topLeft.get_u();
210  this->top = topLeft.get_v();
211 
212  setBottom(bottomRight.get_v());
213  setRight(bottomRight.get_u());
214 }
215 
219 void vpRect::set(const vpRect &r) { *this = r; }
220 
225 bool vpRect::operator==(const vpRect &r) const
226 {
227  // --comment: return top == r.top and left == r.left and width == r.width and height == r.height
228  return ((std::fabs(top - r.top) <= (std::fabs(top) * std::numeric_limits<double>::epsilon())) &&
229  (std::fabs(left - r.left) <= (std::fabs(left) * std::numeric_limits<double>::epsilon())) &&
230  (std::fabs(width - r.width) <= (std::fabs(width) * std::numeric_limits<double>::epsilon())) &&
231  (std::fabs(height - r.height) <= (std::fabs(height) * std::numeric_limits<double>::epsilon())));
232 }
233 
238 bool vpRect::operator!=(const vpRect &r) const
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  return !(*this == r);
251 }
252 
261 VISP_EXPORT bool inRectangle(const vpImagePoint &ip, const vpRect &rect)
262 {
263  return ((ip.get_i() <= rect.getBottom()) && (ip.get_i() >= rect.getTop()) && (ip.get_j() <= rect.getRight()) &&
264  (ip.get_j() >= rect.getLeft()));
265 }
266 
267 VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpRect &r)
268 {
269  os << r.getLeft() << ", " << r.getTop() << ", " << r.getWidth() << ", " << r.getHeight();
270  return os;
271 }
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:600
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:50
double getWidth() const
Definition: vpRect.h:224
bool isInside(const vpImagePoint &ip) const
Definition: vpRect.cpp:121
void set(double left, double top, double width, double height)
Definition: vpRect.cpp:135
void setTop(double pos)
Definition: vpRect.h:354
double getLeft() const
Definition: vpRect.h:170
vpRect & operator=(const vpRect &r)
Definition: vpRect.cpp:98
bool operator==(const vpRect &r) const
Definition: vpRect.cpp:225
bool operator!=(const vpRect &r) const
Definition: vpRect.cpp:238
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