Point Cloud Library (PCL)
1.15.1
Toggle main menu visibility
Loading...
Searching...
No Matches
pcl
filters
impl
random_sample.hpp
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.hpp 1897 2011-07-26 20:35:49Z rusu $
35
*
36
*/
37
38
#ifndef PCL_FILTERS_IMPL_RANDOM_SAMPLE_H_
39
#define PCL_FILTERS_IMPL_RANDOM_SAMPLE_H_
40
41
#include <pcl/filters/random_sample.h>
42
43
44
///////////////////////////////////////////////////////////////////////////////
45
template
<
typename
Po
int
T>
46
void
47
pcl::RandomSample<PointT>::applyFilter
(
Indices
&indices)
48
{
49
std::size_t N =
indices_
->size ();
50
std::size_t sample_size =
negative_
? N -
sample_
:
sample_
;
51
// If sample size is 0 or if the sample size is greater then input cloud size
52
// then return all indices
53
if
(sample_size >= N)
54
{
55
indices = *
indices_
;
56
removed_indices_
->clear ();
57
}
58
else
59
{
60
// Resize output indices to sample size
61
indices.resize (sample_size);
62
if
(
extract_removed_indices_
)
63
removed_indices_
->resize (N - sample_size);
64
65
// Set random seed so derived indices are the same each time the filter runs
66
#ifdef __OpenBSD__
67
srand_deterministic (
seed_
);
// OpenBSD only offers repeatable sequences with this function
68
#else
69
std::srand (
seed_
);
70
#endif
// __OpenBSD__
71
72
// Algorithm S
73
std::size_t i = 0;
74
std::size_t index = 0;
75
std::vector<bool> added;
76
if
(
extract_removed_indices_
)
77
added.resize (
indices_
->size (),
false
);
78
std::size_t n = sample_size;
79
while
(n > 0)
80
{
81
// Step 1: [Generate U.] Generate a random variate U that is uniformly distributed between 0 and 1.
82
const
float
U =
unifRand
();
83
// Step 2: [Test.] If N * U > n, go to Step 4.
84
if
((N * U) <= n)
85
{
86
// Step 3: [Select.] Select the next record in the file for the sample, and set n : = n - 1.
87
if
(
extract_removed_indices_
)
88
added[index] =
true
;
89
indices[i++] = (*indices_)[index];
90
--n;
91
}
92
// Step 4: [Don't select.] Skip over the next record (do not include it in the sample).
93
// Set N : = N - 1.
94
--N;
95
++index;
96
// If n > 0, then return to Step 1; otherwise, the sample is complete and the algorithm terminates.
97
}
98
99
// Now populate removed_indices_ appropriately
100
if
(
extract_removed_indices_
)
101
{
102
std::size_t ri = 0;
103
for
(std::size_t i = 0; i < added.size (); i++)
104
{
105
if
(!added[i])
106
{
107
(*removed_indices_)[ri++] = (*indices_)[i];
108
}
109
}
110
}
111
}
112
}
113
114
#define PCL_INSTANTIATE_RandomSample(T) template class PCL_EXPORTS pcl::RandomSample<T>;
115
116
#endif
// PCL_FILTERS_IMPL_RANDOM_SAMPLE_H_
pcl::Filter::extract_removed_indices_
bool extract_removed_indices_
Set to true if we want to return the indices of the removed points.
Definition
filter.h:161
pcl::Filter::removed_indices_
IndicesPtr removed_indices_
Indices of the points that are removed.
Definition
filter.h:155
pcl::FilterIndices::negative_
bool negative_
False = normal filter behavior (default), true = inverted behavior.
Definition
filter_indices.h:167
pcl::PCLBase::indices_
IndicesPtr indices_
A pointer to the vector of point indices to use.
Definition
pcl_base.h:150
pcl::RandomSample::unifRand
float unifRand()
Return a random number fast using a LCG (Linear Congruential Generator) algorithm.
Definition
random_sample.h:136
pcl::RandomSample::applyFilter
void applyFilter(Indices &indices) override
Sample of point indices.
Definition
random_sample.hpp:47
pcl::RandomSample::sample_
unsigned int sample_
Number of indices that will be returned.
Definition
random_sample.h:122
pcl::RandomSample::seed_
unsigned int seed_
Random number seed.
Definition
random_sample.h:124
pcl::Indices
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition
types.h:133