publish_meshfile.cpp
Go to the documentation of this file.
1 /************************************************
2 Publish_meshfile file code
3 This file which displays the CAD mesh in rviz
4 represents one node of the entire
5 demonstrator
6 ************************************************/
7 
8 // ROS headers
9 #include <ros/ros.h>
10 #include <ros/service.h>
11 #include <visualization_msgs/Marker.h>
12 #include <pcl_ros/point_cloud.h>
13 
14 #include <fanuc_grinding_publish_meshfile/PublishMeshfileService.h> // Description of the Service we will use
15 
16 // PCL headers
17 #include <pcl/point_cloud.h>
18 #include <pcl/io/ply_io.h>
19 #include <pcl/io/vtk_lib_io.h>
20 #include <pcl/PolygonMesh.h>
21 
23 
25 
27 typedef pcl::PointCloud<PointXYZ> PointCloudXYZ;
28 
35 bool publishMeshFile(fanuc_grinding_publish_meshfile::PublishMeshfileService::Request &req,
36  fanuc_grinding_publish_meshfile::PublishMeshfileService::Response &res)
37 {
38  if (req.ColorA <= 0.1)
39  ROS_WARN_STREAM("publishMeshFile: Alpha is set to 0, mesh will be invisible!");
40 
41  pcl::PolygonMesh mesh;
42  if (boost::filesystem::path (req.MeshName).extension () == ".ply")
43  {
44  if (pcl::io::loadPLYFile(req.MeshName, mesh))
45  {
46  ROS_ERROR_STREAM("publishMeshFile: Could not read PLY file");
47  return true;
48  }
49  }
50  else
51  {
52  pcl::io::loadPolygonFile(req.MeshName, mesh);
53  }
54 
55  visualization_msgs::Marker marker;
56  if(mesh.polygons.size() == 0)
57  {
58  ROS_WARN_STREAM("publishMeshFile: There is no polygon in file, publishing a point cloud");
59  *pub = node->advertise<sensor_msgs::PointCloud2>(req.MarkerName, 1, true);
60  mesh.cloud.header.frame_id = "base_link";
61  pub->publish(mesh.cloud);
62  }
63  else
64  {
65  *pub = node->advertise<visualization_msgs::Marker>(req.MarkerName, 1, true); // Latched
66 
67  marker.header.frame_id = "base_link";
68  marker.header.stamp = ros::Time::now();
69  marker.id = 0;
70 
71  marker.type = visualization_msgs::Marker::MESH_RESOURCE;
72  marker.action = visualization_msgs::Marker::ADD;
73  marker.pose.position.x = req.PosX;
74  marker.pose.position.y = req.PosY;
75  marker.pose.position.z = req.PosZ;
76  marker.pose.orientation.x = req.RotX;
77  marker.pose.orientation.y = req.RotY;
78  marker.pose.orientation.z = req.RotZ;
79  marker.pose.orientation.w = req.RotW;
80  marker.scale.x = 1;
81  marker.scale.y = 1;
82  marker.scale.z = 1;
83  marker.color.a = req.ColorA; // Don't forget to set the alpha!
84  marker.color.r = req.ColorR;
85  marker.color.g = req.ColorG;
86  marker.color.b = req.ColorB;
87  marker.lifetime = ros::Duration();
88  // Only if using a MESH_RESOURCE marker type:
89  marker.mesh_resource = "file://" + req.MeshName;
90  pub->publish(marker);
91  }
92 
93  while (pub->getNumSubscribers() < 1)
94  {
95  ROS_WARN_STREAM("publishMeshFile: No subscriber to the marker: " + req.MarkerName);
96  if (!req.WaitForSubscriber)
97  break;
98  sleep(1);
99  }
100 
101  res.ReturnStatus = true;
102  res.ReturnMessage = req.MarkerName + " published";
103  return true;
104 }
105 
106 int main(int argc, char **argv)
107 {
108  ros::init(argc, argv, "publish_meshfile");
109  node.reset(new ros::NodeHandle);
110  pub.reset(new ros::Publisher);
111  // Create service server and wait for incoming requests
112  ros::ServiceServer service = node->advertiseService("publish_meshfile_service", publishMeshFile);
114  spinner.start();
115 
116  while (node->ok())
117  {
118  sleep(1);
119  }
120  spinner.stop();
121  return 0;
122 }
int main(int argc, char **argv)
bool publishMeshFile(fanuc_grinding_publish_meshfile::PublishMeshfileService::Request &req, fanuc_grinding_publish_meshfile::PublishMeshfileService::Response &res)
boost::shared_ptr< ros::Publisher > pub
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void spinner()
pcl::PointXYZ PointXYZ
boost::shared_ptr< ros::NodeHandle > node
pcl::PointCloud< PointXYZ > PointCloudXYZ
#define ROS_WARN_STREAM(args)
static Time now()
#define ROS_ERROR_STREAM(args)


publish_meshfile
Author(s): Kévin Bolloré, Victor Lamoine - Institut Maupertuis
autogenerated on Thu Dec 19 2019 03:38:21