Point Cloud Library (PCL)  1.7.0
uniform_sampling.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2011, Willow Garage, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of Willow Garage, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  * $Id$
37  *
38  */
39 
40 #ifndef PCL_KEYPOINTS_UNIFORM_SAMPLING_H_
41 #define PCL_KEYPOINTS_UNIFORM_SAMPLING_H_
42 
43 #include <pcl/keypoints/keypoint.h>
44 #include <boost/unordered_map.hpp>
45 
46 namespace pcl
47 {
48  /** \brief @b UniformSampling assembles a local 3D grid over a given PointCloud, and downsamples + filters the data.
49  *
50  * The @b UniformSampling class creates a *3D voxel grid* (think about a voxel
51  * grid as a set of tiny 3D boxes in space) over the input point cloud data.
52  * Then, in each *voxel* (i.e., 3D box), all the points present will be
53  * approximated (i.e., *downsampled*) with their centroid. This approach is
54  * a bit slower than approximating them with the center of the voxel, but it
55  * represents the underlying surface more accurately.
56  *
57  * \author Radu Bogdan Rusu
58  * \ingroup keypoints
59  */
60  template <typename PointInT>
61  class UniformSampling: public Keypoint<PointInT, int>
62  {
63  typedef typename Keypoint<PointInT, int>::PointCloudIn PointCloudIn;
64  typedef typename Keypoint<PointInT, int>::PointCloudOut PointCloudOut;
65 
71 
72  public:
73  typedef boost::shared_ptr<UniformSampling<PointInT> > Ptr;
74  typedef boost::shared_ptr<const UniformSampling<PointInT> > ConstPtr;
75 
76  /** \brief Empty constructor. */
78  leaves_ (),
79  leaf_size_ (Eigen::Vector4f::Zero ()),
80  inverse_leaf_size_ (Eigen::Vector4f::Zero ()),
81  min_b_ (Eigen::Vector4i::Zero ()),
82  max_b_ (Eigen::Vector4i::Zero ()),
83  div_b_ (Eigen::Vector4i::Zero ()),
84  divb_mul_ (Eigen::Vector4i::Zero ())
85  {
86  name_ = "UniformSampling";
87  }
88 
89  /** \brief Destructor. */
90  virtual ~UniformSampling ()
91  {
92  leaves_.clear();
93  }
94 
95  /** \brief Set the 3D grid leaf size.
96  * \param radius the 3D grid leaf size
97  */
98  virtual inline void
99  setRadiusSearch (double radius)
100  {
101  leaf_size_[0] = leaf_size_[1] = leaf_size_[2] = static_cast<float> (radius);
102  // Avoid division errors
103  if (leaf_size_[3] == 0)
104  leaf_size_[3] = 1;
105  // Use multiplications instead of divisions
106  inverse_leaf_size_ = Eigen::Array4f::Ones () / leaf_size_.array ();
107  search_radius_ = radius;
108  }
109 
110  protected:
111  /** \brief Simple structure to hold an nD centroid and the number of points in a leaf. */
112  struct Leaf
113  {
114  Leaf () : idx (-1) { }
115  int idx;
116  };
117 
118  /** \brief The 3D grid leaves. */
119  boost::unordered_map<size_t, Leaf> leaves_;
120 
121  /** \brief The size of a leaf. */
122  Eigen::Vector4f leaf_size_;
123 
124  /** \brief Internal leaf sizes stored as 1/leaf_size_ for efficiency reasons. */
125  Eigen::Array4f inverse_leaf_size_;
126 
127  /** \brief The minimum and maximum bin coordinates, the number of divisions, and the division multiplier. */
128  Eigen::Vector4i min_b_, max_b_, div_b_, divb_mul_;
129 
130  /** \brief Downsample a Point Cloud using a voxelized grid approach
131  * \param output the resultant point cloud message
132  */
133  void
134  detectKeypoints (PointCloudOut &output);
135  };
136 }
137 
138 #ifdef PCL_NO_PRECOMPILE
139 #include <pcl/keypoints/impl/uniform_sampling.hpp>
140 #endif
141 
142 #endif //#ifndef PCL_KEYPOINTS_UNIFORM_SAMPLING_H_
143 
UniformSampling()
Empty constructor.
std::string name_
The key point detection method&#39;s name.
Definition: keypoint.h:167
Eigen::Vector4i max_b_
double search_radius_
The nearest neighbors search radius for each point.
Definition: keypoint.h:185
boost::shared_ptr< UniformSampling< PointInT > > Ptr
boost::unordered_map< size_t, Leaf > leaves_
The 3D grid leaves.
virtual void setRadiusSearch(double radius)
Set the 3D grid leaf size.
Keypoint represents the base class for key points.
Definition: keypoint.h:55
void detectKeypoints(PointCloudOut &output)
Downsample a Point Cloud using a voxelized grid approach.
Eigen::Vector4f leaf_size_
The size of a leaf.
Eigen::Vector4i divb_mul_
Eigen::Vector4i div_b_
virtual ~UniformSampling()
Destructor.
Simple structure to hold an nD centroid and the number of points in a leaf.
Eigen::Vector4i min_b_
The minimum and maximum bin coordinates, the number of divisions, and the division multiplier...
UniformSampling assembles a local 3D grid over a given PointCloud, and downsamples + filters the data...
boost::shared_ptr< const UniformSampling< PointInT > > ConstPtr
Eigen::Array4f inverse_leaf_size_
Internal leaf sizes stored as 1/leaf_size_ for efficiency reasons.