Point Cloud Library (PCL)  1.9.1-dev
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 #pragma once
41 
42 #include <pcl/filters/filter.h>
43 #include <boost/unordered_map.hpp>
44 
45 namespace pcl
46 {
47  /** \brief @b UniformSampling assembles a local 3D grid over a given PointCloud, and downsamples + filters the data.
48  *
49  * The @b UniformSampling class creates a *3D voxel grid* (think about a voxel
50  * grid as a set of tiny 3D boxes in space) over the input point cloud data.
51  * Then, in each *voxel* (i.e., 3D box), all the points present will be
52  * approximated (i.e., *downsampled*) with their centroid. This approach is
53  * a bit slower than approximating them with the center of the voxel, but it
54  * represents the underlying surface more accurately.
55  *
56  * \author Radu Bogdan Rusu
57  * \ingroup keypoints
58  */
59  template <typename PointT>
60  class UniformSampling: public Filter<PointT>
61  {
62  typedef typename Filter<PointT>::PointCloud PointCloud;
63 
68 
69  public:
70  typedef boost::shared_ptr<UniformSampling<PointT> > Ptr;
71  typedef boost::shared_ptr<const UniformSampling<PointT> > ConstPtr;
72 
73  /** \brief Empty constructor. */
74  UniformSampling (bool extract_removed_indices = false) :
75  Filter<PointT>(extract_removed_indices),
76  leaves_ (),
77  leaf_size_ (Eigen::Vector4f::Zero ()),
78  inverse_leaf_size_ (Eigen::Vector4f::Zero ()),
79  min_b_ (Eigen::Vector4i::Zero ()),
80  max_b_ (Eigen::Vector4i::Zero ()),
81  div_b_ (Eigen::Vector4i::Zero ()),
82  divb_mul_ (Eigen::Vector4i::Zero ()),
83  search_radius_ (0)
84  {
85  filter_name_ = "UniformSampling";
86  }
87 
88  /** \brief Destructor. */
90  {
91  leaves_.clear();
92  }
93 
94  /** \brief Set the 3D grid leaf size.
95  * \param radius the 3D grid leaf size
96  */
97  virtual inline void
98  setRadiusSearch (double radius)
99  {
100  leaf_size_[0] = leaf_size_[1] = leaf_size_[2] = static_cast<float> (radius);
101  // Avoid division errors
102  if (leaf_size_[3] == 0)
103  leaf_size_[3] = 1;
104  // Use multiplications instead of divisions
105  inverse_leaf_size_ = Eigen::Array4f::Ones () / leaf_size_.array ();
106  search_radius_ = radius;
107  }
108 
109  protected:
110  /** \brief Simple structure to hold an nD centroid and the number of points in a leaf. */
111  struct Leaf
112  {
113  Leaf () : idx (-1) { }
114  int idx;
115  };
116 
117  /** \brief The 3D grid leaves. */
118  boost::unordered_map<size_t, Leaf> leaves_;
119 
120  /** \brief The size of a leaf. */
121  Eigen::Vector4f leaf_size_;
122 
123  /** \brief Internal leaf sizes stored as 1/leaf_size_ for efficiency reasons. */
124  Eigen::Array4f inverse_leaf_size_;
125 
126  /** \brief The minimum and maximum bin coordinates, the number of divisions, and the division multiplier. */
127  Eigen::Vector4i min_b_, max_b_, div_b_, divb_mul_;
128 
129  /** \brief The nearest neighbors search radius for each point. */
131 
132  /** \brief Downsample a Point Cloud using a voxelized grid approach
133  * \param[out] output the resultant point cloud message
134  */
135  void
136  applyFilter (PointCloud &output) override;
137  };
138 }
139 
140 #ifdef PCL_NO_PRECOMPILE
141 #include <pcl/filters/impl/uniform_sampling.hpp>
142 #endif
Eigen::Vector4i max_b_
Eigen::Vector4i divb_mul_
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:44
Eigen::Vector4f leaf_size_
The size of a leaf.
boost::shared_ptr< const UniformSampling< PointT > > ConstPtr
boost::unordered_map< size_t, Leaf > leaves_
The 3D grid leaves.
Definition: bfgs.h:9
double search_radius_
The nearest neighbors search radius for each point.
void applyFilter(PointCloud &output) override
Downsample a Point Cloud using a voxelized grid approach.
Eigen::Vector4i div_b_
UniformSampling(bool extract_removed_indices=false)
Empty constructor.
Filter represents the base filter class.
Definition: filter.h:83
Eigen::Vector4i min_b_
The minimum and maximum bin coordinates, the number of divisions, and the division multiplier...
boost::shared_ptr< UniformSampling< PointT > > Ptr
PointCloud represents the base class in PCL for storing collections of 3D points. ...
~UniformSampling()
Destructor.
std::string filter_name_
The filter name.
Definition: filter.h:164
A point structure representing Euclidean xyz coordinates, and the RGB color.
virtual void setRadiusSearch(double radius)
Set the 3D grid leaf size.
Eigen::Array4f inverse_leaf_size_
Internal leaf sizes stored as 1/leaf_size_ for efficiency reasons.
Simple structure to hold an nD centroid and the number of points in a leaf.
UniformSampling assembles a local 3D grid over a given PointCloud, and downsamples + filters the data...