Skip to content

Commit 6919738

Browse files
committed
Refactor of all particle-filter data structs
- mrpt::bayes::CProbabilityParticle (which affects all PF-based classes in MRPT) has been greatly simplified via usage of the new mrpt::utils::copy_ptr<> pointee-copy-semantics smart pointer. - getCurrentMetricMapEstimation() renamed mrpt::slam::CMultiMetricMapPDF::getAveragedMetricMapEstimation() to avoid confusions
1 parent b1e98e7 commit 6919738

31 files changed

Lines changed: 150 additions & 381 deletions

apps/rbpf-slam/rbpf-slam.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ void MapBuilding_RBPF()
225225
mapBuilder.initialize(dummySimpleMap,&startPose);
226226

227227
for (CMultiMetricMapPDF::CParticleList::iterator it=mapBuilder.mapPDF.m_particles.begin();it!=mapBuilder.mapPDF.m_particles.end();++it) {
228-
CRBPFParticleData* part_d = it->d;
228+
CRBPFParticleData* part_d = it->d.get();
229229
CMultiMetricMap &mmap = part_d->mapTillNow;
230230
mrpt::maps::COccupancyGridMap2DPtr it_grid = mmap.getMapByClass<mrpt::maps::COccupancyGridMap2D>();
231231
ASSERTMSG_(it_grid.present(), "No gridmap in multimetric map definition, but metric map continuation was set (!)" );
@@ -370,17 +370,17 @@ void MapBuilding_RBPF()
370370

371371
if (0==(step % LOG_FREQUENCY))
372372
{
373-
CMultiMetricMap *mostLikMap = mapBuilder.mapPDF.getCurrentMostLikelyMetricMap();
373+
const CMultiMetricMap *mostLikMap = mapBuilder.mapPDF.getCurrentMostLikelyMetricMap();
374374

375375
if (GENERATE_LOG_INFO)
376376
{
377-
378377
printf("Saving info log information...");
379378

380379
tictac_JH.Tic();
381380

382-
ASSERT_( mapBuilder.getCurrentlyBuiltMetricMap()->m_gridMaps.size()>0 );
383-
COccupancyGridMap2DPtr grid = mapBuilder.getCurrentlyBuiltMetricMap()->m_gridMaps[0];
381+
const CMultiMetricMap * avrMap = mapBuilder.mapPDF.getAveragedMetricMapEstimation();
382+
ASSERT_(avrMap->m_gridMaps.size()>0 );
383+
COccupancyGridMap2DPtr grid = avrMap->m_gridMaps[0];
384384
grid->computeEntropy( entropy );
385385

386386
grid->saveAsBitmapFile(format("%s/EMMI_gridmap_%03u.bmp",OUT_DIR,step));
@@ -600,7 +600,7 @@ void MapBuilding_RBPF()
600600
filOut << finalMap;
601601

602602
// Save gridmap extend (if exists):
603-
CMultiMetricMap *mostLikMap = mapBuilder.mapPDF.getCurrentMostLikelyMetricMap();
603+
const CMultiMetricMap *mostLikMap = mapBuilder.mapPDF.getCurrentMostLikelyMetricMap();
604604
if (mostLikMap->m_gridMaps.size()>0)
605605
{
606606
CMatrix auxMat(1,4);

apps/ro-localization/CPosePDFParticlesExtended.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ void CPosePDFParticlesExtended::prediction_and_update_pfAuxiliaryPFOptimal(
814814
for (i=0;i<M;i++)
815815
{
816816
oldParticles[i].log_w = m_particles[i].log_w;
817-
oldParticles[i].d = new TExtendedCPose2D(*m_particles[i].d);
817+
oldParticles[i].d.reset( new TExtendedCPose2D(*m_particles[i].d));
818818
}
819819
820820
@@ -952,8 +952,8 @@ void CPosePDFParticlesExtended::resetUniform(
952952
{
953953
clear();
954954
m_particles.resize(particlesCount);
955-
for (int i=0;i<particlesCount;i++)
956-
m_particles[i].d = new TExtendedCPose2D();
955+
for (int i = 0; i < particlesCount; i++)
956+
m_particles[i].d.reset(new TExtendedCPose2D());
957957
}
958958

959959
size_t i,M = m_particles.size();

doc/doxygen-pages/changeLog_doc.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@
5252
- mrpt::poses::CRobot2DPoseEstimator now uses a more generic odometry-based velocity model (vx,vy,omega).
5353
- New template mrpt::utils::ts_hash_map<> for thread-safe, std::map-like containers based on hash functions.
5454
- Included exprtk header-only library to runtime compile & evaluation of mathematical expressions, under `<mrpt/otherlibs/exprtk.hpp>`
55-
- New smart pointer templates: `mrpt::utils::copy_ptr<>`, `mrpt::utils::clone_ptr<>`.
55+
- New smart pointer templates: `mrpt::utils::copy_ptr<>`, `mrpt::utils::poly_ptr<>`.
5656
- \ref mrpt_bayes_grp
57-
- [API change] `verbose` is no longer a field of mrpt::bayes::CParticleFilter::TParticleFilterOptions. Use the setVerbosityLevel() method of the CParticleFilter class itself.
57+
- [API change] `verbose` is no longer a field of mrpt::bayes::CParticleFilter::TParticleFilterOptions. Use the setVerbosityLevel() method of the CParticleFilter class itself.
58+
- [API change] mrpt::bayes::CProbabilityParticle (which affects all PF-based classes in MRPT) has been greatly simplified via usage of the new mrpt::utils::copy_ptr<> pointee-copy-semantics smart pointer.
5859
- \ref mrpt_gui_grp
5960
- mrpt::gui::CMyGLCanvasBase is now derived from mrpt::opengl::CTextMessageCapable so they can draw text labels
6061
- New class mrpt::gui::CDisplayWindow3DLocker for exception-safe 3D scene lock in 3D windows.
@@ -83,6 +84,7 @@
8384
- [ABI change] mrpt::opengl::CAxis now has many new options exposed to configure its look.
8485
- \ref mrpt_slam_grp
8586
- [API change] mrpt::slam::CMetricMapBuilder::TOptions does not have a `verbose` field anymore. It's supersedded now by the verbosity level of the CMetricMapBuilder class itself.
87+
- [API change] getCurrentMetricMapEstimation() renamed mrpt::slam::CMultiMetricMapPDF::getAveragedMetricMapEstimation() to avoid confusions.
8688
- \ref mrpt_hwdrivers_grp
8789
- mrpt::hwdrivers::CGenericSensor: external image format is now `png` by default instead of `jpg` to avoid losses.
8890
- [ABI change] mrpt::hwdrivers::COpenNI2Generic:

libs/base/include/mrpt/bayes/CParticleFilterData.h

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,7 @@ namespace bayes
118118
for (j=lastIndxOld;j<sorted_idx;j++)
119119
{
120120
if (!oldParticlesReused[j]) /* If reused we can not delete that memory! */
121-
{
122-
delete derived().m_particles[j].d;
123-
derived().m_particles[j].d = NULL;
124-
}
121+
derived().m_particles[j].d.reset();
125122
}
126123

127124
/* For the next iteration:*/
@@ -139,23 +136,19 @@ namespace bayes
139136
{
140137
/* Make a copy of the particle's data: */
141138
ASSERT_( derived().m_particles[ sorted_idx ].d != NULL);
142-
parts[i].d = new typename Derived::CParticleDataContent( *derived().m_particles[ sorted_idx ].d );
139+
parts[i].d.reset(new typename Derived::CParticleDataContent(*derived().m_particles[sorted_idx].d));
143140
}
144141
}
145142
/* Free memory of unused particles */
146-
for (itSrc=derived().m_particles.begin(),oldPartIt=oldParticlesReused.begin();itSrc!=derived().m_particles.end();itSrc++,oldPartIt++)
147-
if (! *oldPartIt )
148-
{
149-
delete itSrc->d;
150-
itSrc->d = NULL;
151-
}
143+
for (itSrc = derived().m_particles.begin(), oldPartIt = oldParticlesReused.begin(); itSrc != derived().m_particles.end(); itSrc++, oldPartIt++)
144+
if (!*oldPartIt)
145+
itSrc->d.reset();
152146
/* Copy the pointers only to the final destination */
153147
derived().m_particles.resize( parts.size() );
154148
for (itSrc=parts.begin(),itDest=derived().m_particles.begin(); itSrc!=parts.end(); itSrc++, itDest++ )
155149
{
156150
itDest->log_w = itSrc->log_w;
157-
itDest->d = itSrc->d;
158-
itSrc->d = NULL;
151+
itDest->d.move_from(itSrc->d);
159152
}
160153
parts.clear();
161154
MRPT_END
@@ -184,24 +177,12 @@ namespace bayes
184177
CParticleList m_particles; //!< The array of particles
185178

186179
/** Default constructor */
187-
CParticleFilterData() : m_particles(0)
188-
{ }
180+
CParticleFilterData() : m_particles(0) {}
189181

190-
/** Free the memory of all the particles and reset the array "m_particles" to length zero.
191-
*/
182+
/** Free the memory of all the particles and reset the array "m_particles" to length zero */
192183
void clearParticles()
193184
{
194-
MRPT_START
195-
for (typename CParticleList::iterator it=m_particles.begin();it!=m_particles.end();++it)
196-
if (it->d) delete it->d;
197185
m_particles.clear();
198-
MRPT_END
199-
}
200-
201-
/** Virtual destructor */
202-
virtual ~CParticleFilterData()
203-
{
204-
clearParticles();
205186
}
206187

207188
/** Dumps the sequence of particles and their weights to a stream (requires T implementing CSerializable).
@@ -234,7 +215,7 @@ namespace bayes
234215
for (it=m_particles.begin();it!=m_particles.end();++it)
235216
{
236217
in >> it->log_w;
237-
it->d = new T();
218+
it->d.reset(new T());
238219
in >> *it->d;
239220
}
240221
MRPT_END

libs/base/include/mrpt/bayes/CProbabilityParticle.h

Lines changed: 6 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#ifndef CPROBABILITYPARTICLE_H
1010
#define CPROBABILITYPARTICLE_H
1111

12+
#include <mrpt/utils/copy_ptr.h>
13+
1214
namespace mrpt
1315
{
1416
namespace bayes
@@ -25,54 +27,11 @@ namespace bayes
2527
struct CProbabilityParticle
2628
{
2729
public:
28-
/** The data associated with this particle.
29-
*/
30-
T *d;
31-
32-
/** The (logarithmic) weight value for this particle.
33-
*/
34-
double log_w;
35-
36-
/** Default constructor:
37-
*/
38-
CProbabilityParticle() : d(NULL), log_w(0)
39-
{
40-
}
41-
42-
/** Copy constructor:
43-
*/
44-
CProbabilityParticle(const CProbabilityParticle &o) : d(NULL), log_w(o.log_w)
45-
{
46-
if (o.d)
47-
{
48-
// Copy
49-
d = new T(*o.d);
50-
}
51-
}
30+
mrpt::utils::copy_ptr<T> d; //!< The data associated with this particle. The use of copy_ptr<> allows relying on compiler-generated copy ctor, etc.
31+
double log_w; //!< The (logarithmic) weight value for this particle.
5232

53-
/** Copy operator
54-
*/
55-
CProbabilityParticle<T> & operator =(const CProbabilityParticle &o)
56-
{
57-
if (this == &o) return *this;
58-
log_w = o.log_w;
59-
if (o.d)
60-
{
61-
// Copy semantic:
62-
if (d)
63-
*d = *o.d; // Copy using the object "operator =".
64-
else d = new T(*o.d); // Create a new object from the copy constructor
65-
}
66-
else
67-
{
68-
if (d)
69-
{
70-
delete d;
71-
d = NULL;
72-
}
73-
}
74-
return *this;
75-
}
33+
/** Default constructor */
34+
CProbabilityParticle() : d(), log_w(.0) {}
7635
};
7736

7837
} // end namespace

libs/base/include/mrpt/poses/CPointPDFParticles.h

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,16 @@ namespace poses
6060
DEFINE_SERIALIZABLE( CPointPDFParticles )
6161

6262
public:
63-
/** Default constructor
64-
*/
63+
/** Default constructor */
6564
CPointPDFParticles(size_t numParticles = 1);
6665

67-
/** Destructor
68-
*/
69-
virtual ~CPointPDFParticles();
70-
71-
/** Clear all the particles (free memory)
72-
*/
73-
void clear() { setSize(0); }
66+
/** Clear all the particles (free memory) */
67+
void clear();
7468

75-
/** Erase all the previous particles and change the number of particles, with a given initial value
76-
*/
69+
/** Erase all the previous particles and change the number of particles, with a given initial value */
7770
void setSize(size_t numberParticles, const CPoint3D &defaultValue = CPoint3D(0,0,0) );
7871

79-
/** Returns the number of particles
80-
*/
72+
/** Returns the number of particles */
8173
size_t size() const
8274
{
8375
return m_particles.size();

libs/base/include/mrpt/poses/CPose3DPDFParticles.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,6 @@ namespace mrpt
4444
*/
4545
CPose3DPDFParticles( size_t M = 1 );
4646

47-
/** Copy constructor */
48-
inline CPose3DPDFParticles( const CPose3DPDFParticles& obj ) :
49-
CPose3DPDF(),
50-
CParticleFilterData<CPose3D>()
51-
{
52-
copyFrom( obj );
53-
}
54-
virtual ~CPose3DPDFParticles();//!< Destructor
55-
5647
void copyFrom(const CPose3DPDF &o) MRPT_OVERRIDE; //!< Copy operator, translating if necesary (for example, between m_particles and gaussian representations)
5748

5849
/** Reset the PDF to a single point: All m_particles will be set exactly to the supplied pose.

libs/base/include/mrpt/poses/CPosePDFParticles.h

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,13 @@ namespace mrpt
3939
DEFINE_SERIALIZABLE( CPosePDFParticles )
4040

4141
public:
42-
/** Free all the memory associated to m_particles, and set the number of parts = 0
43-
*/
44-
void clear();
42+
void clear(); //!< Free all the memory associated to m_particles, and set the number of parts = 0
4543

4644
/** Constructor
4745
* \param M The number of m_particles.
4846
*/
4947
CPosePDFParticles( size_t M = 1 );
5048

51-
/** Copy constructor:
52-
*/
53-
inline CPosePDFParticles( const CPosePDFParticles& obj )
54-
{
55-
copyFrom( obj );
56-
}
57-
58-
/** Destructor
59-
*/
60-
virtual ~CPosePDFParticles();
61-
6249
/** Copy operator, translating if necesary (for example, between m_particles and gaussian representations)
6350
*/
6451
void copyFrom(const CPosePDF &o) MRPT_OVERRIDE;

libs/base/src/poses/CPointPDFParticles.cpp

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,14 @@ using namespace mrpt::system;
2424
IMPLEMENTS_SERIALIZABLE( CPointPDFParticles, CPointPDF, mrpt::poses )
2525
IMPLEMENTS_SERIALIZABLE( TSimple3DPoint, CSerializable, mrpt::poses )
2626

27-
28-
/*---------------------------------------------------------------
29-
Constructor
30-
---------------------------------------------------------------*/
3127
CPointPDFParticles::CPointPDFParticles(size_t numParticles)
3228
{
3329
setSize(numParticles);
3430
}
3531

36-
/*---------------------------------------------------------------
37-
Destructor
38-
---------------------------------------------------------------*/
39-
CPointPDFParticles::~CPointPDFParticles()
40-
{
41-
clear();
32+
/** Clear all the particles (free memory) */
33+
void CPointPDFParticles::clear() {
34+
setSize(0);
4235
}
4336

4437
/*---------------------------------------------------------------
@@ -48,16 +41,12 @@ void CPointPDFParticles::setSize(
4841
size_t numberParticles,
4942
const CPoint3D &defaultValue)
5043
{
51-
// Free old particles:
52-
CParticleList::iterator it;
53-
for (it=m_particles.begin();it!=m_particles.end();++it)
54-
delete it->d;
55-
44+
// Free old particles: automatic via smart ptr
5645
m_particles.resize(numberParticles);
57-
for (it=m_particles.begin();it!=m_particles.end();++it)
46+
for (auto &it : m_particles)
5847
{
59-
it->log_w = 0;
60-
it->d = new TSimple3DPoint(defaultValue);
48+
it.log_w = 0;
49+
it.d.reset(new TSimple3DPoint(defaultValue));
6150
}
6251
}
6352

0 commit comments

Comments
 (0)