Point Cloud Library (PCL)  1.7.1
pfh.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2011, Willow Garage, Inc.
6  * Copyright (c) 2012-, Open Perception, Inc.
7  *
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
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * $Id$
38  *
39  */
40 
41 #ifndef PCL_PFH_H_
42 #define PCL_PFH_H_
43 
44 #include <pcl/point_types.h>
45 #include <pcl/features/feature.h>
46 #include <pcl/features/pfh_tools.h>
47 #include <map>
48 
49 namespace pcl
50 {
51  /** \brief PFHEstimation estimates the Point Feature Histogram (PFH) descriptor for a given point cloud dataset
52  * containing points and normals.
53  *
54  * A commonly used type for PointOutT is pcl::PFHSignature125.
55  *
56  * \note If you use this code in any academic work, please cite:
57  *
58  * - R.B. Rusu, N. Blodow, Z.C. Marton, M. Beetz.
59  * Aligning Point Cloud Views using Persistent Feature Histograms.
60  * In Proceedings of the 21st IEEE/RSJ International Conference on Intelligent Robots and Systems (IROS),
61  * Nice, France, September 22-26 2008.
62  * - R.B. Rusu, Z.C. Marton, N. Blodow, M. Beetz.
63  * Learning Informative Point Classes for the Acquisition of Object Model Maps.
64  * In Proceedings of the 10th International Conference on Control, Automation, Robotics and Vision (ICARCV),
65  * Hanoi, Vietnam, December 17-20 2008.
66  *
67  * \attention
68  * The convention for PFH features is:
69  * - if a query point's nearest neighbors cannot be estimated, the PFH feature will be set to NaN
70  * (not a number)
71  * - it is impossible to estimate a PFH descriptor for a point that
72  * doesn't have finite 3D coordinates. Therefore, any point that contains
73  * NaN data on x, y, or z, will have its PFH feature property set to NaN.
74  *
75  * \note The code is stateful as we do not expect this class to be multicore parallelized. Please look at
76  * \ref FPFHEstimationOMP for examples on parallel implementations of the FPFH (Fast Point Feature Histogram).
77  *
78  * \author Radu B. Rusu
79  * \ingroup features
80  */
81  template <typename PointInT, typename PointNT, typename PointOutT = pcl::PFHSignature125>
82  class PFHEstimation : public FeatureFromNormals<PointInT, PointNT, PointOutT>
83  {
84  public:
85  typedef boost::shared_ptr<PFHEstimation<PointInT, PointNT, PointOutT> > Ptr;
86  typedef boost::shared_ptr<const PFHEstimation<PointInT, PointNT, PointOutT> > ConstPtr;
95 
98 
99  /** \brief Empty constructor.
100  * Sets \a use_cache_ to false, \a nr_subdiv_ to 5, and the internal maximum cache size to 1GB.
101  */
103  nr_subdiv_ (5),
104  pfh_histogram_ (),
105  pfh_tuple_ (),
106  d_pi_ (1.0f / (2.0f * static_cast<float> (M_PI))),
107  feature_map_ (),
108  key_list_ (),
109  // Default 1GB memory size. Need to set it to something more conservative.
110  max_cache_size_ ((1ul*1024ul*1024ul*1024ul) / sizeof (std::pair<std::pair<int, int>, Eigen::Vector4f>)),
111  use_cache_ (false)
112  {
113  feature_name_ = "PFHEstimation";
114  };
115 
116  /** \brief Set the maximum internal cache size. Defaults to 2GB worth of entries.
117  * \param[in] cache_size maximum cache size
118  */
119  inline void
120  setMaximumCacheSize (unsigned int cache_size)
121  {
122  max_cache_size_ = cache_size;
123  }
124 
125  /** \brief Get the maximum internal cache size. */
126  inline unsigned int
128  {
129  return (max_cache_size_);
130  }
131 
132  /** \brief Set whether to use an internal cache mechanism for removing redundant calculations or not.
133  *
134  * \note Depending on how the point cloud is ordered and how the nearest
135  * neighbors are estimated, using a cache could have a positive or a
136  * negative influence. Please test with and without a cache on your
137  * data, and choose whatever works best!
138  *
139  * See \ref setMaximumCacheSize for setting the maximum cache size
140  *
141  * \param[in] use_cache set to true to use the internal cache, false otherwise
142  */
143  inline void
144  setUseInternalCache (bool use_cache)
145  {
146  use_cache_ = use_cache;
147  }
148 
149  /** \brief Get whether the internal cache is used or not for computing the PFH features. */
150  inline bool
152  {
153  return (use_cache_);
154  }
155 
156  /** \brief Compute the 4-tuple representation containing the three angles and one distance between two points
157  * represented by Cartesian coordinates and normals.
158  * \note For explanations about the features, please see the literature mentioned above (the order of the
159  * features might be different).
160  * \param[in] cloud the dataset containing the XYZ Cartesian coordinates of the two points
161  * \param[in] normals the dataset containing the surface normals (assuming normalized vectors) at each point in cloud
162  * \param[in] p_idx the index of the first point (source)
163  * \param[in] q_idx the index of the second point (target)
164  * \param[out] f1 the first angular feature (angle between the projection of nq_idx and u)
165  * \param[out] f2 the second angular feature (angle between nq_idx and v)
166  * \param[out] f3 the third angular feature (angle between np_idx and |p_idx - q_idx|)
167  * \param[out] f4 the distance feature (p_idx - q_idx)
168  * \note For efficiency reasons, we assume that the point data passed to the method is finite.
169  */
170  bool
172  int p_idx, int q_idx, float &f1, float &f2, float &f3, float &f4);
173 
174  /** \brief Estimate the PFH (Point Feature Histograms) individual signatures of the three angular (f1, f2, f3)
175  * features for a given point based on its spatial neighborhood of 3D points with normals
176  * \param[in] cloud the dataset containing the XYZ Cartesian coordinates of the two points
177  * \param[in] normals the dataset containing the surface normals at each point in \a cloud
178  * \param[in] indices the k-neighborhood point indices in the dataset
179  * \param[in] nr_split the number of subdivisions for each angular feature interval
180  * \param[out] pfh_histogram the resultant (combinatorial) PFH histogram representing the feature at the query point
181  */
182  void
184  const std::vector<int> &indices, int nr_split, Eigen::VectorXf &pfh_histogram);
185 
186  protected:
187  /** \brief Estimate the Point Feature Histograms (PFH) descriptors at a set of points given by
188  * <setInputCloud (), setIndices ()> using the surface in setSearchSurface () and the spatial locator in
189  * setSearchMethod ()
190  * \param[out] output the resultant point cloud model dataset that contains the PFH feature estimates
191  */
192  void
193  computeFeature (PointCloudOut &output);
194 
195  /** \brief The number of subdivisions for each angular feature interval. */
197 
198  /** \brief Placeholder for a point's PFH signature. */
199  Eigen::VectorXf pfh_histogram_;
200 
201  /** \brief Placeholder for a PFH 4-tuple. */
202  Eigen::Vector4f pfh_tuple_;
203 
204  /** \brief Placeholder for a histogram index. */
205  int f_index_[3];
206 
207  /** \brief Float constant = 1.0 / (2.0 * M_PI) */
208  float d_pi_;
209 
210  /** \brief Internal hashmap, used to optimize efficiency of redundant computations. */
211  std::map<std::pair<int, int>, Eigen::Vector4f, std::less<std::pair<int, int> >, Eigen::aligned_allocator<Eigen::Vector4f> > feature_map_;
212 
213  /** \brief Queue of pairs saved, used to constrain memory usage. */
214  std::queue<std::pair<int, int> > key_list_;
215 
216  /** \brief Maximum size of internal cache memory. */
217  unsigned int max_cache_size_;
218 
219  /** \brief Set to true to use the internal cache for removing redundant computations. */
221  };
222 }
223 
224 #ifdef PCL_NO_PRECOMPILE
225 #include <pcl/features/impl/pfh.hpp>
226 #endif
227 
228 #endif //#ifndef PCL_PFH_H_
229 
void computePointPFHSignature(const pcl::PointCloud< PointInT > &cloud, const pcl::PointCloud< PointNT > &normals, const std::vector< int > &indices, int nr_split, Eigen::VectorXf &pfh_histogram)
Estimate the PFH (Point Feature Histograms) individual signatures of the three angular (f1...
Definition: pfh.hpp:58
unsigned int max_cache_size_
Maximum size of internal cache memory.
Definition: pfh.h:217
std::queue< std::pair< int, int > > key_list_
Queue of pairs saved, used to constrain memory usage.
Definition: pfh.h:214
std::string feature_name_
The feature name.
Definition: feature.h:222
PointCloud represents the base class in PCL for storing collections of 3D points. ...
float d_pi_
Float constant = 1.0 / (2.0 * M_PI)
Definition: pfh.h:208
Feature represents the base feature class.
Definition: feature.h:105
unsigned int getMaximumCacheSize()
Get the maximum internal cache size.
Definition: pfh.h:127
boost::shared_ptr< const PFHEstimation< PointInT, PointNT, PointOutT > > ConstPtr
Definition: pfh.h:86
bool getUseInternalCache()
Get whether the internal cache is used or not for computing the PFH features.
Definition: pfh.h:151
bool use_cache_
Set to true to use the internal cache for removing redundant computations.
Definition: pfh.h:220
Feature< PointInT, PointOutT >::PointCloudOut PointCloudOut
Definition: pfh.h:96
void computeFeature(PointCloudOut &output)
Estimate the Point Feature Histograms (PFH) descriptors at a set of points given by <setInputCloud ()...
Definition: pfh.hpp:157
Eigen::Vector4f pfh_tuple_
Placeholder for a PFH 4-tuple.
Definition: pfh.h:202
PFHEstimation estimates the Point Feature Histogram (PFH) descriptor for a given point cloud dataset ...
Definition: pfh.h:82
int nr_subdiv_
The number of subdivisions for each angular feature interval.
Definition: pfh.h:196
void setMaximumCacheSize(unsigned int cache_size)
Set the maximum internal cache size.
Definition: pfh.h:120
Eigen::VectorXf pfh_histogram_
Placeholder for a point's PFH signature.
Definition: pfh.h:199
bool computePairFeatures(const pcl::PointCloud< PointInT > &cloud, const pcl::PointCloud< PointNT > &normals, int p_idx, int q_idx, float &f1, float &f2, float &f3, float &f4)
Compute the 4-tuple representation containing the three angles and one distance between two points re...
Definition: pfh.hpp:46
int f_index_[3]
Placeholder for a histogram index.
Definition: pfh.h:205
PFHEstimation()
Empty constructor.
Definition: pfh.h:102
void setUseInternalCache(bool use_cache)
Set whether to use an internal cache mechanism for removing redundant calculations or not...
Definition: pfh.h:144
Feature< PointInT, PointOutT >::PointCloudIn PointCloudIn
Definition: pfh.h:97
boost::shared_ptr< PFHEstimation< PointInT, PointNT, PointOutT > > Ptr
Definition: pfh.h:85
std::map< std::pair< int, int >, Eigen::Vector4f, std::less< std::pair< int, int > >, Eigen::aligned_allocator< Eigen::Vector4f > > feature_map_
Internal hashmap, used to optimize efficiency of redundant computations.
Definition: pfh.h:211