Main MRPT website > C++ reference for MRPT 1.9.9
CPosePDFGrid.cpp
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 
10 #include "base-precomp.h" // Precompiled headers
11 
13 #include <mrpt/poses/CPose3D.h>
15 #include <mrpt/utils/CStream.h>
16 #include <mrpt/random.h>
17 #include <mrpt/system/os.h>
19 
20 using namespace std;
21 using namespace mrpt;
22 using namespace mrpt::utils;
23 using namespace mrpt::math;
24 using namespace mrpt::poses;
25 using namespace mrpt::system;
26 
28 
29 /*---------------------------------------------------------------
30  Constructor
31  ---------------------------------------------------------------*/
33  double xMin, double xMax, double yMin, double yMax, double resolutionXY,
34  double resolutionPhi, double phiMin, double phiMax)
35  : CPose2DGridTemplate<double>(
36  xMin, xMax, yMin, yMax, resolutionXY, resolutionPhi, phiMin, phiMax)
37 {
38  uniformDistribution();
39 }
40 
41 void CPosePDFGrid::copyFrom(const CPosePDF& o)
42 {
43  if (this == &o) return; // It may be used sometimes
44 
45  THROW_EXCEPTION("Not implemented yet!");
46 }
47 
48 /*---------------------------------------------------------------
49  Destructor
50  ---------------------------------------------------------------*/
51 CPosePDFGrid::~CPosePDFGrid() {}
52 /*---------------------------------------------------------------
53  getMean
54  Returns an estimate of the pose, (the mean, or mathematical expectation of the
55  PDF), computed
56  as a weighted average over all particles.
57  ---------------------------------------------------------------*/
58 void CPosePDFGrid::getMean(CPose2D& p) const
59 {
60  // Calc average on SE(2)
61  mrpt::poses::SE_average<2> se_averager;
62  for (size_t phiInd = 0; phiInd < m_sizePhi; phiInd++)
63  for (size_t y = 0; y < m_sizeY; y++)
64  for (size_t x = 0; x < m_sizeX; x++)
65  {
66  const double w = *getByIndex(x, y, phiInd);
67  se_averager.append(
68  CPose2D(idx2x(x), idx2y(y), idx2phi(phiInd)), w);
69  }
70  se_averager.get_average(p);
71 }
72 
73 /*---------------------------------------------------------------
74  getCovarianceAndMean
75  ---------------------------------------------------------------*/
76 void CPosePDFGrid::getCovarianceAndMean(CMatrixDouble33& cov, CPose2D& p) const
77 {
78  CPosePDFParticles auxParts;
79  auxParts.resetDeterministic(
80  CPose2D(0, 0, 0), m_sizePhi * m_sizeY * m_sizeX);
81  size_t idx = 0;
82  for (size_t phiInd = 0; phiInd < m_sizePhi; phiInd++)
83  {
84  for (size_t y = 0; y < m_sizeY; y++)
85  for (size_t x = 0; x < m_sizeX; x++)
86  {
87  auxParts.m_particles[idx].log_w =
88  log(*getByIndex(x, y, phiInd));
89  *auxParts.m_particles[idx].d =
90  CPose2D(idx2x(x), idx2y(y), idx2phi(phiInd));
91  }
92  }
93  auxParts.getCovarianceAndMean(cov, p);
94 }
95 
96 /*---------------------------------------------------------------
97  writeToStream
98  ---------------------------------------------------------------*/
99 void CPosePDFGrid::writeToStream(mrpt::utils::CStream& out, int* version) const
100 {
101  if (version)
102  *version = 0;
103  else
104  {
105  // The size:
106  out << m_xMin << m_xMax << m_yMin << m_yMax << m_phiMin << m_phiMax
107  << m_resolutionXY << m_resolutionPhi
108  << static_cast<int32_t>(m_sizeX) << static_cast<int32_t>(m_sizeY)
109  << static_cast<int32_t>(m_sizePhi) << static_cast<int32_t>(m_sizeXY)
110  << static_cast<int32_t>(m_idxLeftX)
111  << static_cast<int32_t>(m_idxLeftY)
112  << static_cast<int32_t>(m_idxLeftPhi);
113 
114  // The data:
115  out << m_data;
116  }
117 }
118 
119 /*---------------------------------------------------------------
120  readFromStream
121  ---------------------------------------------------------------*/
122 void CPosePDFGrid::readFromStream(mrpt::utils::CStream& in, int version)
123 {
124  switch (version)
125  {
126  case 0:
127  {
128  // The size:
129  in >> m_xMin >> m_xMax >> m_yMin >> m_yMax >> m_phiMin >>
130  m_phiMax >> m_resolutionXY >> m_resolutionPhi;
131 
132  int32_t sizeX, sizeY, sizePhi, sizeXY, idxLeftX, idxLeftY,
133  idxLeftPhi;
134 
135  in >> sizeX >> sizeY >> sizePhi >> sizeXY >> idxLeftX >> idxLeftY >>
136  idxLeftPhi;
137 
138  m_sizeX = sizeX;
139  m_sizeY = sizeY;
140  m_sizePhi = sizePhi;
141  m_sizeXY = sizeXY;
142  m_idxLeftX = idxLeftX;
143  m_idxLeftY = idxLeftY;
144  m_idxLeftPhi = idxLeftPhi;
145 
146  // The data:
147  in >> m_data;
148  }
149  break;
150  default:
152  };
153 }
154 
155 /*---------------------------------------------------------------
156  saveToTextFile
157  ---------------------------------------------------------------*/
158 void CPosePDFGrid::saveToTextFile(const std::string& dataFile) const
159 {
160  char dimsFile[1000];
161  os::sprintf(dimsFile, 1000, "%s_dims.txt", dataFile.c_str());
162 
163  FILE* f_d = os::fopen(dataFile.c_str(), "wt");
164  if (!f_d) return;
165 
166  FILE* f_s = os::fopen(dimsFile, "wt");
167  if (!f_s)
168  {
169  os::fclose(f_d);
170  return;
171  }
172 
173  // Save dims:
174  os::fprintf(
175  f_s, "%u %u %u %f %f %f %f %f %f\n", (unsigned)m_sizeX,
176  (unsigned)m_sizeY, (unsigned)m_sizePhi, m_xMin, m_xMax, m_yMin, m_yMax,
177  m_phiMin, m_phiMax);
178 
179  // Save one rectangular matrix each time:
180  for (unsigned int phiInd = 0; phiInd < m_sizePhi; phiInd++)
181  {
182  for (unsigned int y = 0; y < m_sizeY; y++)
183  {
184  for (unsigned int x = 0; x < m_sizeX; x++)
185  os::fprintf(f_d, "%.5e ", *getByIndex(x, y, phiInd));
186  os::fprintf(f_d, "\n");
187  }
188  }
189 
190  os::fclose(f_s);
191  os::fclose(f_d);
192  return; // Done!
193 }
194 
195 /*---------------------------------------------------------------
196  changeCoordinatesReference
197  ---------------------------------------------------------------*/
198 void CPosePDFGrid::changeCoordinatesReference(const CPose3D& newReferenceBase)
199 {
200  MRPT_UNUSED_PARAM(newReferenceBase);
201  THROW_EXCEPTION("Not implemented yet!");
202 }
203 
204 /*---------------------------------------------------------------
205  bayesianFusion
206  ---------------------------------------------------------------*/
207 void CPosePDFGrid::bayesianFusion(
208  const CPosePDF& p1, const CPosePDF& p2,
209  const double& minMahalanobisDistToDrop)
210 {
211  MRPT_UNUSED_PARAM(p1);
212  MRPT_UNUSED_PARAM(p2);
213  MRPT_UNUSED_PARAM(minMahalanobisDistToDrop);
214  THROW_EXCEPTION("Not implemented yet!");
215 }
216 
217 /*---------------------------------------------------------------
218  inverse
219  ---------------------------------------------------------------*/
221 {
223  THROW_EXCEPTION("Not implemented yet!");
224 }
225 
226 /*---------------------------------------------------------------
227  drawSingleSample
228  ---------------------------------------------------------------*/
229 void CPosePDFGrid::drawSingleSample(CPose2D& outPart) const
230 {
231  MRPT_UNUSED_PARAM(outPart);
232  THROW_EXCEPTION("Not implemented yet!");
233 }
234 
235 /*---------------------------------------------------------------
236  drawSingleSample
237  ---------------------------------------------------------------*/
238 void CPosePDFGrid::drawManySamples(
239  size_t N, std::vector<CVectorDouble>& outSamples) const
240 {
242  MRPT_UNUSED_PARAM(outSamples);
243 
244  THROW_EXCEPTION("Not implemented yet!");
245 }
246 
247 /*---------------------------------------------------------------
248  CPosePDFGrid
249  ---------------------------------------------------------------*/
251 {
252  double SUM = 0;
253 
254  // SUM:
255  for (vector<double>::const_iterator it = m_data.begin(); it != m_data.end();
256  ++it)
257  SUM += *it;
258 
259  if (SUM > 0)
260  {
261  // Normalize:
262  for (vector<double>::iterator it = m_data.begin(); it != m_data.end();
263  ++it)
264  *it /= SUM;
265  }
266 }
267 
268 /*---------------------------------------------------------------
269  uniformDistribution
270  ---------------------------------------------------------------*/
271 void CPosePDFGrid::uniformDistribution()
272 {
273  double val = 1.0f / m_data.size();
274 
275  for (vector<double>::iterator it = m_data.begin(); it != m_data.end(); ++it)
276  *it = val;
277 }
Computes weighted and un-weighted averages of SE(2) poses.
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:30
int void fclose(FILE *f)
An OS-independent version of fclose.
Definition: os.cpp:272
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
#define THROW_EXCEPTION(msg)
Scalar * iterator
Definition: eigen_plugins.h:26
void getCovarianceAndMean(mrpt::math::CMatrixDouble33 &cov, CPose2D &mean_point) const override
Returns an estimate of the pose covariance matrix (3x3 cov matrix) and the mean, both at once...
void saveToTextFile(const std::string &file, mrpt::math::TMatrixTextFileFormat fileFormat=mrpt::math::MATRIX_FORMAT_ENG, bool appendMRPTHeader=false, const std::string &userHeader=std::string()) const
Save matrix to a text file, compatible with MATLAB text format (see also the methods of matrix classe...
STL namespace.
const Scalar * const_iterator
Definition: eigen_plugins.h:27
void append(const mrpt::poses::CPose2D &p)
Adds a new pose to the computation.
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:4178
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
CParticleList m_particles
The array of particles.
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Eigen::Matrix< dataType, 4, 4 > inverse(Eigen::Matrix< dataType, 4, 4 > &pose)
Definition: Miscellaneous.h:82
int val
Definition: mrpt_jpeglib.h:955
Declares a class that represents a Probability Density Function (PDF) over a 2D pose (x...
GLsizei const GLchar ** string
Definition: glext.h:4101
Declares a class that represents a probability density function (pdf) of a 2D pose (x...
Definition: CPosePDF.h:41
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CPoint.h:17
int fprintf(FILE *fil, const char *format,...) noexcept MRPT_printf_format_check(2
An OS-independent version of fprintf.
Definition: os.cpp:405
void normalize(Scalar valMin, Scalar valMax)
Scales all elements such as the minimum & maximum values are shifted to the given values...
__int32 int32_t
Definition: rptypes.h:46
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
A class used to store a 2D pose, including the 2D coordinate point and a heading (phi) angle...
Definition: CPose2D.h:40
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
void resetDeterministic(const CPose2D &location, size_t particlesCount=0)
Reset the PDF to a single point: All m_particles will be set exactly to the supplied pose...
GLuint in
Definition: glext.h:7274
GLenum GLint GLint y
Definition: glext.h:3538
FILE * fopen(const char *fileName, const char *mode) noexcept
An OS-independent version of fopen.
Definition: os.cpp:254
Eigen::Matrix< typename MATRIX::Scalar, MATRIX::ColsAtCompileTime, MATRIX::ColsAtCompileTime > cov(const MATRIX &v)
Computes the covariance matrix from a list of samples in an NxM matrix, where each row is a sample...
Definition: ops_matrices.h:148
GLenum GLint x
Definition: glext.h:3538
Declares a class that represents a Probability Distribution function (PDF) of a 2D pose (x...
Definition: CPosePDFGrid.h:27
GLfloat GLfloat p
Definition: glext.h:6305
This is a template class for storing a 3D (2D+heading) grid containing any kind of data...
int sprintf(char *buf, size_t bufSize, const char *format,...) noexcept MRPT_printf_format_check(3
An OS-independent version of sprintf (Notice the bufSize param, which may be ignored in some compiler...
Definition: os.cpp:188
void get_average(mrpt::poses::CPose2D &out_mean) const
Returns the average pose.



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