Main MRPT website > C++ reference for MRPT 1.5.7
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 href="http://www.mrpt.org/2D_3D_Geometry">2D/3D Geometry tutorial</a> online.
21  * \note This class is based on the CRTP design pattern
22  * \sa CPoseOrPoint, CPose
23  * \ingroup poses_grp
24  */
25  template <class DERIVEDCLASS>
26  class CPoint : public CPoseOrPoint<DERIVEDCLASS>
27  {
28  public:
29  /** @name Methods common to all 2D or 3D points
30  @{ */
31 
32  /** Scalar addition of all coordinates.
33  * This is diferent from poses/point composition, which is implemented as "+" operators in classes derived from "CPose"
34  */
35  template <class OTHERCLASS>
36  inline void AddComponents(const OTHERCLASS &b)
37  {
38  const int dims = std::min( size_t(DERIVEDCLASS::static_size), size_t(OTHERCLASS::is3DPoseOrPoint() ? 3:2));
39  for (int i=0;i<dims;i++)
40  static_cast<DERIVEDCLASS*>(this)->m_coords[i]+= static_cast<const OTHERCLASS*>(&b)->m_coords[i];
41  }
42 
43  /** Scalar multiplication. */
44  inline void operator *=(const double s)
45  {
46  for (int i=0;i<DERIVEDCLASS::static_size;i++)
47  static_cast<DERIVEDCLASS*>(this)->m_coords[i] *= s;
48  }
49 
50  /** Return the pose or point as a 1x2 or 1x3 vector [x y] or [x y z] */
52  {
54  for (int i=0;i<DERIVEDCLASS::static_size;i++)
55  v[i] = static_cast<const DERIVEDCLASS*>(this)->m_coords[i];
56  }
57  //! \overload
59 
60  /** Returns the corresponding 4x4 homogeneous transformation matrix for the point(translation) or pose (translation+orientation).
61  * \sa getInverseHomogeneousMatrix
62  */
64  {
65  out_HM.unit(4,1.0);
66  out_HM.get_unsafe(0,3)= static_cast<const DERIVEDCLASS*>(this)->x();
67  out_HM.get_unsafe(1,3)= static_cast<const DERIVEDCLASS*>(this)->y();
68  if (DERIVEDCLASS::is3DPoseOrPoint())
69  out_HM.get_unsafe(2,3)= static_cast<const DERIVEDCLASS*>(this)->m_coords[2];
70  }
71 
72  /** Returns a human-readable textual representation of the object (eg: "[0.02 1.04]" )
73  * \sa fromString
74  */
75  void asString(std::string &s) const
76  {
77  s = (!DERIVEDCLASS::is3DPoseOrPoint()) ?
78  mrpt::format("[%f %f]", static_cast<const DERIVEDCLASS*>(this)->x(), static_cast<const DERIVEDCLASS*>(this)->y()) :
79  mrpt::format("[%f %f %f]",static_cast<const DERIVEDCLASS*>(this)->x(), static_cast<const DERIVEDCLASS*>(this)->y(), static_cast<const DERIVEDCLASS*>(this)->m_coords[2]);
80  }
81  inline std::string asString() const { std::string s; asString(s); return s; }
82 
83  /** Set the current object value from a string generated by 'asString' (eg: "[0.02 1.04]" )
84  * \sa asString
85  * \exception std::exception On invalid format
86  */
87  void fromString(const std::string &s)
88  {
90  if (!m.fromMatlabStringFormat(s)) THROW_EXCEPTION("Malformed expression in ::fromString");
93  for (int i=0;i<DERIVEDCLASS::static_size;i++)
94  static_cast<DERIVEDCLASS*>(this)->m_coords[i] = m.get_unsafe(0,i);
95  }
96 
97  inline const double &operator[](unsigned int i) const { return static_cast<const DERIVEDCLASS*>(this)->m_coords[i]; }
98  inline double &operator[](unsigned int i) { return static_cast<DERIVEDCLASS*>(this)->m_coords[i]; }
99 
100  /** @} */
101 
102  }; // End of class def.
103 
104  /** Dumps a point as a string [x,y] or [x,y,z] */
105  template <class DERIVEDCLASS>
106  std::ostream &operator << (std::ostream& o, const CPoint<DERIVEDCLASS>& p)
107  {
108  o << "(" << p[0] << "," << p[1];
109  if (p.is3DPoseOrPoint()) o << "," << p[2];
110  o <<")";
111  return o;
112  }
113 
114  /** Used by STL algorithms */
115  template <class DERIVEDCLASS>
116  bool operator < (const CPoint<DERIVEDCLASS> &a, const CPoint<DERIVEDCLASS> &b)
117  {
118  if (a.x()<b.x()) return true;
119  else
120  {
121  if (!a.is3DPoseOrPoint())
122  return a.y()<b.y();
123  else if (a.y()<b.y())
124  return true;
125  else return a[2]<b[2];
126  }
127  }
128 
129  template <class DERIVEDCLASS>
131  {
132  for (int i=0;i<DERIVEDCLASS::static_size;i++)
133  if (p1[i]!=p2[i]) return false; //-V550
134  return true;
135  }
136 
137  template <class DERIVEDCLASS>
139  {
140  for (int i=0;i<DERIVEDCLASS::static_size;i++)
141  if (p1[i]!=p2[i]) return true; //-V550
142  return false;
143  }
144 
145 
146 
147  } // End of namespace
148 } // End of namespace
149 
150 #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:75
double x() const
Common members of all points & poses classes.
Definition: CPoseOrPoint.h:113
double & operator[](unsigned int i)
Definition: CPoint.h:98
#define min(a, b)
#define THROW_EXCEPTION(msg)
std::string asString() const
Definition: CPoint.h:81
Column vector, like Eigen::MatrixX*, but automatically initialized to zeros since construction...
Definition: eigen_frwds.h:35
GLdouble s
Definition: glext.h:3602
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:87
A numeric matrix of compile-time fixed size.
A base class for representing a point in 2D or 3D.
Definition: CPoint.h:26
void AddComponents(const OTHERCLASS &b)
Scalar addition of all coordinates.
Definition: CPoint.h:36
bool operator!=(const CPoint< DERIVEDCLASS > &p1, const CPoint< DERIVEDCLASS > &p2)
Definition: CPoint.h:138
GLubyte GLubyte b
Definition: glext.h:5575
const double & operator[](unsigned int i) const
Definition: CPoint.h:97
The base template class for 2D & 3D points and poses.
Definition: CPoseOrPoint.h:107
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
GLsizei const GLchar ** string
Definition: glext.h:3919
const GLdouble * v
Definition: glext.h:3603
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:130
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:51
mrpt::math::CVectorDouble getAsVector() const
Definition: CPoint.h:58
void operator*=(const double s)
Scalar multiplication.
Definition: CPoint.h:44
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:63
GLubyte GLubyte GLubyte a
Definition: glext.h:5575
GLfloat GLfloat p
Definition: glext.h:5587



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019