load_and_publish_clouds.cpp
Go to the documentation of this file.
1 
18 #include <Utils.h>
19 
20 #include "leica_scanstation_msgs/PointCloudFile.h"
21 #include "leica_scanstation_utils/LeicaUtils.h"
22 
23 #include <CADToPointCloud.h>
24 
25 typedef pcl::PointCloud<pcl::PointXYZ> PointCloudXYZ;
26 typedef pcl::PointCloud<pcl::PointXYZRGB> PointCloudRGB;
27 
28 const int FREQ = 1; // Hz
29 
31 {
32 
33 public:
36 
37  sensor_msgs::PointCloud2 source_cloud_msg_;
38  sensor_msgs::PointCloud2 target_cloud_msg_;
39 
40  bool serviceCb(leica_scanstation_msgs::PointCloudFile::Request& req,
41  leica_scanstation_msgs::PointCloudFile::Response& res)
42  {
43  ROS_INFO("Request to publish clouds");
44 
45  int r = loadCloud(req.source_cloud_file, source_cloud_msg_, 255, 0, 128);
46  if(r==-1) return false;
47 
48  r = loadCloud(req.target_cloud_file, target_cloud_msg_, 0, 0, 255);
49  if(r==-1) return false;
50 
51  ROS_INFO("Publishing clouds on topics: \n\t%s \n\t%s", TARGET_CLOUD_TOPIC.c_str(),
52  SOURCE_CLOUD_TOPIC.c_str());
53  res.message = "Finished service to receive cloud";
54  res.success = true;
55  return true;
56  }
57 
58  int loadCloud(const std::string& file_name,
59  sensor_msgs::PointCloud2& cloud_msg,
60  int R, int G, int B)
61  {
62  PointCloudRGB::Ptr cloud(new PointCloudRGB);
63 
64  std::string extension = boost::filesystem::extension(file_name);
65 
66  std::string f = LeicaUtils::getFilePath(file_name);
67  if (file_name.empty() || extension.empty() || !boost::filesystem::exists(f))
68  {
69  ROS_ERROR("Couldn't read file %s", f.c_str());
70  return -1;
71  }
72 
73  if (extension==".pcd")
74  {
75  pcl::io::loadPCDFile<pcl::PointXYZRGB>(f, *cloud);
76  if(!Utils::isValidCloud(cloud)) return -1;
77  Utils::colorizeCloud(cloud, R, G, B); // scanned cloud is pink
78  }
79  else if (extension==".ply" || extension==".obj")
80  {
81  int sample_points = 500000;
82  CADToPointCloud cad2pc(f, sample_points);
83  cad2pc.convertCloud(cloud);
84  if(!Utils::isValidCloud(cloud)) return -1;
85  Utils::colorizeCloud(cloud, 0, 0, 255); // cad cloud is blue
86  }
87  else
88  ROS_ERROR("Couldn't read file %s", f.c_str());
89 
90  ROS_INFO("Loaded file: %s", f.c_str());
91  Utils::cloudToROSMsg(cloud, cloud_msg);
92 
93  return 0;
94  }
95 };
96 
97 
98 int main(int argc, char** argv)
99 {
100  ros::init(argc, argv, "cloud_publisher");
101  ros::NodeHandle nh;
102 
103  std::string pointcloud_folder_path;
104  if (!nh.getParam("/pointcloud_folder", pointcloud_folder_path))
105  {
106  pointcloud_folder_path = LeicaUtils::findPointcloudFolderPath();
107  }
108 
109  ROS_INFO("Search for pointcloud in %s", pointcloud_folder_path.c_str());
110 
111  CloudLoader cl;
112 
113  ros::ServiceServer service = nh.advertiseService("publish_clouds",
114  &CloudLoader::serviceCb, &cl);
115  ros::Publisher pub = nh.advertise<sensor_msgs::PointCloud2>("source/cloud", 1);
116  ros::Publisher tar_pub = nh.advertise<sensor_msgs::PointCloud2>("target/cloud", 1);
117 
118  ROS_INFO("Service waiting for call to /publish_clouds");
119 
120  ros::Rate r(FREQ);
121  while (ros::ok())
122  {
123  if (Utils::isValidCloudMsg(cl.source_cloud_msg_) &&
124  Utils::isValidCloudMsg(cl.target_cloud_msg_))
125  {
126  pub.publish(cl.source_cloud_msg_);
127  r.sleep();
128  tar_pub.publish(cl.target_cloud_msg_);
129  }
130  ros::spinOnce();
131  r.sleep();
132  }
133 
134  return 0;
135 }
pcl::PointCloud< pcl::PointXYZRGB > PointCloudRGB
bool serviceCb(leica_scanstation_msgs::PointCloudFile::Request &req, leica_scanstation_msgs::PointCloudFile::Response &res)
void publish(const boost::shared_ptr< M > &message) const
f
const std::string SOURCE_CLOUD_TOPIC
Definition: Utils.cpp:25
sensor_msgs::PointCloud2 target_cloud_msg_
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
pcl::PointCloud< pcl::PointXYZ > PointCloudXYZ
static void colorizeCloud(PointCloudRGB::Ptr cloud_rgb, int R, int G, int B)
Apply RGB values to cloud.
Definition: Utils.cpp:84
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
static bool isValidCloud(PointCloudXYZ::Ptr cloud)
Check whether cloud contains data and is not empty.
Definition: Utils.cpp:46
int main(int argc, char **argv)
const int FREQ
const std::string TARGET_CLOUD_TOPIC
Definition: Utils.cpp:24
static void cloudToROSMsg(PointCloudRGB::Ptr cloud, sensor_msgs::PointCloud2 &cloud_msg, const std::string &frameid="world")
Convert XYZRGB cloud to PointCloud2.
Definition: Utils.cpp:100
#define ROS_INFO(...)
ROSCPP_DECL bool ok()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
This class is used to obtain pcl::PointCloud from CAD file. Supported formats: .OBJ.
bool sleep()
void convertCloud(PointCloudRGB::Ptr cloud)
convert cloud defined by path in construstor into cloud
bool getParam(const std::string &key, std::string &s) const
ROSCPP_DECL void spinOnce()
static bool isValidCloudMsg(const sensor_msgs::PointCloud2 &cloud_msg)
Check whether PointCloud2 contains data and is not empty.
Definition: Utils.cpp:66
#define ROS_ERROR(...)
sensor_msgs::PointCloud2 source_cloud_msg_
pcl::PointCloud< pcl::PointXYZRGB > PointCloudRGB
int loadCloud(const std::string &file_name, sensor_msgs::PointCloud2 &cloud_msg, int R, int G, int B)


leica_point_cloud_processing
Author(s): Ines Lara Sicilia
autogenerated on Fri Feb 5 2021 03:20:30