Point Cloud Library (PCL)  1.11.0
normal_space.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2012-, Open Perception, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of the copyright holder(s) nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
37 
38 #ifndef PCL_FILTERS_IMPL_NORMAL_SPACE_SAMPLE_H_
39 #define PCL_FILTERS_IMPL_NORMAL_SPACE_SAMPLE_H_
40 
41 #include <pcl/filters/normal_space.h>
42 #include <pcl/common/io.h>
43 
44 #include <vector>
45 #include <list>
46 
47 ///////////////////////////////////////////////////////////////////////////////
48 template<typename PointT, typename NormalT> bool
50 {
52  return false;
53 
54  // If sample size is 0 or if the sample size is greater then input cloud size then return entire copy of cloud
55  if (sample_ >= input_->size ())
56  {
57  PCL_ERROR ("[NormalSpaceSampling::initCompute] Requested more samples than the input cloud size: %d vs %lu\n",
58  sample_, input_->size ());
59  return false;
60  }
61 
62  rng_.seed (seed_);
63  return (true);
64 }
65 
66 ///////////////////////////////////////////////////////////////////////////////
67 template<typename PointT, typename NormalT> bool
69  unsigned int start_index,
70  unsigned int length)
71 {
72  bool status = true;
73  for (unsigned int i = start_index; i < start_index + length; i++)
74  {
75  status &= array.test (i);
76  }
77  return status;
78 }
79 
80 ///////////////////////////////////////////////////////////////////////////////
81 template<typename PointT, typename NormalT> unsigned int
83 {
84  const unsigned ix = static_cast<unsigned> (std::round (0.5f * (binsx_ - 1.f) * (normal[0] + 1.f)));
85  const unsigned iy = static_cast<unsigned> (std::round (0.5f * (binsy_ - 1.f) * (normal[1] + 1.f)));
86  const unsigned iz = static_cast<unsigned> (std::round (0.5f * (binsz_ - 1.f) * (normal[2] + 1.f)));
87  return ix * (binsy_*binsz_) + iy * binsz_ + iz;
88 }
89 
90 ///////////////////////////////////////////////////////////////////////////////
91 template<typename PointT, typename NormalT> void
93 {
94  if (!initCompute ())
95  {
96  indices = *indices_;
97  return;
98  }
99 
100  unsigned int max_values = (std::min) (sample_, static_cast<unsigned int> (input_normals_->size ()));
101  // Resize output indices to sample size
102  indices.resize (max_values);
103  removed_indices_->resize (max_values);
104 
105  // Allocate memory for the histogram of normals. Normals will then be sampled from each bin.
106  unsigned int n_bins = binsx_ * binsy_ * binsz_;
107  // list<int> holds the indices of points in that bin. Using list to avoid repeated resizing of vectors.
108  // Helps when the point cloud is large.
109  std::vector<std::list <int> > normals_hg;
110  normals_hg.reserve (n_bins);
111  for (unsigned int i = 0; i < n_bins; i++)
112  normals_hg.emplace_back();
113 
114  for (const auto index : *indices_)
115  {
116  unsigned int bin_number = findBin ((*input_normals_)[index].normal);
117  normals_hg[bin_number].push_back (index);
118  }
119 
120 
121  // Setting up random access for the list created above. Maintaining the iterators to individual elements of the list
122  // in a vector. Using vector now as the size of the list is known.
123  std::vector<std::vector<std::list<int>::iterator> > random_access (normals_hg.size ());
124  for (std::size_t i = 0; i < normals_hg.size (); i++)
125  {
126  random_access.emplace_back();
127  random_access[i].resize (normals_hg[i].size ());
128 
129  std::size_t j = 0;
130  for (std::list<int>::iterator itr = normals_hg[i].begin (); itr != normals_hg[i].end (); ++itr, ++j)
131  random_access[i][j] = itr;
132  }
133  std::vector<std::size_t> start_index (normals_hg.size ());
134  start_index[0] = 0;
135  std::size_t prev_index = 0;
136  for (std::size_t i = 1; i < normals_hg.size (); i++)
137  {
138  start_index[i] = prev_index + normals_hg[i-1].size ();
139  prev_index = start_index[i];
140  }
141 
142  // Maintaining flags to check if a point is sampled
143  boost::dynamic_bitset<> is_sampled_flag (input_normals_->points.size ());
144  // Maintaining flags to check if all points in the bin are sampled
145  boost::dynamic_bitset<> bin_empty_flag (normals_hg.size ());
146  unsigned int i = 0;
147  while (i < sample_)
148  {
149  // Iterating through every bin and picking one point at random, until the required number of points are sampled.
150  for (std::size_t j = 0; j < normals_hg.size (); j++)
151  {
152  unsigned int M = static_cast<unsigned int> (normals_hg[j].size ());
153  if (M == 0 || bin_empty_flag.test (j)) // bin_empty_flag(i) is set if all points in that bin are sampled..
154  continue;
155 
156  unsigned int pos = 0;
157  unsigned int random_index = 0;
158  std::uniform_int_distribution<unsigned> rng_uniform_distribution (0u, M - 1u);
159 
160  // Picking up a sample at random from jth bin
161  do
162  {
163  random_index = rng_uniform_distribution (rng_);
164  pos = start_index[j] + random_index;
165  } while (is_sampled_flag.test (pos));
166 
167  is_sampled_flag.flip (start_index[j] + random_index);
168 
169  // Checking if all points in bin j are sampled.
170  if (isEntireBinSampled (is_sampled_flag, start_index[j], static_cast<unsigned int> (normals_hg[j].size ())))
171  bin_empty_flag.flip (j);
172 
173  unsigned int index = *(random_access[j][random_index]);
174  indices[i] = index;
175  i++;
176  if (i == sample_)
177  break;
178  }
179  }
180 
181  // If we need to return the indices that we haven't sampled
182  if (extract_removed_indices_)
183  {
184  std::vector<int> indices_temp = indices;
185  std::sort (indices_temp.begin (), indices_temp.end ());
186 
187  std::vector<int> all_indices_temp = *indices_;
188  std::sort (all_indices_temp.begin (), all_indices_temp.end ());
189  set_difference (all_indices_temp.begin (), all_indices_temp.end (),
190  indices_temp.begin (), indices_temp.end (),
191  inserter (*removed_indices_, removed_indices_->begin ()));
192  }
193 }
194 
195 #define PCL_INSTANTIATE_NormalSpaceSampling(T,NT) template class PCL_EXPORTS pcl::NormalSpaceSampling<T,NT>;
196 
197 #endif // PCL_FILTERS_IMPL_NORMAL_SPACE_SAMPLE_H_
FilterIndices represents the base class for filters that are about binary point removal.
NormalSpaceSampling samples the input point cloud in the space of normal directions computed at every...
Definition: normal_space.h:54
void applyFilter(std::vector< int > &indices) override
Sample of point indices.