Loading [MathJax]/extensions/tex2jax.js
ed_gui_server
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules
rviz_publisher.cpp
Go to the documentation of this file.
1 #include <ed_gui_server_msgs/EntityInfos.h>
2 #include <ed_gui_server_msgs/QueryMeshes.h>
3 
4 #include <ros/init.h>
5 #include <ros/node_handle.h>
6 #include <ros/subscriber.h>
7 #include <ros/publisher.h>
8 
10 
11 #include <visualization_msgs/MarkerArray.h>
12 
13 struct EntityViz
14 {
16 
17  unsigned int visual_revision;
18  visualization_msgs::Marker marker;
19  visualization_msgs::Marker text_marker;
20  unsigned int num_id;
21  unsigned int text_num_id;
22 
23 };
24 
26 ed_gui_server_msgs::QueryMeshes query_meshes_srv;
27 
28 visualization_msgs::MarkerArray marker_msg;
29 
30 double RATE = 10;
31 
32 // ----------------------------------------------------------------------------------------------------
33 
34 float COLORS[27][3] = { { 0.6, 0.6, 0.6},
35  { 0.6, 0.6, 0.4},
36  { 0.6, 0.6, 0.2},
37  { 0.6, 0.4, 0.6},
38  { 0.6, 0.4, 0.4},
39  { 0.6, 0.4, 0.2},
40  { 0.6, 0.2, 0.6},
41  { 0.6, 0.2, 0.4},
42  { 0.6, 0.2, 0.2},
43  { 0.4, 0.6, 0.6},
44  { 0.4, 0.6, 0.4},
45  { 0.4, 0.6, 0.2},
46  { 0.4, 0.4, 0.6},
47  { 0.4, 0.4, 0.4},
48  { 0.4, 0.4, 0.2},
49  { 0.4, 0.2, 0.6},
50  { 0.4, 0.2, 0.4},
51  { 0.4, 0.2, 0.2},
52  { 0.2, 0.6, 0.6},
53  { 0.2, 0.6, 0.4},
54  { 0.2, 0.6, 0.2},
55  { 0.2, 0.4, 0.6},
56  { 0.2, 0.4, 0.4},
57  { 0.2, 0.4, 0.2},
58  { 0.2, 0.2, 0.6},
59  { 0.2, 0.2, 0.4},
60  { 0.2, 0.2, 0.2}
61  };
62 
63 // ----------------------------------------------------------------------------------------------------
64 
65 unsigned int djb2(const std::string& str)
66 {
67  int hash = 5381;
68  for(unsigned int i = 0; i < str.size(); ++i)
69  hash = ((hash << 5) + hash) + str[i]; /* hash * 33 + c */
70 
71  if (hash < 0)
72  hash = -hash;
73 
74  return hash;
75 }
76 
77 // ----------------------------------------------------------------------------------------------------
78 
79 void initMarker(const std::string& id, visualization_msgs::Marker& m)
80 {
81  m.color.a = 1;
82  m.lifetime = ros::Duration(1.0 / RATE * 4);
83  m.action = visualization_msgs::Marker::ADD;
84  m.header.frame_id = "map";
85 
86  int i_color = djb2(id) % 27;
87  m.color.r = COLORS[i_color][0];
88  m.color.g = COLORS[i_color][1];
89  m.color.b = COLORS[i_color][2];
90 }
91 
92 // ----------------------------------------------------------------------------------------------------
93 
94 void meshToMarker(const ed_gui_server_msgs::Mesh& mesh, visualization_msgs::Marker& m)
95 {
96  m.type = visualization_msgs::Marker::TRIANGLE_LIST;
97  m.scale.x = m.scale.y = m.scale.z = 1.0;
98 
99  m.points.resize(mesh.vertices.size() / 3);
100  for(unsigned int i = 0; i < m.points.size(); ++i )
101  {
102  unsigned int i3 = 3 * i;
103  m.points[i].x = mesh.vertices[i3];
104  m.points[i].y = mesh.vertices[i3 + 1];
105  m.points[i].z = mesh.vertices[i3 + 2];
106  }
107 }
108 
109 // ----------------------------------------------------------------------------------------------------
110 
111 void polygonToMarker(const ed_gui_server_msgs::EntityInfo& e, visualization_msgs::Marker& m)
112 {
113  m.type = visualization_msgs::Marker::LINE_LIST;
114  m.scale.x = 0.01;
115 
116  for(unsigned int i = 0; i < e.polygon.xs.size(); ++i)
117  {
118  int j = (i + 1) % e.polygon.xs.size();
119 
120  float x1 = e.polygon.xs[i];
121  float x2 = e.polygon.xs[j];
122 
123  float y1 = e.polygon.ys[i];
124  float y2 = e.polygon.ys[j];
125 
126 
127  geometry_msgs::Point p;
128 
129  // low line
130  p.x = x1; p.y = y1; p.z = e.polygon.z_min;
131  m.points.push_back(p);
132 
133  p.x = x2; p.y = y2; p.z = e.polygon.z_min;
134  m.points.push_back(p);
135 
136  // high line
137  p.x = x1; p.y = y1; p.z = e.polygon.z_max;
138  m.points.push_back(p);
139 
140  p.x = x2; p.y = y2; p.z = e.polygon.z_max;
141  m.points.push_back(p);
142 
143  // vertical line
144  p.x = x1; p.y = y1; p.z = e.polygon.z_min;
145  m.points.push_back(p);
146 
147  p.x = x1; p.y = y1; p.z = e.polygon.z_max;
148  m.points.push_back(p);
149  }
150 }
151 
152 // ----------------------------------------------------------------------------------------------------
153 
154 void entityCallback(const ed_gui_server_msgs::EntityInfos::ConstPtr& msg)
155 {
156  for(unsigned int i = 0; i < msg->entities.size(); ++i)
157  {
158  const ed_gui_server_msgs::EntityInfo& info = msg->entities[i];
159 
160  if (info.id.size() >= 5 && info.id.substr(info.id.size() - 5) == "floor")
161  continue; // Filter floor
162 
163  if (!info.has_pose)
164  continue;
165 
166  EntityViz* entity_viz;
167 
169  if (it == entities.end())
170  {
171  entity_viz = &entities[info.id];
172  entity_viz->num_id = (entities.size() - 1) * 2;
173  entity_viz->text_num_id = entity_viz->num_id + 1;
174 
175  initMarker(info.id, entity_viz->marker);
176  entity_viz->marker.id = entity_viz->num_id;
177 
178  initMarker(info.id, entity_viz->text_marker);
179  entity_viz->text_marker.id = entity_viz->text_num_id * 1000;
180  }
181  else
182  entity_viz = &it->second;
183 
184  // HACK!: Check if this is a human
185  if (info.color.a == 1 && info.color.r == 2 && info.color.g == 3 && (info.color.b == 4 || info.color.b == 5))
186  {
187  // This is a human
188  marker_msg.markers.push_back(visualization_msgs::Marker());
189  visualization_msgs::Marker& m = marker_msg.markers.back();
190 
191  initMarker(info.id, m);
192  m.id = entity_viz->num_id;
193 
194  m.header.stamp = ros::Time::now();
195  m.type = visualization_msgs::Marker::CYLINDER;
196 
197  m.color.a = 1.0;
198 
199  if (info.color.b == 4)
200  {
201  m.color.r = 0.2; m.color.g = 0.7; m.color.b = 1.0; // human
202  }
203  else
204  {
205  m.color.r = 1.0; m.color.g = 0.7; m.color.b = 0.2; // possible human (based on laser)
206  }
207 
208  m.pose = info.pose;
209  m.pose.position.z = .5;
210 
211  m.scale.x = 0.3; m.scale.y = 0.3; m.scale.z = 1.0;
212  m.ns = "humans";
213 
214  continue;
215  }
216 
217  if (info.visual_revision > entity_viz->visual_revision)
218  {
219  query_meshes_srv.request.entity_ids.push_back(info.id);
220  continue;
221  }
222 
223  marker_msg.markers.push_back(entity_viz->marker);
224  visualization_msgs::Marker& m = marker_msg.markers.back();
225 
226  // Set the pose and timestamp
227  m.pose = info.pose;
228  m.header.stamp = ros::Time::now();
229  m.ns = "entities";
230 
231  // Set the color
232  if (info.color.a != 0)
233  {
234  m.color.r = (float)info.color.r / 255;
235  m.color.g = (float)info.color.g / 255;
236  m.color.b = (float)info.color.b / 255;
237  }
238 
239  m.color.a = info.existence_probability;
240 
241  if (info.visual_revision == 0)
242  {
243  // Update polygon
244  polygonToMarker(info, m);
245  }
246 
247  // Add text
248  marker_msg.markers.push_back(entity_viz->text_marker);
249  visualization_msgs::Marker& m_text = marker_msg.markers.back();
250 
251  m_text.type = visualization_msgs::Marker::TEXT_VIEW_FACING;
252 
253  m_text.scale.x = m_text.scale.y = m_text.scale.z = 0.1;
254 
255  m_text.color.r = 0.9;
256  m_text.color.g = 0.9;
257  m_text.color.b = 0.9;
258 
259  m_text.pose = m.pose;
260  m_text.pose.position.z += 0.1;
261  m_text.header = m.header;
262  m_text.ns = "labels";
263 
264  m_text.text = info.id.substr(0,4);
265 
266  if (!info.type.empty())
267  m_text.text += " (" + info.type + ")";
268 
269 // std::stringstream ss_text;
270 // ss_text << info.id.substr(0, 4);
271 // if (!info.type.empty())
272 // ss_text << " (" << info.type.substr(0, 4) << ")";
273 
274 // ss_text.precision(2);
275 // ss_text << std::fixed << " (" << info.existence_probability << ")";
276 
277 
278 
279 // if (info.type != "" && (!info.polygon.xs.empty() || info.mesh_revision > 0))
280 // {
281 // std::stringstream ss_text;
282 // ss_text << info.type;
283 // m_text.text = ss_text.str();
284 // }
285 // else
286 // {
287 // m_text.text = "";
288 // }
289  }
290 }
291 
292 // ----------------------------------------------------------------------------------------------------
293 
294 int main(int argc, char **argv)
295 {
296  ros::init(argc, argv, "ed_rviz_publisher");
297 
298  std::string pub_topic = "ed/rviz";
299  if (argc > 1)
300  pub_topic = argv[1];
301 
302  if (argc > 2)
303  RATE = atof(argv[2]);
304 
305  ros::NodeHandle nh;
306  ros::Subscriber sub = nh.subscribe("ed/gui/entities", 1, entityCallback);
307 
308  ros::ServiceClient client = nh.serviceClient<ed_gui_server_msgs::QueryMeshes>("ed/gui/query_meshes");
309 
310  ros::Publisher pub = nh.advertise<visualization_msgs::MarkerArray>(pub_topic, 1);
311 
312  ros::Rate r(RATE);
313  while(ros::ok())
314  {
315  query_meshes_srv.request.entity_ids.clear();
316  marker_msg.markers.clear();
317 
318  ros::spinOnce();
319 
320  if (!marker_msg.markers.empty())
321  pub.publish(marker_msg);
322 
323  // Query missing meshes (if needed)
324  if (!query_meshes_srv.request.entity_ids.empty())
325  {
326  if (client.call(query_meshes_srv))
327  {
328 
329  for (const ed_gui_server_msgs::EntityMeshAndVolumes& entity_geomertry : query_meshes_srv.response.entity_geometries)
330  {
331  const std::string& id = entity_geomertry.id;
332  const ed_gui_server_msgs::Mesh& mesh = entity_geomertry.mesh;
333 
335  if (it == entities.end())
336  continue;
337 
338  meshToMarker(mesh, it->second.marker);
339 
340  it->second.visual_revision = entity_geomertry.visual_revision;
341  }
342  }
343  else
344  {
345  ROS_ERROR("[ED RVIZ PUBLISHER] Could not query for meshes.");
346  }
347  }
348 
349  r.sleep();
350  }
351 
352  return 0;
353 }
EntityViz::num_id
unsigned int num_id
Definition: rviz_publisher.cpp:20
std::string
initMarker
void initMarker(const std::string &id, visualization_msgs::Marker &m)
Definition: rviz_publisher.cpp:79
std::string::size
T size(T... args)
EntityViz
Definition: rviz_publisher.cpp:13
hash
int hash(const char *str, int max_val)
EntityViz::marker
visualization_msgs::Marker marker
Definition: rviz_publisher.cpp:18
EntityViz::text_num_id
unsigned int text_num_id
Definition: rviz_publisher.cpp:21
marker_msg
visualization_msgs::MarkerArray marker_msg
Definition: rviz_publisher.cpp:28
info
std::ostream & info()
polygonToMarker
void polygonToMarker(const ed_gui_server_msgs::EntityInfo &e, visualization_msgs::Marker &m)
Definition: rviz_publisher.cpp:111
EntityViz::EntityViz
EntityViz()
Definition: rviz_publisher.cpp:15
EntityViz::visual_revision
unsigned int visual_revision
Definition: rviz_publisher.cpp:17
client
ros::ServiceClient client
std::map
COLORS
float COLORS[27][3]
Definition: rviz_publisher.cpp:34
main
int main(int argc, char **argv)
Definition: rviz_publisher.cpp:294
entityCallback
void entityCallback(const ed_gui_server_msgs::EntityInfos::ConstPtr &msg)
Definition: rviz_publisher.cpp:154
meshToMarker
void meshToMarker(const ed_gui_server_msgs::Mesh &mesh, visualization_msgs::Marker &m)
Definition: rviz_publisher.cpp:94
djb2
unsigned int djb2(const std::string &str)
Definition: rviz_publisher.cpp:65
EntityViz::text_marker
visualization_msgs::Marker text_marker
Definition: rviz_publisher.cpp:19
RATE
double RATE
Definition: rviz_publisher.cpp:30
query_meshes_srv
ed_gui_server_msgs::QueryMeshes query_meshes_srv
Definition: rviz_publisher.cpp:26
msg_conversions.h
entities
std::map< std::string, EntityViz > entities
Definition: rviz_publisher.cpp:25