9 #ifndef CParticleFilterData_H 10 #define CParticleFilterData_H 31 template <
class Derived,
class particle_list_t>
37 return *
dynamic_cast<const Derived*
>(
this);
40 inline Derived&
derived() {
return *
dynamic_cast<Derived*
>(
this); }
41 double getW(
size_t i)
const override 43 if (i >=
derived().m_particles.size())
45 return derived().m_particles[i].log_w;
48 void setW(
size_t i,
double w)
override 50 if (i >=
derived().m_particles.size())
57 return derived().m_particles.size();
63 if (
derived().m_particles.empty())
return 0;
64 double minW =
derived().m_particles[0].log_w;
70 it !=
derived().m_particles.end(); ++it)
72 maxW = std::max<double>(maxW, it->log_w);
73 minW = std::min<double>(minW, it->log_w);
78 it !=
derived().m_particles.end(); ++it)
80 if (out_max_log_w) *out_max_log_w = maxW;
83 return exp(maxW - minW);
87 double ESS()
const override 93 double sumLinearWeights = 0;
96 it !=
derived().m_particles.end(); ++it)
97 sumLinearWeights += exp(it->log_w);
101 it !=
derived().m_particles.end(); ++it)
107 return 1.0 / (
derived().m_particles.size() * cum);
117 particle_list_t parts;
119 const size_t M_old =
derived().m_particles.size();
120 size_t i, j, lastIndxOld = 0;
121 std::vector<bool> oldParticlesReused(M_old,
false);
123 std::vector<size_t> sorted_indx(indx);
126 std::sort(sorted_indx.begin(), sorted_indx.end());
128 parts.resize(sorted_indx.size());
129 for (i = 0, itDest = parts.begin(); itDest != parts.end();
132 const size_t sorted_idx = sorted_indx[i];
133 itDest->log_w =
derived().m_particles[sorted_idx].log_w;
136 for (j = lastIndxOld; j < sorted_idx; j++)
138 if (!oldParticlesReused
140 derived().m_particles[j].d.reset();
144 lastIndxOld = sorted_idx;
149 if (!oldParticlesReused[sorted_idx])
152 parts[i].d.reset(
derived().m_particles[sorted_idx].d.get());
153 oldParticlesReused[sorted_idx] =
true;
159 parts[i].d.reset(
new typename Derived::CParticleDataContent(
160 *
derived().m_particles[sorted_idx].d));
164 for (itSrc =
derived().m_particles.begin(),
165 oldPartIt = oldParticlesReused.begin();
166 itSrc !=
derived().m_particles.end(); itSrc++, oldPartIt++)
167 if (!*oldPartIt) itSrc->d.reset();
169 derived().m_particles.resize(parts.size());
170 for (itSrc = parts.begin(), itDest =
derived().m_particles.begin();
171 itSrc != parts.end(); itSrc++, itDest++)
173 itDest->log_w = itSrc->log_w;
174 itDest->d.move_from(itSrc->d);
219 template <
class STREAM>
227 out << it->log_w << (*it->d);
235 template <
class STREAM>
247 it->d.reset(
new T());
262 for (it = out_logWeights.begin(), it2 =
m_particles.begin();
279 if (ret ==
nullptr || it->log_w > ret->log_w) ret = &(*it);
Derived & derived()
CRTP helper method.
double normalizeWeights(double *out_max_log_w=nullptr) override
Normalize the (logarithmic) weights, such as the maximum weight is zero.
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
double ESS() const override
Returns the normalized ESS (Estimated Sample Size), in the range [0,1].
const CParticleData * getMostLikelyParticle() const
Returns the particle with the highest weight.
const Scalar * const_iterator
GLubyte GLubyte GLubyte GLubyte w
size_t particlesCount() const override
Get the m_particles count.
void getWeights(std::vector< double > &out_logWeights) const
Returns a vector with the sequence of the logaritmic weights of all the samples.
T square(const T x)
Inline function for the square of a number.
T CParticleDataContent
This is the type inside the corresponding CParticleData class.
CParticleList m_particles
The array of particles.
CProbabilityParticle< T > CParticleData
Use this to refer to each element in the m_particles array.
This virtual class defines the interface that any particles based PDF class must implement in order t...
This template class declares the array of particles and its internal data, managing some memory-relat...
void performSubstitution(const std::vector< size_t > &indx) override
Replaces the old particles by copies determined by the indexes in "indx", performing an efficient cop...
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
double getW(size_t i) const override
Access to i'th particle (logarithm) weight, where first one is index 0.
std::deque< CParticleData > CParticleList
Use this type to refer to the list of particles m_particles.
A template class for holding a the data and the weight of a particle.
void readParticlesFromStream(STREAM &in)
Reads the sequence of particles and their weights from a stream (requires T implementing CSerializabl...
const Derived & derived() const
CRTP helper method.
void clearParticles()
Free the memory of all the particles and reset the array "m_particles" to length zero.
CParticleFilterData()
Default constructor.
unsigned __int32 uint32_t
A curiously recurring template pattern (CRTP) approach to providing the basic functionality of any CP...
void setW(size_t i, double w) override
Modifies i'th particle (logarithm) weight, where first one is index 0.
void writeParticlesToStream(STREAM &out) const
Dumps the sequence of particles and their weights to a stream (requires T implementing CSerializable)...