38 #ifndef PCL_FILTERS_IMPL_NORMAL_SPACE_SAMPLE_H_
39 #define PCL_FILTERS_IMPL_NORMAL_SPACE_SAMPLE_H_
41 #include <pcl/filters/normal_space.h>
42 #include <pcl/common/io.h>
48 template<
typename Po
intT,
typename NormalT>
bool
55 if (sample_ >= input_->size ())
57 PCL_ERROR (
"[NormalSpaceSampling::initCompute] Requested more samples than the input cloud size: %d vs %lu\n",
58 sample_, input_->size ());
62 boost::mt19937 rng (
static_cast<unsigned int> (seed_));
63 boost::uniform_int<unsigned int> uniform_distrib (0,
unsigned (input_->size ()));
64 delete rng_uniform_distribution_;
65 rng_uniform_distribution_ =
new boost::variate_generator<boost::mt19937, boost::uniform_int<unsigned int> > (rng, uniform_distrib);
71 template<
typename Po
intT,
typename NormalT>
void
74 std::vector<int> indices;
77 bool temp = extract_removed_indices_;
78 extract_removed_indices_ =
true;
79 applyFilter (indices);
80 extract_removed_indices_ = temp;
83 for (
int rii = 0; rii < static_cast<int> (removed_indices_->size ()); ++rii)
84 output.
points[(*removed_indices_)[rii]].x = output.
points[(*removed_indices_)[rii]].y = output.
points[(*removed_indices_)[rii]].z = user_filter_value_;
85 if (!std::isfinite (user_filter_value_))
91 applyFilter (indices);
97 template<
typename Po
intT,
typename NormalT>
bool
99 unsigned int start_index,
103 for (
unsigned int i = start_index; i < start_index + length; i++)
105 status &= array.test (i);
111 template<
typename Po
intT,
typename NormalT>
unsigned int
114 unsigned int bin_number = 0;
116 unsigned int t[3] = {0,0,0};
120 float bin_size = 0.0;
123 float min_cos = -1.0;
127 bin_size = (max_cos - min_cos) /
static_cast<float> (binsx_);
131 for (
float i = min_cos; (i + bin_size) < (max_cos - bin_size); i += bin_size , k++)
133 if (dcos >= i && dcos <= (i+bin_size))
142 bin_size = (max_cos - min_cos) /
static_cast<float> (binsy_);
146 for (
float i = min_cos; (i + bin_size) < (max_cos - bin_size); i += bin_size , k++)
148 if (dcos >= i && dcos <= (i+bin_size))
157 bin_size = (max_cos - min_cos) /
static_cast<float> (binsz_);
161 for (
float i = min_cos; (i + bin_size) < (max_cos - bin_size); i += bin_size , k++)
163 if (dcos >= i && dcos <= (i+bin_size))
170 bin_number = t[0] * (binsy_*binsz_) + t[1] * binsz_ + t[2];
175 template<
typename Po
intT,
typename NormalT>
void
184 unsigned int max_values = (std::min) (sample_,
static_cast<unsigned int> (input_normals_->size ()));
186 indices.resize (max_values);
187 removed_indices_->resize (max_values);
190 unsigned int n_bins = binsx_ * binsy_ * binsz_;
193 std::vector<std::list <int> > normals_hg;
194 normals_hg.reserve (n_bins);
195 for (
unsigned int i = 0; i < n_bins; i++)
196 normals_hg.emplace_back();
198 for (std::vector<int>::const_iterator it = indices_->begin (); it != indices_->end (); ++it)
200 unsigned int bin_number = findBin (input_normals_->points[*it].normal, n_bins);
201 normals_hg[bin_number].push_back (*it);
207 std::vector<std::vector<std::list<int>::iterator> > random_access (normals_hg.size ());
208 for (std::size_t i = 0; i < normals_hg.size (); i++)
210 random_access.emplace_back();
211 random_access[i].resize (normals_hg[i].size ());
214 for (std::list<int>::iterator itr = normals_hg[i].begin (); itr != normals_hg[i].end (); itr++, j++)
215 random_access[i][j] = itr;
217 std::vector<std::size_t> start_index (normals_hg.size ());
219 std::size_t prev_index = 0;
220 for (std::size_t i = 1; i < normals_hg.size (); i++)
222 start_index[i] = prev_index + normals_hg[i-1].size ();
223 prev_index = start_index[i];
227 boost::dynamic_bitset<> is_sampled_flag (input_normals_->points.size ());
229 boost::dynamic_bitset<> bin_empty_flag (normals_hg.size ());
234 for (std::size_t j = 0; j < normals_hg.size (); j++)
236 unsigned int M =
static_cast<unsigned int> (normals_hg[j].size ());
237 if (M == 0 || bin_empty_flag.test (j))
240 unsigned int pos = 0;
241 unsigned int random_index = 0;
246 random_index =
static_cast<unsigned int> ((*rng_uniform_distribution_) () % M);
247 pos = start_index[j] + random_index;
248 }
while (is_sampled_flag.test (pos));
250 is_sampled_flag.flip (start_index[j] + random_index);
253 if (isEntireBinSampled (is_sampled_flag, start_index[j],
static_cast<unsigned int> (normals_hg[j].size ())))
254 bin_empty_flag.flip (j);
256 unsigned int index = *(random_access[j][random_index]);
265 if (extract_removed_indices_)
267 std::vector<int> indices_temp = indices;
268 std::sort (indices_temp.begin (), indices_temp.end ());
270 std::vector<int> all_indices_temp = *indices_;
271 std::sort (all_indices_temp.begin (), all_indices_temp.end ());
272 set_difference (all_indices_temp.begin (), all_indices_temp.end (),
273 indices_temp.begin (), indices_temp.end (),
274 inserter (*removed_indices_, removed_indices_->begin ()));
278 #define PCL_INSTANTIATE_NormalSpaceSampling(T,NT) template class PCL_EXPORTS pcl::NormalSpaceSampling<T,NT>;
280 #endif // PCL_FILTERS_IMPL_NORMAL_SPACE_SAMPLE_H_