PointcloudProcessor.cpp
Go to the documentation of this file.
1 /*
2  * PointcloudProcessor.cpp
3  *
4  * Created on: Nov 21, 2019
5  * Author: Edo Jelavic
6  * Institute: ETH Zurich, Robotic Systems Lab
7  */
8 
10 
11 #include <pcl/common/common.h>
12 #include <pcl/common/transforms.h>
13 #include <pcl/conversions.h>
14 #include <pcl/filters/passthrough.h>
15 #include <pcl/filters/statistical_outlier_removal.h>
16 #include <pcl/filters/voxel_grid.h>
17 #include <pcl/io/pcd_io.h>
18 #include <pcl/kdtree/kdtree_flann.h>
19 #include <pcl/point_types.h>
20 #include <pcl/segmentation/extract_clusters.h>
21 
22 #include <ros/console.h>
23 
24 namespace grid_map {
25 namespace grid_map_pcl {
26 
28  params_ = std::make_unique<grid_map_pcl::PclLoaderParameters>();
29 }
30 
31 void PointcloudProcessor::loadParameters(const std::string& filename) {
32  params_->loadParameters(filename);
33 }
34 
35 Pointcloud::Ptr PointcloudProcessor::removeOutliersFromInputCloud(Pointcloud::ConstPtr inputCloud) const {
36  pcl::StatisticalOutlierRemoval<pcl::PointXYZ> sor;
37  sor.setInputCloud(inputCloud);
38  sor.setMeanK(params_->get().outlierRemoval_.meanK_);
39  sor.setStddevMulThresh(params_->get().outlierRemoval_.stddevThreshold_);
40  Pointcloud::Ptr filteredCloud(new Pointcloud());
41  sor.filter(*filteredCloud);
42  return filteredCloud;
43 }
44 
45 std::vector<Pointcloud::Ptr> PointcloudProcessor::extractClusterCloudsFromPointcloud(Pointcloud::ConstPtr inputCloud) const {
46  std::vector<pcl::PointIndices> clusterIndices = extractClusterIndicesFromPointcloud(inputCloud);
47  std::vector<Pointcloud::Ptr> clusterClouds;
48  clusterClouds.reserve(clusterIndices.size());
49  for (const auto& indicesSet : clusterIndices) {
50  Pointcloud::Ptr clusterCloud = makeCloudFromIndices(indicesSet.indices, inputCloud);
51  clusterClouds.push_back(clusterCloud);
52  }
53 
54  return clusterClouds;
55 }
56 
57 // todo (jelavice) maybe use the libpointmatcher for this?? faster?
58 std::vector<pcl::PointIndices> PointcloudProcessor::extractClusterIndicesFromPointcloud(Pointcloud::ConstPtr inputCloud) const {
59  // Create a kd tree to cluster the input point cloud
60  pcl::search::KdTree<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>);
61  tree->setInputCloud(inputCloud);
62  std::vector<pcl::PointIndices> clusterIndices;
63  pcl::EuclideanClusterExtraction<pcl::PointXYZ> euclideanClusterExtraction;
64  euclideanClusterExtraction.setClusterTolerance(params_->get().clusterExtraction_.clusterTolerance_);
65  euclideanClusterExtraction.setMinClusterSize(params_->get().clusterExtraction_.minNumPoints_);
66  euclideanClusterExtraction.setMaxClusterSize(params_->get().clusterExtraction_.maxNumPoints_);
67  euclideanClusterExtraction.setSearchMethod(tree);
68  euclideanClusterExtraction.setInputCloud(inputCloud);
69  euclideanClusterExtraction.extract(clusterIndices);
70 
71  return clusterIndices;
72 }
73 
74 Pointcloud::Ptr PointcloudProcessor::makeCloudFromIndices(const std::vector<int>& indices, Pointcloud::ConstPtr inputCloud) {
75  Pointcloud::Ptr cloud(new Pointcloud());
76 
77  cloud->points.reserve(indices.size());
78  for (auto index : indices) {
79  cloud->points.push_back(inputCloud->points[index]);
80  }
81 
82  cloud->is_dense = true;
83 
84  return cloud;
85 }
86 
87 Pointcloud::Ptr PointcloudProcessor::downsampleInputCloud(Pointcloud::ConstPtr inputCloud) const {
88  pcl::VoxelGrid<pcl::PointXYZ> voxelGrid;
89  voxelGrid.setInputCloud(inputCloud);
90  const auto& voxelSize = params_->get().downsampling_.voxelSize_;
91  voxelGrid.setLeafSize(voxelSize.x(), voxelSize.y(), voxelSize.z());
92  Pointcloud::Ptr downsampledCloud(new Pointcloud());
93  voxelGrid.filter(*downsampledCloud);
94  return downsampledCloud;
95 }
96 
97 void PointcloudProcessor::savePointCloudAsPcdFile(const std::string& filename, const Pointcloud& cloud) {
98  pcl::PCDWriter writer;
99  pcl::PCLPointCloud2 pointCloud2;
100  pcl::toPCLPointCloud2(cloud, pointCloud2);
101  writer.write(filename, pointCloud2, Eigen::Vector4f::Zero(), Eigen::Quaternionf::Identity(), false);
102 }
103 
104 Pointcloud::Ptr PointcloudProcessor::applyRigidBodyTransformation(Pointcloud::ConstPtr inputCloud) const {
105  auto transformedCloud =
106  grid_map_pcl::transformCloud(inputCloud, grid_map_pcl::getRigidBodyTransform(params_->get().cloudTransformation_.translation_,
107  params_->get().cloudTransformation_.rpyIntrinsic_));
108  return transformedCloud;
109 }
110 
111 } /* namespace grid_map_pcl */
112 
113 } /* namespace grid_map*/
std::vector< Pointcloud::Ptr > extractClusterCloudsFromPointcloud(Pointcloud::ConstPtr inputCloud) const
Pointcloud::Ptr applyRigidBodyTransformation(Pointcloud::ConstPtr inputCloud) const
static Pointcloud::Ptr makeCloudFromIndices(const std::vector< int > &indices, Pointcloud::ConstPtr inputCloud)
Pointcloud::Ptr downsampleInputCloud(Pointcloud::ConstPtr inputCloud) const
void loadParameters(const std::string &filename)
std::vector< pcl::PointIndices > extractClusterIndicesFromPointcloud(Pointcloud::ConstPtr inputCloud) const
Pointcloud::Ptr transformCloud(Pointcloud::ConstPtr inputCloud, const Eigen::Affine3f &transformMatrix)
Definition: helpers.cpp:156
static void savePointCloudAsPcdFile(const std::string &filename, const Pointcloud &cloud)
Pointcloud::Ptr removeOutliersFromInputCloud(Pointcloud::ConstPtr inputCloud) const
::pcl::PointCloud< Point > Pointcloud
Definition: helpers.hpp:44
Eigen::Affine3f getRigidBodyTransform(const Eigen::Vector3d &translation, const Eigen::Vector3d &intrinsicRpy)
Definition: helpers.cpp:104
std::unique_ptr< grid_map_pcl::PclLoaderParameters > params_


grid_map_pcl
Author(s): Dominic Jud , Edo Jelavic
autogenerated on Tue Jun 1 2021 02:13:43