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