Main MRPT website > C++ reference for MRPT 1.9.9
CPoint.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #ifndef CPOINT_H
10 #define CPOINT_H
11 
14 
15 namespace mrpt
16 {
17 namespace poses
18 {
19 /** A base class for representing a point in 2D or 3D.
20  * For more information refer to the <a
21  * href="http://www.mrpt.org/2D_3D_Geometry">2D/3D Geometry tutorial</a> online.
22  * \note This class is based on the CRTP design pattern
23  * \sa CPoseOrPoint, CPose
24  * \ingroup poses_grp
25  */
26 template <class DERIVEDCLASS>
27 class CPoint : public CPoseOrPoint<DERIVEDCLASS>
28 {
29  public:
30  /** @name Methods common to all 2D or 3D points
31  @{ */
32 
33  /** Scalar addition of all coordinates.
34  * This is diferent from poses/point composition, which is implemented as
35  * "+" operators in classes derived from "CPose"
36  */
37  template <class OTHERCLASS>
38  inline void AddComponents(const OTHERCLASS& b)
39  {
40  const int dims = std::min(
42  size_t(OTHERCLASS::is3DPoseOrPoint() ? 3 : 2));
43  for (int i = 0; i < dims; i++)
44  static_cast<DERIVEDCLASS*>(this)->m_coords[i] +=
45  static_cast<const OTHERCLASS*>(&b)->m_coords[i];
46  }
47 
48  /** Scalar multiplication. */
49  inline void operator*=(const double s)
50  {
51  for (int i = 0; i < DERIVEDCLASS::static_size; i++)
52  static_cast<DERIVEDCLASS*>(this)->m_coords[i] *= s;
53  }
54 
55  /** Return the pose or point as a 1x2 or 1x3 vector [x y] or [x y z] */
57  {
59  for (int i = 0; i < DERIVEDCLASS::static_size; i++)
60  v[i] = static_cast<const DERIVEDCLASS*>(this)->m_coords[i];
61  }
62  //! \overload
64  {
66  getAsVector(v);
67  return v;
68  }
69 
70  /** Returns the corresponding 4x4 homogeneous transformation matrix for the
71  * point(translation) or pose (translation+orientation).
72  * \sa getInverseHomogeneousMatrix
73  */
75  {
76  out_HM.unit(4, 1.0);
77  out_HM.get_unsafe(0, 3) = static_cast<const DERIVEDCLASS*>(this)->x();
78  out_HM.get_unsafe(1, 3) = static_cast<const DERIVEDCLASS*>(this)->y();
79  if (DERIVEDCLASS::is3DPoseOrPoint())
80  out_HM.get_unsafe(2, 3) =
81  static_cast<const DERIVEDCLASS*>(this)->m_coords[2];
82  }
83 
84  /** Returns a human-readable textual representation of the object (eg:
85  * "[0.02 1.04]" )
86  * \sa fromString
87  */
88  void asString(std::string& s) const
89  {
90  s = (!DERIVEDCLASS::is3DPoseOrPoint())
91  ? mrpt::format(
92  "[%f %f]", static_cast<const DERIVEDCLASS*>(this)->x(),
93  static_cast<const DERIVEDCLASS*>(this)->y())
94  : mrpt::format(
95  "[%f %f %f]", static_cast<const DERIVEDCLASS*>(this)->x(),
96  static_cast<const DERIVEDCLASS*>(this)->y(),
97  static_cast<const DERIVEDCLASS*>(this)->m_coords[2]);
98  }
99  inline std::string asString() const
100  {
101  std::string s;
102  asString(s);
103  return s;
104  }
105 
106  /** Set the current object value from a string generated by 'asString' (eg:
107  * "[0.02 1.04]" )
108  * \sa asString
109  * \exception std::exception On invalid format
110  */
111  void fromString(const std::string& s)
112  {
114  if (!m.fromMatlabStringFormat(s))
115  THROW_EXCEPTION("Malformed expression in ::fromString");
118  for (int i = 0; i < DERIVEDCLASS::static_size; i++)
119  static_cast<DERIVEDCLASS*>(this)->m_coords[i] = m.get_unsafe(0, i);
120  }
121 
122  inline const double& operator[](unsigned int i) const
123  {
124  return static_cast<const DERIVEDCLASS*>(this)->m_coords[i];
125  }
126  inline double& operator[](unsigned int i)
127  {
128  return static_cast<DERIVEDCLASS*>(this)->m_coords[i];
129  }
130 
131  /** @} */
132 
133 }; // End of class def.
134 
135 /** Dumps a point as a string [x,y] or [x,y,z] */
136 template <class DERIVEDCLASS>
137 std::ostream& operator<<(std::ostream& o, const CPoint<DERIVEDCLASS>& p)
138 {
139  o << "(" << p[0] << "," << p[1];
140  if (p.is3DPoseOrPoint()) o << "," << p[2];
141  o << ")";
142  return o;
143 }
144 
145 /** Used by STL algorithms */
146 template <class DERIVEDCLASS>
147 bool operator<(const CPoint<DERIVEDCLASS>& a, const CPoint<DERIVEDCLASS>& b)
148 {
149  if (a.x() < b.x())
150  return true;
151  else
152  {
153  if (!a.is3DPoseOrPoint())
154  return a.y() < b.y();
155  else if (a.y() < b.y())
156  return true;
157  else
158  return a[2] < b[2];
159  }
160 }
161 
162 template <class DERIVEDCLASS>
164 {
165  for (int i = 0; i < DERIVEDCLASS::static_size; i++)
166  if (p1[i] != p2[i]) return false; //-V550
167  return true;
168 }
169 
170 template <class DERIVEDCLASS>
172 {
173  for (int i = 0; i < DERIVEDCLASS::static_size; i++)
174  if (p1[i] != p2[i]) return true; //-V550
175  return false;
176 }
177 
178 } // End of namespace
179 } // End of namespace
180 
181 #endif
#define ASSERT_EQUAL_(__A, __B)
void asString(std::string &s) const
Returns a human-readable textual representation of the object (eg: "[0.02 1.04]" ) ...
Definition: CPoint.h:88
double x() const
Common members of all points & poses classes.
Definition: CPoseOrPoint.h:135
double & operator[](unsigned int i)
Definition: CPoint.h:126
#define min(a, b)
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
#define THROW_EXCEPTION(msg)
std::string asString() const
Definition: CPoint.h:99
Column vector, like Eigen::MatrixX*, but automatically initialized to zeros since construction...
Definition: eigen_frwds.h:42
GLdouble s
Definition: glext.h:3676
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: CPoint.h:111
A numeric matrix of compile-time fixed size.
A base class for representing a point in 2D or 3D.
Definition: CPoint.h:27
void AddComponents(const OTHERCLASS &b)
Scalar addition of all coordinates.
Definition: CPoint.h:38
bool operator!=(const CPoint< DERIVEDCLASS > &p1, const CPoint< DERIVEDCLASS > &p2)
Definition: CPoint.h:171
GLubyte GLubyte b
Definition: glext.h:6279
const double & operator[](unsigned int i) const
Definition: CPoint.h:122
The base template class for 2D & 3D points and poses.
Definition: CPoseOrPoint.h:125
GLsizei const GLchar ** string
Definition: glext.h:4101
const GLdouble * v
Definition: glext.h:3678
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
size_t size(const MATRIXLIKE &m, const int dim)
bool operator==(const CPoint< DERIVEDCLASS > &p1, const CPoint< DERIVEDCLASS > &p2)
Definition: CPoint.h:163
void getAsVector(mrpt::math::CVectorDouble &v) const
Return the pose or point as a 1x2 or 1x3 vector [x y] or [x y z].
Definition: CPoint.h:56
mrpt::math::CVectorDouble getAsVector() const
Definition: CPoint.h:63
void operator*=(const double s)
Scalar multiplication.
Definition: CPoint.h:49
void getHomogeneousMatrix(mrpt::math::CMatrixDouble44 &out_HM) const
Returns the corresponding 4x4 homogeneous transformation matrix for the point(translation) or pose (t...
Definition: CPoint.h:74
GLubyte GLubyte GLubyte a
Definition: glext.h:6279
GLfloat GLfloat p
Definition: glext.h:6305



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019