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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020