Point Cloud Library (PCL) 1.15.1-dev
Loading...
Searching...
No Matches
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 <limits>
43
44namespace 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
67 using PointCloud = typename FilterIndices<PointT>::PointCloud;
68 using PointCloudPtr = typename PointCloud::Ptr;
69 using PointCloudConstPtr = typename PointCloud::ConstPtr;
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_ (std::numeric_limits<unsigned int>::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
97 getSample () const
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
114 getSeed () const
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 () / static_cast<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 {
150 using FilterIndices<pcl::PCLPointCloud2>::filter_name_;
151 using FilterIndices<pcl::PCLPointCloud2>::getClassName;
152
154 using PCLPointCloud2Ptr = PCLPointCloud2::Ptr;
155 using PCLPointCloud2ConstPtr = PCLPointCloud2::ConstPtr;
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. */
164 sample_ (std::numeric_limits<unsigned int>::max()),
165 seed_ (static_cast<unsigned int>(time(nullptr)))
166 {
167 filter_name_ = "RandomSample";
168 }
169
170 /** \brief Set number of indices to be sampled.
171 * \param sample
172 */
173 inline void
174 setSample (unsigned int sample)
175 {
176 sample_ = sample;
177 }
178
179 /** \brief Get the value of the internal \a sample parameter.
180 */
181 inline unsigned int
182 getSample () const
183 {
184 return (sample_);
185 }
186
187 /** \brief Set seed of random function.
188 * \param seed
189 */
190 inline void
191 setSeed (unsigned int seed)
192 {
193 seed_ = seed;
194 }
195
196 /** \brief Get the value of the internal \a seed parameter.
197 */
198 inline unsigned int
199 getSeed () const
200 {
201 return (seed_);
202 }
203
204 protected:
205
206 /** \brief Number of indices that will be returned. */
207 unsigned int sample_;
208 /** \brief Random number seed. */
209 unsigned int seed_;
210
211 /** \brief Sample of point indices into a separate PointCloud
212 * \param output the resultant point cloud
213 */
214 void
215 applyFilter (PCLPointCloud2 &output) override;
216
217 /** \brief Sample of point indices
218 * \param indices the resultant point cloud indices
219 */
220 void
221 applyFilter (Indices &indices) override;
222
223 /** \brief Return a random number fast using a LCG (Linear Congruential Generator) algorithm.
224 * See http://software.intel.com/en-us/articles/fast-random-number-generator-on-the-intel-pentiumr-4-processor/ for more information.
225 */
226 inline float
228 {
229 return (static_cast<float> (rand () / static_cast<double>(RAND_MAX)));
230 }
231 };
232}
233
234#ifdef PCL_NO_PRECOMPILE
235#include <pcl/filters/impl/random_sample.hpp>
236#endif
bool extract_removed_indices_
Set to true if we want to return the indices of the removed points.
Definition filter.h:161
const std::string & getClassName() const
Get a string representation of the name of this class.
Definition filter.h:174
std::string filter_name_
The filter name.
Definition filter.h:158
IndicesPtr removed_indices_
Indices of the points that are removed.
Definition filter.h:155
FilterIndices represents the base class for filters that are about binary point removal.
float user_filter_value_
The user given value that the filtered point dimensions should be set to (default = NaN).
bool keep_organized_
False = remove points (default), true = redefine points, keep structure.
bool negative_
False = normal filter behavior (default), true = inverted behavior.
PointCloudConstPtr input_
The input point cloud dataset.
Definition pcl_base.h:147
IndicesPtr indices_
A pointer to the vector of point indices to use.
Definition pcl_base.h:150
PointCloud represents the base class in PCL for storing collections of 3D points.
shared_ptr< PointCloud< PointT > > Ptr
shared_ptr< const PointCloud< PointT > > ConstPtr
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.
shared_ptr< RandomSample< pcl::PCLPointCloud2 > > Ptr
void setSeed(unsigned int seed)
Set seed of random function.
void setSample(unsigned int sample)
Set number of indices to be sampled.
unsigned int getSample() const
Get the value of the internal sample parameter.
shared_ptr< const RandomSample< pcl::PCLPointCloud2 > > ConstPtr
void applyFilter(Indices &indices) override
Sample of point indices.
unsigned int getSeed() const
Get the value of the internal seed parameter.
float unifRand()
Return a random number fast using a LCG (Linear Congruential Generator) algorithm.
RandomSample applies a random sampling with uniform probability.
void setSeed(unsigned int seed)
Set seed of random function.
shared_ptr< const RandomSample< PointT > > ConstPtr
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.
unsigned int sample_
Number of indices that will be returned.
unsigned int getSeed() const
Get the value of the internal seed parameter.
unsigned int seed_
Random number seed.
RandomSample(bool extract_removed_indices=false)
Empty constructor.
shared_ptr< RandomSample< PointT > > Ptr
unsigned int getSample() const
Get the value of the internal sample parameter.
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition types.h:133
shared_ptr< ::pcl::PCLPointCloud2 > Ptr
shared_ptr< const ::pcl::PCLPointCloud2 > ConstPtr
A point structure representing Euclidean xyz coordinates, and the RGB color.