Main MRPT website > C++ reference
MRPT logo
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-2014, 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 
15 namespace mrpt
16 {
17 namespace poses
18 {
20 
21  /** This is a template class for storing a 3D (2D+heading) grid containing any 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  */
30  double m_xMin, m_xMax,
31  m_yMin, m_yMax,
34 
35  /** The size of "m_data" is m_sizeX ·m_sizeY ·m_sizePhi
36  */
38 
39  /** The indexes of the "left" borders:
40  */
42 
43  /** The data:
44  */
45  std::vector<T> m_data;
46 
47  public:
48  /** Returns "indexes" from coordinates:
49  */
50  size_t x2idx(double x) const
51  {
52  int idx = mrpt::utils::round( (x-m_xMin) / m_resolutionXY );
53  ASSERT_(idx>=0 && idx< static_cast<int>(m_sizeX));
54  return idx;
55  }
56 
57  /** Returns "indexes" from coordinates:
58  */
59  size_t y2idx(double y) const
60  {
61  int idx = mrpt::utils::round( (y-m_yMin) / m_resolutionXY );
62  ASSERT_(idx>=0 && idx< static_cast<int>(m_sizeY));
63  return idx;
64  }
65 
66  /** Returns "indexes" from coordinates:
67  */
68  size_t phi2idx(double phi) const
69  {
70  int idx = mrpt::utils::round( (phi-m_phiMin) / m_resolutionPhi );
71  ASSERT_(idx>=0 && idx< static_cast<int>(m_sizePhi) );
72  return idx;
73  }
74 
75  /** Returns coordinates from "indexes":
76  */
77  double idx2x(size_t x) const
78  {
79  ASSERT_(x<m_sizeX);
80  return m_xMin + x * m_resolutionXY;
81  }
82 
83  /** Returns coordinates from "indexes":
84  */
85  double idx2y(size_t y) const
86  {
87  ASSERT_(y<m_sizeY);
88  return m_yMin + y * m_resolutionXY;
89  }
90 
91  /** Returns coordinates from "indexes":
92  */
93  double idx2phi(size_t phi) const
94  {
95  ASSERT_(phi<m_sizePhi);
96  return m_phiMin + phi * m_resolutionPhi;
97  }
98 
99  /** Default constructor:
100  */
102  double xMin = -1.0f,
103  double xMax = 1.0f,
104  double yMin = -1.0f,
105  double yMax = 1.0f,
106  double resolutionXY = 0.5f,
107  double resolutionPhi = DEG2RAD(180),
108  double phiMin = -M_PIf,
109  double phiMax = M_PIf
110  ) :
111  m_xMin(), m_xMax(),
112  m_yMin(), m_yMax(),
113  m_phiMin(), m_phiMax(),
114  m_resolutionXY(),m_resolutionPhi(),
115  m_sizeX(),m_sizeY(),m_sizePhi(), m_sizeXY(),
116  m_idxLeftX(), m_idxLeftY(), m_idxLeftPhi(),
117  m_data()
118  {
119  setSize(xMin,xMax,yMin,yMax,resolutionXY,resolutionPhi,phiMin,phiMax);
120  }
121 
122  virtual ~CPose2DGridTemplate() { }
123 
124 
125  /** Changes the limits and size of the grid, erasing previous contents:
126  */
127  void setSize(
128  double xMin,
129  double xMax,
130  double yMin,
131  double yMax,
132  double resolutionXY,
133  double resolutionPhi,
134  double phiMin = -M_PIf,
135  double phiMax = M_PIf
136  )
137  {
138  // Checks
139  ASSERT_( xMax > xMin );
140  ASSERT_( yMax > yMin );
141  ASSERT_( phiMax >= phiMin );
142  ASSERT_( resolutionXY>0 );
143  ASSERT_( resolutionPhi>0 );
144 
145  // Copy data:
146  m_xMin = xMin; m_xMax = xMax;
147  m_yMin = yMin; m_yMax = yMax;
148  m_phiMin = phiMin; m_phiMax = phiMax;
149  m_resolutionXY = resolutionXY;
150  m_resolutionPhi = resolutionPhi;
151 
152  // Compute the indexes of the starting borders:
153  m_idxLeftX = mrpt::utils::round( xMin/resolutionXY ) ;
154  m_idxLeftY = mrpt::utils::round( yMin/resolutionXY ) ;
155  m_idxLeftPhi = mrpt::utils::round( phiMin/resolutionPhi ) ;
156 
157  // Compute new required space:
158  m_sizeX = mrpt::utils::round( xMax/resolutionXY ) - m_idxLeftX + 1;
159  m_sizeY = mrpt::utils::round( yMax/resolutionXY ) - m_idxLeftY + 1;
160  m_sizePhi = mrpt::utils::round( phiMax/resolutionPhi ) - m_idxLeftPhi + 1;
161  m_sizeXY = m_sizeX * m_sizeY;
162 
163  // Resize "m_data":
164  m_data.clear();
165  m_data.resize( m_sizeX * m_sizeY * m_sizePhi );
166  }
167 
168  /** Reads the contents of a cell
169  */
170  const T* getByPos( double x,double y, double phi ) const
171  {
172  return getByIndex( x2idx(x),y2idx(y),phi2idx(phi) );
173  }
174 
175  /** Reads the contents of a cell
176  */
177  T* getByPos( double x,double y, double phi )
178  {
179  return getByIndex( x2idx(x),y2idx(y),phi2idx(phi) );
180  }
181 
182  /** Reads the contents of a cell
183  */
184  const T* getByIndex( size_t x,size_t y, size_t phi ) const
185  {
186  ASSERT_(x<m_sizeX && y<m_sizeY && phi<m_sizePhi)
187  return &m_data[ phi*m_sizeXY + y*m_sizeX + x ];
188  }
189 
190  /** Reads the contents of a cell
191  */
192  T* getByIndex( size_t x,size_t y, size_t phi )
193  {
194  ASSERT_(x<m_sizeX && y<m_sizeY && phi<m_sizePhi)
195  return &m_data[ phi*m_sizeXY + y*m_sizeX + x ];
196  }
197 
198  /** Returns the whole grid as a matrix, for a given constant "phi" and where 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 
227 
228  }; // End of class def.
229 
230  } // End of namespace
231 } // End of namespace
232 
233 #endif
double DEG2RAD(const double x)
Degrees to radians.
int round(const T value)
Returns the closer integer (int) to x.
Definition: round.h:22
double getXMin() const
Get info about the grid:
size_t m_sizeX
The size of "m_data" is m_sizeX ·m_sizeY ·m_sizePhi.
CPose2DGridTemplate(double xMin=-1.0f, double xMax=1.0f, double yMin=-1.0f, double yMax=1.0f, double resolutionXY=0.5f, double resolutionPhi=DEG2RAD(180), double phiMin=-M_PIf, double phiMax=M_PIf)
Default constructor:
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:
#define DEG2RAD(x)
Definition: Utils.h:61
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
const T * getByIndex(size_t x, size_t y, size_t phi) const
Reads the contents of a cell.
size_t phi2idx(double phi) const
Returns "indexes" from coordinates:
double idx2x(size_t x) const
Returns coordinates from "indexes":
const T * getByPos(double x, double y, double phi) const
Reads the contents of a cell.
size_t y2idx(double y) const
Returns "indexes" from coordinates:
T * getByPos(double x, double y, double phi)
Reads the contents of a cell.
#define MRPT_START
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
size_t x2idx(double x) 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)
double idx2y(size_t y) const
Returns coordinates from "indexes":
double idx2phi(size_t phi) const
Returns coordinates from "indexes":
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.8 for MRPT 1.2.2 SVN:Unversioned directory at Tue Oct 14 02:14:08 UTC 2014