Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ppf_registration.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011, Alexandru-Eugen Ichim
5  * Willow Garage, Inc
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/or other materials provided
17  * with the distribution.
18  * * Neither the name of Willow Garage, Inc. 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  * $Id: ppf_registration.h 5066 2012-03-14 06:42:21Z rusu $
36  */
37 
38 
39 #ifndef PCL_PPF_REGISTRATION_H_
40 #define PCL_PPF_REGISTRATION_H_
41 
43 #include <pcl/features/ppf.h>
44 #include <boost/unordered_map.hpp>
45 
46 namespace pcl
47 {
49  {
50  public:
56  struct HashKeyStruct : public std::pair <int, std::pair <int, std::pair <int, int> > >
57  {
58  HashKeyStruct(int a, int b, int c, int d)
59  {
60  this->first = a;
61  this->second.first = b;
62  this->second.second.first = c;
63  this->second.second.second = d;
64  }
65  };
66  typedef boost::unordered_multimap<HashKeyStruct, std::pair<size_t, size_t> > FeatureHashMapType;
67  typedef boost::shared_ptr<FeatureHashMapType> FeatureHashMapTypePtr;
68  typedef boost::shared_ptr<PPFHashMapSearch> Ptr;
69 
70 
75  PPFHashMapSearch (float angle_discretization_step = 12.0f / 180.0f * static_cast<float> (M_PI),
76  float distance_discretization_step = 0.01f)
77  : alpha_m_ ()
78  , feature_hash_map_ (new FeatureHashMapType)
79  , internals_initialized_ (false)
80  , angle_discretization_step_ (angle_discretization_step)
81  , distance_discretization_step_ (distance_discretization_step)
82  , max_dist_ (-1.0f)
83  {
84  }
85 
89  void
90  setInputFeatureCloud (PointCloud<PPFSignature>::ConstPtr feature_cloud);
91 
100  void
101  nearestNeighborSearch (float &f1, float &f2, float &f3, float &f4,
102  std::vector<std::pair<size_t, size_t> > &indices);
103 
105  Ptr
106  makeShared() { return Ptr (new PPFHashMapSearch (*this)); }
107 
109  inline float
110  getAngleDiscretizationStep () { return angle_discretization_step_; }
111 
113  inline float
114  getDistanceDiscretizationStep () { return distance_discretization_step_; }
115 
117  inline float
118  getModelDiameter () { return max_dist_; }
119 
120  std::vector <std::vector <float> > alpha_m_;
121  private:
122  FeatureHashMapTypePtr feature_hash_map_;
123  bool internals_initialized_;
124 
125  float angle_discretization_step_, distance_discretization_step_;
126  float max_dist_;
127  };
128 
140  template <typename PointSource, typename PointTarget>
141  class PPFRegistration : public Registration<PointSource, PointTarget>
142  {
143  public:
149  {
150  PoseWithVotes(Eigen::Affine3f &a_pose, unsigned int &a_votes)
151  : pose (a_pose),
152  votes (a_votes)
153  {}
154 
155  Eigen::Affine3f pose;
156  unsigned int votes;
157  };
158  typedef std::vector<PoseWithVotes, Eigen::aligned_allocator<PoseWithVotes> > PoseWithVotesList;
159 
167 
171 
175 
176 
179  : Registration<PointSource, PointTarget> (),
180  search_method_ (),
181  scene_reference_point_sampling_rate_ (5),
182  clustering_position_diff_threshold_ (0.01f),
183  clustering_rotation_diff_threshold_ (20.0f / 180.0f * static_cast<float> (M_PI))
184  {}
185 
190  inline void
191  setPositionClusteringThreshold (float clustering_position_diff_threshold) { clustering_position_diff_threshold_ = clustering_position_diff_threshold; }
192 
197  inline float
198  getPositionClusteringThreshold () { return clustering_position_diff_threshold_; }
199 
204  inline void
205  setRotationClusteringThreshold (float clustering_rotation_diff_threshold) { clustering_rotation_diff_threshold_ = clustering_rotation_diff_threshold; }
206 
209  inline float
210  getRotationClusteringThreshold () { return clustering_rotation_diff_threshold_; }
211 
215  inline void
216  setSceneReferencePointSamplingRate (unsigned int scene_reference_point_sampling_rate) { scene_reference_point_sampling_rate_ = scene_reference_point_sampling_rate; }
217 
219  inline unsigned int
220  getSceneReferencePointSamplingRate () { return scene_reference_point_sampling_rate_; }
221 
227  inline void
228  setSearchMethod (PPFHashMapSearch::Ptr search_method) { search_method_ = search_method; }
229 
231  inline PPFHashMapSearch::Ptr
232  getSearchMethod () { return search_method_; }
233 
237  void
239 
240 
241  private:
243  void
244  computeTransformation (PointCloudSource &output, const Eigen::Matrix4f& guess);
245 
246 
248  PPFHashMapSearch::Ptr search_method_;
249 
251  unsigned int scene_reference_point_sampling_rate_;
252 
255  float clustering_position_diff_threshold_, clustering_rotation_diff_threshold_;
256 
258  typename pcl::KdTreeFLANN<PointTarget>::Ptr scene_search_tree_;
259 
262  static bool
263  poseWithVotesCompareFunction (const PoseWithVotes &a,
264  const PoseWithVotes &b);
265 
268  static bool
269  clusterVotesCompareFunction (const std::pair<size_t, unsigned int> &a,
270  const std::pair<size_t, unsigned int> &b);
271 
274  void
275  clusterPoses (PoseWithVotesList &poses,
276  PoseWithVotesList &result);
277 
280  bool
281  posesWithinErrorBounds (Eigen::Affine3f &pose1,
282  Eigen::Affine3f &pose2);
283  };
284 }
285 
287 
288 #endif // PCL_PPF_REGISTRATION_H_