Point Cloud Library (PCL)  1.12.0
random_sample.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2009, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the copyright holder(s) nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  * $Id: extract_indices.h 1370 2011-06-19 01:06:01Z jspricke $
35  *
36  */
37 
38 #pragma once
39 
40 #include <pcl/filters/filter_indices.h>
41 #include <ctime>
42 #include <climits>
43 
44 namespace pcl
45 {
46  /** \brief @b RandomSample applies a random sampling with uniform probability.
47  * Based off Algorithm A from the paper "Faster Methods for Random Sampling"
48  * by Jeffrey Scott Vitter. The algorithm runs in O(N) and results in sorted
49  * indices
50  * http://www.ittc.ku.edu/~jsv/Papers/Vit84.sampling.pdf
51  * \author Justin Rosen
52  * \ingroup filters
53  */
54  template<typename PointT>
55  class RandomSample : public FilterIndices<PointT>
56  {
66 
68  using PointCloudPtr = typename PointCloud::Ptr;
70 
71  public:
72 
73  using Ptr = shared_ptr<RandomSample<PointT> >;
74  using ConstPtr = shared_ptr<const RandomSample<PointT> >;
75 
76  /** \brief Empty constructor. */
77  RandomSample (bool extract_removed_indices = false) :
78  FilterIndices<PointT> (extract_removed_indices),
79  sample_ (UINT_MAX),
80  seed_ (static_cast<unsigned int> (time (nullptr)))
81  {
82  filter_name_ = "RandomSample";
83  }
84 
85  /** \brief Set number of indices to be sampled.
86  * \param sample
87  */
88  inline void
89  setSample (unsigned int sample)
90  {
91  sample_ = sample;
92  }
93 
94  /** \brief Get the value of the internal \a sample parameter.
95  */
96  inline unsigned int
98  {
99  return (sample_);
100  }
101 
102  /** \brief Set seed of random function.
103  * \param seed
104  */
105  inline void
106  setSeed (unsigned int seed)
107  {
108  seed_ = seed;
109  }
110 
111  /** \brief Get the value of the internal \a seed parameter.
112  */
113  inline unsigned int
115  {
116  return (seed_);
117  }
118 
119  protected:
120 
121  /** \brief Number of indices that will be returned. */
122  unsigned int sample_;
123  /** \brief Random number seed. */
124  unsigned int seed_;
125 
126  /** \brief Sample of point indices
127  * \param indices the resultant point cloud indices
128  */
129  void
130  applyFilter (Indices &indices) override;
131 
132  /** \brief Return a random number fast using a LCG (Linear Congruential Generator) algorithm.
133  * See http://software.intel.com/en-us/articles/fast-random-number-generator-on-the-intel-pentiumr-4-processor/ for more information.
134  */
135  inline float
137  {
138  return (static_cast<float>(rand () / double (RAND_MAX)));
139  //return (((214013 * seed_ + 2531011) >> 16) & 0x7FFF);
140  }
141  };
142 
143  /** \brief @b RandomSample applies a random sampling with uniform probability.
144  * \author Justin Rosen
145  * \ingroup filters
146  */
147  template<>
148  class PCL_EXPORTS RandomSample<pcl::PCLPointCloud2> : public FilterIndices<pcl::PCLPointCloud2>
149  {
152 
156 
157  public:
158 
159  using Ptr = shared_ptr<RandomSample<pcl::PCLPointCloud2> >;
160  using ConstPtr = shared_ptr<const RandomSample<pcl::PCLPointCloud2> >;
161 
162  /** \brief Empty constructor. */
163  RandomSample () : sample_ (UINT_MAX), seed_ (static_cast<unsigned int> (time (nullptr)))
164  {
165  filter_name_ = "RandomSample";
166  }
167 
168  /** \brief Set number of indices to be sampled.
169  * \param sample
170  */
171  inline void
172  setSample (unsigned int sample)
173  {
174  sample_ = sample;
175  }
176 
177  /** \brief Get the value of the internal \a sample parameter.
178  */
179  inline unsigned int
181  {
182  return (sample_);
183  }
184 
185  /** \brief Set seed of random function.
186  * \param seed
187  */
188  inline void
189  setSeed (unsigned int seed)
190  {
191  seed_ = seed;
192  }
193 
194  /** \brief Get the value of the internal \a seed parameter.
195  */
196  inline unsigned int
198  {
199  return (seed_);
200  }
201 
202  protected:
203 
204  /** \brief Number of indices that will be returned. */
205  unsigned int sample_;
206  /** \brief Random number seed. */
207  unsigned int seed_;
208 
209  /** \brief Sample of point indices into a separate PointCloud
210  * \param output the resultant point cloud
211  */
212  void
213  applyFilter (PCLPointCloud2 &output) override;
214 
215  /** \brief Sample of point indices
216  * \param indices the resultant point cloud indices
217  */
218  void
219  applyFilter (Indices &indices) override;
220 
221  /** \brief Return a random number fast using a LCG (Linear Congruential Generator) algorithm.
222  * See http://software.intel.com/en-us/articles/fast-random-number-generator-on-the-intel-pentiumr-4-processor/ for more information.
223  */
224  inline float
226  {
227  return (static_cast<float> (rand () / double (RAND_MAX)));
228  }
229  };
230 }
231 
232 #ifdef PCL_NO_PRECOMPILE
233 #include <pcl/filters/impl/random_sample.hpp>
234 #endif
shared_ptr< const Filter< pcl::PCLPointCloud2 > > ConstPtr
Definition: filter.h:190
shared_ptr< Filter< pcl::PCLPointCloud2 > > Ptr
Definition: filter.h:189
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
FilterIndices represents the base class for filters that are about binary point removal.
PCLPointCloud2::Ptr PCLPointCloud2Ptr
Definition: pcl_base.h:185
PCLPointCloud2::ConstPtr PCLPointCloud2ConstPtr
Definition: pcl_base.h:186
typename PointCloud::Ptr PointCloudPtr
Definition: pcl_base.h:73
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: pcl_base.h:74
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: point_cloud.h:173
shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:413
shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:414
unsigned int seed_
Random number seed.
void applyFilter(PCLPointCloud2 &output) override
Sample of point indices into a separate PointCloud.
unsigned int sample_
Number of indices that will be returned.
unsigned int getSample()
Get the value of the internal sample parameter.
void setSeed(unsigned int seed)
Set seed of random function.
void setSample(unsigned int sample)
Set number of indices to be sampled.
void applyFilter(Indices &indices) override
Sample of point indices.
float unifRand()
Return a random number fast using a LCG (Linear Congruential Generator) algorithm.
unsigned int getSeed()
Get the value of the internal seed parameter.
RandomSample applies a random sampling with uniform probability.
Definition: random_sample.h:56
void setSeed(unsigned int seed)
Set seed of random function.
float unifRand()
Return a random number fast using a LCG (Linear Congruential Generator) algorithm.
void applyFilter(Indices &indices) override
Sample of point indices.
void setSample(unsigned int sample)
Set number of indices to be sampled.
Definition: random_sample.h:89
unsigned int getSample()
Get the value of the internal sample parameter.
Definition: random_sample.h:97
unsigned int sample_
Number of indices that will be returned.
unsigned int getSeed()
Get the value of the internal seed parameter.
unsigned int seed_
Random number seed.
RandomSample(bool extract_removed_indices=false)
Empty constructor.
Definition: random_sample.h:77
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:133
#define PCL_EXPORTS
Definition: pcl_macros.h:323
shared_ptr< ::pcl::PCLPointCloud2 > Ptr
shared_ptr< const ::pcl::PCLPointCloud2 > ConstPtr
A point structure representing Euclidean xyz coordinates, and the RGB color.