Skip to content

Commit 7fddcb6

Browse files
committed
port mrpt_bayes
1 parent b792e8d commit 7fddcb6

File tree

6 files changed

+147
-66
lines changed

6 files changed

+147
-66
lines changed

modules/mrpt_bayes/CMakeLists.txt

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,49 @@
1-
#---------------------------------------------
2-
# Macro declared in "DeclareMRPTLib.cmake":
3-
#---------------------------------------------
4-
define_mrpt_lib(
5-
# Lib name
6-
bayes
7-
# Dependencies
8-
mrpt-math
9-
mrpt-config
10-
)
11-
12-
if(BUILD_mrpt-bayes)
13-
14-
endif()
1+
# ------------------------------------------------------------------------------
2+
# Mobile Robot Programming Toolkit (MRPT)
3+
#
4+
# Copyright (c) 2005-2025, Jose Luis Blanco-Claraco, contributors (see Git history)
5+
# All rights reserved.
6+
# Released under BSD-3 license. See LICENSE file
7+
# ------------------------------------------------------------------------------
8+
9+
cmake_minimum_required(VERSION 3.16)
10+
11+
# Tell CMake we'll use C++ for use in its tests/flags
12+
project(mrpt_bayes LANGUAGES C CXX)
13+
14+
# MRPT CMake scripts: "mrpt_xxx()"
15+
find_package(mrpt_common REQUIRED)
16+
find_package(mrpt_config REQUIRED)
17+
find_package(mrpt_math REQUIRED)
18+
19+
20+
# define lib:
21+
set(LIB_SRCS
22+
src/CParticleFilter.cpp
23+
src/CParticleFilterCapable.cpp
24+
)
25+
26+
set(LIB_PUBLIC_HDRS
27+
include/mrpt/bayes/CRejectionSamplingCapable.h
28+
include/mrpt/bayes/CKalmanFilterCapable.h
29+
include/mrpt/bayes/registerAllClasses.h
30+
include/mrpt/bayes/CParticleFilterCapable.h
31+
include/mrpt/bayes/CProbabilityParticle.h
32+
include/mrpt/bayes/CParticleFilterData.h
33+
include/mrpt/bayes/CKalmanFilterCapable_impl.h
34+
include/mrpt/bayes/CParticleFilter.h
35+
)
36+
37+
mrpt_add_library(
38+
TARGET ${PROJECT_NAME}
39+
SOURCES ${LIB_SRCS} ${LIB_PUBLIC_HDRS}
40+
PUBLIC_LINK_LIBRARIES
41+
mrpt::mrpt_config
42+
mrpt::mrpt_math
43+
# PRIVATE_LINK_LIBRARIES
44+
CMAKE_DEPENDENCIES
45+
mrpt_config
46+
mrpt_math
47+
# Other imported targets:
48+
)
49+

modules/mrpt_bayes/package.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0"?>
2+
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
3+
<!-- This is a ROS package file, intended to allow this library to be built
4+
side-by-side to ROS packages in a catkin/ament environment.
5+
-->
6+
<package format="3">
7+
<name>mrpt_bayes</name>
8+
<version>2.20.0</version>
9+
<description>The MRPT C++ library mrpt_bayes</description>
10+
11+
<maintainer email="[email protected]">Jose-Luis Blanco-Claraco</maintainer>
12+
<license file="LICENSE">BSD</license>
13+
14+
<url type="website">https://github.com/MRPT/</url>
15+
16+
<!-- BUILD TOOLS -->
17+
<buildtool_depend>cmake</buildtool_depend>
18+
19+
<depend>mrpt_common</depend>
20+
<depend>mrpt_config</depend>
21+
<depend>mrpt_math</depend>
22+
23+
<export>
24+
<build_type>cmake</build_type>
25+
</export>
26+
27+
</package>

modules/mrpt_bayes/src/CParticleFilter.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,15 @@
77
| Released under BSD License. See: https://www.mrpt.org/License |
88
+------------------------------------------------------------------------+ */
99

