Point Cloud Library (PCL)  1.9.1-dev
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 #include <pcl/common/io.h>
43 #include <pcl/point_traits.h>
44 
45 
46 ///////////////////////////////////////////////////////////////////////////////
47 template<typename PointT> void
49 {
50  std::vector<int> indices;
51  if (keep_organized_)
52  {
53  bool temp = extract_removed_indices_;
54  extract_removed_indices_ = true;
55  applyFilter (indices);
56  extract_removed_indices_ = temp;
57  copyPointCloud (*input_, output);
58  // Get X, Y, Z fields
59  std::vector<pcl::PCLPointField> fields;
60  pcl::getFields (*input_, fields);
61  std::vector<size_t> offsets;
62  for (const auto &field : fields)
63  {
64  if (field.name == "x" ||
65  field.name == "y" ||
66  field.name == "z")
67  offsets.push_back (field.offset);
68  }
69  // For every "removed" point, set the x,y,z fields to user_filter_value_
70  const static float user_filter_value = user_filter_value_;
71  for (size_t rii = 0; rii < removed_indices_->size (); ++rii)
72  {
73  uint8_t* pt_data = reinterpret_cast<uint8_t*> (&output[(*removed_indices_)[rii]]);
74  for (const unsigned long &offset : offsets)
75  {
76  memcpy (pt_data + offset, &user_filter_value, sizeof (float));
77  }
78  if (!std::isfinite (user_filter_value_))
79  output.is_dense = false;
80  }
81  }
82  else
83  {
84  output.is_dense = true;
85  applyFilter (indices);
86  copyPointCloud (*input_, indices, output);
87  }
88 }
89 
90 ///////////////////////////////////////////////////////////////////////////////
91 template<typename PointT>
92 void
93 pcl::RandomSample<PointT>::applyFilter (std::vector<int> &indices)
94 {
95  size_t N = indices_->size ();
96  size_t sample_size = negative_ ? N - sample_ : sample_;
97  // If sample size is 0 or if the sample size is greater then input cloud size
98  // then return all indices
99  if (sample_size >= N)
100  {
101  indices = *indices_;
102  removed_indices_->clear ();
103  }
104  else
105  {
106  // Resize output indices to sample size
107  indices.resize (sample_size);
108  if (extract_removed_indices_)
109  removed_indices_->resize (N - sample_size);
110 
111  // Set random seed so derived indices are the same each time the filter runs
112  std::srand (seed_);
113 
114  // Algorithm S
115  size_t i = 0;
116  size_t index = 0;
117  std::vector<bool> added;
118  if (extract_removed_indices_)
119  added.resize (indices_->size (), false);
120  size_t n = sample_size;
121  while (n > 0)
122  {
123  // Step 1: [Generate U.] Generate a random variate U that is uniformly distributed between 0 and 1.
124  const float U = unifRand ();
125  // Step 2: [Test.] If N * U > n, go to Step 4.
126  if ((N * U) <= n)
127  {
128  // Step 3: [Select.] Select the next record in the file for the sample, and set n : = n - 1.
129  if (extract_removed_indices_)
130  added[index] = true;
131  indices[i++] = (*indices_)[index];
132  --n;
133  }
134  // Step 4: [Don't select.] Skip over the next record (do not include it in the sample).
135  // Set N : = N - 1.
136  --N;
137  ++index;
138  // If n > 0, then return to Step 1; otherwise, the sample is complete and the algorithm terminates.
139  }
140 
141  // Now populate removed_indices_ appropriately
142  if (extract_removed_indices_)
143  {
144  size_t ri = 0;
145  for (size_t i = 0; i < added.size (); i++)
146  {
147  if (!added[i])
148  {
149  (*removed_indices_)[ri++] = (*indices_)[i];
150  }
151  }
152  }
153  }
154 }
155 
156 #define PCL_INSTANTIATE_RandomSample(T) template class PCL_EXPORTS pcl::RandomSample<T>;
157 
158 #endif // PCL_FILTERS_IMPL_RANDOM_SAMPLE_H_
PCL_EXPORTS void copyPointCloud(const pcl::PCLPointCloud2 &cloud_in, const std::vector< int > &indices, pcl::PCLPointCloud2 &cloud_out)
Extract the indices of a given point cloud as a new point cloud.
void applyFilter(PointCloud &output) override
Sample of point indices into a separate PointCloud.
PointCloud represents the base class in PCL for storing collections of 3D points. ...
bool is_dense
True if no points are invalid (e.g., have NaN or Inf values in any of their floating point fields)...
Definition: point_cloud.h:431
void getFields(const pcl::PointCloud< PointT > &cloud, std::vector< pcl::PCLPointField > &fields)
Get the list of available fields (i.e., dimension/channel)
Definition: io.hpp:79