helpers.cpp
Go to the documentation of this file.
1 /*
2  * helpers.cpp
3  *
4  * Created on: Nov 20, 2019
5  * Author: Edo Jelavic
6  * Institute: ETH Zurich, Robotic Systems Lab
7  */
8 
10 
13 
14 #include <cstdlib>
15 #include <memory>
16 
17 #include <pcl/common/common.h>
18 #include <pcl/common/transforms.h>
19 #include <pcl/conversions.h>
20 #include <pcl/io/pcd_io.h>
21 #include <pcl/point_types.h>
22 
23 #include <ros/console.h>
24 #include <ros/package.h>
25 
27 
28 namespace grid_map {
29 namespace grid_map_pcl {
30 
32  bool isSetVerbosityLevelToDebug;
33  nh.param<bool>("set_verbosity_to_debug", isSetVerbosityLevelToDebug, false);
34 
35  if (!isSetVerbosityLevelToDebug) {
36  return;
37  }
38 
41  }
42 }
43 
44 std::string getParameterPath(const ros::NodeHandle& nh) {
45  std::string defaultPath = ros::package::getPath("grid_map_pcl") + "/config/parameters.yaml";
46  std::string pathToConfig;
47  nh.param<std::string>("config_file_path", pathToConfig, defaultPath);
48  return pathToConfig;
49 }
50 
51 std::string getOutputBagPath(const ros::NodeHandle& nh) {
52  std::string pathToOutputBag;
53  const std::string defaultPath = ros::package::getPath("grid_map_pcl") + "/data/output_grid_map.bag";
54  nh.param<std::string>("output_grid_map", pathToOutputBag, defaultPath);
55  return pathToOutputBag;
56 }
57 
58 std::string getPcdFilePath(const ros::NodeHandle& nh) {
59  std::string pathToCloud, folderPath;
60  const std::string defaultPathToCloud = "/data/input_cloud.pcd";
61  const std::string defaultFolderPath = ros::package::getPath("grid_map_pcl");
62  nh.param<std::string>("pcd_filename", pathToCloud, defaultPathToCloud);
63  nh.param<std::string>("folder_path", folderPath, defaultFolderPath);
64  return folderPath + "/" + pathToCloud;
65 }
66 
67 std::string getMapFrame(const ros::NodeHandle& nh) {
68  std::string mapFrame;
69  nh.param<std::string>("map_frame", mapFrame, "map");
70  return mapFrame;
71 }
72 
73 std::string getMapRosbagTopic(const ros::NodeHandle& nh) {
74  std::string mapRosbagTopic;
75  nh.param<std::string>("map_rosbag_topic", mapRosbagTopic, "grid_map");
76  return mapRosbagTopic;
77 }
78 
79 std::string getMapLayerName(const ros::NodeHandle& nh) {
80  std::string mapLayerName;
81  nh.param<std::string>("map_layer_name", mapLayerName, "elevation");
82  return mapLayerName;
83 }
84 
85 void saveGridMap(const grid_map::GridMap& gridMap, const ros::NodeHandle& nh, const std::string& mapTopic) {
86  std::string pathToOutputBag = getOutputBagPath(nh);
87  const bool savingSuccessful = grid_map::GridMapRosConverter::saveToBag(gridMap, pathToOutputBag, mapTopic);
88  ROS_INFO_STREAM("Saving grid map successful: " << std::boolalpha << savingSuccessful);
89 }
90 
91 inline void printTimeElapsedToRosInfoStream(const std::chrono::system_clock::time_point& start, const std::string& prefix) {
92  const auto stop = std::chrono::high_resolution_clock::now();
93  const auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start).count() / 1000.0;
94  ROS_INFO_STREAM(prefix << duration << " sec");
95 }
96 
97 void processPointcloud(grid_map::GridMapPclLoader* gridMapPclLoader, const ros::NodeHandle& nh) {
98  const auto start = std::chrono::high_resolution_clock::now();
99  gridMapPclLoader->preProcessInputCloud();
100  gridMapPclLoader->initializeGridMapGeometryFromInputCloud();
101  printTimeElapsedToRosInfoStream(start, "Initialization took: ");
102  gridMapPclLoader->addLayerFromInputCloud(getMapLayerName(nh));
103  printTimeElapsedToRosInfoStream(start, "Total time: ");
104 }
105 
106 Eigen::Affine3f getRigidBodyTransform(const Eigen::Vector3d& translation, const Eigen::Vector3d& intrinsicRpy) {
107  Eigen::Affine3f rigidBodyTransform;
108  rigidBodyTransform.setIdentity();
109  rigidBodyTransform.translation() << translation.x(), translation.y(), translation.z();
110  Eigen::Matrix3f rotation(Eigen::Matrix3f::Identity());
111  rotation *= getRotationMatrix(intrinsicRpy.x(), XYZ::X);
112  rotation *= getRotationMatrix(intrinsicRpy.y(), XYZ::Y);
113  rotation *= getRotationMatrix(intrinsicRpy.z(), XYZ::Z);
114  rigidBodyTransform.rotate(rotation);
115 
116  return rigidBodyTransform;
117 }
118 
119 Eigen::Matrix3f getRotationMatrix(double angle, XYZ axis) {
120  Eigen::Matrix3f rotationMatrix = Eigen::Matrix3f::Identity();
121  switch (axis) {
122  case XYZ::X: {
123  rotationMatrix = Eigen::AngleAxisf(angle, Eigen::Vector3f::UnitX());
124  break;
125  }
126  case XYZ::Y: {
127  rotationMatrix = Eigen::AngleAxisf(angle, Eigen::Vector3f::UnitY());
128  break;
129  }
130  case XYZ::Z: {
131  rotationMatrix = Eigen::AngleAxisf(angle, Eigen::Vector3f::UnitZ());
132  break;
133  }
134  default:
135  ROS_ERROR("Unknown axis while trying to rotate the pointcloud");
136  }
137  return rotationMatrix;
138 }
139 
140 Eigen::Vector3d calculateMeanOfPointPositions(Pointcloud::ConstPtr inputCloud) {
141  Eigen::Vector3d mean = Eigen::Vector3d::Zero();
142  for (const auto& point : inputCloud->points) {
143  mean += Eigen::Vector3d(point.x, point.y, point.z);
144  }
145  mean /= inputCloud->points.size();
146 
147  return mean;
148 }
149 
150 Pointcloud::Ptr loadPointcloudFromPcd(const std::string& filename) {
151  Pointcloud::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
152  pcl::PCLPointCloud2 cloudBlob;
153  pcl::io::loadPCDFile(filename, cloudBlob);
154  pcl::fromPCLPointCloud2(cloudBlob, *cloud);
155  return cloud;
156 }
157 
158 Pointcloud::Ptr transformCloud(Pointcloud::ConstPtr inputCloud, const Eigen::Affine3f& transformMatrix) {
159  Pointcloud::Ptr transformedCloud(new Pointcloud());
160  pcl::transformPointCloud(*inputCloud, *transformedCloud, transformMatrix);
161  return transformedCloud;
162 }
163 
164 } /* namespace grid_map_pcl*/
165 } /* namespace grid_map */
void printTimeElapsedToRosInfoStream(const std::chrono::system_clock::time_point &start, const std::string &prefix)
Definition: helpers.cpp:91
ROSCPP_DECL void start()
ROSCONSOLE_DECL void notifyLoggerLevelsChanged()
std::string getParameterPath(const ros::NodeHandle &nh)
Definition: helpers.cpp:44
std::string getPcdFilePath(const ros::NodeHandle &nh)
Definition: helpers.cpp:58
std::string getOutputBagPath(const ros::NodeHandle &nh)
Definition: helpers.cpp:51
Eigen::Matrix3f getRotationMatrix(double angle, XYZ axis)
Definition: helpers.cpp:119
Eigen::Vector3d calculateMeanOfPointPositions(Pointcloud::ConstPtr inputCloud)
Definition: helpers.cpp:140
bool param(const std::string &param_name, T &param_val, const T &default_val) const
static bool saveToBag(const grid_map::GridMap &gridMap, const std::string &pathToBag, const std::string &topic)
void addLayerFromInputCloud(const std::string &layer)
int loadPCDFile(const std::string &file_name, sensor_msgs::PointCloud2 &cloud)
void processPointcloud(grid_map::GridMapPclLoader *gridMapPclLoader, const ros::NodeHandle &nh)
Definition: helpers.cpp:97
Pointcloud::Ptr loadPointcloudFromPcd(const std::string &filename)
Definition: helpers.cpp:150
std::string getMapLayerName(const ros::NodeHandle &nh)
Definition: helpers.cpp:79
ROSLIB_DECL std::string getPath(const std::string &package_name)
Pointcloud::Ptr transformCloud(Pointcloud::ConstPtr inputCloud, const Eigen::Affine3f &transformMatrix)
Definition: helpers.cpp:158
#define ROS_INFO_STREAM(args)
void setVerbosityLevelToDebugIfFlagSet(const ros::NodeHandle &nh)
Definition: helpers.cpp:31
void saveGridMap(const grid_map::GridMap &gridMap, const ros::NodeHandle &nh, const std::string &mapTopic)
Definition: helpers.cpp:85
ROSCONSOLE_DECL bool set_logger_level(const std::string &name, levels::Level level)
::pcl::PointCloud< Point > Pointcloud
Definition: helpers.hpp:44
Eigen::Affine3f getRigidBodyTransform(const Eigen::Vector3d &translation, const Eigen::Vector3d &intrinsicRpy)
Definition: helpers.cpp:106
std::string getMapFrame(const ros::NodeHandle &nh)
Definition: helpers.cpp:67
#define ROS_ERROR(...)
#define ROSCONSOLE_DEFAULT_NAME
std::string getMapRosbagTopic(const ros::NodeHandle &nh)
Definition: helpers.cpp:73


grid_map_pcl
Author(s): Dominic Jud , Edo Jelavic
autogenerated on Wed Jul 5 2023 02:23:51