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