Point Cloud Library (PCL)  1.8.1-dev
rsd.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2009, 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_RSD_H_
42 #define PCL_RSD_H_
43 
44 #include <pcl/features/feature.h>
45 
46 namespace pcl
47 {
48  /** \brief Transform a list of 2D matrices into a point cloud containing the values in a vector (Histogram<N>).
49  * Can be used to transform the 2D histograms obtained in \ref RSDEstimation into a point cloud.
50  * @note The template paramter N should be (greater or) equal to the product of the number of rows and columns.
51  * \param[in] histograms2D the list of neighborhood 2D histograms
52  * \param[out] histogramsPC the dataset containing the linearized matrices
53  * \ingroup features
54  */
55  template <int N> void
56  getFeaturePointCloud (const std::vector<Eigen::MatrixXf, Eigen::aligned_allocator<Eigen::MatrixXf> > &histograms2D, PointCloud<Histogram<N> > &histogramsPC)
57  {
58  histogramsPC.points.resize (histograms2D.size ());
59  histogramsPC.width = histograms2D.size ();
60  histogramsPC.height = 1;
61  histogramsPC.is_dense = true;
62 
63  const int rows = histograms2D.at(0).rows();
64  const int cols = histograms2D.at(0).cols();
65 
66  typename PointCloud<Histogram<N> >::VectorType::iterator it = histogramsPC.points.begin ();
67  BOOST_FOREACH (Eigen::MatrixXf h, histograms2D)
68  {
69  Eigen::Map<Eigen::MatrixXf> histogram (&(it->histogram[0]), rows, cols);
70  histogram = h;
71  ++it;
72  }
73  }
74 
75  /** \brief Estimate the Radius-based Surface Descriptor (RSD) for a given point based on its spatial neighborhood of 3D points with normals
76  * \param[in] surface the dataset containing the XYZ points
77  * \param[in] normals the dataset containing the surface normals at each point in the dataset
78  * \param[in] indices the neighborhood point indices in the dataset (first point is used as the reference)
79  * \param[in] max_dist the upper bound for the considered distance interval
80  * \param[in] nr_subdiv the number of subdivisions for the considered distance interval
81  * \param[in] plane_radius maximum radius, above which everything can be considered planar
82  * \param[in] radii the output point of a type that should have r_min and r_max fields
83  * \param[in] compute_histogram if not false, the full neighborhood histogram is provided, usable as a point signature
84  * \ingroup features
85  */
86  template <typename PointInT, typename PointNT, typename PointOutT> Eigen::MatrixXf
87  computeRSD (boost::shared_ptr<const pcl::PointCloud<PointInT> > &surface, boost::shared_ptr<const pcl::PointCloud<PointNT> > &normals,
88  const std::vector<int> &indices, double max_dist,
89  int nr_subdiv, double plane_radius, PointOutT &radii, bool compute_histogram = false);
90 
91  /** \brief Estimate the Radius-based Surface Descriptor (RSD) for a given point based on its spatial neighborhood of 3D points with normals
92  * \param[in] normals the dataset containing the surface normals at each point in the dataset
93  * \param[in] indices the neighborhood point indices in the dataset (first point is used as the reference)
94  * \param[in] sqr_dists the squared distances from the first to all points in the neighborhood
95  * \param[in] max_dist the upper bound for the considered distance interval
96  * \param[in] nr_subdiv the number of subdivisions for the considered distance interval
97  * \param[in] plane_radius maximum radius, above which everything can be considered planar
98  * \param[in] radii the output point of a type that should have r_min and r_max fields
99  * \param[in] compute_histogram if not false, the full neighborhood histogram is provided, usable as a point signature
100  * \ingroup features
101  */
102  template <typename PointNT, typename PointOutT> Eigen::MatrixXf
103  computeRSD (boost::shared_ptr<const pcl::PointCloud<PointNT> > &normals,
104  const std::vector<int> &indices, const std::vector<float> &sqr_dists, double max_dist,
105  int nr_subdiv, double plane_radius, PointOutT &radii, bool compute_histogram = false);
106 
107  /** \brief @b RSDEstimation estimates the Radius-based Surface Descriptor (minimal and maximal radius of the local surface's curves)
108  * for a given point cloud dataset containing points and normals.
109  *
110  * @note If you use this code in any academic work, please cite:
111  *
112  * <ul>
113  * <li> Z.C. Marton , D. Pangercic , N. Blodow , J. Kleinehellefort, M. Beetz
114  * General 3D Modelling of Novel Objects from a Single View
115  * In Proceedings of the 2010 IEEE/RSJ International Conference on Intelligent Robots and Systems (IROS)
116  * Taipei, Taiwan, October 18-22, 2010
117  * </li>
118  * <li> Z.C. Marton, D. Pangercic, N. Blodow, Michael Beetz.
119  * Combined 2D-3D Categorization and Classification for Multimodal Perception Systems.
120  * In The International Journal of Robotics Research, Sage Publications
121  * pages 1378--1402, Volume 30, Number 11, September 2011.
122  * </li>
123  * </ul>
124  *
125  * @note The code is stateful as we do not expect this class to be multicore parallelized.
126  * \author Zoltan-Csaba Marton
127  * \ingroup features
128  */
129  template <typename PointInT, typename PointNT, typename PointOutT>
130  class RSDEstimation : public FeatureFromNormals<PointInT, PointNT, PointOutT>
131  {
132  public:
140 
143 
144  typedef typename boost::shared_ptr<RSDEstimation<PointInT, PointNT, PointOutT> > Ptr;
145  typedef typename boost::shared_ptr<const RSDEstimation<PointInT, PointNT, PointOutT> > ConstPtr;
146 
147 
148  /** \brief Empty constructor. */
149  RSDEstimation () : nr_subdiv_ (5), plane_radius_ (0.2), save_histograms_ (false)
150  {
151  feature_name_ = "RadiusSurfaceDescriptor";
152  };
153 
154  /** \brief Set the number of subdivisions for the considered distance interval.
155  * \param[in] nr_subdiv the number of subdivisions
156  */
157  inline void
158  setNrSubdivisions (int nr_subdiv) { nr_subdiv_ = nr_subdiv; }
159 
160  /** \brief Get the number of subdivisions for the considered distance interval.
161  * \return the number of subdivisions
162  */
163  inline int
164  getNrSubdivisions () const { return (nr_subdiv_); }
165 
166  /** \brief Set the maximum radius, above which everything can be considered planar.
167  * \note the order of magnitude should be around 10-20 times the search radius (0.2 works well for typical datasets).
168  * \note on accurate 3D data (e.g. openni sernsors) a search radius as low as 0.01 still gives good results.
169  * \param[in] plane_radius the new plane radius
170  */
171  inline void
172  setPlaneRadius (double plane_radius) { plane_radius_ = plane_radius; }
173 
174  /** \brief Get the maximum radius, above which everything can be considered planar.
175  * \return the plane_radius used
176  */
177  inline double
178  getPlaneRadius () const { return (plane_radius_); }
179 
180  /** \brief Disables the setting of the number of k nearest neighbors to use for the feature estimation. */
181  inline void
182  setKSearch (int)
183  {
184  PCL_ERROR ("[pcl::%s::setKSearch] RSD does not work with k nearest neighbor search. Use setRadiusSearch() instead!\n", getClassName ().c_str ());
185  }
186 
187  /** \brief Set whether the full distance-angle histograms should be saved.
188  * @note Obtain the list of histograms by getHistograms ()
189  * \param[in] save set to true if histograms should be saved
190  */
191  inline void
192  setSaveHistograms (bool save) { save_histograms_ = save; }
193 
194  /** \brief Returns whether the full distance-angle histograms are being saved.
195  * \return true if the histograms are being be saved
196  */
197  inline bool
198  getSaveHistograms () const { return (save_histograms_); }
199 
200  /** \brief Returns a pointer to the list of full distance-angle histograms for all points.
201  * \return the histogram being saved when computing RSD
202  */
203  inline boost::shared_ptr<std::vector<Eigen::MatrixXf, Eigen::aligned_allocator<Eigen::MatrixXf> > >
204  getHistograms () const { return (histograms_); }
205 
206  protected:
207 
208  /** \brief Estimate the estimates the Radius-based Surface Descriptor (RSD) at a set of points given by
209  * <setInputCloud (), setIndices ()> using the surface in setSearchSurface () and the spatial locator in
210  * setSearchMethod ()
211  * \param output the resultant point cloud model dataset that contains the RSD feature estimates (r_min and r_max values)
212  */
213  void
214  computeFeature (PointCloudOut &output);
215 
216  /** \brief The list of full distance-angle histograms for all points. */
217  boost::shared_ptr<std::vector<Eigen::MatrixXf, Eigen::aligned_allocator<Eigen::MatrixXf> > > histograms_;
218 
219  private:
220  /** \brief The number of subdivisions for the considered distance interval. */
221  int nr_subdiv_;
222 
223  /** \brief The maximum radius, above which everything can be considered planar. */
224  double plane_radius_;
225 
226  /** \brief Signals whether the full distance-angle histograms are being saved. */
227  bool save_histograms_;
228 
229  public:
230  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
231  };
232 }
233 
234 #ifdef PCL_NO_PRECOMPILE
235 #include <pcl/features/impl/rsd.hpp>
236 #endif
237 
238 #endif //#ifndef PCL_RSD_H_
boost::shared_ptr< std::vector< Eigen::MatrixXf, Eigen::aligned_allocator< Eigen::MatrixXf > > > getHistograms() const
Returns a pointer to the list of full distance-angle histograms for all points.
Definition: rsd.h:204
Feature< PointInT, PointOutT >::PointCloudOut PointCloudOut
Definition: rsd.h:141
Eigen::MatrixXf computeRSD(boost::shared_ptr< const pcl::PointCloud< PointInT > > &surface, boost::shared_ptr< const pcl::PointCloud< PointNT > > &normals, const std::vector< int > &indices, double max_dist, int nr_subdiv, double plane_radius, PointOutT &radii, bool compute_histogram=false)
Estimate the Radius-based Surface Descriptor (RSD) for a given point based on its spatial neighborhoo...
Definition: rsd.hpp:49
RSDEstimation()
Empty constructor.
Definition: rsd.h:149
std::string feature_name_
The feature name.
Definition: feature.h:222
boost::shared_ptr< RSDEstimation< PointInT, PointNT, PointOutT > > Ptr
Definition: rsd.h:144
void getFeaturePointCloud(const std::vector< Eigen::MatrixXf, Eigen::aligned_allocator< Eigen::MatrixXf > > &histograms2D, PointCloud< Histogram< N > > &histogramsPC)
Transform a list of 2D matrices into a point cloud containing the values in a vector (Histogram<N>)...
Definition: rsd.h:56
int getNrSubdivisions() const
Get the number of subdivisions for the considered distance interval.
Definition: rsd.h:164
void computeFeature(PointCloudOut &output)
Estimate the estimates the Radius-based Surface Descriptor (RSD) at a set of points given by <setInpu...
Definition: rsd.hpp:249
boost::shared_ptr< std::vector< Eigen::MatrixXf, Eigen::aligned_allocator< Eigen::MatrixXf > > > histograms_
The list of full distance-angle histograms for all points.
Definition: rsd.h:217
void setPlaneRadius(double plane_radius)
Set the maximum radius, above which everything can be considered planar.
Definition: rsd.h:172
const std::string & getClassName() const
Get a string representation of the name of this class.
Definition: feature.h:246
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:410
A point structure representing an N-D histogram.
RSDEstimation estimates the Radius-based Surface Descriptor (minimal and maximal radius of the local ...
Definition: rsd.h:130
bool getSaveHistograms() const
Returns whether the full distance-angle histograms are being saved.
Definition: rsd.h:198
PointCloud represents the base class in PCL for storing collections of 3D points. ...
void setKSearch(int)
Disables the setting of the number of k nearest neighbors to use for the feature estimation.
Definition: rsd.h:182
void setNrSubdivisions(int nr_subdiv)
Set the number of subdivisions for the considered distance interval.
Definition: rsd.h:158
Feature represents the base feature class.
Definition: feature.h:105
PCL_EXPORTS int save(const std::string &file_name, const pcl::PCLPointCloud2 &blob, unsigned precision=5)
Save point cloud data to a binary file when available else to ASCII.
double getPlaneRadius() const
Get the maximum radius, above which everything can be considered planar.
Definition: rsd.h:178
Feature< PointInT, PointOutT >::PointCloudIn PointCloudIn
Definition: rsd.h:142
boost::shared_ptr< const RSDEstimation< PointInT, PointNT, PointOutT > > ConstPtr
Definition: rsd.h:145
void setSaveHistograms(bool save)
Set whether the full distance-angle histograms should be saved.
Definition: rsd.h:192