Point Cloud Library (PCL)  1.14.0-dev
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 #pragma once
42 
43 #include <pcl/point_types.h>
44 #include <pcl/features/feature.h>
45 #include <map>
46 #include <queue> // for std::queue
47 
48 namespace pcl
49 {
50  /** \brief PFHEstimation estimates the Point Feature Histogram (PFH) descriptor for a given point cloud dataset
51  * containing points and normals.
52  *
53  * A commonly used type for PointOutT is pcl::PFHSignature125.
54  *
55  * \note If you use this code in any academic work, please cite:
56  *
57  * - R.B. Rusu, N. Blodow, Z.C. Marton, M. Beetz.
58  * Aligning Point Cloud Views using Persistent Feature Histograms.
59  * In Proceedings of the 21st IEEE/RSJ International Conference on Intelligent Robots and Systems (IROS),
60  * Nice, France, September 22-26 2008.
61  * - R.B. Rusu, Z.C. Marton, N. Blodow, M. Beetz.
62  * Learning Informative Point Classes for the Acquisition of Object Model Maps.
63  * In Proceedings of the 10th International Conference on Control, Automation, Robotics and Vision (ICARCV),
64  * Hanoi, Vietnam, December 17-20 2008.
65  *
66  * \attention
67  * The convention for PFH features is:
68  * - if a query point's nearest neighbors cannot be estimated, the PFH feature will be set to NaN
69  * (not a number)
70  * - it is impossible to estimate a PFH descriptor for a point that
71  * doesn't have finite 3D coordinates. Therefore, any point that contains
72  * NaN data on x, y, or z, will have its PFH feature property set to NaN.
73  *
74  * \note The code is stateful as we do not expect this class to be multicore parallelized. Please look at
75  * \ref FPFHEstimationOMP for examples on parallel implementations of the FPFH (Fast Point Feature Histogram).
76  *
77  * \author Radu B. Rusu
78  * \ingroup features
79  */
80  template <typename PointInT, typename PointNT, typename PointOutT = pcl::PFHSignature125>
81  class PFHEstimation : public FeatureFromNormals<PointInT, PointNT, PointOutT>
82  {
83  public:
84  using Ptr = shared_ptr<PFHEstimation<PointInT, PointNT, PointOutT> >;
85  using ConstPtr = shared_ptr<const PFHEstimation<PointInT, PointNT, PointOutT> >;
94 
97 
98  /** \brief Empty constructor.
99  * Sets \a use_cache_ to false, \a nr_subdiv_ to 5, and the internal maximum cache size to 1GB.
100  */
102 
103  d_pi_ (1.0f / (2.0f * static_cast<float> (M_PI))),
104  key_list_ (),
105  // Default 1GB memory size. Need to set it to something more conservative.
106  max_cache_size_ ((1ul*1024ul*1024ul*1024ul) / sizeof (std::pair<std::pair<int, int>, Eigen::Vector4f>))
107  {
108  feature_name_ = "PFHEstimation";
109  }
110 
111  /** \brief Set the maximum internal cache size. Defaults to 2GB worth of entries.
112  * \param[in] cache_size maximum cache size
113  */
114  inline void
115  setMaximumCacheSize (unsigned int cache_size)
116  {
117  max_cache_size_ = cache_size;
118  }
119 
120  /** \brief Get the maximum internal cache size. */
121  inline unsigned int
123  {
124  return (max_cache_size_);
125  }
126 
127  /** \brief Set whether to use an internal cache mechanism for removing redundant calculations or not.
128  *
129  * \note Depending on how the point cloud is ordered and how the nearest
130  * neighbors are estimated, using a cache could have a positive or a
131  * negative influence. Please test with and without a cache on your
132  * data, and choose whatever works best!
133  *
134  * See \ref setMaximumCacheSize for setting the maximum cache size
135  *
136  * \param[in] use_cache set to true to use the internal cache, false otherwise
137  */
138  inline void
139  setUseInternalCache (bool use_cache)
140  {
141  use_cache_ = use_cache;
142  }
143 
144  /** \brief Get whether the internal cache is used or not for computing the PFH features. */
145  inline bool
147  {
148  return (use_cache_);
149  }
150 
151  /** \brief Compute the 4-tuple representation containing the three angles and one distance between two points
152  * represented by Cartesian coordinates and normals.
153  * \note For explanations about the features, please see the literature mentioned above (the order of the
154  * features might be different).
155  * \param[in] cloud the dataset containing the XYZ Cartesian coordinates of the two points
156  * \param[in] normals the dataset containing the surface normals (assuming normalized vectors) at each point in cloud
157  * \param[in] p_idx the index of the first point (source)
158  * \param[in] q_idx the index of the second point (target)
159  * \param[out] f1 the first angular feature (angle between the projection of nq_idx and u)
160  * \param[out] f2 the second angular feature (angle between nq_idx and v)
161  * \param[out] f3 the third angular feature (angle between np_idx and |p_idx - q_idx|)
162  * \param[out] f4 the distance feature (p_idx - q_idx)
163  * \note For efficiency reasons, we assume that the point data passed to the method is finite.
164  */
165  bool
167  int p_idx, int q_idx, float &f1, float &f2, float &f3, float &f4);
168 
169  /** \brief Estimate the PFH (Point Feature Histograms) individual signatures of the three angular (f1, f2, f3)
170  * features for a given point based on its spatial neighborhood of 3D points with normals
171  * \param[in] cloud the dataset containing the XYZ Cartesian coordinates of the two points
172  * \param[in] normals the dataset containing the surface normals at each point in \a cloud
173  * \param[in] indices the k-neighborhood point indices in the dataset
174  * \param[in] nr_split the number of subdivisions for each angular feature interval
175  * \param[out] pfh_histogram the resultant (combinatorial) PFH histogram representing the feature at the query point
176  */
177  void
179  const pcl::Indices &indices, int nr_split, Eigen::VectorXf &pfh_histogram);
180 
181  protected:
182  /** \brief Estimate the Point Feature Histograms (PFH) descriptors at a set of points given by
183  * <setInputCloud (), setIndices ()> using the surface in setSearchSurface () and the spatial locator in
184  * setSearchMethod ()
185  * \param[out] output the resultant point cloud model dataset that contains the PFH feature estimates
186  */
187  void
188  computeFeature (PointCloudOut &output) override;
189 
190  /** \brief The number of subdivisions for each angular feature interval. */
191  int nr_subdiv_{5};
192 
193  /** \brief Placeholder for a point's PFH signature. */
194  Eigen::VectorXf pfh_histogram_;
195 
196  /** \brief Placeholder for a PFH 4-tuple. */
197  Eigen::Vector4f pfh_tuple_;
198 
199  /** \brief Placeholder for a histogram index. */
200  int f_index_[3];
201 
202  /** \brief Float constant = 1.0 / (2.0 * M_PI) */
203  float d_pi_;
204 
205  /** \brief Internal hashmap, used to optimize efficiency of redundant computations. */
206  std::map<std::pair<int, int>, Eigen::Vector4f, std::less<>, Eigen::aligned_allocator<std::pair<const std::pair<int, int>, Eigen::Vector4f> > > feature_map_;
207 
208  /** \brief Queue of pairs saved, used to constrain memory usage. */
209  std::queue<std::pair<int, int> > key_list_;
210 
211  /** \brief Maximum size of internal cache memory. */
212  unsigned int max_cache_size_;
213 
214  /** \brief Set to true to use the internal cache for removing redundant computations. */
215  bool use_cache_{false};
216  };
217 }
218 
219 #ifdef PCL_NO_PRECOMPILE
220 #include <pcl/features/impl/pfh.hpp>
221 #endif
Feature represents the base feature class.
Definition: feature.h:107
shared_ptr< Feature< PointInT, PointOutT > > Ptr
Definition: feature.h:114
std::string feature_name_
The feature name.
Definition: feature.h:220
shared_ptr< const Feature< PointInT, PointOutT > > ConstPtr
Definition: feature.h:115
PFHEstimation estimates the Point Feature Histogram (PFH) descriptor for a given point cloud dataset ...
Definition: pfh.h:82
void computePointPFHSignature(const pcl::PointCloud< PointInT > &cloud, const pcl::PointCloud< PointNT > &normals, const pcl::Indices &indices, int nr_split, Eigen::VectorXf &pfh_histogram)
Estimate the PFH (Point Feature Histograms) individual signatures of the three angular (f1,...
Definition: pfh.hpp:61
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:49
float d_pi_
Float constant = 1.0 / (2.0 * M_PI)
Definition: pfh.h:203
int f_index_[3]
Placeholder for a histogram index.
Definition: pfh.h:200
void computeFeature(PointCloudOut &output) override
Estimate the Point Feature Histograms (PFH) descriptors at a set of points given by <setInputCloud ()...
Definition: pfh.hpp:167
Eigen::VectorXf pfh_histogram_
Placeholder for a point's PFH signature.
Definition: pfh.h:194
Eigen::Vector4f pfh_tuple_
Placeholder for a PFH 4-tuple.
Definition: pfh.h:197
PFHEstimation()
Empty constructor.
Definition: pfh.h:101
int nr_subdiv_
The number of subdivisions for each angular feature interval.
Definition: pfh.h:191
void setUseInternalCache(bool use_cache)
Set whether to use an internal cache mechanism for removing redundant calculations or not.
Definition: pfh.h:139
bool getUseInternalCache()
Get whether the internal cache is used or not for computing the PFH features.
Definition: pfh.h:146
bool use_cache_
Set to true to use the internal cache for removing redundant computations.
Definition: pfh.h:215
unsigned int max_cache_size_
Maximum size of internal cache memory.
Definition: pfh.h:212
void setMaximumCacheSize(unsigned int cache_size)
Set the maximum internal cache size.
Definition: pfh.h:115
std::queue< std::pair< int, int > > key_list_
Queue of pairs saved, used to constrain memory usage.
Definition: pfh.h:209
typename Feature< PointInT, PointOutT >::PointCloudOut PointCloudOut
Definition: pfh.h:95
std::map< std::pair< int, int >, Eigen::Vector4f, std::less<>, Eigen::aligned_allocator< std::pair< const std::pair< int, int >, Eigen::Vector4f > > > feature_map_
Internal hashmap, used to optimize efficiency of redundant computations.
Definition: pfh.h:206
unsigned int getMaximumCacheSize()
Get the maximum internal cache size.
Definition: pfh.h:122
Defines all the PCL implemented PointT point type structures.
Definition: bfgs.h:10
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:133
#define M_PI
Definition: pcl_macros.h:201