Visual Servoing Platform  version 3.5.1 under development (2022-07-05)
vpImagePoint.h
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2022 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  * Julien Dufour
38  *
39  *****************************************************************************/
40 
41 #ifndef vpImagePoint_H
42 #define vpImagePoint_H
43 
50 #include <visp3/core/vpConfig.h>
51 
52 #include <cmath> // std::fabs
53 #include <limits> // numeric_limits
54 #include <ostream>
55 #include <vector>
56 
57 class vpRect;
58 
88 class VISP_EXPORT vpImagePoint
89 {
90 public:
95  inline vpImagePoint() : i(0), j(0) {}
100  inline vpImagePoint(double ii, double jj) : i(ii), j(jj) {}
108  inline vpImagePoint(const vpImagePoint &ip) : i(ip.i), j(ip.j) {}
110  inline virtual ~vpImagePoint() {}
111 
121  inline double get_i() const { return i; }
122 
132  inline double get_j() const { return j; }
133 
143  inline double get_u() const { return j; }
144 
154  inline double get_v() const { return i; }
155 
156  bool inRectangle(const vpRect &rect) const;
157 
169  inline bool inSegment(const vpImagePoint &start, const vpImagePoint &end) const
170  {
171  return ((end.get_j() >= start.get_j() && end.get_j() >= this->j && this->j >= start.get_j()) ||
172  (end.get_j() <= start.get_j() && end.get_j() <= this->j && this->j <= start.get_j())) &&
173  ((end.get_i() >= start.get_i() && end.get_i() >= this->i && this->i >= start.get_i()) ||
174  (end.get_i() <= start.get_i() && end.get_i() <= this->i && this->i <= start.get_i()));
175  }
176 
222  inline vpImagePoint nextInSegment(const vpImagePoint &start, const vpImagePoint &end) const
223  {
224  const double line_slope = (end.get_i() - start.get_i()) / (end.get_j() - start.get_j());
225  if (fabs(end.get_j() - this->j) > fabs(end.get_i() - this->i)) {
226  double j = (end.get_j() > this->j ? this->j + 1 : this->j - 1);
227 #if (VISP_CXX_STANDARD > VISP_CXX_STANDARD_98)
228  return {end.get_i() - line_slope * (end.get_j() - j), j};
229 #else
230  return vpImagePoint(end.get_i() - line_slope * (end.get_j() - j), j);
231 #endif
232  } else {
233  double i = (end.get_i() > this->i ? this->i + 1 : this->i - 1);
234 #if (VISP_CXX_STANDARD > VISP_CXX_STANDARD_98)
235  return {i, end.get_j() - ((end.get_i() - i) / line_slope)};
236 #else
237  return vpImagePoint(i, end.get_j() - ((end.get_i() - i) / line_slope));
238 #endif
239  }
240  }
241 
246  {
247  this->i = ip.i;
248  this->j = ip.j;
249  return *this;
250  }
251 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
255  inline vpImagePoint &operator=(const vpImagePoint &&ip) noexcept
256  {
257  this->i = ip.i;
258  this->j = ip.j;
259  return *this;
260  }
261 #endif
262 
263  vpImagePoint &operator+=(const vpImagePoint &ip);
264 
271  {
272  this->i -= ip.i;
273  this->j -= ip.j;
274  return *this;
275  }
276  vpImagePoint &operator/=(double scale);
277 
282  inline vpImagePoint &operator*=(double scale)
283  {
284  this->i *= scale;
285  this->j *= scale;
286  return *this;
287  }
288 
298  inline void set_i(double ii) { this->i = ii; }
299 
309  inline void set_j(double jj) { this->j = jj; }
310 
320  inline void set_ij(double ii, double jj)
321  {
322  this->i = ii;
323  this->j = jj;
324  }
325 
335  inline void set_u(double u) { j = u; }
336 
346  inline void set_v(double v) { i = v; }
347 
357  inline void set_uv(double u, double v)
358  {
359  this->i = v;
360  this->j = u;
361  }
362 
363  static double distance(const vpImagePoint &iP1, const vpImagePoint &iP2);
364  static vpRect getBBox(const std::vector<vpImagePoint> &ipVec);
365  static double sqrDistance(const vpImagePoint &iP1, const vpImagePoint &iP2);
366 
367  friend VISP_EXPORT bool operator==(const vpImagePoint &ip1, const vpImagePoint &ip2);
368  friend VISP_EXPORT bool operator!=(const vpImagePoint &ip1, const vpImagePoint &ip2);
369  friend VISP_EXPORT vpImagePoint operator+=(const vpImagePoint &ip1, const vpImagePoint &ip2);
370  friend VISP_EXPORT vpImagePoint operator+(const vpImagePoint &ip1, const vpImagePoint &ip2);
371  friend VISP_EXPORT vpImagePoint operator+(const vpImagePoint &ip1, int offset);
372  friend VISP_EXPORT vpImagePoint operator+(const vpImagePoint &ip1, unsigned int offset);
373  friend VISP_EXPORT vpImagePoint operator+(const vpImagePoint &ip1, double offset);
374  friend VISP_EXPORT vpImagePoint operator-(const vpImagePoint &ip1, const vpImagePoint &ip2);
375  friend VISP_EXPORT vpImagePoint operator-(const vpImagePoint &ip1, int offset);
376  friend VISP_EXPORT vpImagePoint operator-(const vpImagePoint &ip1, unsigned int offset);
377  friend VISP_EXPORT vpImagePoint operator-(const vpImagePoint &ip1, double offset);
378  friend VISP_EXPORT vpImagePoint operator*(const vpImagePoint &ip1, double scale);
379  friend VISP_EXPORT vpImagePoint operator/(const vpImagePoint &ip1, double scale);
380  friend VISP_EXPORT std::ostream &operator<<(std::ostream &os, const vpImagePoint &ip);
381 
382 private:
383  double i, j;
384 };
385 
386 #endif
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:89
void set_j(double jj)
Definition: vpImagePoint.h:309
double get_j() const
Definition: vpImagePoint.h:132
vpImagePoint & operator=(const vpImagePoint &&ip) noexcept
Definition: vpImagePoint.h:255
vpImagePoint(double ii, double jj)
Definition: vpImagePoint.h:100
vpImagePoint & operator=(const vpImagePoint &ip)
Definition: vpImagePoint.h:245
void set_ij(double ii, double jj)
Definition: vpImagePoint.h:320
virtual ~vpImagePoint()
Destructor.
Definition: vpImagePoint.h:110
void set_i(double ii)
Definition: vpImagePoint.h:298
vpImagePoint nextInSegment(const vpImagePoint &start, const vpImagePoint &end) const
Definition: vpImagePoint.h:222
double get_u() const
Definition: vpImagePoint.h:143
void set_u(double u)
Definition: vpImagePoint.h:335
void set_uv(double u, double v)
Definition: vpImagePoint.h:357
vpImagePoint & operator*=(double scale)
Definition: vpImagePoint.h:282
vpImagePoint(const vpImagePoint &ip)
Definition: vpImagePoint.h:108
void set_v(double v)
Definition: vpImagePoint.h:346
vpImagePoint & operator-=(const vpImagePoint &ip)
Definition: vpImagePoint.h:270
bool inSegment(const vpImagePoint &start, const vpImagePoint &end) const
Definition: vpImagePoint.h:169
double get_i() const
Definition: vpImagePoint.h:121
double get_v() const
Definition: vpImagePoint.h:154
Defines a rectangle in the plane.
Definition: vpRect.h:80
vpColVector operator*(const double &x, const vpColVector &v)