Visual Servoing Platform
version 3.7.0
Toggle main menu visibility
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
42
BEGIN_VISP_NAMESPACE
47
48
vpRect::vpRect
() : left(0), top(0), width(0), height(0) { }
49
59
vpRect::vpRect
(
double
l,
double
t,
double
w,
double
h) : left(l), top(t), width(w), height(h) { }
60
69
vpRect::vpRect
(
const
vpImagePoint
&topLeft,
double
w,
double
h)
70
: left(topLeft.get_u()), top(topLeft.get_v()), width(w), height(h)
71
{ }
72
77
vpRect::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
90
vpRect::vpRect
(
const
vpRect
&r) : left(0), top(0), width(0), height(0) { *
this
= r; }
91
96
vpRect
&
vpRect::operator=
(
const
vpRect
&r)
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
110
vpRect::vpRect
(
const
std::vector<vpImagePoint> &ip) : left(0), top(0), width(0), height(0) {
set
(ip); }
111
119
bool
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
133
void
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
149
void
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
162
void
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
205
void
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
217
void
vpRect::set
(
const
vpRect
&r) { *
this
= r; }
218
223
bool
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
236
bool
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
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
}
272
END_VISP_NAMESPACE
vpException
error that can be emitted by ViSP classes.
Definition
vpException.h:60
vpException::dimensionError
@ dimensionError
Bad dimension.
Definition
vpException.h:71
vpImagePoint
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition
vpImagePoint.h:82
vpImagePoint::get_j
double get_j() const
Definition
vpImagePoint.h:125
vpImagePoint::get_u
double get_u() const
Definition
vpImagePoint.h:136
vpImagePoint::get_i
double get_i() const
Definition
vpImagePoint.h:114
vpImagePoint::get_v
double get_v() const
Definition
vpImagePoint.h:147
vpRect::inRectangle
friend VISP_EXPORT bool inRectangle(const vpImagePoint &ip, const vpRect &rect)
Definition
vpRect.cpp:261
vpRect::isInside
bool isInside(const vpImagePoint &ip) const
Definition
vpRect.cpp:119
vpRect::set
void set(double left, double top, double width, double height)
Definition
vpRect.cpp:133
vpRect::operator<<
friend VISP_EXPORT std::ostream & operator<<(std::ostream &os, const vpRect &r)
Definition
vpRect.cpp:267
vpRect::setTop
void setTop(double pos)
Definition
vpRect.h:357
vpRect::getLeft
double getLeft() const
Definition
vpRect.h:173
vpRect::vpRect
vpRect()
Definition
vpRect.cpp:48
vpRect::operator=
vpRect & operator=(const vpRect &r)
Definition
vpRect.cpp:96
vpRect::operator==
bool operator==(const vpRect &r) const
Definition
vpRect.cpp:223
vpRect::operator!=
bool operator!=(const vpRect &r) const
Definition
vpRect.cpp:236
vpRect::setLeft
void setLeft(double pos)
Definition
vpRect.h:321
vpRect::setRight
void setRight(double pos)
Definition
vpRect.h:348
vpRect::getRight
double getRight() const
Definition
vpRect.h:179
vpRect::getBottom
double getBottom() const
Definition
vpRect.h:97
vpRect::setBottom
void setBottom(double pos)
Definition
vpRect.h:288
vpRect::getTop
double getTop() const
Definition
vpRect.h:192
BEGIN_VISP_NAMESPACE
Definition
vpMbtDistanceCircle.cpp:55
modules
core
src
tools
geometry
vpRect.cpp
Generated by
1.17.0