MRPT  1.9.9
TPoint2D.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <mrpt/core/bits_math.h>
12 #include <mrpt/core/format.h>
13 #include <mrpt/math/TPoseOrPoint.h>
14 #include <cmath>
15 #include <vector>
16 
17 namespace mrpt::math
18 {
19 /** \ingroup geometry_grp */
21 {
22  /** X,Y coordinates */
23  double x, y;
24 };
25 
26 /**
27  * Lightweight 2D point. Allows coordinate access using [] operator.
28  * \sa mrpt::poses::CPoint2D
29  * \ingroup geometry_grp
30  */
31 struct TPoint2D : public TPoseOrPoint,
32  public TPoint2D_data,
33  public internal::ProvideStaticResize<TPoint2D>
34 {
35  enum
36  {
38  };
39  /** Default constructor. Initializes to zeros */
40  constexpr TPoint2D() : TPoint2D_data{0, 0} {}
41  /** Constructor from coordinates */
42  constexpr TPoint2D(double xx, double yy) : TPoint2D_data{xx, yy} {}
43  constexpr TPoint2D(const TPoint2D_data& d) : TPoint2D_data{d.x, d.y} {}
44 
45  /** Constructor from TPose2D, discarding phi.
46  * \sa TPose2D
47  */
48  explicit TPoint2D(const TPose2D& p);
49  /**
50  * Constructor from TPoint3D, discarding z.
51  * \sa TPoint3D
52  */
53  explicit TPoint2D(const TPoint3D& p);
54  /**
55  * Constructor from TPose3D, discarding z and the angular coordinates.
56  * \sa TPose3D
57  */
58  explicit TPoint2D(const TPose3D& p);
59 
60  /** Coordinate access using operator[]. Order: x,y */
61  double& operator[](size_t i)
62  {
63  switch (i)
64  {
65  case 0:
66  return x;
67  case 1:
68  return y;
69  default:
70  throw std::out_of_range("index out of range");
71  }
72  }
73  /** Coordinate access using operator[]. Order: x,y */
74  constexpr const double& operator[](size_t i) const
75  {
76  switch (i)
77  {
78  case 0:
79  return x;
80  case 1:
81  return y;
82  default:
83  throw std::out_of_range("index out of range");
84  }
85  }
86 
87  /**
88  * Transformation into vector.
89  */
90  void asVector(std::vector<double>& v) const
91  {
92  v.resize(2);
93  v[0] = x;
94  v[1] = y;
95  }
96 
97  bool operator<(const TPoint2D& p) const;
98 
100  {
101  x += p.x;
102  y += p.y;
103  return *this;
104  }
105 
107  {
108  x -= p.x;
109  y -= p.y;
110  return *this;
111  }
112 
113  TPoint2D& operator*=(double d)
114  {
115  x *= d;
116  y *= d;
117  return *this;
118  }
119 
120  TPoint2D& operator/=(double d)
121  {
122  x /= d;
123  y /= d;
124  return *this;
125  }
126 
127  constexpr TPoint2D operator+(const TPoint2D& p) const
128  {
129  return {x + p.x, y + p.y};
130  }
131 
132  constexpr TPoint2D operator-(const TPoint2D& p) const
133  {
134  return {x - p.x, y - p.y};
135  }
136 
137  constexpr TPoint2D operator*(double d) const { return {d * x, d * y}; }
138  constexpr TPoint2D operator/(double d) const { return {x / d, y / d}; }
139  /** Returns a human-readable textual representation of the object (eg:
140  * "[0.02 1.04]" )
141  * \sa fromString
142  */
143  void asString(std::string& s) const { s = mrpt::format("[%f %f]", x, y); }
145  {
146  std::string s;
147  asString(s);
148  return s;
149  }
150 
151  /** Set the current object value from a string generated by 'asString' (eg:
152  * "[0.02 1.04]" )
153  * \sa asString
154  * \exception std::exception On invalid format
155  */
156  void fromString(const std::string& s);
157 
159  {
160  TPoint2D o;
161  o.fromString(s);
162  return o;
163  }
164 
165  /** Squared norm: |v|^2 = x^2+y^2 */
166  double sqrNorm() const { return x * x + y * y; }
167 
168  /** Point norm: |v| = sqrt(x^2+y^2) */
169  double norm() const { return std::sqrt(sqrNorm()); }
170 };
171 
172 /** Exact comparison between 2D points */
173 constexpr bool operator==(const TPoint2D& p1, const TPoint2D& p2)
174 {
175  return (p1.x == p2.x) && (p1.y == p2.y); //-V550
176 }
177 /** Exact comparison between 2D points */
178 constexpr bool operator!=(const TPoint2D& p1, const TPoint2D& p2)
179 {
180  return (p1.x != p2.x) || (p1.y != p2.y); //-V550
181 }
182 
183 } // namespace mrpt::math
184 
185 namespace mrpt::typemeta
186 {
187 // Specialization must occur in the same namespace
189 } // namespace mrpt::typemeta
TPoint2D & operator+=(const TPoint2D &p)
Definition: TPoint2D.h:99
double & operator[](size_t i)
Coordinate access using operator[].
Definition: TPoint2D.h:61
double x
X,Y coordinates.
Definition: TPoint2D.h:23
void fromString(const std::string &s)
Set the current object value from a string generated by &#39;asString&#39; (eg: "[0.02 1.04]" ) ...
constexpr TPoint2D(const TPoint2D_data &d)
Definition: TPoint2D.h:43
TPoint2D & operator*=(double d)
Definition: TPoint2D.h:113
void asString(std::string &s) const
Returns a human-readable textual representation of the object (eg: "[0.02 1.04]" ) ...
Definition: TPoint2D.h:143
constexpr const double & operator[](size_t i) const
Coordinate access using operator[].
Definition: TPoint2D.h:74
std::string std::string format(std::string_view fmt, ARGS &&... args)
Definition: format.h:26
Base type of all TPoseXX and TPointXX classes in mrpt::math.
Definition: TPoseOrPoint.h:24
constexpr TPoint2D operator/(double d) const
Definition: TPoint2D.h:138
constexpr TPoint2D operator-(const TPoint2D &p) const
Definition: TPoint2D.h:132
constexpr TPoint2D operator+(const TPoint2D &p) const
Definition: TPoint2D.h:127
GLdouble s
Definition: glext.h:3682
This base provides a set of functions for maths stuff.
constexpr TPoint2D(double xx, double yy)
Constructor from coordinates.
Definition: TPoint2D.h:42
std::string asString() const
Definition: TPoint2D.h:144
double sqrNorm() const
Squared norm: |v|^2 = x^2+y^2.
Definition: TPoint2D.h:166
#define MRPT_DECLARE_TTYPENAME_NO_NAMESPACE(_TYPE, __NS)
Declares a typename to be "type" (without the NS prefix)
Definition: TTypeName.h:128
constexpr TPoint2D()
Default constructor.
Definition: TPoint2D.h:40
GLsizei const GLchar ** string
Definition: glext.h:4116
double norm() const
Point norm: |v| = sqrt(x^2+y^2)
Definition: TPoint2D.h:169
const GLdouble * v
Definition: glext.h:3684
Provided for STL and matrices/vectors compatibility.
Definition: TPoseOrPoint.h:54
void asVector(std::vector< double > &v) const
Transformation into vector.
Definition: TPoint2D.h:90
GLenum GLint GLint y
Definition: glext.h:3542
static TPoint2D FromString(const std::string &s)
Definition: TPoint2D.h:158
TPoint2D & operator-=(const TPoint2D &p)
Definition: TPoint2D.h:106
GLenum GLint x
Definition: glext.h:3542
constexpr bool operator==(const TPoint2D &p1, const TPoint2D &p2)
Exact comparison between 2D points.
Definition: TPoint2D.h:173
Lightweight 2D point.
Definition: TPoint2D.h:31
GLfloat GLfloat p
Definition: glext.h:6398
constexpr TPoint2D operator*(double d) const
Definition: TPoint2D.h:137
constexpr bool operator!=(const TPoint2D &p1, const TPoint2D &p2)
Exact comparison between 2D points.
Definition: TPoint2D.h:178
bool operator<(const TPoint2D &p) const
TPoint2D & operator/=(double d)
Definition: TPoint2D.h:120



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: dad381fd7 Sun Oct 20 13:36:46 2019 +0200 at dom oct 20 13:40:10 CEST 2019