Visual Servoing Platform  version 3.4.1 under development (2021-04-20)
vpImagePoint.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 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 http://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  * 2D point useful for image processing
33  *
34  * Authors:
35  * Nicolas Melchior
36  * Fabien Spindler
37  *
38  *****************************************************************************/
39 
40 #include <visp3/core/vpConfig.h>
41 #include <visp3/core/vpImagePoint.h>
42 #include <visp3/core/vpRect.h>
43 #include <visp3/core/vpMath.h>
44 
53 bool vpImagePoint::inRectangle(const vpRect &rect) const
54 {
55  return (this->i <= rect.getBottom() && this->i >= rect.getTop() && this->j <= rect.getRight() &&
56  this->j >= rect.getLeft());
57 }
58 
87 {
88  this->i += ip.i;
89  this->j += ip.j;
90  return *this;
91 }
92 
120 {
121  this->i /= scale;
122  this->j /= scale;
123  return *this;
124 }
125 
131 VISP_EXPORT bool operator==(const vpImagePoint &ip1, const vpImagePoint &ip2)
132 {
133  // return ( ( ip1.get_i() == ip2.get_i() ) && ( ip1.get_j() == ip2.get_j() )
134  // );
135 
136  double i1 = ip1.get_i();
137  double j1 = ip1.get_j();
138  double i2 = ip2.get_i();
139  double j2 = ip2.get_j();
140 
141  return ((std::fabs(i1 - i2) <= std::fabs(vpMath::maximum(i1, i2)) * std::numeric_limits<double>::epsilon()) &&
142  (std::fabs(j1 - j2) <= std::fabs(vpMath::maximum(j1, j2)) * std::numeric_limits<double>::epsilon()));
143 }
144 
150 VISP_EXPORT bool operator!=(const vpImagePoint &ip1, const vpImagePoint &ip2)
151 {
152  // return ( ( ip1.get_i() != ip2.get_i() ) || ( ip1.get_j() != ip2.get_j() )
153  // );
154  double i1 = ip1.get_i();
155  double j1 = ip1.get_j();
156  double i2 = ip2.get_i();
157  double j2 = ip2.get_j();
158 
159  return ((std::fabs(i1 - i2) > std::fabs(vpMath::maximum(i1, i2)) * std::numeric_limits<double>::epsilon()) ||
160  (std::fabs(j1 - j2) > std::fabs(vpMath::maximum(j1, j2)) * std::numeric_limits<double>::epsilon()));
161 }
162 
168 VISP_EXPORT vpImagePoint operator+(const vpImagePoint &ip1, const vpImagePoint &ip2)
169 {
170  return (vpImagePoint(ip1.get_i() + ip2.get_i(), ip1.get_j() + ip2.get_j()));
171 }
172 
178 VISP_EXPORT vpImagePoint operator+=(const vpImagePoint &ip1, const vpImagePoint &ip2)
179 {
180  return (vpImagePoint(ip1.get_i() + ip2.get_i(), ip1.get_j() + ip2.get_j()));
181 }
182 
202 VISP_EXPORT vpImagePoint operator+(const vpImagePoint &ip1, int offset)
203 {
204  return (vpImagePoint(ip1.get_i() + offset, ip1.get_j() + offset));
205 }
206 
226 VISP_EXPORT vpImagePoint operator+(const vpImagePoint &ip1, unsigned int offset)
227 {
228  return (vpImagePoint(ip1.get_i() + offset, ip1.get_j() + offset));
229 }
230 
250 VISP_EXPORT vpImagePoint operator+(const vpImagePoint &ip1, double offset)
251 {
252  return (vpImagePoint(ip1.get_i() + offset, ip1.get_j() + offset));
253 }
254 
261 VISP_EXPORT vpImagePoint operator-(const vpImagePoint &ip1, const vpImagePoint &ip2)
262 {
263  return (vpImagePoint(ip1.get_i() - ip2.get_i(), ip1.get_j() - ip2.get_j()));
264 }
265 
285 VISP_EXPORT vpImagePoint operator-(const vpImagePoint &ip1, int offset)
286 {
287  return (vpImagePoint(ip1.get_i() - offset, ip1.get_j() - offset));
288 }
289 
309 VISP_EXPORT vpImagePoint operator-(const vpImagePoint &ip1, unsigned int offset)
310 {
311  return (vpImagePoint(ip1.get_i() - offset, ip1.get_j() - offset));
312 }
313 
333 VISP_EXPORT vpImagePoint operator-(const vpImagePoint &ip1, double offset)
334 {
335  return (vpImagePoint(ip1.get_i() - offset, ip1.get_j() - offset));
336 }
337 
357 VISP_EXPORT vpImagePoint operator*(const vpImagePoint &ip1, double scale)
358 {
359  return (vpImagePoint(ip1.get_i() * scale, ip1.get_j() * scale));
360 }
361 
381 VISP_EXPORT vpImagePoint operator/(const vpImagePoint &ip1, double scale)
382 {
383  return (vpImagePoint(ip1.get_i() / scale, ip1.get_j() / scale));
384 }
385 
417 VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpImagePoint &ip)
418 {
419  os << ip.get_i() << ", " << ip.get_j();
420  return os;
421 }
422 
428 vpRect vpImagePoint::getBBox(const std::vector<vpImagePoint> &ipVec)
429 {
430  vpRect rec(ipVec);
431 
432  return rec;
433 }
434 
443 double vpImagePoint::distance(const vpImagePoint &iP1, const vpImagePoint &iP2)
444 {
445  return sqrt(vpMath::sqr(iP1.get_i() - iP2.get_i()) + vpMath::sqr(iP1.get_j() - iP2.get_j()));
446 }
447 
457 {
458  return vpMath::sqr(iP1.get_i() - iP2.get_i()) + vpMath::sqr(iP1.get_j() - iP2.get_j());
459 }
double get_i() const
Definition: vpImagePoint.h:203
double getTop() const
Definition: vpRect.h:193
static vpRect getBBox(const std::vector< vpImagePoint > &ipVec)
vpImagePoint & operator+=(const vpImagePoint &ip)
bool inRectangle(const vpRect &rect) const
friend VISP_EXPORT bool operator!=(const vpImagePoint &ip1, const vpImagePoint &ip2)
double getRight() const
Definition: vpRect.h:180
static Type maximum(const Type &a, const Type &b)
Definition: vpMath.h:145
friend VISP_EXPORT vpImagePoint operator*(const vpImagePoint &ip1, double scale)
static double sqrDistance(const vpImagePoint &iP1, const vpImagePoint &iP2)
friend VISP_EXPORT bool operator==(const vpImagePoint &ip1, const vpImagePoint &ip2)
static double distance(const vpImagePoint &iP1, const vpImagePoint &iP2)
static double sqr(double x)
Definition: vpMath.h:116
double get_j() const
Definition: vpImagePoint.h:214
friend VISP_EXPORT std::ostream & operator<<(std::ostream &os, const vpImagePoint &ip)
double getLeft() const
Definition: vpRect.h:174
friend VISP_EXPORT vpImagePoint operator-(const vpImagePoint &ip1, const vpImagePoint &ip2)
friend VISP_EXPORT vpImagePoint operator/(const vpImagePoint &ip1, double scale)
Defines a rectangle in the plane.
Definition: vpRect.h:79
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:87
double getBottom() const
Definition: vpRect.h:98
friend VISP_EXPORT vpImagePoint operator+(const vpImagePoint &ip1, const vpImagePoint &ip2)
vpImagePoint & operator/=(double scale)