Main MRPT website > C++ reference for MRPT 1.9.9
CPose2DGridTemplate.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 CPose2DGridTemplate_H
10 #define CPose2DGridTemplate_H
11 
13 #include <mrpt/utils/round.h> // for round()
14 #include <mrpt/utils/bits.h> // for DEG2RAD()
15 
16 namespace mrpt
17 {
18 namespace poses
19 {
20 /** This is a template class for storing a 3D (2D+heading) grid containing any
21  * kind of data.
22  * \ingroup poses_pdf_grp
23  */
24 template <class T>
26 {
27  protected:
28  /** The limits and resolution of the grid:
29  */
32 
33  /** The size of "m_data" is m_sizeX * m_sizeY * m_sizePhi
34  */
36 
37  /** The indexes of the "left" borders:
38  */
40 
41  /** The data:
42  */
43  std::vector<T> m_data;
44 
45  public:
46  /** Returns "indexes" from coordinates:
47  */
48  size_t x2idx(double x) const
49  {
50  int idx = mrpt::utils::round((x - m_xMin) / m_resolutionXY);
51  ASSERT_(idx >= 0 && idx < static_cast<int>(m_sizeX));
52  return idx;
53  }
54 
55  /** Returns "indexes" from coordinates:
56  */
57  size_t y2idx(double y) const
58  {
59  int idx = mrpt::utils::round((y - m_yMin) / m_resolutionXY);
60  ASSERT_(idx >= 0 && idx < static_cast<int>(m_sizeY));
61  return idx;
62  }
63 
64  /** Returns "indexes" from coordinates:
65  */
66  size_t phi2idx(double phi) const
67  {
68  int idx = mrpt::utils::round((phi - m_phiMin) / m_resolutionPhi);
69  ASSERT_(idx >= 0 && idx < static_cast<int>(m_sizePhi));
70  return idx;
71  }
72 
73  /** Returns coordinates from "indexes":
74  */
75  double idx2x(size_t x) const
76  {
77  ASSERT_(x < m_sizeX);
78  return m_xMin + x * m_resolutionXY;
79  }
80 
81  /** Returns coordinates from "indexes":
82  */
83  double idx2y(size_t y) const
84  {
85  ASSERT_(y < m_sizeY);
86  return m_yMin + y * m_resolutionXY;
87  }
88 
89  /** Returns coordinates from "indexes":
90  */
91  double idx2phi(size_t phi) const
92  {
93  ASSERT_(phi < m_sizePhi);
94  return m_phiMin + phi * m_resolutionPhi;
95  }
96 
97  /** Default constructor:
98  */
100  double xMin = -1.0f, double xMax = 1.0f, double yMin = -1.0f,
101  double yMax = 1.0f, double resolutionXY = 0.5f,
102  double resolutionPhi = mrpt::utils::DEG2RAD(180),
103  double phiMin = -M_PIf, double phiMax = M_PIf)
104  : m_xMin(),
105  m_xMax(),
106  m_yMin(),
107  m_yMax(),
108  m_phiMin(),
109  m_phiMax(),
110  m_resolutionXY(),
111  m_resolutionPhi(),
112  m_sizeX(),
113  m_sizeY(),
114  m_sizePhi(),
115  m_sizeXY(),
116  m_idxLeftX(),
117  m_idxLeftY(),
118  m_idxLeftPhi(),
119  m_data()
120  {
121  setSize(
122  xMin, xMax, yMin, yMax, resolutionXY, resolutionPhi, phiMin,
123  phiMax);
124  }
125 
126  virtual ~CPose2DGridTemplate() {}
127  /** Changes the limits and size of the grid, erasing previous contents:
128  */
129  void setSize(
130  double xMin, double xMax, double yMin, double yMax, double resolutionXY,
131  double resolutionPhi, double phiMin = -M_PIf, double phiMax = M_PIf)
132  {
133  // Checks
134  ASSERT_(xMax > xMin);
135  ASSERT_(yMax > yMin);
136  ASSERT_(phiMax >= phiMin);
137  ASSERT_(resolutionXY > 0);
138  ASSERT_(resolutionPhi > 0);
139 
140  // Copy data:
141  m_xMin = xMin;
142  m_xMax = xMax;
143  m_yMin = yMin;
144  m_yMax = yMax;
145  m_phiMin = phiMin;
146  m_phiMax = phiMax;
147  m_resolutionXY = resolutionXY;
148  m_resolutionPhi = resolutionPhi;
149 
150  // Compute the indexes of the starting borders:
151  m_idxLeftX = mrpt::utils::round(xMin / resolutionXY);
152  m_idxLeftY = mrpt::utils::round(yMin / resolutionXY);
153  m_idxLeftPhi = mrpt::utils::round(phiMin / resolutionPhi);
154 
155  // Compute new required space:
156  m_sizeX = mrpt::utils::round(xMax / resolutionXY) - m_idxLeftX + 1;
157  m_sizeY = mrpt::utils::round(yMax / resolutionXY) - m_idxLeftY + 1;
158  m_sizePhi =
159  mrpt::utils::round(phiMax / resolutionPhi) - m_idxLeftPhi + 1;
161 
162  // Resize "m_data":
163  m_data.clear();
164  m_data.resize(m_sizeX * m_sizeY * m_sizePhi);
165  }
166 
167  /** Reads the contents of a cell
168  */
169  const T* getByPos(double x, double y, double phi) const
170  {
171  return getByIndex(x2idx(x), y2idx(y), phi2idx(phi));
172  }
173 
174  /** Reads the contents of a cell
175  */
176  T* getByPos(double x, double y, double phi)
177  {
178  return getByIndex(x2idx(x), y2idx(y), phi2idx(phi));
179  }
180 
181  /** Reads the contents of a cell
182  */
183  const T* getByIndex(size_t x, size_t y, size_t phi) const
184  {
185  ASSERT_(x < m_sizeX && y < m_sizeY && phi < m_sizePhi)
186  return &m_data[phi * m_sizeXY + y * m_sizeX + x];
187  }
188 
189  /** Reads the contents of a cell
190  */
191  T* getByIndex(size_t x, size_t y, size_t phi)
192  {
193  ASSERT_(x < m_sizeX && y < m_sizeY && phi < m_sizePhi)
194  return &m_data[phi * m_sizeXY + y * m_sizeX + x];
195  }
196 
197  /** Returns the whole grid as a matrix, for a given constant "phi" and where
198  * each row contains values for a fixed "y".
199  */
200  template <class MATRIXLIKE>
201  void getAsMatrix(const double& phi, MATRIXLIKE& outMat)
202  {
203  MRPT_START
204  outMat.setSize(m_sizeY, m_sizeX);
205  size_t phiIdx = phi2idx(phi);
206  ASSERT_(phi < m_sizePhi);
207  for (size_t y = 0; y < m_sizeY; y++)
208  for (size_t x = 0; x < m_sizeX; x++)
209  outMat(y, x) = m_data[phiIdx * m_sizeXY + y * m_sizeX + x];
210  MRPT_END
211  }
212 
213  /** Get info about the grid:
214  */
215  double getXMin() const { return m_xMin; }
216  double getXMax() const { return m_xMax; }
217  double getYMin() const { return m_yMin; }
218  double getYMax() const { return m_yMax; }
219  double getPhiMin() const { return m_phiMin; }
220  double getPhiMax() const { return m_phiMax; }
221  double getResolutionXY() const { return m_resolutionXY; }
222  double getResolutionPhi() const { return m_resolutionPhi; }
223  size_t getSizeX() const { return m_sizeX; }
224  size_t getSizeY() const { return m_sizeY; }
225  size_t getSizePhi() const { return m_sizePhi; }
226 }; // End of class def.
227 
228 } // End of namespace
229 } // End of namespace
230 
231 #endif
double getXMin() const
Get info about the grid:
double DEG2RAD(const double x)
Degrees to radians.
size_t m_sizeX
The size of "m_data" is m_sizeX * m_sizeY * m_sizePhi.
double idx2y(size_t y) const
Returns coordinates from "indexes":
const T * getByPos(double x, double y, double phi) const
Reads the contents of a cell.
void getAsMatrix(const double &phi, MATRIXLIKE &outMat)
Returns the whole grid as a matrix, for a given constant "phi" and where each row contains values for...
std::vector< T > m_data
The data:
const T * getByIndex(size_t x, size_t y, size_t phi) const
Reads the contents of a cell.
void setSize(double xMin, double xMax, double yMin, double yMax, double resolutionXY, double resolutionPhi, double phiMin=-M_PIf, double phiMax=M_PIf)
Changes the limits and size of the grid, erasing previous contents:
#define M_PIf
#define MRPT_END
size_t phi2idx(double phi) const
Returns "indexes" from coordinates:
size_t x2idx(double x) const
Returns "indexes" from coordinates:
T * getByPos(double x, double y, double phi)
Reads the contents of a cell.
double idx2phi(size_t phi) const
Returns coordinates from "indexes":
#define MRPT_START
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
size_t y2idx(double y) const
Returns "indexes" from coordinates:
double m_xMin
The limits and resolution of the grid:
int m_idxLeftX
The indexes of the "left" borders:
#define ASSERT_(f)
int round(const T value)
Returns the closer integer (int) to x.
Definition: round.h:25
GLenum GLint GLint y
Definition: glext.h:3538
double idx2x(size_t x) const
Returns coordinates from "indexes":
CPose2DGridTemplate(double xMin=-1.0f, double xMax=1.0f, double yMin=-1.0f, double yMax=1.0f, double resolutionXY=0.5f, double resolutionPhi=mrpt::utils::DEG2RAD(180), double phiMin=-M_PIf, double phiMax=M_PIf)
Default constructor:
GLenum GLint x
Definition: glext.h:3538
This is a template class for storing a 3D (2D+heading) grid containing any kind of data...
T * getByIndex(size_t x, size_t y, size_t phi)
Reads the contents of a cell.



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