38 #ifndef PCL_FILTERS_IMPL_VOXEL_GRID_H_
39 #define PCL_FILTERS_IMPL_VOXEL_GRID_H_
43 #include <pcl/common/io.h>
44 #include <pcl/filters/voxel_grid.h>
45 #include <boost/sort/spreadsort/integer_sort.hpp>
48 template <
typename Po
intT>
void
50 const std::string &distance_field_name,
float min_distance,
float max_distance,
51 Eigen::Vector4f &min_pt, Eigen::Vector4f &max_pt,
bool limit_negative)
53 Eigen::Array4f min_p, max_p;
54 min_p.setConstant (FLT_MAX);
55 max_p.setConstant (-FLT_MAX);
58 std::vector<pcl::PCLPointField> fields;
59 int distance_idx = pcl::getFieldIndex<PointT> (distance_field_name, fields);
65 for (
const auto& point: *cloud)
68 const std::uint8_t* pt_data =
reinterpret_cast<const std::uint8_t*
> (&point);
69 memcpy (&distance_value, pt_data + fields[distance_idx].offset,
sizeof (
float));
74 if ((distance_value < max_distance) && (distance_value > min_distance))
80 if ((distance_value > max_distance) || (distance_value < min_distance))
85 min_p = min_p.min (pt);
86 max_p = max_p.max (pt);
91 for (
const auto& point: *cloud)
94 const std::uint8_t* pt_data =
reinterpret_cast<const std::uint8_t*
> (&point);
95 memcpy (&distance_value, pt_data + fields[distance_idx].offset,
sizeof (
float));
100 if ((distance_value < max_distance) && (distance_value > min_distance))
106 if ((distance_value > max_distance) || (distance_value < min_distance))
115 min_p = min_p.min (pt);
116 max_p = max_p.max (pt);
124 template <
typename Po
intT>
void
127 const std::string &distance_field_name,
float min_distance,
float max_distance,
128 Eigen::Vector4f &min_pt, Eigen::Vector4f &max_pt,
bool limit_negative)
130 Eigen::Array4f min_p, max_p;
131 min_p.setConstant (FLT_MAX);
132 max_p.setConstant (-FLT_MAX);
135 std::vector<pcl::PCLPointField> fields;
136 int distance_idx = pcl::getFieldIndex<PointT> (distance_field_name, fields);
138 float distance_value;
142 for (
const auto &index : indices)
145 const std::uint8_t* pt_data =
reinterpret_cast<const std::uint8_t*
> (&(*cloud)[index]);
146 memcpy (&distance_value, pt_data + fields[distance_idx].offset,
sizeof (
float));
151 if ((distance_value < max_distance) && (distance_value > min_distance))
157 if ((distance_value > max_distance) || (distance_value < min_distance))
162 min_p = min_p.min (pt);
163 max_p = max_p.max (pt);
168 for (
const auto &index : indices)
171 const std::uint8_t* pt_data =
reinterpret_cast<const std::uint8_t*
> (&(*cloud)[index]);
172 memcpy (&distance_value, pt_data + fields[distance_idx].offset,
sizeof (
float));
177 if ((distance_value < max_distance) && (distance_value > min_distance))
183 if ((distance_value > max_distance) || (distance_value < min_distance))
188 if (!std::isfinite ((*cloud)[index].x) ||
189 !std::isfinite ((*cloud)[index].y) ||
190 !std::isfinite ((*cloud)[index].z))
194 min_p = min_p.min (pt);
195 max_p = max_p.max (pt);
213 template <
typename Po
intT>
void
219 PCL_WARN (
"[pcl::%s::applyFilter] No input dataset given!\n", getClassName ().c_str ());
229 Eigen::Vector4f min_p, max_p;
231 if (!filter_field_name_.empty ())
232 getMinMax3D<PointT> (input_, *indices_, filter_field_name_,
static_cast<float> (filter_limit_min_),
static_cast<float> (filter_limit_max_), min_p, max_p, filter_limit_negative_);
234 getMinMax3D<PointT> (*input_, *indices_, min_p, max_p);
237 std::int64_t dx =
static_cast<std::int64_t
>((max_p[0] - min_p[0]) * inverse_leaf_size_[0])+1;
238 std::int64_t dy =
static_cast<std::int64_t
>((max_p[1] - min_p[1]) * inverse_leaf_size_[1])+1;
239 std::int64_t dz =
static_cast<std::int64_t
>((max_p[2] - min_p[2]) * inverse_leaf_size_[2])+1;
241 if ((dx*dy*dz) >
static_cast<std::int64_t
>(std::numeric_limits<std::int32_t>::max()))
243 PCL_WARN(
"[pcl::%s::applyFilter] Leaf size is too small for the input dataset. Integer indices would overflow.", getClassName().c_str());
249 min_b_[0] =
static_cast<int> (std::floor (min_p[0] * inverse_leaf_size_[0]));
250 max_b_[0] =
static_cast<int> (std::floor (max_p[0] * inverse_leaf_size_[0]));
251 min_b_[1] =
static_cast<int> (std::floor (min_p[1] * inverse_leaf_size_[1]));
252 max_b_[1] =
static_cast<int> (std::floor (max_p[1] * inverse_leaf_size_[1]));
253 min_b_[2] =
static_cast<int> (std::floor (min_p[2] * inverse_leaf_size_[2]));
254 max_b_[2] =
static_cast<int> (std::floor (max_p[2] * inverse_leaf_size_[2]));
257 div_b_ = max_b_ - min_b_ + Eigen::Vector4i::Ones ();
261 divb_mul_ = Eigen::Vector4i (1, div_b_[0], div_b_[0] * div_b_[1], 0);
264 std::vector<cloud_point_index_idx> index_vector;
265 index_vector.reserve (indices_->size ());
268 if (!filter_field_name_.empty ())
271 std::vector<pcl::PCLPointField> fields;
272 int distance_idx = pcl::getFieldIndex<PointT> (filter_field_name_, fields);
273 if (distance_idx == -1)
274 PCL_WARN (
"[pcl::%s::applyFilter] Invalid filter field name. Index is %d.\n", getClassName ().c_str (), distance_idx);
279 for (
const auto& index : (*indices_))
281 if (!input_->is_dense)
287 const std::uint8_t* pt_data =
reinterpret_cast<const std::uint8_t*
> (&(*input_)[index]);
288 float distance_value = 0;
289 memcpy (&distance_value, pt_data + fields[distance_idx].offset,
sizeof (
float));
291 if (filter_limit_negative_)
294 if ((distance_value < filter_limit_max_) && (distance_value > filter_limit_min_))
300 if ((distance_value > filter_limit_max_) || (distance_value < filter_limit_min_))
304 int ijk0 =
static_cast<int> (std::floor ((*input_)[index].x * inverse_leaf_size_[0]) -
static_cast<float> (min_b_[0]));
305 int ijk1 =
static_cast<int> (std::floor ((*input_)[index].y * inverse_leaf_size_[1]) -
static_cast<float> (min_b_[1]));
306 int ijk2 =
static_cast<int> (std::floor ((*input_)[index].z * inverse_leaf_size_[2]) -
static_cast<float> (min_b_[2]));
309 int idx = ijk0 * divb_mul_[0] + ijk1 * divb_mul_[1] + ijk2 * divb_mul_[2];
310 index_vector.emplace_back(
static_cast<unsigned int> (idx), index);
319 for (
const auto& index : (*indices_))
321 if (!input_->is_dense)
326 int ijk0 =
static_cast<int> (std::floor ((*input_)[index].x * inverse_leaf_size_[0]) -
static_cast<float> (min_b_[0]));
327 int ijk1 =
static_cast<int> (std::floor ((*input_)[index].y * inverse_leaf_size_[1]) -
static_cast<float> (min_b_[1]));
328 int ijk2 =
static_cast<int> (std::floor ((*input_)[index].z * inverse_leaf_size_[2]) -
static_cast<float> (min_b_[2]));
331 int idx = ijk0 * divb_mul_[0] + ijk1 * divb_mul_[1] + ijk2 * divb_mul_[2];
332 index_vector.emplace_back(
static_cast<unsigned int> (idx), index);
339 boost::sort::spreadsort::integer_sort(index_vector.begin(), index_vector.end(), rightshift_func);
343 unsigned int total = 0;
344 unsigned int index = 0;
348 std::vector<std::pair<unsigned int, unsigned int> > first_and_last_indices_vector;
350 first_and_last_indices_vector.reserve (index_vector.size ());
351 while (index < index_vector.size ())
353 unsigned int i = index + 1;
354 while (i < index_vector.size () && index_vector[i].idx == index_vector[index].idx)
356 if (i - index >= min_points_per_voxel_)
359 first_and_last_indices_vector.emplace_back(index, i);
366 if (save_leaf_layout_)
371 std::uint32_t new_layout_size = div_b_[0]*div_b_[1]*div_b_[2];
373 std::uint32_t reinit_size = std::min (
static_cast<unsigned int> (new_layout_size),
static_cast<unsigned int> (leaf_layout_.size()));
374 for (std::uint32_t i = 0; i < reinit_size; i++)
376 leaf_layout_[i] = -1;
378 leaf_layout_.resize (new_layout_size, -1);
380 catch (std::bad_alloc&)
382 throw PCLException(
"VoxelGrid bin size is too low; impossible to allocate memory for layout",
383 "voxel_grid.hpp",
"applyFilter");
385 catch (std::length_error&)
387 throw PCLException(
"VoxelGrid bin size is too low; impossible to allocate memory for layout",
388 "voxel_grid.hpp",
"applyFilter");
393 for (
const auto &cp : first_and_last_indices_vector)
396 unsigned int first_index = cp.first;
397 unsigned int last_index = cp.second;
400 if (save_leaf_layout_)
401 leaf_layout_[index_vector[first_index].idx] = index;
404 if (!downsample_all_data_)
406 Eigen::Vector4f centroid (Eigen::Vector4f::Zero ());
408 for (
unsigned int li = first_index; li < last_index; ++li)
409 centroid += (*input_)[index_vector[li].cloud_point_index].getVector4fMap ();
411 centroid /=
static_cast<float> (last_index - first_index);
412 output[index].getVector4fMap () = centroid;
419 for (
unsigned int li = first_index; li < last_index; ++li)
420 centroid.
add ((*input_)[index_vector[li].cloud_point_index]);
422 centroid.
get (output[index]);
430 #define PCL_INSTANTIATE_VoxelGrid(T) template class PCL_EXPORTS pcl::VoxelGrid<T>;
431 #define PCL_INSTANTIATE_getMinMax3D(T) template PCL_EXPORTS void pcl::getMinMax3D<T> (const pcl::PointCloud<T>::ConstPtr &, const std::string &, float, float, Eigen::Vector4f &, Eigen::Vector4f &, bool);
433 #endif // PCL_FILTERS_IMPL_VOXEL_GRID_H_