Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve VoxelGrid filter runtime by ~20% by using spread sort #3853

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions filters/include/pcl/filters/impl/voxel_grid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <pcl/common/common.h>
#include <pcl/common/io.h>
#include <pcl/filters/voxel_grid.h>
#include <boost/sort/spreadsort/integer_sort.hpp>

///////////////////////////////////////////////////////////////////////////////////////////
template <typename PointT> void
Expand Down Expand Up @@ -205,6 +206,7 @@ struct cloud_point_index_idx
unsigned int idx;
unsigned int cloud_point_index;

cloud_point_index_idx() = default;
cloud_point_index_idx (unsigned int idx_, unsigned int cloud_point_index_) : idx (idx_), cloud_point_index (cloud_point_index_) {}
bool operator < (const cloud_point_index_idx &p) const { return (idx < p.idx); }
};
Expand Down Expand Up @@ -339,8 +341,9 @@ pcl::VoxelGrid<PointT>::applyFilter (PointCloud &output)

// Second pass: sort the index_vector vector using value representing target cell as index
// in effect all points belonging to the same output cell will be next to each other
std::sort (index_vector.begin (), index_vector.end (), std::less<cloud_point_index_idx> ());

auto rightshift_func = [](const cloud_point_index_idx &x, const unsigned offset) { return x.idx >> offset; };
boost::sort::spreadsort::integer_sort(index_vector.begin(), index_vector.end(), rightshift_func);

// Third pass: count output cells
// we need to skip all the same, adjacent idx values
unsigned int total = 0;
Expand Down
5 changes: 3 additions & 2 deletions filters/src/voxel_grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <iostream>
#include <pcl/common/io.h>
#include <pcl/filters/impl/voxel_grid.hpp>
#include <boost/sort/spreadsort/integer_sort.hpp>

using Array4size_t = Eigen::Array<std::size_t, 4, 1>;

Expand Down Expand Up @@ -377,7 +378,8 @@ pcl::VoxelGrid<pcl::PCLPointCloud2>::applyFilter (PCLPointCloud2 &output)

// Second pass: sort the index_vector vector using value representing target cell as index
// in effect all points belonging to the same output cell will be next to each other
std::sort (index_vector.begin (), index_vector.end (), std::less<cloud_point_index_idx> ());
auto rightshift_func = [](const cloud_point_index_idx &x, const unsigned offset) { return x.idx >> offset; };
boost::sort::spreadsort::integer_sort(index_vector.begin(), index_vector.end(), rightshift_func);

// Third pass: count output cells
// we need to skip all the same, adjacenent idx values
Expand Down Expand Up @@ -549,4 +551,3 @@ PCL_INSTANTIATE(getMinMax3D, PCL_XYZ_POINT_TYPES)
PCL_INSTANTIATE(VoxelGrid, PCL_XYZ_POINT_TYPES)

#endif // PCL_NO_PRECOMPILE