Point Cloud Library (PCL)  1.9.1-dev
narf.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010, 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  */
38 
39 #pragma once
40 
41 #include <pcl/features/eigen.h>
42 #include <pcl/common/common_headers.h>
43 #include <pcl/point_representation.h>
44 
45 namespace pcl
46 {
47  // Forward declarations
48  class RangeImage;
49  struct InterestPoint;
50 
51 #define NARF_DEFAULT_SURFACE_PATCH_PIXEL_SIZE 10
52 
53  /**
54  * \brief NARF (Normal Aligned Radial Features) is a point feature descriptor type for 3D data.
55  * Please refer to pcl/features/narf_descriptor.h if you want the class derived from pcl Feature.
56  * See B. Steder, R. B. Rusu, K. Konolige, and W. Burgard
57  * Point Feature Extraction on 3D Range Scans Taking into Account Object Boundaries
58  * In Proc. of the IEEE Int. Conf. on Robotics &Automation (ICRA). 2011.
59  * \author Bastian Steder
60  * \ingroup features
61  */
62  class PCL_EXPORTS Narf
63  {
64  public:
65  // =====CONSTRUCTOR & DESTRUCTOR=====
66  //! Constructor
67  Narf();
68  //! Copy Constructor
69  Narf(const Narf& other);
70  //! Destructor
71  ~Narf();
72 
73  // =====Operators=====
74  //! Assignment operator
75  const Narf& operator=(const Narf& other);
76 
77  // =====STATIC=====
78  /** The maximum number of openmp threads that can be used in this class */
79  static int max_no_of_threads;
80 
81  /** Add features extracted at the given interest point and add them to the list */
82  static void
83  extractFromRangeImageAndAddToList (const RangeImage& range_image, const Eigen::Vector3f& interest_point, int descriptor_size,
84  float support_size, bool rotation_invariant, std::vector<Narf*>& feature_list);
85  /** Same as above */
86  static void
87  extractFromRangeImageAndAddToList (const RangeImage& range_image, float image_x, float image_y, int descriptor_size,
88  float support_size, bool rotation_invariant, std::vector<Narf*>& feature_list);
89  /** Get a list of features from the given interest points. */
90  static void
91  extractForInterestPoints (const RangeImage& range_image, const PointCloud<InterestPoint>& interest_points,
92  int descriptor_size, float support_size, bool rotation_invariant, std::vector<Narf*>& feature_list);
93  /** Extract an NARF for every point in the range image. */
94  static void
95  extractForEveryRangeImagePointAndAddToList (const RangeImage& range_image, int descriptor_size, float support_size,
96  bool rotation_invariant, std::vector<Narf*>& feature_list);
97 
98  // =====PUBLIC METHODS=====
99  /** Method to extract a NARF feature from a certain 3D point using a range image.
100  * pose determines the coordinate system of the feature, whereas it transforms a point from the world into the feature system.
101  * This means the interest point at which the feature is extracted will be the inverse application of pose onto (0,0,0).
102  * descriptor_size_ determines the size of the descriptor,
103  * support_size determines the support size of the feature, meaning the size in the world it covers */
104  bool
105  extractFromRangeImage (const RangeImage& range_image, const Eigen::Affine3f& pose, int descriptor_size, float support_size,
106  int surface_patch_world_size=NARF_DEFAULT_SURFACE_PATCH_PIXEL_SIZE);
107 
108  //! Same as above, but determines the transformation from the surface in the range image
109  bool
110  extractFromRangeImage (const RangeImage& range_image, float x, float y, int descriptor_size, float support_size);
111 
112  //! Same as above
113  bool
114  extractFromRangeImage (const RangeImage& range_image, const InterestPoint& interest_point, int descriptor_size, float support_size);
115 
116  //! Same as above
117  bool
118  extractFromRangeImage (const RangeImage& range_image, const Eigen::Vector3f& interest_point, int descriptor_size, float support_size);
119 
120  /** Same as above, but using the rotational invariant version by choosing the best extracted rotation around the normal.
121  * Use extractFromRangeImageAndAddToList if you want to enable the system to return multiple features with different rotations. */
122  bool
123  extractFromRangeImageWithBestRotation (const RangeImage& range_image, const Eigen::Vector3f& interest_point,
124  int descriptor_size, float support_size);
125 
126  /* Get the dominant rotations of the current descriptor
127  * \param rotations the returned rotations
128  * \param strength values describing how pronounced the corresponding rotations are
129  */
130  void
131  getRotations (std::vector<float>& rotations, std::vector<float>& strengths) const;
132 
133  /* Get the feature with a different rotation around the normal
134  * You are responsible for deleting the new features!
135  * \param range_image the source from which the feature is extracted
136  * \param rotations list of angles (in radians)
137  * \param rvps returned features
138  */
139  void
140  getRotatedVersions (const RangeImage& range_image, const std::vector<float>& rotations, std::vector<Narf*>& features) const;
141 
142  //! Calculate descriptor distance, value in [0,1] with 0 meaning identical and 1 every cell above maximum distance
143  inline float
144  getDescriptorDistance (const Narf& other) const;
145 
146  //! How many points on each beam of the gradient star are used to calculate the descriptor?
147  inline int
148  getNoOfBeamPoints () const { return (static_cast<int> (pcl_lrint (std::ceil (0.5f * float (surface_patch_pixel_size_))))); }
149 
150  //! Copy the descriptor and pose to the point struct Narf36
151  inline void
152  copyToNarf36 (Narf36& narf36) const;
153 
154  /** Write to file */
155  void
156  saveBinary (const std::string& filename) const;
157  /** Write to output stream */
158  void
159  saveBinary (std::ostream& file) const;
160 
161  /** Read from file */
162  void
163  loadBinary (const std::string& filename);
164  /** Read from input stream */
165  void
166  loadBinary (std::istream& file);
167 
168  //! Create the descriptor from the already set other members
169  bool
170  extractDescriptor (int descriptor_size);
171 
172  // =====GETTERS=====
173  //! Getter (const) for the descriptor
174  inline const float*
175  getDescriptor () const { return descriptor_;}
176  //! Getter for the descriptor
177  inline float*
178  getDescriptor () { return descriptor_;}
179  //! Getter (const) for the descriptor length
180  inline const int&
181  getDescriptorSize () const { return descriptor_size_;}
182  //! Getter for the descriptor length
183  inline int&
184  getDescriptorSize () { return descriptor_size_;}
185  //! Getter (const) for the position
186  inline const Eigen::Vector3f&
187  getPosition () const { return position_;}
188  //! Getter for the position
189  inline Eigen::Vector3f&
190  getPosition () { return position_;}
191  //! Getter (const) for the 6DoF pose
192  inline const Eigen::Affine3f&
193  getTransformation () const { return transformation_;}
194  //! Getter for the 6DoF pose
195  inline Eigen::Affine3f&
196  getTransformation () { return transformation_;}
197  //! Getter (const) for the pixel size of the surface patch (only one dimension)
198  inline const int&
199  getSurfacePatchPixelSize () const { return surface_patch_pixel_size_;}
200  //! Getter for the pixel size of the surface patch (only one dimension)
201  inline int&
202  getSurfacePatchPixelSize () { return surface_patch_pixel_size_;}
203  //! Getter (const) for the world size of the surface patch
204  inline const float&
205  getSurfacePatchWorldSize () const { return surface_patch_world_size_;}
206  //! Getter for the world size of the surface patch
207  inline float&
208  getSurfacePatchWorldSize () { return surface_patch_world_size_;}
209  //! Getter (const) for the rotation of the surface patch
210  inline const float&
211  getSurfacePatchRotation () const { return surface_patch_rotation_;}
212  //! Getter for the rotation of the surface patch
213  inline float&
214  getSurfacePatchRotation () { return surface_patch_rotation_;}
215  //! Getter (const) for the surface patch
216  inline const float*
217  getSurfacePatch () const { return surface_patch_;}
218  //! Getter for the surface patch
219  inline float*
220  getSurfacePatch () { return surface_patch_;}
221  //! Method to erase the surface patch and free the memory
222  inline void
223  freeSurfacePatch () { delete[] surface_patch_; surface_patch_=nullptr; surface_patch_pixel_size_=0; }
224 
225  // =====SETTERS=====
226  //! Setter for the descriptor
227  inline void
228  setDescriptor (float* descriptor) { descriptor_ = descriptor;}
229  //! Setter for the surface patch
230  inline void
231  setSurfacePatch (float* surface_patch) { surface_patch_ = surface_patch;}
232 
233  // =====PUBLIC MEMBER VARIABLES=====
234 
235  // =====PUBLIC STRUCTS=====
237  {
238  typedef Narf* PointT;
239  FeaturePointRepresentation(int nr_dimensions) { this->nr_dimensions_ = nr_dimensions; }
240  /** \brief Empty destructor */
242  void copyToFloatArray (const PointT& p, float* out) const override { memcpy(out, p->getDescriptor(), sizeof(*p->getDescriptor())*this->nr_dimensions_); }
243  };
244 
245  protected:
246  // =====PROTECTED METHODS=====
247  //! Reset al members to default values and free allocated memory
248  void
249  reset ();
250  //! Create a deep copy of other
251  void
252  deepCopy (const Narf& other);
253  //! Get the surface patch with a blur on it
254  float*
255  getBlurredSurfacePatch (int new_pixel_size, int blur_radius) const;
256 
257  /** Write header to output stream */
258  void
259  saveHeader (std::ostream& file) const;
260  /** Read header from input stream */
261  int
262  loadHeader (std::istream& file) const;
263 
264  // =====PROTECTED STATIC METHODS=====
265  static const std::string
266  getHeaderKeyword () { return "NARF"; }
267 
268  // =====PROTECTED STATIC VARIABLES=====
269  const static int VERSION = 1;
270 
271  // =====PROTECTED MEMBER VARIABLES=====
272  Eigen::Vector3f position_;
273  Eigen::Affine3f transformation_;
278  float* descriptor_;
280 
281  // =====STATIC PROTECTED=====
282 
283  public:
284  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
285  };
286 #undef NARF_DEFAULT_SURFACE_PATCH_PIXEL_SIZE
287 
288 } // end namespace pcl
289 
290 #include <pcl/features/impl/narf.hpp>
const float * getSurfacePatch() const
Getter (const) for the surface patch.
Definition: narf.h:217
Eigen::Affine3f & getTransformation()
Getter for the 6DoF pose.
Definition: narf.h:196
float & getSurfacePatchRotation()
Getter for the rotation of the surface patch.
Definition: narf.h:214
void loadBinary(Eigen::MatrixBase< Derived > const &matrix, std::istream &file)
Read a matrix from an input stream.
Definition: eigen.hpp:717
int getNoOfBeamPoints() const
How many points on each beam of the gradient star are used to calculate the descriptor?
Definition: narf.h:148
~FeaturePointRepresentation()
Empty destructor.
Definition: narf.h:241
Eigen::Affine3f transformation_
Definition: narf.h:273
RangeImage is derived from pcl/PointCloud and provides functionalities with focus on situations where...
Definition: range_image.h:54
void copyToFloatArray(const PointT &p, float *out) const override
Definition: narf.h:242
const Eigen::Affine3f & getTransformation() const
Getter (const) for the 6DoF pose.
Definition: narf.h:193
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:44
const float & getSurfacePatchWorldSize() const
Getter (const) for the world size of the surface patch.
Definition: narf.h:205
int & getDescriptorSize()
Getter for the descriptor length.
Definition: narf.h:184
int & getSurfacePatchPixelSize()
Getter for the pixel size of the surface patch (only one dimension)
Definition: narf.h:202
float surface_patch_world_size_
Definition: narf.h:276
const int & getSurfacePatchPixelSize() const
Getter (const) for the pixel size of the surface patch (only one dimension)
Definition: narf.h:199
void saveBinary(const Eigen::MatrixBase< Derived > &matrix, std::ostream &file)
Write a matrix to an output stream.
Definition: eigen.hpp:702
float * surface_patch_
Definition: narf.h:274
PointRepresentation provides a set of methods for converting a point structs/object into an n-dimensi...
int surface_patch_pixel_size_
Definition: narf.h:275
static const std::string getHeaderKeyword()
Definition: narf.h:266
FeaturePointRepresentation(int nr_dimensions)
Definition: narf.h:239
A point structure representing an interest point with Euclidean xyz coordinates, and an interest valu...
const Eigen::Vector3f & getPosition() const
Getter (const) for the position.
Definition: narf.h:187
NARF (Normal Aligned Radial Features) is a point feature descriptor type for 3D data.
Definition: narf.h:62
Eigen::Vector3f position_
Definition: narf.h:272
const int & getDescriptorSize() const
Getter (const) for the descriptor length.
Definition: narf.h:181
void setSurfacePatch(float *surface_patch)
Setter for the surface patch.
Definition: narf.h:231
PointCloud represents the base class in PCL for storing collections of 3D points. ...
float & getSurfacePatchWorldSize()
Getter for the world size of the surface patch.
Definition: narf.h:208
const float * getDescriptor() const
Getter (const) for the descriptor.
Definition: narf.h:175
static int max_no_of_threads
The maximum number of openmp threads that can be used in this class.
Definition: narf.h:79
const float & getSurfacePatchRotation() const
Getter (const) for the rotation of the surface patch.
Definition: narf.h:211
float * getSurfacePatch()
Getter for the surface patch.
Definition: narf.h:220
float surface_patch_rotation_
Definition: narf.h:277
void setDescriptor(float *descriptor)
Setter for the descriptor.
Definition: narf.h:228
A point structure representing the Narf descriptor.
Eigen::Vector3f & getPosition()
Getter for the position.
Definition: narf.h:190
void freeSurfacePatch()
Method to erase the surface patch and free the memory.
Definition: narf.h:223
float * descriptor_
Definition: narf.h:278
int descriptor_size_
Definition: narf.h:279
float * getDescriptor()
Getter for the descriptor.
Definition: narf.h:178