Point Cloud Library (PCL)  1.11.1
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 (std::vector<int> &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 (std::vector<int> &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< ::pcl::PCLPointCloud2 > Ptr
shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:429
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.
shared_ptr< const ::pcl::PCLPointCloud2 > ConstPtr
RandomSample(bool extract_removed_indices=false)
Empty constructor.
Definition: random_sample.h:77
shared_ptr< Filter< pcl::PCLPointCloud2 > > Ptr
Definition: filter.h:192
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.
shared_ptr< Filter< PointT > > Ptr
Definition: filter.h:86
typename PointCloud::Ptr PointCloudPtr
Definition: pcl_base.h:76
PointCloud represents the base class in PCL for storing collections of 3D points. ...
Definition: distances.h:55
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(std::vector< int > &indices) override
Sample of point indices.
PCLPointCloud2::ConstPtr PCLPointCloud2ConstPtr
Definition: pcl_base.h:189
unsigned int seed_
Random number seed.
shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:430
unsigned int getSample()
Get the value of the internal sample parameter.
std::string filter_name_
The filter name.
Definition: filter.h:161
shared_ptr< const Filter< pcl::PCLPointCloud2 > > ConstPtr
Definition: filter.h:193
float unifRand()
Return a random number fast using a LCG (Linear Congruential Generator) algorithm.
shared_ptr< const Filter< PointT > > ConstPtr
Definition: filter.h:87
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
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:328
PCLPointCloud2::Ptr PCLPointCloud2Ptr
Definition: pcl_base.h:188
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: pcl_base.h:77