10-
#include "bayes-precomp.h" // Precompiled headers
11-
//
1210
#include <mrpt/bayes/CParticleFilter.h> // for CParticleFilter::TPar...
1311
#include <mrpt/bayes/CParticleFilterCapable.h> // for CParticleFilterCapable
1412
#include <mrpt/config/CConfigFileBase.h> // for CConfigFileBase, MRPT...
1513
#include <mrpt/core/bits_math.h> // square()
1614
#include <mrpt/system/COutputLogger.h> // for COutputLogger, MRPT_L...
1715

18-
#include <cmath> // for exp
19-
#include <cstddef> // for size_t
20-
#include <exception> // for exception
21-
#include <string> // for string, allocator
16+
#include <cmath> // for exp
17+
#include <cstddef> // for size_t
18+
#include <string> // for string, allocator
2219

2320
namespace mrpt
2421
{

modules/mrpt_bayes/src/CParticleFilterCapable.cpp

Lines changed: 68 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
| Released under BSD License. See: https://www.mrpt.org/License |
88
+------------------------------------------------------------------------+ */
99

10-
#include "bayes-precomp.h" // Precompiled headers
11-
//
1210
#include <mrpt/bayes/CParticleFilterCapable.h>
1311
#include <mrpt/math/ops_vectors.h>
1412
#include <mrpt/random.h>
@@ -47,7 +45,10 @@ void CParticleFilterCapable::performResampling(
4745
performSubstitution(indxs);
4846

4947
// Finally, equal weights:
50-
for (size_t i = 0; i < out_particle_count; i++) setW(i, 0 /* Logarithmic weight */);
48+
for (size_t i = 0; i < out_particle_count; i++)
49+
{
50+
setW(i, 0 /* Logarithmic weight */);
51+
}
5152

5253
MRPT_END
5354
}
@@ -69,7 +70,10 @@ void CParticleFilterCapable::computeResampling(
6970
size_t i, j, M = in_logWeights.size();
7071
ASSERT_(M > 0);
7172

72-
if (!out_particle_count) out_particle_count = M;
73+
if (!out_particle_count)
74+
{
75+
out_particle_count = M;
76+
}
7377

7478
vector<double> linW(M, 0);
7579
vector<double>::const_iterator inIt;
@@ -79,7 +83,9 @@ void CParticleFilterCapable::computeResampling(
7983
// This is to avoid float point range problems:
8084
double max_log_w = math::maximum(in_logWeights);
8185
for (i = 0, inIt = in_logWeights.begin(), outIt = linW.begin(); i < M; i++, inIt++, outIt++)
86+
{
8287
linW_SUM += ((*outIt) = exp((*inIt) - max_log_w));
88+
}
8389

8490
// Normalize weights:
8591
ASSERT_(linW_SUM > 0);
@@ -132,7 +138,7 @@ void CParticleFilterCapable::computeResampling(
132138
size_t R = 0; // Remainder or residual count
133139
for (i = 0; i < M; i++)
134140
{
135-
N[i] = int(M * linW[i]);
141+
N[i] = static_cast<uint32_t>(M * linW[i]);
136142
R += N[i];
137143
}
138144
size_t N_rnd =
@@ -143,7 +149,12 @@ void CParticleFilterCapable::computeResampling(
143149
// Fillout the deterministic part of the resampling:
144150
out_indexes.resize(out_particle_count);
145151
for (i = 0, j = 0; i < out_particle_count; i++)
146-
for (size_t k = 0; k < N[i]; k++) out_indexes[j++] = i;
152+
{
153+
for (size_t k = 0; k < N[i]; k++)
154+
{
155+
out_indexes[j++] = i;
156+
}
157+
}
147158

148159
size_t M_fixed = j;
149160

@@ -156,7 +167,10 @@ void CParticleFilterCapable::computeResampling(
156167
// Compute modified weights:
157168
vector<double> linW_mod(M);
158169
const double M_R_1 = 1.0 / N_rnd;
159-
for (i = 0; i < M; i++) linW_mod[i] = M_R_1 * (M * linW[i] - N[i]);
170+
for (i = 0; i < M; i++)
171+
{
172+
linW_mod[i] = M_R_1 * (M * linW[i] - N[i]);
173+
}
160174

161175
// perform resampling:
162176
vector<double> Q;
@@ -214,7 +228,9 @@ void CParticleFilterCapable::computeResampling(
214228
while (i < out_particle_count)
215229
{
216230
if (T[i] < Q[j])
231+
{
217232
out_indexes[i++] = (unsigned int)j;
233+
}
218234
else
219235
{
220236
j++;
@@ -236,15 +252,20 @@ void CParticleFilterCapable::computeResampling(
236252
vector<double> T(M + 1);
237253
double _1_M = 1.0 / M;
238254
T[0] = getRandomGenerator().drawUniform(0.0, _1_M);
239-
for (i = 1; i < M; i++) T[i] = T[i - 1] + _1_M;
255+
for (i = 1; i < M; i++)
256+
{
257+
T[i] = T[i - 1] + _1_M;
258+
}
240259
T[M] = 1;
241260

242261
out_indexes.resize(out_particle_count);
243262
i = j = 0;
244263
while (i < out_particle_count)
245264
{
246265
if (T[i] < Q[j])
266+
{
247267
out_indexes[i++] = (unsigned int)j;
268+
}
248269
else
249270
{
250271
j++;
@@ -355,9 +376,11 @@ void CParticleFilterCapable::prepareFastDrawSample(
355376
// -> Use m_fastDrawAuxiliary.CDF, PDF, CDF_indexes
356377
// --------------------------------------------------------
357378
if (PF_options.resamplingMethod != CParticleFilter::prMultinomial)
379+
{
358380
THROW_EXCEPTION(
359381
"resamplingMethod must be 'prMultinomial' for a dynamic number "
360382
"of particles!");
383+
}
361384

362385
size_t i, j = 666666, M = particlesCount();
363386

@@ -376,7 +399,9 @@ void CParticleFilterCapable::prepareFastDrawSample(
376399
double SUM = 0;
377400
// Save the log likelihoods:
378401
for (i = 0; i < M; i++)
402+
{
379403
m_fastDrawAuxiliary.PDF[i] = partEvaluator(PF_options, this, i, action, observation);
404+
}
380405
// "Normalize":
381406
m_fastDrawAuxiliary.PDF += -math::maximum(m_fastDrawAuxiliary.PDF);
382407
for (i = 0; i < M; i++) SUM += m_fastDrawAuxiliary.PDF[i] = exp(m_fastDrawAuxiliary.PDF[i]);
@@ -386,34 +411,37 @@ void CParticleFilterCapable::prepareFastDrawSample(
386411

387412
// Compute the CDF thresholds:
388413
for (i = 0; i < PARTICLE_FILTER_CAPABLE_FAST_DRAW_BINS; i++)
414+
{
389415
m_fastDrawAuxiliary.CDF[i] = ((double)i) / ((double)PARTICLE_FILTER_CAPABLE_FAST_DRAW_BINS);
416+
}
390417
m_fastDrawAuxiliary.CDF[PARTICLE_FILTER_CAPABLE_FAST_DRAW_BINS] = 1.0;
391418

392419
// Compute the CDF and save threshold indexes:
393420
double CDF = 0; // Cumulative density func.
394421
for (i = 0, j = 0; i < M && j < PARTICLE_FILTER_CAPABLE_FAST_DRAW_BINS; i++)
395422
{
396423
double CDF_next = CDF + m_fastDrawAuxiliary.PDF[i];
397-
if (i == (M - 1)) CDF_next = 1.0; // rounds fix...
398-
if (CDF_next > 1.0) CDF_next = 1.0;
424+
if (i == (M - 1))
425+
{
426+
CDF_next = 1.0; // rounds fix...
427+
}
428+
if (CDF_next > 1.0)
429+
{
430+
CDF_next = 1.0;
431+
}
399432

400433
while (m_fastDrawAuxiliary.CDF[j] < CDF_next)
434+
{
401435
m_fastDrawAuxiliary.CDF_indexes[j++] = (unsigned int)i;
436+
}
402437

403438
CDF = CDF_next;
404439
}
405440

406441
ASSERT_(j == PARTICLE_FILTER_CAPABLE_FAST_DRAW_BINS);
407442

408-
// Done!
409-
#if !defined(_MSC_VER) || (_MSC_VER > 1400) // <=VC2005 doesn't work with this!
410-
MRPT_END_WITH_CLEAN_UP( /* Debug: */
411-
cout << "j=" << j << "\nm_fastDrawAuxiliary.CDF_indexes:"
412-
<< m_fastDrawAuxiliary.CDF_indexes << endl;
413-
cout << "m_fastDrawAuxiliary.CDF:" << m_fastDrawAuxiliary.CDF << endl;);
414-
#else
443+
// Done!
415444
MRPT_END
416-
#endif
417445
}
418446
else
419447
{
@@ -426,9 +454,10 @@ void CParticleFilterCapable::prepareFastDrawSample(
426454
size_t i, M = particlesCount();
427455
vector<double> PDF(M, 0);
428456
for (i = 0; i < M; i++)
429-
PDF[i] = partEvaluator(
430-
PF_options, this, i, action,
431-
observation); // Default evaluator: takes current weight.
457+
{
458+
// Default evaluator: takes current weight.
459+
PDF[i] = partEvaluator(PF_options, this, i, action, observation);
460+
}
432461

433462
vector<size_t> idxs;
434463

@@ -440,7 +469,9 @@ void CParticleFilterCapable::prepareFastDrawSample(
440469
m_fastDrawAuxiliary.alreadyDrawnIndexes.resize(idxs.size());
441470
for (it = idxs.begin(), it2 = m_fastDrawAuxiliary.alreadyDrawnIndexes.begin(); it != idxs.end();
442471
++it, ++it2)
472+
{
443473
*it2 = (unsigned int)(*it);
474+
}
444475

445476
m_fastDrawAuxiliary.alreadyDrawnNextOne = 0;
446477
}
@@ -463,9 +494,11 @@ size_t CParticleFilterCapable::fastDrawSample(
463494
// -> Use m_fastDrawAuxiliary.CDF, PDF, CDF_indexes
464495
// --------------------------------------------------------
465496
if (PF_options.resamplingMethod != CParticleFilter::prMultinomial)
497+
{
466498
THROW_EXCEPTION(
467499
"resamplingMethod must be 'prMultinomial' for a dynamic number "
468500
"of particles!");
501+
}
469502

470503
double draw = getRandomGenerator().drawUniform(0.0, 0.999999);
471504
double CDF_next = -1.;
@@ -500,9 +533,11 @@ size_t CParticleFilterCapable::fastDrawSample(
500533
// -> Use m_fastDrawAuxiliary.alreadyDrawnIndexes & alreadyDrawnNextOne
501534
// --------------------------------------------------------
502535
if (m_fastDrawAuxiliary.alreadyDrawnNextOne >= m_fastDrawAuxiliary.alreadyDrawnIndexes.size())
536+
{
503537
THROW_EXCEPTION(
504538
"Have you called 'fastDrawSample' more times than the sample "
505539
"size? Did you forget calling 'prepareFastCall' before?");
540+
}
506541

507542
return m_fastDrawAuxiliary.alreadyDrawnIndexes[m_fastDrawAuxiliary.alreadyDrawnNextOne++];
508543
}
@@ -522,15 +557,24 @@ void CParticleFilterCapable::log2linearWeights(
522557

523558
out_linWeights.resize(N);
524559

525-
if (!N) return;
560+
if (!N)
561+
{
562+
return;
563+
}
526564

527565
double sumW = 0;
528566
size_t i;
529-
for (i = 0; i < N; i++) sumW += out_linWeights[i] = exp(in_logWeights[i]);
567+
for (i = 0; i < N; i++)
568+
{
569+
sumW += out_linWeights[i] = exp(in_logWeights[i]);
570+
}
530571

531572
ASSERT_(sumW > 0);
532573

533-
for (i = 0; i < N; i++) out_linWeights[i] /= sumW;
574+
for (i = 0; i < N; i++)
575+
{
576+
out_linWeights[i] /= sumW;
577+
}
534578

535579
MRPT_END
536580
}

modules/mrpt_bayes/src/bayes-precomp.cpp

Lines changed: 0 additions & 10 deletions
This file was deleted.

modules/mrpt_bayes/src/bayes-precomp.h

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)