38 #ifndef PCL_FILTERS_IMPL_NORMAL_SPACE_SAMPLE_H_
39 #define PCL_FILTERS_IMPL_NORMAL_SPACE_SAMPLE_H_
47 template<
typename Po
intT,
typename NormalT>
void
52 if (sample_ >= input_->size ())
59 output.
points.resize (sample_);
60 output.
width = sample_;
64 unsigned int n_bins = binsx_ * binsy_ * binsz_;
67 std::vector< std::list <int> > normals_hg;
68 normals_hg.reserve (n_bins);
69 for (
unsigned int i = 0; i < n_bins; i++)
70 normals_hg.push_back (std::list<int> ());
72 for (
unsigned int i = 0; i < input_normals_->points.size (); i++)
74 unsigned int bin_number = findBin (input_normals_->points[i].normal, n_bins);
75 normals_hg[bin_number].push_back (i);
80 std::vector< std::vector <std::list<int>::iterator> > random_access (normals_hg.size ());
81 for (
unsigned int i = 0; i < normals_hg.size (); i++)
83 random_access.push_back (std::vector< std::list<int>::iterator> ());
84 random_access[i].resize (normals_hg[i].size ());
87 for (std::list<int>::iterator itr = normals_hg[i].begin (); itr != normals_hg[i].end (); itr++, j++)
89 random_access[i][j] = itr;
92 unsigned int* start_index =
new unsigned int[normals_hg.size ()];
94 unsigned int prev_index = start_index[0];
95 for (
unsigned int i = 1; i < normals_hg.size (); i++)
97 start_index[i] = prev_index +
static_cast<unsigned int> (normals_hg[i-1].size ());
98 prev_index = start_index[i];
102 boost::dynamic_bitset<> is_sampled_flag (input_normals_->points.size ());
104 boost::dynamic_bitset<> bin_empty_flag (normals_hg.size ());
109 for (
unsigned int j = 0; j < normals_hg.size (); j++)
111 unsigned int M =
static_cast<unsigned int> (normals_hg[j].size ());
112 if (M == 0 || bin_empty_flag.test (j))
117 unsigned int pos = 0;
118 unsigned int random_index = 0;
122 random_index = std::rand () % M;
123 pos = start_index[j] + random_index;
124 }
while (is_sampled_flag.test (pos));
126 is_sampled_flag.flip (start_index[j] + random_index);
129 if (isEntireBinSampled (is_sampled_flag, start_index[j], static_cast<unsigned int> (normals_hg[j].size ())))
131 bin_empty_flag.flip (j);
134 unsigned int index = *(random_access[j][random_index]);
135 output.
points[i] = input_->points[index];
143 delete[] start_index;
147 template<
typename Po
intT,
typename NormalT>
bool
151 for (
unsigned int i = start_index; i < start_index + length; i++)
153 status = status & array.test (i);
159 template<
typename Po
intT,
typename NormalT>
unsigned int
162 unsigned int bin_number = 0;
164 unsigned int t[3] = {0,0,0};
168 float bin_size = 0.0;
171 float min_cos = -1.0;
173 dcos = cosf (normal[0]);
174 bin_size = (max_cos - min_cos) / static_cast<float> (binsx_);
178 for (
float i = min_cos; (i + bin_size) < (max_cos - bin_size); i += bin_size , k++)
180 if (dcos >= i && dcos <= (i+bin_size))
187 dcos = cosf (normal[1]);
188 bin_size = (max_cos - min_cos) / static_cast<float> (binsy_);
192 for (
float i = min_cos; (i + bin_size) < (max_cos - bin_size); i += bin_size , k++)
194 if (dcos >= i && dcos <= (i+bin_size))
201 dcos = cosf (normal[2]);
202 bin_size = (max_cos - min_cos) / static_cast<float> (binsz_);
206 for (
float i = min_cos; (i + bin_size) < (max_cos - bin_size); i += bin_size , k++)
208 if (dcos >= i && dcos <= (i+bin_size))
215 bin_number = t[0] * (binsy_*binsz_) + t[1] * binsz_ + t[2];
220 template<
typename Po
intT,
typename NormalT>
226 if (sample_ >= input_->width * input_->height)
233 indices.resize (sample_);
236 unsigned int n_bins = binsx_ * binsy_ * binsz_;
239 std::vector< std::list <int> > normals_hg;
240 normals_hg.reserve (n_bins);
241 for (
unsigned int i = 0; i < n_bins; i++)
242 normals_hg.push_back (std::list<int> ());
244 for (
unsigned int i=0; i < input_normals_->points.size (); i++)
246 unsigned int bin_number = findBin (input_normals_->points[i].normal, n_bins);
247 normals_hg[bin_number].push_back (i);
252 std::vector< std::vector <std::list<int>::iterator> > random_access (normals_hg.size ());
253 for (
unsigned int i = 0; i < normals_hg.size (); i++)
255 random_access.push_back (std::vector<std::list<int>::iterator> ());
256 random_access[i].resize (normals_hg[i].size ());
259 for (std::list<int>::iterator itr = normals_hg[i].begin (); itr != normals_hg[i].end (); itr++, j++)
261 random_access[i][j] = itr;
264 unsigned int* start_index =
new unsigned int[normals_hg.size ()];
266 unsigned int prev_index = start_index[0];
267 for (
unsigned int i = 1; i < normals_hg.size (); i++)
269 start_index[i] = prev_index +
static_cast<unsigned int> (normals_hg[i-1].size ());
270 prev_index = start_index[i];
274 boost::dynamic_bitset<> is_sampled_flag (input_normals_->points.size ());
276 boost::dynamic_bitset<> bin_empty_flag (normals_hg.size ());
281 for (
unsigned int j = 0; j < normals_hg.size (); j++)
283 unsigned int M =
static_cast<unsigned int> (normals_hg[j].size ());
284 if (M==0 || bin_empty_flag.test (j))
289 unsigned int pos = 0;
290 unsigned int random_index = 0;
295 random_index = std::rand () % M;
296 pos = start_index[j] + random_index;
297 }
while (is_sampled_flag.test (pos));
299 is_sampled_flag.flip (start_index[j] + random_index);
302 if (isEntireBinSampled (is_sampled_flag, start_index[j], static_cast<unsigned int> (normals_hg[j].size ())))
304 bin_empty_flag.flip (j);
307 unsigned int index = *(random_access[j][random_index]);
316 delete[] start_index;
319 #define PCL_INSTANTIATE_NormalSpaceSampling(T,NT) template class PCL_EXPORTS pcl::NormalSpaceSampling<T,NT>;
321 #endif // PCL_FILTERS_IMPL_NORMAL_SPACE_SAMPLE_H_