Visual Servoing Platform  version 3.6.1 under development (2024-12-17)
vpImagePoint.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  * 2D point useful for image processing
32  *
33 *****************************************************************************/
34 
35 #include <visp3/core/vpConfig.h>
36 #include <visp3/core/vpImagePoint.h>
37 #include <visp3/core/vpMath.h>
38 #include <visp3/core/vpRect.h>
39 
40 #include <limits> // numeric_limits
41 
42 BEGIN_VISP_NAMESPACE
51 bool vpImagePoint::inRectangle(const vpRect &rect) const
52 {
53  return ((this->i <= rect.getBottom()) && (this->i >= rect.getTop()) && (this->j <= rect.getRight()) &&
54  (this->j >= rect.getLeft()));
55 }
56 
88 {
89  this->i += ip.i;
90  this->j += ip.j;
91  return *this;
92 }
93 
124 {
125  this->i /= scale;
126  this->j /= scale;
127  return *this;
128 }
129 
135 VISP_EXPORT bool operator==(const vpImagePoint &ip1, const vpImagePoint &ip2)
136 {
137 // --comment: return ip1 dot get_i() eq ip2 dot get_i() and ip1 dot get_j() eq ip2 dot get_j()
138 
139  double i1 = ip1.get_i();
140  double j1 = ip1.get_j();
141  double i2 = ip2.get_i();
142  double j2 = ip2.get_j();
143 
144  return ((std::fabs(i1 - i2) <= (std::fabs(vpMath::maximum(i1, i2)) * std::numeric_limits<double>::epsilon())) &&
145  (std::fabs(j1 - j2) <= (std::fabs(vpMath::maximum(j1, j2)) * std::numeric_limits<double>::epsilon())));
146 }
147 
153 VISP_EXPORT bool operator!=(const vpImagePoint &ip1, const vpImagePoint &ip2)
154 {
155 // --comment: return ip1 dot get_i() diff ip2 dot get_i() or ip1 dot get_j() diff ip2 dot get_j()
156  double i1 = ip1.get_i();
157  double j1 = ip1.get_j();
158  double i2 = ip2.get_i();
159  double j2 = ip2.get_j();
160 
161  return ((std::fabs(i1 - i2) > (std::fabs(vpMath::maximum(i1, i2)) * std::numeric_limits<double>::epsilon())) ||
162  (std::fabs(j1 - j2) > (std::fabs(vpMath::maximum(j1, j2)) * std::numeric_limits<double>::epsilon())));
163 }
164 
170 VISP_EXPORT vpImagePoint operator+(const vpImagePoint &ip1, const vpImagePoint &ip2)
171 {
172  return (vpImagePoint(ip1.get_i() + ip2.get_i(), ip1.get_j() + ip2.get_j()));
173 }
174 
180 VISP_EXPORT vpImagePoint operator+=(const vpImagePoint &ip1, const vpImagePoint &ip2)
181 {
182  return (vpImagePoint(ip1.get_i() + ip2.get_i(), ip1.get_j() + ip2.get_j()));
183 }
184 
208 VISP_EXPORT vpImagePoint operator+(const vpImagePoint &ip1, int offset)
209 {
210  return (vpImagePoint(ip1.get_i() + offset, ip1.get_j() + offset));
211 }
212 
236 VISP_EXPORT vpImagePoint operator+(const vpImagePoint &ip1, unsigned int offset)
237 {
238  return (vpImagePoint(ip1.get_i() + offset, ip1.get_j() + offset));
239 }
240 
264 VISP_EXPORT vpImagePoint operator+(const vpImagePoint &ip1, double offset)
265 {
266  return (vpImagePoint(ip1.get_i() + offset, ip1.get_j() + offset));
267 }
268 
275 VISP_EXPORT vpImagePoint operator-(const vpImagePoint &ip1, const vpImagePoint &ip2)
276 {
277  return (vpImagePoint(ip1.get_i() - ip2.get_i(), ip1.get_j() - ip2.get_j()));
278 }
279 
303 VISP_EXPORT vpImagePoint operator-(const vpImagePoint &ip1, int offset)
304 {
305  return (vpImagePoint(ip1.get_i() - offset, ip1.get_j() - offset));
306 }
307 
331 VISP_EXPORT vpImagePoint operator-(const vpImagePoint &ip1, unsigned int offset)
332 {
333  return (vpImagePoint(ip1.get_i() - offset, ip1.get_j() - offset));
334 }
335 
359 VISP_EXPORT vpImagePoint operator-(const vpImagePoint &ip1, double offset)
360 {
361  return (vpImagePoint(ip1.get_i() - offset, ip1.get_j() - offset));
362 }
363 
387 VISP_EXPORT vpImagePoint operator*(const vpImagePoint &ip1, double scale)
388 {
389  return (vpImagePoint(ip1.get_i() * scale, ip1.get_j() * scale));
390 }
391 
415 VISP_EXPORT vpImagePoint operator/(const vpImagePoint &ip1, double scale)
416 {
417  return (vpImagePoint(ip1.get_i() / scale, ip1.get_j() / scale));
418 }
419 
456 VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpImagePoint &ip)
457 {
458  os << ip.get_i() << ", " << ip.get_j();
459  return os;
460 }
461 
467 vpRect vpImagePoint::getBBox(const std::vector<vpImagePoint> &ipVec)
468 {
469  vpRect rec(ipVec);
470 
471  return rec;
472 }
473 
482 double vpImagePoint::distance(const vpImagePoint &iP1, const vpImagePoint &iP2)
483 {
484  return sqrt(vpMath::sqr(iP1.get_i() - iP2.get_i()) + vpMath::sqr(iP1.get_j() - iP2.get_j()));
485 }
486 
496 {
497  return vpMath::sqr(iP1.get_i() - iP2.get_i()) + vpMath::sqr(iP1.get_j() - iP2.get_j());
498 }
499 END_VISP_NAMESPACE
bool operator==(const vpArray2D< double > &A) const
Definition: vpArray2D.h:1313
bool operator!=(const vpArray2D< Type > &A) const
Definition: vpArray2D.h:1351
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:614
vpColVector operator*(const double &x, const vpColVector &v)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
vpImagePoint & operator/=(double scale)
double get_j() const
Definition: vpImagePoint.h:125
static double distance(const vpImagePoint &iP1, const vpImagePoint &iP2)
vpImagePoint & operator+=(const vpImagePoint &ip)
bool inRectangle(const vpRect &rect) const
VISP_EXPORT vpImagePoint operator+=(const vpImagePoint &ip1, const vpImagePoint &ip2)
VISP_EXPORT vpImagePoint operator+(const vpImagePoint &ip1, const vpImagePoint &ip2)
static vpRect getBBox(const std::vector< vpImagePoint > &ipVec)
VISP_EXPORT vpImagePoint operator/(const vpImagePoint &ip1, double scale)
static double sqrDistance(const vpImagePoint &iP1, const vpImagePoint &iP2)
VISP_EXPORT vpImagePoint operator-(const vpImagePoint &ip1, const vpImagePoint &ip2)
double get_i() const
Definition: vpImagePoint.h:114
static Type maximum(const Type &a, const Type &b)
Definition: vpMath.h:254
static double sqr(double x)
Definition: vpMath.h:203
Defines a rectangle in the plane.
Definition: vpRect.h:79
double getLeft() const
Definition: vpRect.h:173
double getRight() const
Definition: vpRect.h:179
double getBottom() const
Definition: vpRect.h:97
double getTop() const
Definition: vpRect.h:192