Point Cloud Library (PCL)  1.7.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 #ifndef PCL_FILTERS_RANDOM_SUBSAMPLE_H_
39 #define PCL_FILTERS_RANDOM_SUBSAMPLE_H_
40 
41 #include <pcl/filters/filter_indices.h>
42 #include <time.h>
43 #include <limits.h>
44 
45 namespace pcl
46 {
47  /** \brief @b RandomSample applies a random sampling with uniform probability.
48  * Based off Algorithm A from the paper "Faster Methods for Random Sampling"
49  * by Jeffrey Scott Vitter. The algorithm runs in O(N) and results in sorted
50  * indices
51  * http://www.ittc.ku.edu/~jsv/Papers/Vit84.sampling.pdf
52  * \author Justin Rosen
53  * \ingroup filters
54  */
55  template<typename PointT>
56  class RandomSample : public FilterIndices<PointT>
57  {
67 
69  typedef typename PointCloud::Ptr PointCloudPtr;
71 
72  public:
73 
74  typedef boost::shared_ptr< RandomSample<PointT> > Ptr;
75  typedef boost::shared_ptr< const RandomSample<PointT> > ConstPtr;
76 
77  /** \brief Empty constructor. */
78  RandomSample (bool extract_removed_indices = false) :
79  FilterIndices<PointT> (extract_removed_indices),
80  sample_ (UINT_MAX),
81  seed_ (static_cast<unsigned int> (time (NULL)))
82  {
83  filter_name_ = "RandomSample";
84  }
85 
86  /** \brief Set number of indices to be sampled.
87  * \param sample
88  */
89  inline void
90  setSample (unsigned int sample)
91  {
92  sample_ = sample;
93  }
94 
95  /** \brief Get the value of the internal \a sample parameter.
96  */
97  inline unsigned int
99  {
100  return (sample_);
101  }
102 
103  /** \brief Set seed of random function.
104  * \param seed
105  */
106  inline void
107  setSeed (unsigned int seed)
108  {
109  seed_ = seed;
110  }
111 
112  /** \brief Get the value of the internal \a seed parameter.
113  */
114  inline unsigned int
116  {
117  return (seed_);
118  }
119 
120  protected:
121 
122  /** \brief Number of indices that will be returned. */
123  unsigned int sample_;
124  /** \brief Random number seed. */
125  unsigned int seed_;
126 
127  /** \brief Sample of point indices into a separate PointCloud
128  * \param output the resultant point cloud
129  */
130  void
131  applyFilter (PointCloud &output);
132 
133  /** \brief Sample of point indices
134  * \param indices the resultant point cloud indices
135  */
136  void
137  applyFilter (std::vector<int> &indices);
138 
139  /** \brief Return a random number fast using a LCG (Linear Congruential Generator) algorithm.
140  * See http://software.intel.com/en-us/articles/fast-random-number-generator-on-the-intel-pentiumr-4-processor/ for more information.
141  */
142  inline float
144  {
145  return (static_cast<float>(rand () / double (RAND_MAX)));
146  //return (((214013 * seed_ + 2531011) >> 16) & 0x7FFF);
147  }
148  };
149 
150  /** \brief @b RandomSample applies a random sampling with uniform probability.
151  * \author Justin Rosen
152  * \ingroup filters
153  */
154  template<>
155  class PCL_EXPORTS RandomSample<pcl::PCLPointCloud2> : public FilterIndices<pcl::PCLPointCloud2>
156  {
159 
163 
164  public:
165 
166  typedef boost::shared_ptr<RandomSample<pcl::PCLPointCloud2> > Ptr;
167  typedef boost::shared_ptr<const RandomSample<pcl::PCLPointCloud2> > ConstPtr;
168 
169  /** \brief Empty constructor. */
170  RandomSample () : sample_ (UINT_MAX), seed_ (static_cast<unsigned int> (time (NULL)))
171  {
172  filter_name_ = "RandomSample";
173  }
174 
175  /** \brief Set number of indices to be sampled.
176  * \param sample
177  */
178  inline void
179  setSample (unsigned int sample)
180  {
181  sample_ = sample;
182  }
183 
184  /** \brief Get the value of the internal \a sample parameter.
185  */
186  inline unsigned int
188  {
189  return (sample_);
190  }
191 
192  /** \brief Set seed of random function.
193  * \param seed
194  */
195  inline void
196  setSeed (unsigned int seed)
197  {
198  seed_ = seed;
199  }
200 
201  /** \brief Get the value of the internal \a seed parameter.
202  */
203  inline unsigned int
205  {
206  return (seed_);
207  }
208 
209  protected:
210 
211  /** \brief Number of indices that will be returned. */
212  unsigned int sample_;
213  /** \brief Random number seed. */
214  unsigned int seed_;
215 
216  /** \brief Sample of point indices into a separate PointCloud
217  * \param output the resultant point cloud
218  */
219  void
220  applyFilter (PCLPointCloud2 &output);
221 
222  /** \brief Sample of point indices
223  * \param indices the resultant point cloud indices
224  */
225  void
226  applyFilter (std::vector<int> &indices);
227 
228  /** \brief Return a random number fast using a LCG (Linear Congruential Generator) algorithm.
229  * See http://software.intel.com/en-us/articles/fast-random-number-generator-on-the-intel-pentiumr-4-processor/ for more information.
230  */
231  inline float
233  {
234  return (static_cast<float> (rand () / double (RAND_MAX)));
235  //return (((214013 * seed_ + 2531011) >> 16) & 0x7FFF);
236  }
237  };
238 }
239 
240 #ifdef PCL_NO_PRECOMPILE
241 #include <pcl/filters/impl/random_sample.hpp>
242 #endif
243 
244 #endif //#ifndef PCL_FILTERS_RANDOM_SUBSAMPLE_H_