Point Cloud Library (PCL)  1.9.1-dev
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 = boost::shared_ptr<RandomSample<PointT> >;
74  using ConstPtr = boost::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 into a separate PointCloud
127  * \param output the resultant point cloud
128  */
129  void
130  applyFilter (PointCloud &output) override;
131 
132  /** \brief Sample of point indices
133  * \param indices the resultant point cloud indices
134  */
135  void
136  applyFilter (std::vector<int> &indices) override;
137 
138  /** \brief Return a random number fast using a LCG (Linear Congruential Generator) algorithm.
139  * See http://software.intel.com/en-us/articles/fast-random-number-generator-on-the-intel-pentiumr-4-processor/ for more information.
140  */
141  inline float
143  {
144  return (static_cast<float>(rand () / double (RAND_MAX)));
145  //return (((214013 * seed_ + 2531011) >> 16) & 0x7FFF);
146  }
147  };
148 
149  /** \brief @b RandomSample applies a random sampling with uniform probability.
150  * \author Justin Rosen
151  * \ingroup filters
152  */
153  template<>
154  class PCL_EXPORTS RandomSample<pcl::PCLPointCloud2> : public FilterIndices<pcl::PCLPointCloud2>
155  {
158 
162 
163  public:
164 
165  using Ptr = boost::shared_ptr<RandomSample<pcl::PCLPointCloud2> >;
166  using ConstPtr = boost::shared_ptr<const RandomSample<pcl::PCLPointCloud2> >;
167 
168  /** \brief Empty constructor. */
169  RandomSample () : sample_ (UINT_MAX), seed_ (static_cast<unsigned int> (time (nullptr)))
170  {
171  filter_name_ = "RandomSample";
172  }
173 
174  /** \brief Set number of indices to be sampled.
175  * \param sample
176  */
177  inline void
178  setSample (unsigned int sample)
179  {
180  sample_ = sample;
181  }
182 
183  /** \brief Get the value of the internal \a sample parameter.
184  */
185  inline unsigned int
187  {
188  return (sample_);
189  }
190 
191  /** \brief Set seed of random function.
192  * \param seed
193  */
194  inline void
195  setSeed (unsigned int seed)
196  {
197  seed_ = seed;
198  }
199 
200  /** \brief Get the value of the internal \a seed parameter.
201  */
202  inline unsigned int
204  {
205  return (seed_);
206  }
207 
208  protected:
209 
210  /** \brief Number of indices that will be returned. */
211  unsigned int sample_;
212  /** \brief Random number seed. */
213  unsigned int seed_;
214 
215  /** \brief Sample of point indices into a separate PointCloud
216  * \param output the resultant point cloud
217  */
218  void
219  applyFilter (PCLPointCloud2 &output) override;
220 
221  /** \brief Sample of point indices
222  * \param indices the resultant point cloud indices
223  */
224  void
225  applyFilter (std::vector<int> &indices) override;
226 
227  /** \brief Return a random number fast using a LCG (Linear Congruential Generator) algorithm.
228  * See http://software.intel.com/en-us/articles/fast-random-number-generator-on-the-intel-pentiumr-4-processor/ for more information.
229  */
230  inline float
232  {
233  return (static_cast<float> (rand () / double (RAND_MAX)));
234  }
235  };
236 }
237 
238 #ifdef PCL_NO_PRECOMPILE
239 #include <pcl/filters/impl/random_sample.hpp>
240 #endif
boost::shared_ptr< const Filter< PointT > > ConstPtr
Definition: filter.h:90
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 getSeed()
Get the value of the internal seed parameter.
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
boost::shared_ptr< PCLPointCloud2 const > PCLPointCloud2ConstPtr
Definition: pcl_base.h:190
RandomSample(bool extract_removed_indices=false)
Empty constructor.
Definition: random_sample.h:77
unsigned int sample_
Number of indices that will be returned.
unsigned int seed_
Random number seed.
FilterIndices represents the base class for filters that are about binary point removal.
boost::shared_ptr< Filter< pcl::PCLPointCloud2 > > Ptr
Definition: filter.h:195
boost::shared_ptr< const Filter< pcl::PCLPointCloud2 > > ConstPtr
Definition: filter.h:196
boost::shared_ptr< PCLPointCloud2 > PCLPointCloud2Ptr
Definition: pcl_base.h:189
void applyFilter(PointCloud &output) override
Sample of point indices into a separate PointCloud.
boost::shared_ptr< ::pcl::PCLPointCloud2 > Ptr
typename PointCloud::Ptr PointCloudPtr
Definition: pcl_base.h:73
boost::shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:444
PointCloud represents the base class in PCL for storing collections of 3D points. ...
void setSeed(unsigned int seed)
Set seed of random function.
float unifRand()
Return a random number fast using a LCG (Linear Congruential Generator) algorithm.
boost::shared_ptr< Filter< PointT > > Ptr
Definition: filter.h:89
boost::shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:445
unsigned int seed_
Random number seed.
unsigned int getSample()
Get the value of the internal sample parameter.
std::string filter_name_
The filter name.
Definition: filter.h:164
float unifRand()
Return a random number fast using a LCG (Linear Congruential Generator) algorithm.
A point structure representing Euclidean xyz coordinates, and the RGB color.
unsigned int sample_
Number of indices that will be returned.
RandomSample applies a random sampling with uniform probability.
Definition: random_sample.h:55
boost::shared_ptr< const ::pcl::PCLPointCloud2 > ConstPtr
void setSample(unsigned int sample)
Set number of indices to be sampled.
unsigned int getSeed()
Get the value of the internal seed parameter.
void setSeed(unsigned int seed)
Set seed of random function.
#define PCL_EXPORTS
Definition: pcl_macros.h:227
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: pcl_base.h:74