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 (const pcl::PointCloud<PointInT> &surface, 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  template <typename PointInT, typename PointNT, typename PointOutT>
91  [[deprecated("use computeRSD() overload that takes input point clouds by const reference")]]
92  Eigen::MatrixXf
94  const std::vector<int> &indices, double max_dist,
95  int nr_subdiv, double plane_radius, PointOutT &radii, bool compute_histogram = false)
96  {
97  return computeRSD (*surface, *normals, indices, max_dist, nr_subdiv, plane_radius, radii, compute_histogram);
98  }
99 
100  /** \brief Estimate the Radius-based Surface Descriptor (RSD) for a given point based on its spatial neighborhood of 3D points with normals
101  * \param[in] normals the dataset containing the surface normals at each point in the dataset
102  * \param[in] indices the neighborhood point indices in the dataset (first point is used as the reference)
103  * \param[in] sqr_dists the squared distances from the first to all points in the neighborhood
104  * \param[in] max_dist the upper bound for the considered distance interval
105  * \param[in] nr_subdiv the number of subdivisions for the considered distance interval
106  * \param[in] plane_radius maximum radius, above which everything can be considered planar
107  * \param[in] radii the output point of a type that should have r_min and r_max fields
108  * \param[in] compute_histogram if not false, the full neighborhood histogram is provided, usable as a point signature
109  * \ingroup features
110  */
111  template <typename PointNT, typename PointOutT> Eigen::MatrixXf
112  computeRSD (const pcl::PointCloud<PointNT> &normals,
113  const std::vector<int> &indices, const std::vector<float> &sqr_dists, double max_dist,
114  int nr_subdiv, double plane_radius, PointOutT &radii, bool compute_histogram = false);
115 
116  template <typename PointNT, typename PointOutT>
117  [[deprecated("use computeRSD() overload that takes input point cloud by const reference")]]
118  Eigen::MatrixXf
120  const std::vector<int> &indices, const std::vector<float> &sqr_dists, double max_dist,
121  int nr_subdiv, double plane_radius, PointOutT &radii, bool compute_histogram = false)
122  {
123  return computeRSD (*normals, indices, sqr_dists, max_dist, nr_subdiv, plane_radius, radii, compute_histogram);
124  }
125 
126  /** \brief @b RSDEstimation estimates the Radius-based Surface Descriptor (minimal and maximal radius of the local surface's curves)
127  * for a given point cloud dataset containing points and normals.
128  *
129  * @note If you use this code in any academic work, please cite:
130  *
131  * <ul>
132  * <li> Z.C. Marton , D. Pangercic , N. Blodow , J. Kleinehellefort, M. Beetz
133  * General 3D Modelling of Novel Objects from a Single View
134  * In Proceedings of the 2010 IEEE/RSJ International Conference on Intelligent Robots and Systems (IROS)
135  * Taipei, Taiwan, October 18-22, 2010
136  * </li>
137  * <li> Z.C. Marton, D. Pangercic, N. Blodow, Michael Beetz.
138  * Combined 2D-3D Categorization and Classification for Multimodal Perception Systems.
139  * In The International Journal of Robotics Research, Sage Publications
140  * pages 1378--1402, Volume 30, Number 11, September 2011.
141  * </li>
142  * </ul>
143  *
144  * @note The code is stateful as we do not expect this class to be multicore parallelized.
145  * \author Zoltan-Csaba Marton
146  * \ingroup features
147  */
148  template <typename PointInT, typename PointNT, typename PointOutT>
149  class RSDEstimation : public FeatureFromNormals<PointInT, PointNT, PointOutT>
150  {
151  public:
159 
162 
163  using Ptr = boost::shared_ptr<RSDEstimation<PointInT, PointNT, PointOutT> >;
164  using ConstPtr = boost::shared_ptr<const RSDEstimation<PointInT, PointNT, PointOutT> >;
165 
166 
167  /** \brief Empty constructor. */
168  RSDEstimation () : nr_subdiv_ (5), plane_radius_ (0.2), save_histograms_ (false)
169  {
170  feature_name_ = "RadiusSurfaceDescriptor";
171  };
172 
173  /** \brief Set the number of subdivisions for the considered distance interval.
174  * \param[in] nr_subdiv the number of subdivisions
175  */
176  inline void
177  setNrSubdivisions (int nr_subdiv) { nr_subdiv_ = nr_subdiv; }
178 
179  /** \brief Get the number of subdivisions for the considered distance interval.
180  * \return the number of subdivisions
181  */
182  inline int
183  getNrSubdivisions () const { return (nr_subdiv_); }
184 
185  /** \brief Set the maximum radius, above which everything can be considered planar.
186  * \note the order of magnitude should be around 10-20 times the search radius (0.2 works well for typical datasets).
187  * \note on accurate 3D data (e.g. openni sernsors) a search radius as low as 0.01 still gives good results.
188  * \param[in] plane_radius the new plane radius
189  */
190  inline void
191  setPlaneRadius (double plane_radius) { plane_radius_ = plane_radius; }
192 
193  /** \brief Get the maximum radius, above which everything can be considered planar.
194  * \return the plane_radius used
195  */
196  inline double
197  getPlaneRadius () const { return (plane_radius_); }
198 
199  /** \brief Disables the setting of the number of k nearest neighbors to use for the feature estimation. */
200  inline void
201  setKSearch (int)
202  {
203  PCL_ERROR ("[pcl::%s::setKSearch] RSD does not work with k nearest neighbor search. Use setRadiusSearch() instead!\n", getClassName ().c_str ());
204  }
205 
206  /** \brief Set whether the full distance-angle histograms should be saved.
207  * @note Obtain the list of histograms by getHistograms ()
208  * \param[in] save set to true if histograms should be saved
209  */
210  inline void
211  setSaveHistograms (bool save) { save_histograms_ = save; }
212 
213  /** \brief Returns whether the full distance-angle histograms are being saved.
214  * \return true if the histograms are being be saved
215  */
216  inline bool
217  getSaveHistograms () const { return (save_histograms_); }
218 
219  /** \brief Returns a pointer to the list of full distance-angle histograms for all points.
220  * \return the histogram being saved when computing RSD
221  */
222  inline boost::shared_ptr<std::vector<Eigen::MatrixXf, Eigen::aligned_allocator<Eigen::MatrixXf> > >
223  getHistograms () const { return (histograms_); }
224 
225  protected:
226 
227  /** \brief Estimate the estimates the Radius-based Surface Descriptor (RSD) at a set of points given by
228  * <setInputCloud (), setIndices ()> using the surface in setSearchSurface () and the spatial locator in
229  * setSearchMethod ()
230  * \param output the resultant point cloud model dataset that contains the RSD feature estimates (r_min and r_max values)
231  */
232  void
233  computeFeature (PointCloudOut &output) override;
234 
235  /** \brief The list of full distance-angle histograms for all points. */
236  boost::shared_ptr<std::vector<Eigen::MatrixXf, Eigen::aligned_allocator<Eigen::MatrixXf> > > histograms_;
237 
238  private:
239  /** \brief The number of subdivisions for the considered distance interval. */
240  int nr_subdiv_;
241 
242  /** \brief The maximum radius, above which everything can be considered planar. */
243  double plane_radius_;
244 
245  /** \brief Signals whether the full distance-angle histograms are being saved. */
246  bool save_histograms_;
247 
248  public:
249  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
250  };
251 }
252 
253 #ifdef PCL_NO_PRECOMPILE
254 #include <pcl/features/impl/rsd.hpp>
255 #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:223
boost::shared_ptr< const Feature< PointInT, PointOutT > > ConstPtr
Definition: feature.h:113
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:409
RSDEstimation()
Empty constructor.
Definition: rsd.h:168
std::string feature_name_
The feature name.
Definition: feature.h:221
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:183
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:236
void setPlaneRadius(double plane_radius)
Set the maximum radius, above which everything can be considered planar.
Definition: rsd.h:191
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:149
bool getSaveHistograms() const
Returns whether the full distance-angle histograms are being saved.
Definition: rsd.h:217
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:201
Eigen::MatrixXf computeRSD(const pcl::PointCloud< PointInT > &surface, 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
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:177
boost::shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:428
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:197
boost::shared_ptr< Feature< PointInT, PointOutT > > Ptr
Definition: feature.h:112
void setSaveHistograms(bool save)
Set whether the full distance-angle histograms should be saved.
Definition: rsd.h:211