ParticleFilterFramework.cpp
Go to the documentation of this file.
1 // ****************************************************************************
2 // This file is part of the Integrating Vision Toolkit (IVT).
3 //
4 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
5 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
6 //
7 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT).
8 // All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are met:
12 //
13 // 1. Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright
17 // notice, this list of conditions and the following disclaimer in the
18 // documentation and/or other materials provided with the distribution.
19 //
20 // 3. Neither the name of the KIT nor the names of its contributors may be
21 // used to endorse or promote products derived from this software
22 // without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
25 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
28 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 // ****************************************************************************
35 // ****************************************************************************
36 // Filename: ParticleFilterFramework.cpp
37 // Author: Pedram Azad
38 // Date: 2004
39 // ****************************************************************************
40 
41 
42 // ****************************************************************************
43 // Includes
44 // ****************************************************************************
45 
46 #include <new> // for explicitly using correct new/delete operators on VC DSPs
47 
49 
50 #include "Helpers/helpers.h"
51 
52 #include <math.h>
53 #include <stdio.h>
54 #include <float.h>
55 
56 
57 
58 // ****************************************************************************
59 // Constructor / Destructor
60 // ****************************************************************************
61 
63 {
64  m_nParticles = nParticles;
65  m_nDimension = nDimension;
66 
67  mean_configuration = new double[nDimension];
68  last_configuration = new double[nDimension];
69 
70  sigma = new double[nDimension];
71  lower_limit = new double[nDimension];
72  upper_limit = new double[nDimension];
73 
74  int i;
75 
76  // allocate memory
77  s = new double*[nParticles];
78  for (i = 0; i < nParticles; i++)
79  s[i] = new double[nDimension];
80 
81  s_temp = new double*[nParticles];
82  for (i = 0; i < nParticles; i++)
83  s_temp[i] = new double[nDimension];
84 
85  c = new double[nParticles];
86  pi = new double[nParticles];
87 
88  temp = new double[nDimension];
89 
90  for (i = 0; i < nParticles; i++)
91  {
92  c[i] = 0.0;
93  pi[i] = 0.0;
94  }
95 }
96 
98 {
99  delete [] mean_configuration;
100  delete [] last_configuration;
101  delete [] sigma;
102  delete [] lower_limit;
103  delete [] upper_limit;
104 
105  int i;
106 
107  for (i = 0; i < m_nParticles; i++)
108  delete [] s[i];
109  delete [] s;
110 
111  for (i = 0; i < m_nParticles; i++)
112  delete [] s_temp[i];
113  delete [] s_temp;
114 
115  delete [] c;
116  delete [] pi;
117 
118  delete [] temp;
119 }
120 
121 
122 // ****************************************************************************
123 // Methods
124 // ****************************************************************************
125 
126 void CParticleFilterFramework::GetConfiguration(double *pBestConfiguration, double dMeanFactor)
127 {
128  double mean = 0;
129  int nConfigurations = 0, i;
130 
131  for (i = 0; i < m_nDimension; i++)
132  pBestConfiguration[i] = 0;
133 
134  for (i = 0; i < m_nParticles; i++)
135  mean += pi[i];
136 
137  mean /= m_nParticles * dMeanFactor;
138 
139  for (i = 0; i < m_nParticles; i++)
140  if (pi[i] > mean)
141  {
142  for (int j = 0; j < m_nDimension; j++)
143  pBestConfiguration[j] += s[i][j];
144 
145  nConfigurations++;
146  }
147 
148  if (nConfigurations > 0)
149  {
150  for (i = 0; i < m_nDimension; i++)
151  pBestConfiguration[i] /= nConfigurations;
152  }
153 }
154 
155 void CParticleFilterFramework::GetBestConfiguration(double *pBestConfiguration)
156 {
157  double max = -DBL_MAX;
158  int best_i, i;
159 
160  for (i = 0; i < m_nParticles; i++)
161  if (pi[i] > max)
162  {
163  best_i = i;
164  max = pi[i];
165  }
166 
167 
168  for (i = 0; i < m_nDimension; i++)
169  pBestConfiguration[i] = s[best_i][i];
170 }
171 
172 void CParticleFilterFramework::GetMeanConfiguration(double *pMeanConfiguration)
173 {
174  for (int i = 0; i < m_nDimension; i++)
175  pMeanConfiguration[i] = mean_configuration[i];
176 }
177 
178 void CParticleFilterFramework::GetPredictedConfiguration(double *pPredictedConfiguration)
179 {
180  for (int i = 0; i < m_nDimension; i++)
181  pPredictedConfiguration[i] = mean_configuration[i] + 0.8 * (mean_configuration[i] - last_configuration[i]);
182 }
183 
184 
186 {
187  double choice = uniform_random() * c_total;
188 
189  int low = 0;
190  int high = m_nParticles;
191 
192  while (high > (low + 1))
193  {
194  int middle = (high + low) / 2;
195 
196  if (choice > c[middle])
197  low = middle;
198  else
199  high = middle;
200  }
201 
202  return low;
203 }
204 
205 double CParticleFilterFramework::ParticleFilter(double *pResultMeanConfiguration, double dSigmaFactor)
206 {
207  // push previous state through process model
208  // use dynamic model and add noise
209  PredictNewBases(dSigmaFactor);
210 
211  // apply bayesian measurement weighting
212  c_total = 0;
213  int i;
214  for (i = 0; i < m_nParticles; i++)
215  {
216  // update model (calculate forward kinematics)
217  UpdateModel(i);
218 
219  // evaluate likelihood function (compare edges,...)
220  pi[i] = CalculateProbability(false);
221  }
222 
224 
225  for (i = 0; i < m_nParticles; i++)
226  {
227  c[i] = c_total;
228  c_total += pi[i];
229  }
230 
231  // normalize
232  const double factor = 1 / c_total;
233  for (i = 0; i < m_nParticles; i++)
234  pi[i] *= factor;
235 
236  CalculateMean();
237 
238  GetMeanConfiguration(pResultMeanConfiguration);
239 
240  return CalculateProbabilityForConfiguration(pResultMeanConfiguration);
241 }
242 
244 {
245  int i;
246  double max = -1;
247 
248  for (i = 0; i < m_nDimension; i++)
249  {
251  mean_configuration[i] = 0;
252  }
253 
254  for (i = 0; i < m_nParticles; i++)
255  {
256  for (int j = 0; j < m_nDimension; j++)
257  mean_configuration[j] += pi[i] * s[i][j];
258  }
259 }
260 
262 {
263  // little "trick" for calculating the probability for the mean configuration
264  // without introducing a new virtual function by using s[0]
265 
266  int i;
267 
268  for (i = 0; i < m_nDimension; i++)
269  {
270  temp[i] = s[0][i];
271  s[0][i] = pConfiguration[i];
272  }
273 
274  UpdateModel(0);
275 
276  for (i = 0; i < m_nDimension; i++)
277  s[0][i] = temp[i];
278 
279  const double dResult = CalculateProbability(true);
280 
281  for (i = 0; i < m_nDimension; i++)
282  s[0][i] = temp[i];
283 
284  return dResult;
285 }
double uniform_random()
Definition: helpers.cpp:251
virtual void GetPredictedConfiguration(double *pPredictedConfiguration)
virtual void PredictNewBases(double dSigmaFactor)=0
virtual void GetBestConfiguration(double *pBestConfiguration)
virtual double CalculateProbability(bool bSeparateCall=true)=0
GLdouble s
Definition: glext.h:3211
virtual void CalculateFinalProbabilities()
double ParticleFilter(double *pResultMeanConfiguration, double dSigmaFactor=1)
const GLubyte * c
Definition: glext.h:5181
CParticleFilterFramework(int nParticles, int nDimension)
double CalculateProbabilityForConfiguration(const double *pConfiguration)
virtual void UpdateModel(int nParticle)=0
virtual void GetConfiguration(double *pBestConfiguration, double dMeanFactor)
virtual void GetMeanConfiguration(double *pMeanConfiguration)


asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Mon Dec 2 2019 03:47:28