Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
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
39
40#include <visp3/core/vpRect.h>
41
47
48vpRect::vpRect() : left(0), top(0), width(0), height(0) { }
49
59vpRect::vpRect(double l, double t, double w, double h) : left(l), top(t), width(w), height(h) { }
60
69vpRect::vpRect(const vpImagePoint &topLeft, double w, double h)
70 : left(topLeft.get_u()), top(topLeft.get_v()), width(w), height(h)
71{ }
72
77vpRect::vpRect(const vpImagePoint &topLeft, const vpImagePoint &bottomRight)
78 : left(topLeft.get_u()), top(topLeft.get_v()), width(0), height(0)
79{
80 this->left = topLeft.get_u();
81 this->top = topLeft.get_v();
82
83 setBottom(bottomRight.get_v());
84 setRight(bottomRight.get_u());
85}
86
90vpRect::vpRect(const vpRect &r) : left(0), top(0), width(0), height(0) { *this = r; }
91
97{
98 this->left = r.left;
99 this->top = r.top;
100 this->width = r.width;
101 this->height = r.height;
102 return *this;
103}
104
110vpRect::vpRect(const std::vector<vpImagePoint> &ip) : left(0), top(0), width(0), height(0) { set(ip); }
111
119bool vpRect::isInside(const vpImagePoint &ip) const
120{
121 return ((ip.get_i() <= this->getBottom()) && (ip.get_i() >= this->getTop()) && (ip.get_j() <= this->getRight()) &&
122 (ip.get_j() >= this->getLeft()));
123}
124
133void vpRect::set(double l, double t, double w, double h)
134{
135 left = l;
136 top = t;
137 width = w;
138 height = h;
139}
140
149void vpRect::set(const vpImagePoint &topLeft, double w, double h)
150{
151 left = topLeft.get_u();
152 top = topLeft.get_v();
153 width = w;
154 height = h;
155}
156
162void vpRect::set(const std::vector<vpImagePoint> &ip)
163{
164 if (ip.size() < 1) {
165 throw(vpException(vpException::dimensionError, "At least 1 point is requested to build a rectangle"));
166 }
167 double minu, maxu;
168 double minv, maxv;
169 maxu = ip[0].get_u();
170 maxv = ip[0].get_v();
171 minu = maxu;
172 minv = maxv;
173
174 size_t ip_size = ip.size();
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 }
181 else if (u > maxu) {
182 maxu = u;
183 }
184 if (v < minv) {
185 minv = v;
186 }
187 else if (v > maxv) {
188 maxv = v;
189 }
190 }
191
192 setLeft(minu);
193 setTop(minv);
194 setRight(maxu);
195 setBottom(maxv);
196}
197
205void vpRect::set(const vpImagePoint &topLeft, const vpImagePoint &bottomRight)
206{
207 this->left = topLeft.get_u();
208 this->top = topLeft.get_v();
209
210 setBottom(bottomRight.get_v());
211 setRight(bottomRight.get_u());
212}
213
217void vpRect::set(const vpRect &r) { *this = r; }
218
223bool vpRect::operator==(const vpRect &r) const
224{
225 // --comment: return top == r.top and left == r.left and width == r.width and height == r.height
226 return ((std::fabs(top - r.top) <= (std::fabs(top) * std::numeric_limits<double>::epsilon())) &&
227 (std::fabs(left - r.left) <= (std::fabs(left) * std::numeric_limits<double>::epsilon())) &&
228 (std::fabs(width - r.width) <= (std::fabs(width) * std::numeric_limits<double>::epsilon())) &&
229 (std::fabs(height - r.height) <= (std::fabs(height) * std::numeric_limits<double>::epsilon())));
230}
231
236bool vpRect::operator!=(const vpRect &r) const
237{
238 /*
239 // --comment: return (top != r.top || left != r.left || width != r.width || height !=
240 // --comment: r.height);
241 // --comment: return (std::fabs(top-r.top) >
242 // --comment: std::fabs(top)*std::numeric_limits<double>::epsilon()
243 // --comment: || std::fabs(left-r.left) >
244 // --comment: std::fabs(left)*std::numeric_limits<double>::epsilon()
245 // --comment: || std::fabs(width-r.width) >
246 // --comment: std::fabs(width)*std::numeric_limits<double>::epsilon()
247 // --comment: || std::fabs(height-r.height) >
248 // --comment: std::fabs(height)*std::numeric_limits<double>::epsilon()
249 */
250 return !(*this == r);
251}
252
261VISP_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
267VISP_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}
272END_VISP_NAMESPACE
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 ...
double get_j() const
double get_u() const
double get_i() const
double get_v() const
friend VISP_EXPORT bool inRectangle(const vpImagePoint &ip, const vpRect &rect)
Definition vpRect.cpp:261
bool isInside(const vpImagePoint &ip) const
Definition vpRect.cpp:119
void set(double left, double top, double width, double height)
Definition vpRect.cpp:133
friend VISP_EXPORT std::ostream & operator<<(std::ostream &os, const vpRect &r)
Definition vpRect.cpp:267
void setTop(double pos)
Definition vpRect.h:357
double getLeft() const
Definition vpRect.h:173
vpRect()
Definition vpRect.cpp:48
vpRect & operator=(const vpRect &r)
Definition vpRect.cpp:96
bool operator==(const vpRect &r) const
Definition vpRect.cpp:223
bool operator!=(const vpRect &r) const
Definition vpRect.cpp:236
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
void setBottom(double pos)
Definition vpRect.h:288
double getTop() const
Definition vpRect.h:192