Point Cloud Library (PCL)  1.13.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  nr_subdiv_ (5),
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  use_cache_ (false)
108  {
109  feature_name_ = "PFHEstimation";
110  };
111 
112  /** \brief Set the maximum internal cache size. Defaults to 2GB worth of entries.
113  * \param[in] cache_size maximum cache size
114  */
115  inline void
116  setMaximumCacheSize (unsigned int cache_size)
117  {
118  max_cache_size_ = cache_size;
119  }
120 
121  /** \brief Get the maximum internal cache size. */
122  inline unsigned int
124  {
125  return (max_cache_size_);
126  }
127 
128  /** \brief Set whether to use an internal cache mechanism for removing redundant calculations or not.
129  *
130  * \note Depending on how the point cloud is ordered and how the nearest
131  * neighbors are estimated, using a cache could have a positive or a
132  * negative influence. Please test with and without a cache on your
133  * data, and choose whatever works best!
134  *
135  * See \ref setMaximumCacheSize for setting the maximum cache size
136  *
137  * \param[in] use_cache set to true to use the internal cache, false otherwise
138  */
139  inline void
140  setUseInternalCache (bool use_cache)
141  {
142  use_cache_ = use_cache;
143  }
144 
145  /** \brief Get whether the internal cache is used or not for computing the PFH features. */
146  inline bool
148  {
149  return (use_cache_);
150  }
151 
152  /** \brief Compute the 4-tuple representation containing the three angles and one distance between two points
153  * represented by Cartesian coordinates and normals.
154  * \note For explanations about the features, please see the literature mentioned above (the order of the
155  * features might be different).
156  * \param[in] cloud the dataset containing the XYZ Cartesian coordinates of the two points
157  * \param[in] normals the dataset containing the surface normals (assuming normalized vectors) at each point in cloud
158  * \param[in] p_idx the index of the first point (source)
159  * \param[in] q_idx the index of the second point (target)
160  * \param[out] f1 the first angular feature (angle between the projection of nq_idx and u)
161  * \param[out] f2 the second angular feature (angle between nq_idx and v)
162  * \param[out] f3 the third angular feature (angle between np_idx and |p_idx - q_idx|)
163  * \param[out] f4 the distance feature (p_idx - q_idx)
164  * \note For efficiency reasons, we assume that the point data passed to the method is finite.
165  */
166  bool
168  int p_idx, int q_idx, float &f1, float &f2, float &f3, float &f4);
169 
170  /** \brief Estimate the PFH (Point Feature Histograms) individual signatures of the three angular (f1, f2, f3)
171  * features for a given point based on its spatial neighborhood of 3D points with normals
172  * \param[in] cloud the dataset containing the XYZ Cartesian coordinates of the two points
173  * \param[in] normals the dataset containing the surface normals at each point in \a cloud
174  * \param[in] indices the k-neighborhood point indices in the dataset
175  * \param[in] nr_split the number of subdivisions for each angular feature interval
176  * \param[out] pfh_histogram the resultant (combinatorial) PFH histogram representing the feature at the query point
177  */
178  void
180  const pcl::Indices &indices, int nr_split, Eigen::VectorXf &pfh_histogram);
181 
182  protected:
183  /** \brief Estimate the Point Feature Histograms (PFH) descriptors at a set of points given by
184  * <setInputCloud (), setIndices ()> using the surface in setSearchSurface () and the spatial locator in
185  * setSearchMethod ()
186  * \param[out] output the resultant point cloud model dataset that contains the PFH feature estimates
187  */
188  void
189  computeFeature (PointCloudOut &output) override;
190 
191  /** \brief The number of subdivisions for each angular feature interval. */
193 
194  /** \brief Placeholder for a point's PFH signature. */
195  Eigen::VectorXf pfh_histogram_;
196 
197  /** \brief Placeholder for a PFH 4-tuple. */
198  Eigen::Vector4f pfh_tuple_;
199 
200  /** \brief Placeholder for a histogram index. */
201  int f_index_[3];
202 
203  /** \brief Float constant = 1.0 / (2.0 * M_PI) */
204  float d_pi_;
205 
206  /** \brief Internal hashmap, used to optimize efficiency of redundant computations. */
207  std::map<std::pair<int, int>, Eigen::Vector4f, std::less<>, Eigen::aligned_allocator<std::pair<const std::pair<int, int>, Eigen::Vector4f> > > feature_map_;
208 
209  /** \brief Queue of pairs saved, used to constrain memory usage. */
210  std::queue<std::pair<int, int> > key_list_;
211 
212  /** \brief Maximum size of internal cache memory. */
213  unsigned int max_cache_size_;
214 
215  /** \brief Set to true to use the internal cache for removing redundant computations. */
217  };
218 }
219 
220 #ifdef PCL_NO_PRECOMPILE
221 #include <pcl/features/impl/pfh.hpp>
222 #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:204
int f_index_[3]
Placeholder for a histogram index.
Definition: pfh.h:201
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:195
Eigen::Vector4f pfh_tuple_
Placeholder for a PFH 4-tuple.
Definition: pfh.h:198
PFHEstimation()
Empty constructor.
Definition: pfh.h:101
int nr_subdiv_
The number of subdivisions for each angular feature interval.
Definition: pfh.h:192
void setUseInternalCache(bool use_cache)
Set whether to use an internal cache mechanism for removing redundant calculations or not.
Definition: pfh.h:140
bool getUseInternalCache()
Get whether the internal cache is used or not for computing the PFH features.
Definition: pfh.h:147
bool use_cache_
Set to true to use the internal cache for removing redundant computations.
Definition: pfh.h:216
unsigned int max_cache_size_
Maximum size of internal cache memory.
Definition: pfh.h:213
void setMaximumCacheSize(unsigned int cache_size)
Set the maximum internal cache size.
Definition: pfh.h:116
std::queue< std::pair< int, int > > key_list_
Queue of pairs saved, used to constrain memory usage.
Definition: pfh.h:210
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:207
unsigned int getMaximumCacheSize()
Get the maximum internal cache size.
Definition: pfh.h:123
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