Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
random_sample.h
Go to the documentation of this file.
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 Willow Garage, Inc. 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: random_sample.h 5026 2012-03-12 02:51:44Z rusu $
35  *
36  */
37 
38 #ifndef PCL_FILTERS_RANDOM_SUBSAMPLE_H_
39 #define PCL_FILTERS_RANDOM_SUBSAMPLE_H_
40 
42 #include <time.h>
43 #include <limits.h>
44 
45 namespace pcl
46 {
55  template<typename PointT>
56  class RandomSample : public FilterIndices<PointT>
57  {
62 
64  typedef typename PointCloud::Ptr PointCloudPtr;
66 
67  public:
69  RandomSample () : sample_ (UINT_MAX), seed_ (static_cast<unsigned int> (time (NULL)))
70  {
71  filter_name_ = "RandomSample";
72  }
73 
77  inline void
78  setSample (unsigned int sample)
79  {
80  sample_ = sample;
81  }
82 
85  inline unsigned int
87  {
88  return (sample_);
89  }
90 
94  inline void
95  setSeed (unsigned int seed)
96  {
97  seed_ = seed;
98  }
99 
102  inline unsigned int
104  {
105  return (seed_);
106  }
107 
108  protected:
109 
111  unsigned int sample_;
113  unsigned int seed_;
114 
118  void
119  applyFilter (PointCloud &output);
120 
124  void
125  applyFilter (std::vector<int> &indices);
126 
130  inline float
131  unifRand ()
132  {
133  return (static_cast<float>(rand () / double (RAND_MAX)));
134  //return (((214013 * seed_ + 2531011) >> 16) & 0x7FFF);
135  }
136  };
137 
142  template<>
143  class PCL_EXPORTS RandomSample<sensor_msgs::PointCloud2> : public FilterIndices<sensor_msgs::PointCloud2>
144  {
147 
151 
152  public:
154  RandomSample () : sample_ (UINT_MAX), seed_ (static_cast<unsigned int> (time (NULL)))
155  {
156  filter_name_ = "RandomSample";
157  }
158 
162  inline void
163  setSample (unsigned int sample)
164  {
165  sample_ = sample;
166  }
167 
170  inline unsigned int
172  {
173  return (sample_);
174  }
175 
179  inline void
180  setSeed (unsigned int seed)
181  {
182  seed_ = seed;
183  }
184 
187  inline unsigned int
189  {
190  return (seed_);
191  }
192 
193  protected:
194 
196  unsigned int sample_;
198  unsigned int seed_;
199 
203  void
204  applyFilter (PointCloud2 &output);
205 
209  void
210  applyFilter (std::vector<int> &indices);
211 
215  inline float
216  unifRand ()
217  {
218  return (static_cast<float> (rand () / double (RAND_MAX)));
219  //return (((214013 * seed_ + 2531011) >> 16) & 0x7FFF);
220  }
221  };
222 }
223 
224 #endif //#ifndef PCL_FILTERS_RANDOM_SUBSAMPLE_H_