Point Cloud Library (PCL)  1.14.0-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 pcl::Indices &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 pcl::Indices &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  * \tparam PointOutT Suggested type is `pcl::PrincipalRadiiRSD`
130  */
131  template <typename PointInT, typename PointNT, typename PointOutT>
132  class RSDEstimation : public FeatureFromNormals<PointInT, PointNT, PointOutT>
133  {
134  public:
142 
145 
146  using Ptr = shared_ptr<RSDEstimation<PointInT, PointNT, PointOutT> >;
147  using ConstPtr = shared_ptr<const RSDEstimation<PointInT, PointNT, PointOutT> >;
148 
149 
150  /** \brief Empty constructor. */
152  {
153  feature_name_ = "RadiusSurfaceDescriptor";
154  }
155 
156  /** \brief Set the number of subdivisions for the considered distance interval.
157  * \param[in] nr_subdiv the number of subdivisions
158  */
159  inline void
160  setNrSubdivisions (int nr_subdiv) { nr_subdiv_ = nr_subdiv; }
161 
162  /** \brief Get the number of subdivisions for the considered distance interval.
163  * \return the number of subdivisions
164  */
165  inline int
166  getNrSubdivisions () const { return (nr_subdiv_); }
167 
168  /** \brief Set the maximum radius, above which everything can be considered planar.
169  * \note the order of magnitude should be around 10-20 times the search radius (0.2 works well for typical datasets).
170  * \note on accurate 3D data (e.g. openni sensors) a search radius as low as 0.01 still gives good results.
171  * \param[in] plane_radius the new plane radius
172  */
173  inline void
174  setPlaneRadius (double plane_radius) { plane_radius_ = plane_radius; }
175 
176  /** \brief Get the maximum radius, above which everything can be considered planar.
177  * \return the plane_radius used
178  */
179  inline double
180  getPlaneRadius () const { return (plane_radius_); }
181 
182  /** \brief Disables the setting of the number of k nearest neighbors to use for the feature estimation. */
183  inline void
184  setKSearch (int)
185  {
186  PCL_ERROR ("[pcl::%s::setKSearch] RSD does not work with k nearest neighbor search. Use setRadiusSearch() instead!\n", getClassName ().c_str ());
187  }
188 
189  /** \brief Set whether the full distance-angle histograms should be saved.
190  * @note Obtain the list of histograms by getHistograms ()
191  * \param[in] save set to true if histograms should be saved
192  */
193  inline void
194  setSaveHistograms (bool save) { save_histograms_ = save; }
195 
196  /** \brief Returns whether the full distance-angle histograms are being saved.
197  * \return true if the histograms are being be saved
198  */
199  inline bool
200  getSaveHistograms () const { return (save_histograms_); }
201 
202  /** \brief Returns a pointer to the list of full distance-angle histograms for all points.
203  * \return the histogram being saved when computing RSD
204  */
205  inline shared_ptr<std::vector<Eigen::MatrixXf, Eigen::aligned_allocator<Eigen::MatrixXf> > >
206  getHistograms () const { return (histograms_); }
207 
208  protected:
209 
210  /** \brief Estimate the estimates the Radius-based Surface Descriptor (RSD) at a set of points given by
211  * <setInputCloud (), setIndices ()> using the surface in setSearchSurface () and the spatial locator in
212  * setSearchMethod ()
213  * \param output the resultant point cloud model dataset that contains the RSD feature estimates (r_min and r_max values)
214  */
215  void
216  computeFeature (PointCloudOut &output) override;
217 
218  /** \brief The list of full distance-angle histograms for all points. */
219  shared_ptr<std::vector<Eigen::MatrixXf, Eigen::aligned_allocator<Eigen::MatrixXf> > > histograms_;
220 
221  private:
222  /** \brief The number of subdivisions for the considered distance interval. */
223  int nr_subdiv_{5};
224 
225  /** \brief The maximum radius, above which everything can be considered planar. */
226  double plane_radius_{0.2};
227 
228  /** \brief Signals whether the full distance-angle histograms are being saved. */
229  bool save_histograms_{false};
230 
231  public:
233  };
234 }
235 
236 #ifdef PCL_NO_PRECOMPILE
237 #include <pcl/features/impl/rsd.hpp>
238 #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
const std::string & getClassName() const
Get a string representation of the name of this class.
Definition: feature.h:244
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:173
iterator begin() noexcept
Definition: point_cloud.h:429
RSDEstimation estimates the Radius-based Surface Descriptor (minimal and maximal radius of the local ...
Definition: rsd.h:133
void setNrSubdivisions(int nr_subdiv)
Set the number of subdivisions for the considered distance interval.
Definition: rsd.h:160
void setKSearch(int)
Disables the setting of the number of k nearest neighbors to use for the feature estimation.
Definition: rsd.h:184
typename Feature< PointInT, PointOutT >::PointCloudOut PointCloudOut
Definition: rsd.h:143
int getNrSubdivisions() const
Get the number of subdivisions for the considered distance interval.
Definition: rsd.h:166
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
RSDEstimation()
Empty constructor.
Definition: rsd.h:151
bool getSaveHistograms() const
Returns whether the full distance-angle histograms are being saved.
Definition: rsd.h:200
double getPlaneRadius() const
Get the maximum radius, above which everything can be considered planar.
Definition: rsd.h:180
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:206
void setSaveHistograms(bool save)
Set whether the full distance-angle histograms should be saved.
Definition: rsd.h:194
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:219
void setPlaneRadius(double plane_radius)
Set the maximum radius, above which everything can be considered planar.
Definition: rsd.h:174
#define PCL_MAKE_ALIGNED_OPERATOR_NEW
Macro to signal a class requires a custom allocator.
Definition: memory.h:63
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
Eigen::MatrixXf computeRSD(const pcl::PointCloud< PointInT > &surface, const pcl::PointCloud< PointNT > &normals, const pcl::Indices &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_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.
Defines functions, macros and traits for allocating and using memory.
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:133
Defines all the PCL and non-PCL macros used.
A point structure representing an N-D histogram.