simple_particle_filter.h
Go to the documentation of this file.
1 // -*- mode: c++ -*-
2 /*********************************************************************
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2015, JSK Lab
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/o2r other materials provided
17  * with the distribution.
18  * * Neither the name of the JSK Lab nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *********************************************************************/
35 
36 
37 #ifndef JSK_PCL_ROS_ROS_COLLABORATIVE_PARTICLE_FILTER_H_
38 #define JSK_PCL_ROS_ROS_COLLABORATIVE_PARTICLE_FILTER_H_
39 
40 #include <pcl/tracking/particle_filter.h>
41 #include <pcl/tracking/impl/particle_filter.hpp>
42 
43 namespace pcl
44 {
45  namespace tracking
46  {
47  // Particle filter class which is friendly collaborative with ROS programs.
48  // Default behavior is same to the ParticleFilterTracker but you can customize
49  // the behavior from outer of the class.
50  template <typename PointInT, typename StateT>
51  class ROSCollaborativeParticleFilterTracker: public ParticleFilterTracker<PointInT, StateT>
52  {
53  public:
54  using Tracker<PointInT, StateT>::input_;
55  using ParticleFilterTracker<PointInT, StateT>::particles_;
56  using ParticleFilterTracker<PointInT, StateT>::changed_;
58  typedef typename Tracker<PointInT, StateT>::PointCloudIn PointCloudIn;
59  typedef typename PointCloudIn::Ptr PointCloudInPtr;
60  typedef typename PointCloudIn::ConstPtr PointCloudInConstPtr;
61 
62  typedef typename Tracker<PointInT, StateT>::PointCloudState PointCloudState;
63  typedef typename PointCloudState::Ptr PointCloudStatePtr;
64  typedef typename PointCloudState::ConstPtr PointCloudStateConstPtr;
65 
66  typedef PointCoherence<PointInT> Coherence;
69 
70  typedef PointCloudCoherence<PointInT> CloudCoherence;
73 
74  typedef boost::function<StateT (const StateT&)> CustomSampleFunc;
75  typedef boost::function<void (PointCloudInConstPtr, StateT&)> CustomLikelihoodFunc;
77  ParticleFilterTracker<PointInT, StateT>()
78  {
79  motion_ratio_ = 0.0;
80  changed_ = true;
81  }
82 
83  void setParticles(PointCloudStatePtr particles)
84  {
85  particles_ = particles;
86  }
87 
88  void setCustomSampleFunc(CustomSampleFunc f)
89  {
91  }
92 
93  void setLikelihoodFunc(CustomLikelihoodFunc f)
94  {
96  }
97 
98  protected:
99  bool initCompute()
100  {
101  // Do nothing
102  return true;
103  }
104 
105  void weight()
106  {
107  if (!particles_) {
108  std::cerr << "no particles" << std::endl;
109  }
110  if (!input_) {
111  std::cerr << "no input pointcloud" << std::endl;
112  }
113 
114 #ifdef _OPENMP
115 #pragma omp parallel for
116 #endif
117  for (size_t i = 0; i < particles_->points.size (); i++) {
118  custom_likelihood_func_ (input_, particles_->points[i]);
119  }
120  normalizeWeight();
121  }
122 
123  void
125  {
126  std::vector<int> a (particles_->points.size ());
127  std::vector<double> q (particles_->points.size ());
128  this->genAliasTable (a, q, particles_);
129 
130  // memoize the original list of particles
131 
132  PointCloudStatePtr origparticles = particles_;
133  particles_.reset(new PointCloudState());
134  particles_->points.reserve(origparticles->points.size() + 1); // particle_num_??
135  // the first particle, it is a just copy of the maximum result
136  // StateT p = representative_state_;
137  // particles_->points.push_back (p);
138 
139  // with motion
140  int motion_num = static_cast<int> (particles_->points.size ()) * static_cast<int> (motion_ratio_);
141  for ( int i = 1; i < motion_num; i++ ) {
142  int target_particle_index = sampleWithReplacement (a, q);
143  StateT p = origparticles->points[target_particle_index];
144  p = custom_sample_func_(p);
145  p = p + motion_;
146  particles_->points.push_back (p);
147  }
148 
149  // no motion
150  for ( int i = motion_num; i < particle_num_; i++ ) {
151  int target_particle_index = sampleWithReplacement (a, q);
152  StateT p = origparticles->points[target_particle_index];
153  // add noise using gaussian
154  p = custom_sample_func_(p);
155  particles_->points.push_back (p);
156  }
157  }
158 
159  bool initParticles(bool)
160  {
161  // Do nothing
162  return true;
163  }
164 
166  {
167  // r->w->u ?
168  // w->u->r ?
169  for (int i = 0; i < iteration_num_; i++) {
170  resample();
171  weight();
172  update();
173  }
174  }
175 
177  {
178  double n = 0.0;
179  for (size_t i = 0; i < particles_->points.size(); i++) {
180  n = particles_->points[i].weight + n;
181  }
182  if (n != 0.0) {
183  for (size_t i = 0; i < particles_->points.size(); i++) {
184  particles_->points[i].weight = particles_->points[i].weight / n;
185  }
186  }
187  else {
188  for (size_t i = 0; i < particles_->points.size(); i++) {
189  particles_->points[i].weight = 1.0 / particles_->points.size();
190  }
191  }
192  }
193 
194  using ParticleFilterTracker<PointInT, StateT>::iteration_num_;
195  using ParticleFilterTracker<PointInT, StateT>::update;
196  using ParticleFilterTracker<PointInT, StateT>::representative_state_;
197  using ParticleFilterTracker<PointInT, StateT>::motion_ratio_;
198  using ParticleFilterTracker<PointInT, StateT>::particle_num_;
199  using ParticleFilterTracker<PointInT, StateT>::motion_;
200  using ParticleFilterTracker<PointInT, StateT>::sampleWithReplacement;
201  CustomSampleFunc custom_sample_func_;
202  CustomLikelihoodFunc custom_likelihood_func_;
203  private:
204  };
205  }
206 }
207 
208 #endif
boost::shared_ptr< const Coherence > CoherenceConstPtr
GLfloat n[6][3]
bool update(const T &new_val, T &my_val)
boost::shared_ptr< const CloudCoherence > CloudCoherenceConstPtr
Tracker< PointInT, StateT >::PointCloudState PointCloudState
boost::function< StateT(const StateT &)> CustomSampleFunc
q
boost::shared_ptr< ROSCollaborativeParticleFilterTracker > Ptr
Tracker< PointInT, StateT >::PointCloudIn PointCloudIn
boost::function< void(PointCloudInConstPtr, StateT &)> CustomLikelihoodFunc
p
char a[26]


jsk_pcl_ros
Author(s): Yohei Kakiuchi
autogenerated on Mon May 3 2021 03:03:47