GestureRecognitionToolkit  Version: 0.2.0
The Gesture Recognition Toolkit (GRT) is a cross-platform, open-source, c++ machine learning library for real-time gesture recognition.
FSMParticleFilter.h
Go to the documentation of this file.
1 
27 #ifndef GRT_FSM_PARTICLE_FILTER_HEADER
28 #define GRT_FSM_PARTICLE_FILTER_HEADER
29 
30 #include "../../CoreAlgorithms/ParticleFilter/ParticleFilter.h"
31 #include "FSMParticle.h"
32 
33 GRT_BEGIN_NAMESPACE
34 
35 class FSMParticleFilter : public ParticleFilter< FSMParticle, VectorFloat >{
36  public:
37  FSMParticleFilter():errorLog("[ERROR FSMParticleFilter]"){
38  pt = NULL;
39  pe = NULL;
40  }
41 
42  FSMParticleFilter( const FSMParticleFilter &rhs ):errorLog("[ERROR FSMParticleFilter]"){
43  pt = NULL;
44  pe = NULL;
45  //TODO: Need to handle copy better
46  }
47 
48  virtual ~FSMParticleFilter(){
49 
50  }
51 
52  virtual bool predict( FSMParticle &p ){
53 
54  if( !initialized ){
55  errorLog << "predict( FSMParticle &p ) - Particle Filter has not been initialized!" << std::endl;
56  return false;
57  }
58 
59  if( pt == NULL || pe == NULL ){
60  errorLog << "predict( FSMParticle &p ) - pt or pe are NULL!" << std::endl;
61  return false;
62  }
63 
64  //Update the particles current state
65  p.currentState = random.getRandomNumberWeighted( pt->at( p.currentState ) );
66 
67  //Get the model for the current state
68  const Vector< VectorDouble > &model = pe->at( p.currentState );
69 
70  //Pick a random sample from the model and set this as the particles state vector
71  const unsigned int K = model.getSize();
72  if( K > 0 )
73  p.x = model[ random.getRandomNumberInt(0, K) ];
74 
75  return true;
76  }
77 
78  virtual bool update( FSMParticle &p, VectorDouble &data ){
79 
80  if( !initialized ){
81  errorLog << "update( FSMParticle &p, VectorDouble &data ) - Particle Filter has not been initialized!" << std::endl;
82  return false;
83  }
84 
85  if( p.x.size() != data.size() ){
86  errorLog << "update( FSMParticle &p, VectorDouble &data ) - x does not match data.size()!" << std::endl;
87  return false;
88  }
89 
90  //Estimate the particles weight, given its current state and the sensor data
91  p.w = 1;
92  const size_t N = p.x.size();
93  for(size_t i=0; i<N; i++){
94  p.w *= gauss(p.x[i], data[i], measurementNoise[i]);
95  }
96 
97  /*
98  std::cout << "w: " << p.w << " p.x: ";
99  for(size_t i=0; i<N; i++){
100  std::cout << p.x[i] << " ";
101  }
102  std::cout << " data: ";
103  for(size_t i=0; i<N; i++){
104  std::cout << data[i] << " ";
105  }
106  std::cout << std::endl;
107  */
108 
109  return true;
110  }
111 
112  bool setLookupTables( Vector< Vector< IndexedDouble > > &pt, Vector< Vector< VectorDouble > > &pe ){
113 
114  this->pt = &pt;
115  this->pe = &pe;
116 
117  return true;
118  }
119 
120  Random random;
121  ErrorLog errorLog;
124 };
125 
126 GRT_END_NAMESPACE
127 
128 #endif
bool initialized
A flag that indicates if the filter has been initialized.
virtual bool update(FSMParticle &p, VectorDouble &data)
Definition: Random.h:40
UINT getSize() const
Definition: Vector.h:191
int getRandomNumberWeighted(const Vector< int > &values, const VectorFloat &weights)
Definition: Random.h:105
VectorFloat measurementNoise
The noise covariance in the measurement.
Float gauss(Float x, Float mu, Float sigma)
int getRandomNumberInt(int minRange, int maxRange)
Definition: Random.h:88
Definition: Vector.h:41
virtual bool predict(FSMParticle &p)