MRPT  1.9.9
CPoint.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 
13 
14 namespace mrpt::poses
15 {
16 /** A base class for representing a point in 2D or 3D.
17  * For more information refer to the <a
18  * href="http://www.mrpt.org/2D_3D_Geometry">2D/3D Geometry tutorial</a> online.
19  * \note This class is based on the CRTP design pattern
20  * \sa CPoseOrPoint, CPose
21  * \ingroup poses_grp
22  */
23 template <class DERIVEDCLASS, std::size_t DIM>
24 class CPoint : public CPoseOrPoint<DERIVEDCLASS, DIM>
25 {
26  DERIVEDCLASS& derived() { return *static_cast<DERIVEDCLASS*>(this); }
27  const DERIVEDCLASS& derived() const
28  {
29  return *static_cast<const DERIVEDCLASS*>(this);
30  }
31 
32  public:
33  /** @name Methods common to all 2D or 3D points
34  @{ */
35 
36  /** Scalar addition of all coordinates.
37  * This is diferent from poses/point composition, which is implemented as
38  * "+" operators in classes derived from "CPose"
39  */
40  template <class OTHERCLASS>
41  inline void AddComponents(const OTHERCLASS& b)
42  {
43  const int dims = std::min(
44  size_t(DERIVEDCLASS::static_size),
45  size_t(OTHERCLASS::is3DPoseOrPoint() ? 3 : 2));
46  for (int i = 0; i < dims; i++)
47  derived().m_coords[i] +=
48  static_cast<const OTHERCLASS*>(&b)->m_coords[i];
49  }
50 
51  /** Scalar multiplication. */
52  inline void operator*=(const double s)
53  {
54  for (int i = 0; i < DERIVEDCLASS::static_size; i++)
55  derived().m_coords[i] *= s;
56  }
57 
58  /** Returns the corresponding 4x4 homogeneous transformation matrix for the
59  * point(translation) or pose (translation+orientation).
60  * \sa getInverseHomogeneousMatrix
61  */
62  template <class MATRIX44>
63  void getHomogeneousMatrix(MATRIX44& out_HM) const
64  {
65  out_HM.setIdentity(4);
66  out_HM(0, 3) = static_cast<const DERIVEDCLASS*>(this)->x();
67  out_HM(1, 3) = static_cast<const DERIVEDCLASS*>(this)->y();
68  if (DERIVEDCLASS::is3DPoseOrPoint())
69  out_HM(2, 3) = static_cast<const DERIVEDCLASS*>(this)->m_coords[2];
70  }
71 
72  /** Returns a human-readable textual representation of the object (eg:
73  * "[0.02 1.04]" )
74  * \sa fromString
75  */
76  void asString(std::string& s) const;
77  inline std::string asString() const
78  {
79  std::string s;
80  asString(s);
81  return s;
82  }
83 
84  /** Set the current object value from a string generated by 'asString' (eg:
85  * "[0.02 1.04]" )
86  * \sa asString
87  * \exception std::exception On invalid format
88  */
89  void fromString(const std::string& s);
90 
91  inline double operator[](unsigned int i) const
92  {
93  return static_cast<const DERIVEDCLASS*>(this)->m_coords[i];
94  }
95  inline double& operator[](unsigned int i) { return derived().m_coords[i]; }
96  /** @} */
97 
98 }; // End of class def.
99 
100 /** Used by STL algorithms */
101 template <class DERIVEDCLASS, std::size_t DIM>
104 {
105  if (a.x() < b.x())
106  return true;
107  else
108  {
109  if (!a.is3DPoseOrPoint())
110  return a.y() < b.y();
111  else if (a.y() < b.y())
112  return true;
113  else
114  return a[2] < b[2];
115  }
116 }
117 
118 template <class DERIVEDCLASS, std::size_t DIM>
121 {
122  for (int i = 0; i < DERIVEDCLASS::static_size; i++)
123  if (p1[i] != p2[i]) return false; //-V550
124  return true;
125 }
126 
127 template <class DERIVEDCLASS, std::size_t DIM>
130 {
131  for (int i = 0; i < DERIVEDCLASS::static_size; i++)
132  if (p1[i] != p2[i]) return true; //-V550
133  return false;
134 }
135 } // namespace mrpt::poses
bool operator<(const CPoint< DERIVEDCLASS, DIM > &a, const CPoint< DERIVEDCLASS, DIM > &b)
Used by STL algorithms.
Definition: CPoint.h:102
double & operator[](unsigned int i)
Definition: CPoint.h:95
void AddComponents(const OTHERCLASS &b)
Scalar addition of all coordinates.
Definition: CPoint.h:41
const DERIVEDCLASS & derived() const
Definition: CPoint.h:27
std::string asString() const
Definition: CPoint.h:77
A base class for representing a point in 2D or 3D.
Definition: CPoint.h:24
double x() const
Common members of all points & poses classes.
Definition: CPoseOrPoint.h:143
The base template class for 2D & 3D points and poses.
Definition: CPoseOrPoint.h:123
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
static bool is3DPoseOrPoint()
Return true for poses or points with a Z component, false otherwise.
Definition: CPoseOrPoint.h:180
bool operator!=(const CPoint< DERIVEDCLASS, DIM > &p1, const CPoint< DERIVEDCLASS, DIM > &p2)
Definition: CPoint.h:128
void getHomogeneousMatrix(MATRIX44 &out_HM) const
Returns the corresponding 4x4 homogeneous transformation matrix for the point(translation) or pose (t...
Definition: CPoint.h:63
DERIVEDCLASS & derived()
Definition: CPoint.h:26
bool operator==(const CPoint< DERIVEDCLASS, DIM > &p1, const CPoint< DERIVEDCLASS, DIM > &p2)
Definition: CPoint.h:119
void operator*=(const double s)
Scalar multiplication.
Definition: CPoint.h:52
double operator[](unsigned int i) const
Definition: CPoint.h:91
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.cpp:20



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: c7a3bec24 Sun Mar 29 18:33:13 2020 +0200 at dom mar 29 18:50:38 CEST 2020