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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 24b95e159 Thu Jan 23 01:15:46 2020 +0100 at jue ene 23 01:30:10 CET 2020