Point Cloud Library (PCL)  1.14.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 
44 #include <unordered_map>
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 the closest point to the center of the voxel.
54  *
55  * \author Radu Bogdan Rusu
56  * \ingroup filters
57  */
58  template <typename PointT>
59  class UniformSampling: public Filter<PointT>
60  {
61  using PointCloud = typename Filter<PointT>::PointCloud;
62 
69 
70  public:
71  using Ptr = shared_ptr<UniformSampling<PointT> >;
72  using ConstPtr = shared_ptr<const UniformSampling<PointT> >;
73 
75 
76  /** \brief Empty constructor. */
77  UniformSampling (bool extract_removed_indices = false) :
78  Filter<PointT>(extract_removed_indices),
79  leaves_ (),
80  leaf_size_ (Eigen::Vector4f::Zero ()),
81  inverse_leaf_size_ (Eigen::Vector4f::Zero ()),
82  min_b_ (Eigen::Vector4i::Zero ()),
83  max_b_ (Eigen::Vector4i::Zero ()),
84  div_b_ (Eigen::Vector4i::Zero ()),
85  divb_mul_ (Eigen::Vector4i::Zero ())
86  {
87  filter_name_ = "UniformSampling";
88  }
89 
90  /** \brief Destructor. */
91  ~UniformSampling () override
92  {
93  leaves_.clear();
94  }
95 
96  /** \brief Set the 3D grid leaf size.
97  * \param radius the 3D grid leaf size
98  */
99  virtual inline void
100  setRadiusSearch (double radius)
101  {
102  leaf_size_[0] = leaf_size_[1] = leaf_size_[2] = static_cast<float> (radius);
103  // Avoid division errors
104  if (leaf_size_[3] == 0)
105  leaf_size_[3] = 1;
106  // Use multiplications instead of divisions
107  inverse_leaf_size_ = Eigen::Array4f::Ones () / leaf_size_.array ();
108  search_radius_ = radius;
109  }
110 
111  /** \brief Set the minimum number of points required for a voxel to be used.
112  * \param[in] min_points_per_voxel the minimum number of points for required for a voxel to be used
113  */
114  inline void
115  setMinimumPointsNumberPerVoxel (unsigned int min_points_per_voxel) { min_points_per_voxel_ = min_points_per_voxel; }
116 
117  /** \brief Return the minimum number of points required for a voxel to be used.
118  */
119  inline unsigned int
121 
122  protected:
123  /** \brief Simple structure to hold an nD centroid and the number of points in a leaf. */
124  struct Leaf
125  {
126  Leaf () = default;
127  int idx{-1};
128  unsigned int count{0};
129  };
130 
131  /** \brief The 3D grid leaves. */
132  std::unordered_map<std::size_t, Leaf> leaves_;
133 
134  /** \brief The size of a leaf. */
135  Eigen::Vector4f leaf_size_;
136 
137  /** \brief Internal leaf sizes stored as 1/leaf_size_ for efficiency reasons. */
138  Eigen::Array4f inverse_leaf_size_;
139 
140  /** \brief The minimum and maximum bin coordinates, the number of divisions, and the division multiplier. */
141  Eigen::Vector4i min_b_, max_b_, div_b_, divb_mul_;
142 
143  /** \brief The nearest neighbors search radius for each point. */
144  double search_radius_{0.0};
145 
146  /** \brief Minimum number of points per voxel. */
147  unsigned int min_points_per_voxel_{0};
148 
149  /** \brief Downsample a Point Cloud using a voxelized grid approach
150  * \param[out] output the resultant point cloud message
151  */
152  void
153  applyFilter (PointCloud &output) override;
154  };
155 }
156 
157 #ifdef PCL_NO_PRECOMPILE
158 #include <pcl/filters/impl/uniform_sampling.hpp>
159 #endif
Filter represents the base filter class.
Definition: filter.h:81
shared_ptr< Filter< PointT > > Ptr
Definition: filter.h:83
shared_ptr< const Filter< PointT > > ConstPtr
Definition: filter.h:84
std::string filter_name_
The filter name.
Definition: filter.h:158
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:173
UniformSampling assembles a local 3D grid over a given PointCloud, and downsamples + filters the data...
Eigen::Vector4i min_b_
The minimum and maximum bin coordinates, the number of divisions, and the division multiplier.
~UniformSampling() override
Destructor.
Eigen::Vector4f leaf_size_
The size of a leaf.
Eigen::Array4f inverse_leaf_size_
Internal leaf sizes stored as 1/leaf_size_ for efficiency reasons.
PCL_MAKE_ALIGNED_OPERATOR_NEW UniformSampling(bool extract_removed_indices=false)
Empty constructor.
double search_radius_
The nearest neighbors search radius for each point.
unsigned int min_points_per_voxel_
Minimum number of points per voxel.
unsigned int getMinimumPointsNumberPerVoxel() const
Return the minimum number of points required for a voxel to be used.
void setMinimumPointsNumberPerVoxel(unsigned int min_points_per_voxel)
Set the minimum number of points required for a voxel to be used.
std::unordered_map< std::size_t, Leaf > leaves_
The 3D grid leaves.
Eigen::Vector4i divb_mul_
virtual void setRadiusSearch(double radius)
Set the 3D grid leaf size.
void applyFilter(PointCloud &output) override
Downsample a Point Cloud using a voxelized grid approach.
Eigen::Vector4i div_b_
Eigen::Vector4i max_b_
#define PCL_MAKE_ALIGNED_OPERATOR_NEW
Macro to signal a class requires a custom allocator.
Definition: memory.h:63
Definition: bfgs.h:10
A point structure representing Euclidean xyz coordinates, and the RGB color.
Simple structure to hold an nD centroid and the number of points in a leaf.