random_sample.h
/*
* Software License Agreement (BSD License)
*
* Copyright (c) 2009, Willow Garage, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the copyright holder(s) nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* $Id: extract_indices.h 1370 2011-06-19 01:06:01Z jspricke $
*
*/
#pragma once
#include <pcl/filters/filter_indices.h>
#include <ctime>
#include <climits>
namespace pcl
{
/** \brief @b RandomSample applies a random sampling with uniform probability.
* Based off Algorithm A from the paper "Faster Methods for Random Sampling"
* by Jeffrey Scott Vitter. The algorithm runs in O(N) and results in sorted
* indices
* http://www.ittc.ku.edu/~jsv/Papers/Vit84.sampling.pdf
* \author Justin Rosen
* \ingroup filters
*/
template<typename PointT>
class RandomSample : public FilterIndices<PointT>
{
using FilterIndices<PointT>::filter_name_;
using FilterIndices<PointT>::getClassName;
using FilterIndices<PointT>::indices_;
using FilterIndices<PointT>::input_;
using FilterIndices<PointT>::negative_;
using FilterIndices<PointT>::keep_organized_;
using FilterIndices<PointT>::user_filter_value_;
using FilterIndices<PointT>::extract_removed_indices_;
using FilterIndices<PointT>::removed_indices_;
using PointCloud = typename FilterIndices<PointT>::PointCloud;
using PointCloudPtr = typename PointCloud::Ptr;
using PointCloudConstPtr = typename PointCloud::ConstPtr;
public:
using Ptr = shared_ptr<RandomSample<PointT> >;
using ConstPtr = shared_ptr<const RandomSample<PointT> >;
/** \brief Empty constructor. */
RandomSample (bool extract_removed_indices = false) :
FilterIndices<PointT> (extract_removed_indices),
sample_ (UINT_MAX),
seed_ (static_cast<unsigned int> (time (nullptr)))
{
filter_name_ = "RandomSample";
}
/** \brief Set number of indices to be sampled.
* \param sample
*/
inline void
setSample (unsigned int sample)
{
sample_ = sample;
}
/** \brief Get the value of the internal \a sample parameter.
*/
inline unsigned int
getSample ()
{
return (sample_);
}
/** \brief Set seed of random function.
* \param seed
*/
inline void
setSeed (unsigned int seed)
{
seed_ = seed;
}
/** \brief Get the value of the internal \a seed parameter.
*/
inline unsigned int
getSeed ()
{
return (seed_);
}
protected:
/** \brief Number of indices that will be returned. */
unsigned int sample_;
/** \brief Random number seed. */
unsigned int seed_;
/** \brief Sample of point indices into a separate PointCloud
* \param output the resultant point cloud
*/
void
applyFilter (PointCloud &output) override;
/** \brief Sample of point indices
* \param indices the resultant point cloud indices
*/
void
applyFilter (std::vector<int> &indices) override;
/** \brief Return a random number fast using a LCG (Linear Congruential Generator) algorithm.
* See http://software.intel.com/en-us/articles/fast-random-number-generator-on-the-intel-pentiumr-4-processor/ for more information.
*/
inline float
unifRand ()
{
return (static_cast<float>(rand () / double (RAND_MAX)));
//return (((214013 * seed_ + 2531011) >> 16) & 0x7FFF);
}
};
/** \brief @b RandomSample applies a random sampling with uniform probability.
* \author Justin Rosen
* \ingroup filters
*/
template<>
class PCL_EXPORTS RandomSample<pcl::PCLPointCloud2> : public FilterIndices<pcl::PCLPointCloud2>
{
using FilterIndices<pcl::PCLPointCloud2>::filter_name_;
using FilterIndices<pcl::PCLPointCloud2>::getClassName;
using PCLPointCloud2 = pcl::PCLPointCloud2;
using PCLPointCloud2Ptr = PCLPointCloud2::Ptr;
using PCLPointCloud2ConstPtr = PCLPointCloud2::ConstPtr;
public:
using Ptr = shared_ptr<RandomSample<pcl::PCLPointCloud2> >;
using ConstPtr = shared_ptr<const RandomSample<pcl::PCLPointCloud2> >;
/** \brief Empty constructor. */
RandomSample () : sample_ (UINT_MAX), seed_ (static_cast<unsigned int> (time (nullptr)))
{
filter_name_ = "RandomSample";
}
/** \brief Set number of indices to be sampled.
* \param sample
*/
inline void
setSample (unsigned int sample)
{
sample_ = sample;
}
/** \brief Get the value of the internal \a sample parameter.
*/
inline unsigned int
getSample ()
{
return (sample_);
}
/** \brief Set seed of random function.
* \param seed
*/
inline void
setSeed (unsigned int seed)
{
seed_ = seed;
}
/** \brief Get the value of the internal \a seed parameter.
*/
inline unsigned int
getSeed ()
{
return (seed_);
}
protected:
/** \brief Number of indices that will be returned. */
unsigned int sample_;
/** \brief Random number seed. */
unsigned int seed_;
/** \brief Sample of point indices into a separate PointCloud
* \param output the resultant point cloud
*/
void
applyFilter (PCLPointCloud2 &output) override;
/** \brief Sample of point indices
* \param indices the resultant point cloud indices
*/
void
applyFilter (std::vector<int> &indices) override;
/** \brief Return a random number fast using a LCG (Linear Congruential Generator) algorithm.
* See http://software.intel.com/en-us/articles/fast-random-number-generator-on-the-intel-pentiumr-4-processor/ for more information.
*/
inline float
unifRand ()
{
return (static_cast<float> (rand () / double (RAND_MAX)));
}
};
}
#ifdef PCL_NO_PRECOMPILE
#include <pcl/filters/impl/random_sample.hpp>
#endif
pcl
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
pcl::RandomSample::seed_
unsigned int seed_
Random number seed.
Definition: random_sample.h:124
pcl::RandomSample< pcl::PCLPointCloud2 >::setSeed
void setSeed(unsigned int seed)
Set seed of random function.
Definition: random_sample.h:195
pcl::Filter::Ptr
shared_ptr< Filter< PointT > > Ptr
Definition: filter.h:89
pcl::PCLBase::PointCloudConstPtr
typename PointCloud::ConstPtr PointCloudConstPtr
Definition: pcl_base.h:74
pcl::PCLBase::PointCloudPtr
typename PointCloud::Ptr PointCloudPtr
Definition: pcl_base.h:73
pcl::PCLPointCloud2::Ptr
shared_ptr< ::pcl::PCLPointCloud2 > Ptr
Definition: PCLPointCloud2.h:38
pcl::RandomSample< pcl::PCLPointCloud2 >::getSample
unsigned int getSample()
Get the value of the internal sample parameter.
Definition: random_sample.h:186
pcl::RandomSample::unifRand
float unifRand()
Return a random number fast using a LCG (Linear Congruential Generator) algorithm.
Definition: random_sample.h:142
pcl::Filter< pcl::PCLPointCloud2 >::ConstPtr
shared_ptr< const Filter< pcl::PCLPointCloud2 > > ConstPtr
Definition: filter.h:196
pcl::PCLBase< pcl::PCLPointCloud2 >::PCLPointCloud2ConstPtr
PCLPointCloud2::ConstPtr PCLPointCloud2ConstPtr
Definition: pcl_base.h:190
pcl::PointCloud
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: projection_matrix.h:52
pcl::RandomSample< pcl::PCLPointCloud2 >::seed_
unsigned int seed_
Random number seed.
Definition: random_sample.h:213
pcl::PointXYZRGB
A point structure representing Euclidean xyz coordinates, and the RGB color.
Definition: point_types.hpp:623
pcl::RandomSample::setSample
void setSample(unsigned int sample)
Set number of indices to be sampled.
Definition: random_sample.h:89
pcl::RandomSample::getSeed
unsigned int getSeed()
Get the value of the internal seed parameter.
Definition: random_sample.h:114
pcl::RandomSample::getSample
unsigned int getSample()
Get the value of the internal sample parameter.
Definition: random_sample.h:97
pcl::PCLBase< pcl::PCLPointCloud2 >::PCLPointCloud2Ptr
PCLPointCloud2::Ptr PCLPointCloud2Ptr
Definition: pcl_base.h:189
pcl::Filter< pcl::PCLPointCloud2 >::Ptr
shared_ptr< Filter< pcl::PCLPointCloud2 > > Ptr
Definition: filter.h:195
pcl::PCLPointCloud2::ConstPtr
shared_ptr< const ::pcl::PCLPointCloud2 > ConstPtr
Definition: PCLPointCloud2.h:39
pcl::Filter::ConstPtr
shared_ptr< const Filter< PointT > > ConstPtr
Definition: filter.h:90
pcl::RandomSample< pcl::PCLPointCloud2 >::sample_
unsigned int sample_
Number of indices that will be returned.
Definition: random_sample.h:211
pcl::RandomSample< pcl::PCLPointCloud2 >::setSample
void setSample(unsigned int sample)
Set number of indices to be sampled.
Definition: random_sample.h:178
pcl::RandomSample< pcl::PCLPointCloud2 >::getSeed
unsigned int getSeed()
Get the value of the internal seed parameter.
Definition: random_sample.h:203
pcl::FilterIndices
FilterIndices represents the base class for filters that are about binary point removal.
Definition: filter_indices.h:74
pcl::PCLPointCloud2
Definition: PCLPointCloud2.h:19
pcl::Filter::filter_name_
std::string filter_name_
The filter name.
Definition: filter.h:164
pcl::PointCloud::Ptr
shared_ptr< PointCloud< PointT > > Ptr
Definition: point_cloud.h:415
pcl::RandomSample< pcl::PCLPointCloud2 >::unifRand
float unifRand()
Return a random number fast using a LCG (Linear Congruential Generator) algorithm.
Definition: random_sample.h:231
pcl::RandomSample::setSeed
void setSeed(unsigned int seed)
Set seed of random function.
Definition: random_sample.h:106
pcl::RandomSample::applyFilter
void applyFilter(PointCloud &output) override
Sample of point indices into a separate PointCloud.
Definition: random_sample.hpp:48
pcl::RandomSample::sample_
unsigned int sample_
Number of indices that will be returned.
Definition: random_sample.h:122
pcl::PointCloud::ConstPtr
shared_ptr< const PointCloud< PointT > > ConstPtr
Definition: point_cloud.h:416
pcl::RandomSample
RandomSample applies a random sampling with uniform probability.
Definition: random_sample.h:55
pcl::RandomSample::RandomSample
RandomSample(bool extract_removed_indices=false)
Empty constructor.
Definition: random_sample.h:77
PCL_EXPORTS
#define PCL_EXPORTS
Definition: pcl_macros.h:253
pcl::shared_ptr
boost::shared_ptr< T > shared_ptr
Alias for boost::shared_ptr.
Definition: pcl_macros.h:90
pcl::RandomSample< pcl::PCLPointCloud2 >::RandomSample
RandomSample()
Empty constructor.
Definition: random_sample.h:169