MRPT  2.0.4
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-2020, 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 <mrpt/math/math_frwds.h> // CMatrixFixed
15 #include <cmath>
16 #include <vector>
17 
18 namespace mrpt::math
19 {
20 /** \ingroup geometry_grp */
21 template <typename T>
23 {
24  /** X,Y coordinates */
25  T x, y;
26 };
27 
28 /** Base template for TPoint2D and TPoint2Df
29  * \ingroup geometry_grp
30  */
31 template <typename T>
32 struct TPoint2D_ : public TPoseOrPoint,
33  public TPoint2D_data<T>,
34  public internal::ProvideStaticResize<TPoint2D_<T>>
35 {
36  enum
37  {
39  };
40  /** Default constructor. Initializes to zeros */
41  constexpr TPoint2D_() : TPoint2D_data<T>{0, 0} {}
42  /** Constructor from coordinates */
43  constexpr TPoint2D_(T xx, T yy) : TPoint2D_data<T>{xx, yy} {}
44 
45  /** Explicit constructor from coordinates. */
46  template <typename U>
48  {
49  TPoint2D_data<T>::x = static_cast<T>(p.x);
50  TPoint2D_data<T>::y = static_cast<T>(p.y);
51  }
52 
53  /** Constructor from column vector. */
54  template <typename U>
56  {
57  TPoint2D_data<T>::x = static_cast<T>(m[0]);
58  TPoint2D_data<T>::y = static_cast<T>(m[1]);
59  }
60 
61  /** Constructor from TPose2D, discarding phi.
62  * \sa TPose2D
63  */
64  explicit TPoint2D_(const TPose2D& p);
65  /**
66  * Constructor from TPoint3D, discarding z.
67  * \sa TPoint3D
68  */
69  explicit TPoint2D_(const TPoint3D_<T>& p);
70  /**
71  * Constructor from TPose3D, discarding z and the angular coordinates.
72  * \sa TPose3D
73  */
74  explicit TPoint2D_(const TPose3D& p);
75 
76  /** Return a copy of this object using type U for coordinates */
77  template <typename U>
79  {
80  return TPoint2D_<U>(static_cast<U>(this->x), static_cast<U>(this->y));
81  }
82 
83  /** Coordinate access using operator[]. Order: x,y */
84  T& operator[](size_t i)
85  {
86  switch (i)
87  {
88  case 0:
89  return this->x;
90  case 1:
91  return this->y;
92  default:
93  throw std::out_of_range("index out of range");
94  }
95  }
96  /** Coordinate access using operator[]. Order: x,y */
97  constexpr T operator[](size_t i) const
98  {
99  switch (i)
100  {
101  case 0:
102  return this->x;
103  case 1:
104  return this->y;
105  default:
106  throw std::out_of_range("index out of range");
107  }
108  }
109 
110  /**
111  * Transformation into vector.
112  */
113  template <typename U>
114  void asVector(std::vector<U>& v) const
115  {
116  v.resize(2);
117  v[0] = static_cast<U>(this->x);
118  v[1] = static_cast<U>(this->y);
119  }
120 
121  bool operator<(const TPoint2D_& p) const;
122 
124  {
125  this->x += p.x;
126  this->y += p.y;
127  return *this;
128  }
129 
131  {
132  this->x -= p.x;
133  this->y -= p.y;
134  return *this;
135  }
136 
138  {
139  this->x *= d;
140  this->y *= d;
141  return *this;
142  }
143 
145  {
146  ASSERT_(d != 0);
147  this->x /= d;
148  this->y /= d;
149  return *this;
150  }
151 
152  constexpr TPoint2D_ operator+(const TPoint2D_& p) const
153  {
154  return {this->x + p.x, this->y + p.y};
155  }
156 
157  constexpr TPoint2D_ operator-(const TPoint2D_& p) const
158  {
159  return {this->x - p.x, this->y - p.y};
160  }
161 
162  constexpr TPoint2D_ operator*(T d) const
163  {
164  return {d * this->x, d * this->y};
165  }
166  constexpr TPoint2D_ operator/(T d) const
167  {
168  ASSERT_(d != 0);
169  return {this->x / d, this->y / d};
170  }
171  /** Returns a human-readable textual representation of the object (eg:
172  * "[0.02 1.04]" )
173  * \sa fromString
174  */
175  void asString(std::string& s) const
176  {
177  s = mrpt::format("[%f %f]", this->x, this->y);
178  }
179 
180  std::string asString() const
181  {
182  std::string s;
183  asString(s);
184  return s;
185  }
186 
187  /** Set the current object value from a string generated by 'asString' (eg:
188  * "[0.02 1.04]" )
189  * \sa asString
190  * \exception std::exception On invalid format
191  */
192  void fromString(const std::string& s);
193 
194  static TPoint2D_ FromString(const std::string& s)
195  {
196  TPoint2D_ o;
197  o.fromString(s);
198  return o;
199  }
200 
201  /** Squared norm: |v|^2 = x^2+y^2 */
202  T sqrNorm() const { return this->x * this->x + this->y * this->y; }
203 
204  /** Point norm: |v| = sqrt(x^2+y^2) */
205  T norm() const { return std::sqrt(sqrNorm()); }
206 };
207 
208 /**
209  * Lightweight 2D point. Allows coordinate access using [] operator.
210  * \sa mrpt::poses::CPoint2D
211  * \ingroup geometry_grp
212  */
215 
216 /** Useful type alias for double 2-vectors */
218 /** Useful type alias for float 2-vectors */
220 
221 /** Exact comparison between 2D points */
222 template <typename T>
223 constexpr bool operator==(const TPoint2D_<T>& p1, const TPoint2D_<T>& p2)
224 {
225  return (p1.x == p2.x) && (p1.y == p2.y); //-V550
226 }
227 
228 /** Exact comparison between 2D points */
229 template <typename T>
230 constexpr bool operator!=(const TPoint2D_<T>& p1, const TPoint2D_<T>& p2)
231 {
232  return (p1.x != p2.x) || (p1.y != p2.y); //-V550
233 }
234 
235 } // namespace mrpt::math
236 
237 namespace mrpt::typemeta
238 {
239 // Specialization must occur in the same namespace
241 } // namespace mrpt::typemeta
TPoint2D_(const TPoint2D_data< U > &p)
Explicit constructor from coordinates.
Definition: TPoint2D.h:47
A compile-time fixed-size numeric matrix container.
Definition: CMatrixFixed.h:33
TPoint2D_< double > TPoint2D
Lightweight 2D point.
Definition: TPoint2D.h:213
T & operator[](size_t i)
Coordinate access using operator[].
Definition: TPoint2D.h:84
T x
X,Y coordinates.
Definition: TPoint2D.h:25
std::string std::string format(std::string_view fmt, ARGS &&... args)
Definition: format.h:26
TPoint2D_ & operator*=(T d)
Definition: TPoint2D.h:137
std::string asString() const
Definition: TPoint2D.h:180
Base type of all TPoseXX and TPointXX classes in mrpt::math.
Definition: TPoseOrPoint.h:24
TPoint2D_ & operator/=(T d)
Definition: TPoint2D.h:144
constexpr TPoint2D_()
Default constructor.
Definition: TPoint2D.h:41
constexpr TPoint2D_(T xx, T yy)
Constructor from coordinates.
Definition: TPoint2D.h:43
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
This base provides a set of functions for maths stuff.
constexpr TPoint2D_ operator-(const TPoint2D_ &p) const
Definition: TPoint2D.h:157
void fromString(const std::string &s)
Set the current object value from a string generated by &#39;asString&#39; (eg: "[0.02 1.04]" ) ...
Definition: TPoint2D.cpp:53
TPoint2D_(const mrpt::math::CMatrixFixed< U, 2, 1 > &m)
Constructor from column vector.
Definition: TPoint2D.h:55
#define MRPT_DECLARE_TTYPENAME_NO_NAMESPACE(_TYPE, __NS)
Declares a typename to be "type" (without the NS prefix)
Definition: TTypeName.h:128
constexpr TPoint2D_ operator*(T d) const
Definition: TPoint2D.h:162
constexpr TPoint2D_ operator+(const TPoint2D_ &p) const
Definition: TPoint2D.h:152
constexpr bool operator==(const TPoint2D_< T > &p1, const TPoint2D_< T > &p2)
Exact comparison between 2D points.
Definition: TPoint2D.h:223
Base template for TPoint2D and TPoint2Df.
Definition: TPoint2D.h:32
constexpr bool operator!=(const TPoint2D_< T > &p1, const TPoint2D_< T > &p2)
Exact comparison between 2D points.
Definition: TPoint2D.h:230
TPoint2D_ & operator+=(const TPoint2D_ &p)
Definition: TPoint2D.h:123
TPoint2D_ & operator-=(const TPoint2D_ &p)
Definition: TPoint2D.h:130
Provided for STL and matrices/vectors compatibility.
Definition: TPoseOrPoint.h:63
Base template for TPoint3D and TPoint3Df.
Definition: TPoint3D.h:37
Lightweight 3D pose (three spatial coordinates, plus three angular coordinates).
Definition: TPose3D.h:24
Lightweight 2D pose.
Definition: TPose2D.h:22
void asVector(std::vector< U > &v) const
Transformation into vector.
Definition: TPoint2D.h:114
T norm() const
Point norm: |v| = sqrt(x^2+y^2)
Definition: TPoint2D.h:205
TPoint2D_< U > cast() const
Return a copy of this object using type U for coordinates.
Definition: TPoint2D.h:78
T sqrNorm() const
Squared norm: |v|^2 = x^2+y^2.
Definition: TPoint2D.h:202
constexpr TPoint2D_ operator/(T d) const
Definition: TPoint2D.h:166
bool operator<(const TPoint2D_ &p) const
Definition: TPoint2D.cpp:42
static TPoint2D_ FromString(const std::string &s)
Definition: TPoint2D.h:194
constexpr T operator[](size_t i) const
Coordinate access using operator[].
Definition: TPoint2D.h:97
TPoint2D_< float > TPoint2Df
Definition: TPoint2D.h:214
void asString(std::string &s) const
Returns a human-readable textual representation of the object (eg: "[0.02 1.04]" ) ...
Definition: TPoint2D.h:175



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020