Point Cloud Library (PCL)  1.7.1
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 %zu\n",
58  sample_, input_->size ());
59  return false;
60  }
61 
62  boost::mt19937 rng (static_cast<unsigned int> (seed_));
63  boost::uniform_int<unsigned int> uniform_distrib (0, unsigned (input_->size ()));
64  if (rng_uniform_distribution_ != NULL)
65  delete rng_uniform_distribution_;
66  rng_uniform_distribution_ = new boost::variate_generator<boost::mt19937, boost::uniform_int<unsigned int> > (rng, uniform_distrib);
67 
68  return (true);
69 }
70 
71 ///////////////////////////////////////////////////////////////////////////////
72 template<typename PointT, typename NormalT> void
74 {
75  std::vector<int> indices;
76  if (keep_organized_)
77  {
78  bool temp = extract_removed_indices_;
79  extract_removed_indices_ = true;
80  applyFilter (indices);
81  extract_removed_indices_ = temp;
82 
83  output = *input_;
84  for (int rii = 0; rii < static_cast<int> (removed_indices_->size ()); ++rii) // rii = removed indices iterator
85  output.points[(*removed_indices_)[rii]].x = output.points[(*removed_indices_)[rii]].y = output.points[(*removed_indices_)[rii]].z = user_filter_value_;
86  if (!pcl_isfinite (user_filter_value_))
87  output.is_dense = false;
88  }
89  else
90  {
91  output.is_dense = true;
92  applyFilter (indices);
93  pcl::copyPointCloud (*input_, indices, output);
94  }
95 }
96 
97 ///////////////////////////////////////////////////////////////////////////////
98 template<typename PointT, typename NormalT> bool
100  unsigned int start_index,
101  unsigned int length)
102 {
103  bool status = true;
104  for (unsigned int i = start_index; i < start_index + length; i++)
105  {
106  status = status & array.test (i);
107  }
108  return status;
109 }
110 
111 ///////////////////////////////////////////////////////////////////////////////
112 template<typename PointT, typename NormalT> unsigned int
113 pcl::NormalSpaceSampling<PointT, NormalT>::findBin (const float *normal, unsigned int)
114 {
115  unsigned int bin_number = 0;
116  // Holds the bin numbers for direction cosines in x,y,z directions
117  unsigned int t[3] = {0,0,0};
118 
119  // dcos is the direction cosine.
120  float dcos = 0.0;
121  float bin_size = 0.0;
122  // max_cos and min_cos are the maximum and minimum values of cos(theta) respectively
123  float max_cos = 1.0;
124  float min_cos = -1.0;
125 
126 // dcos = cosf (normal[0]);
127  dcos = normal[0];
128  bin_size = (max_cos - min_cos) / static_cast<float> (binsx_);
129 
130  // Finding bin number for direction cosine in x direction
131  unsigned int k = 0;
132  for (float i = min_cos; (i + bin_size) < (max_cos - bin_size); i += bin_size , k++)
133  {
134  if (dcos >= i && dcos <= (i+bin_size))
135  {
136  break;
137  }
138  }
139  t[0] = k;
140 
141 // dcos = cosf (normal[1]);
142  dcos = normal[1];
143  bin_size = (max_cos - min_cos) / static_cast<float> (binsy_);
144 
145  // Finding bin number for direction cosine in y direction
146  k = 0;
147  for (float i = min_cos; (i + bin_size) < (max_cos - bin_size); i += bin_size , k++)
148  {
149  if (dcos >= i && dcos <= (i+bin_size))
150  {
151  break;
152  }
153  }
154  t[1] = k;
155 
156 // dcos = cosf (normal[2]);
157  dcos = normal[2];
158  bin_size = (max_cos - min_cos) / static_cast<float> (binsz_);
159 
160  // Finding bin number for direction cosine in z direction
161  k = 0;
162  for (float i = min_cos; (i + bin_size) < (max_cos - bin_size); i += bin_size , k++)
163  {
164  if (dcos >= i && dcos <= (i+bin_size))
165  {
166  break;
167  }
168  }
169  t[2] = k;
170 
171  bin_number = t[0] * (binsy_*binsz_) + t[1] * binsz_ + t[2];
172  return bin_number;
173 }
174 
175 ///////////////////////////////////////////////////////////////////////////////
176 template<typename PointT, typename NormalT> void
178 {
179  if (!initCompute ())
180  {
181  indices = *indices_;
182  return;
183  }
184 
185  unsigned int max_values = (std::min) (sample_, static_cast<unsigned int> (input_normals_->size ()));
186  // Resize output indices to sample size
187  indices.resize (max_values);
188  removed_indices_->resize (max_values);
189 
190  // Allocate memory for the histogram of normals. Normals will then be sampled from each bin.
191  unsigned int n_bins = binsx_ * binsy_ * binsz_;
192  // list<int> holds the indices of points in that bin. Using list to avoid repeated resizing of vectors.
193  // Helps when the point cloud is large.
194  std::vector<std::list <int> > normals_hg;
195  normals_hg.reserve (n_bins);
196  for (unsigned int i = 0; i < n_bins; i++)
197  normals_hg.push_back (std::list<int> ());
198 
199  for (std::vector<int>::const_iterator it = indices_->begin (); it != indices_->end (); ++it)
200  {
201  unsigned int bin_number = findBin (input_normals_->points[*it].normal, n_bins);
202  normals_hg[bin_number].push_back (*it);
203  }
204 
205 
206  // Setting up random access for the list created above. Maintaining the iterators to individual elements of the list
207  // in a vector. Using vector now as the size of the list is known.
208  std::vector<std::vector<std::list<int>::iterator> > random_access (normals_hg.size ());
209  for (unsigned int i = 0; i < normals_hg.size (); i++)
210  {
211  random_access.push_back (std::vector<std::list<int>::iterator> ());
212  random_access[i].resize (normals_hg[i].size ());
213 
214  unsigned int j = 0;
215  for (std::list<int>::iterator itr = normals_hg[i].begin (); itr != normals_hg[i].end (); itr++, j++)
216  random_access[i][j] = itr;
217  }
218  std::vector<unsigned int> start_index (normals_hg.size ());
219  start_index[0] = 0;
220  unsigned int prev_index = start_index[0];
221  for (unsigned int i = 1; i < normals_hg.size (); i++)
222  {
223  start_index[i] = prev_index + static_cast<unsigned int> (normals_hg[i-1].size ());
224  prev_index = start_index[i];
225  }
226 
227  // Maintaining flags to check if a point is sampled
228  boost::dynamic_bitset<> is_sampled_flag (input_normals_->points.size ());
229  // Maintaining flags to check if all points in the bin are sampled
230  boost::dynamic_bitset<> bin_empty_flag (normals_hg.size ());
231  unsigned int i = 0;
232  while (i < sample_)
233  {
234  // Iterating through every bin and picking one point at random, until the required number of points are sampled.
235  for (unsigned int j = 0; j < normals_hg.size (); j++)
236  {
237  unsigned int M = static_cast<unsigned int> (normals_hg[j].size ());
238  if (M == 0 || bin_empty_flag.test (j)) // bin_empty_flag(i) is set if all points in that bin are sampled..
239  continue;
240 
241  unsigned int pos = 0;
242  unsigned int random_index = 0;
243 
244  // Picking up a sample at random from jth bin
245  do
246  {
247  random_index = static_cast<unsigned int> ((*rng_uniform_distribution_) () % M);
248  pos = start_index[j] + random_index;
249  } while (is_sampled_flag.test (pos));
250 
251  is_sampled_flag.flip (start_index[j] + random_index);
252 
253  // Checking if all points in bin j are sampled.
254  if (isEntireBinSampled (is_sampled_flag, start_index[j], static_cast<unsigned int> (normals_hg[j].size ())))
255  bin_empty_flag.flip (j);
256 
257  unsigned int index = *(random_access[j][random_index]);
258  indices[i] = index;
259  i++;
260  if (i == sample_)
261  break;
262  }
263  }
264 
265  // If we need to return the indices that we haven't sampled
266  if (extract_removed_indices_)
267  {
268  std::vector<int> indices_temp = indices;
269  std::sort (indices_temp.begin (), indices_temp.end ());
270 
271  std::vector<int> all_indices_temp = *indices_;
272  std::sort (all_indices_temp.begin (), all_indices_temp.end ());
273  set_difference (all_indices_temp.begin (), all_indices_temp.end (),
274  indices_temp.begin (), indices_temp.end (),
275  inserter (*removed_indices_, removed_indices_->begin ()));
276  }
277 }
278 
279 #define PCL_INSTANTIATE_NormalSpaceSampling(T,NT) template class PCL_EXPORTS pcl::NormalSpaceSampling<T,NT>;
280 
281 #endif // PCL_FILTERS_IMPL_NORMAL_SPACE_SAMPLE_H_