marker_helper.cpp
Go to the documentation of this file.
1 
18 #include "marker_helper.h"
19 #include <tf/tf.h>
20 #include <ros/package.h>
21 #include <boost/algorithm/string.hpp>
22 #include <rapidxml.hpp>
23 #include <rapidxml_utils.hpp>
24 
25 namespace visualization_server {
26 
27 MarkerHelper::MarkerHelper(double marker_lifetime, const std::string &dome_config_path, const std::string &mild_config_path)
28  : marker_lifetime_(marker_lifetime),
29  dome_config_path_(dome_config_path),
30  mild_config_path_(mild_config_path)
31 {
32 
33 }
34 
35 
36 visualization_msgs::MarkerArray MarkerHelper::getAllMarkersDome() {
37  visualization_msgs::MarkerArray markers_dome = parseXmlFile(dome_config_path_);
38  return markers_dome;
39 }
40 
41 visualization_msgs::MarkerArray MarkerHelper::getAllMarkersMild() {
42 
43  visualization_msgs::MarkerArray markers_mild = parseXmlFile(mild_config_path_);
44  return markers_mild;
45 }
46 
47 visualization_msgs::MarkerArray MarkerHelper::parseXmlFile(std::string xml_path) {
48  visualization_msgs::MarkerArray markers;
49 
50  std::string abs_xml_path;
51  if (boost::starts_with(xml_path, ".")) {
52  abs_xml_path = ros::package::getPath("asr_visualization_server") + xml_path.substr(1);
53  }
54  else {
55  abs_xml_path = xml_path;
56  }
57 
58  try {
59  rapidxml::file<> xmlFile(abs_xml_path.c_str());
60  rapidxml::xml_document<> doc;
61  doc.parse<0>(xmlFile.data());
62 
63  rapidxml::xml_node<> *root_node = doc.first_node();
64  if (root_node) {
65  rapidxml::xml_node<> *child_node = root_node->first_node();
66  int i = 0;
67  while (child_node) {
68  rapidxml::xml_attribute<> *name_attribute = child_node->first_attribute("name");
69  rapidxml::xml_attribute<> *scale_attribute = child_node->first_attribute("scale");
70  rapidxml::xml_attribute<> *mesh_attribute = child_node->first_attribute("mesh");
71  rapidxml::xml_attribute<> *mat_attribute = child_node->first_attribute("use_mat");
72  if (name_attribute && scale_attribute && mesh_attribute && mat_attribute) {
73  std::string name = name_attribute->value();
74  std::string mesh = mesh_attribute->value();
75  std::string scale = scale_attribute->value();
76  std::string pose_string = child_node->value();
77  bool use_mat = boost::lexical_cast<bool>(mat_attribute->value());
78  std::vector<double> pose_vec;
79  std::vector<double> scale_vec;
80  if (parseDoubleCsv(pose_string, pose_vec, " ,") && pose_vec.size() == 7) {
81  if (parseDoubleCsv(scale, scale_vec, " ,") && scale_vec.size() == 3) {
82  markers.markers.push_back(createMarker(name, mesh, pose_vec, scale_vec, i, use_mat));
83  i++;
84  }
85  }
86 
87  }
88  child_node = child_node->next_sibling();
89  }
90  }
91  } catch(std::runtime_error err) {
92  ROS_DEBUG_STREAM("Can't parse xml-file. Runtime error: " << err.what());
93  } catch (rapidxml::parse_error err) {
94  ROS_DEBUG_STREAM("Can't parse xml-file Parse error: " << err.what());
95  } catch (boost::bad_lexical_cast err) {
96  ROS_DEBUG_STREAM("Can't cast use_mat. Cast error: " << err.what());
97  }
98 
99  return markers;
100 }
101 
102 bool MarkerHelper::parseDoubleCsv(std::string csv_in, std::vector<double> &csv_out, std::string delim) {
103  std::vector<std::string> strvec;
104 
105  boost::algorithm::trim_if(csv_in, boost::algorithm::is_any_of(delim));
106  boost::algorithm::split(strvec, csv_in, boost::algorithm::is_any_of(delim), boost::algorithm::token_compress_on);
107  for (std::string str : strvec) {
108  try {
109  csv_out.push_back(boost::lexical_cast<double>(str));
110  } catch (boost::bad_lexical_cast err) {
111  ROS_DEBUG_STREAM("Can't cast node-value. Cast error: " << err.what());
112  return false;
113  }
114  }
115  return true;
116 }
117 
118 visualization_msgs::Marker MarkerHelper::createMarker(const std::string &name, const std::string &mesh, const std::vector<double> &pose, const std::vector<double> &scale, int id, bool use_mat) {
119  visualization_msgs::Marker m;
120 
121  m.header.frame_id = "/map";
122  m.type = visualization_msgs::Marker::MESH_RESOURCE;
123  m.action = visualization_msgs::Marker::ADD;
124  m.color.a = 1.0;
125  m.color.r = 0.65;
126  m.color.g = 0.65;
127  m.color.b = 0.65;
128  m.lifetime = ros::Duration(marker_lifetime_);
129  m.mesh_resource = mesh;
130  m.mesh_use_embedded_materials = use_mat;
131  m.id = id;
132  m.ns = name;
133  m.scale.x = scale.at(0);
134  m.scale.y = scale.at(1);
135  m.scale.z = scale.at(2);
136  m.pose.position.x = pose.at(0);
137  m.pose.position.y = pose.at(1);
138  m.pose.position.z = pose.at(2);
139  m.pose.orientation.w = pose.at(3);
140  m.pose.orientation.x = pose.at(4);
141  m.pose.orientation.y = pose.at(5);
142  m.pose.orientation.z = pose.at(6);
143 
144  return m;
145 }
146 
147 }
visualization_msgs::MarkerArray getAllMarkersMild()
visualization_msgs::Marker createMarker(const std::string &name, const std::string &mesh, const std::vector< double > &pose, const std::vector< double > &scale, int id, bool use_mat)
visualization_msgs::MarkerArray getAllMarkersDome()
bool parseDoubleCsv(std::string csv_in, std::vector< double > &csv_out, std::string delim)
#define ROS_DEBUG_STREAM(args)
ROSLIB_DECL std::string getPath(const std::string &package_name)
visualization_msgs::MarkerArray parseXmlFile(std::string xml_path)


asr_visualization_server
Author(s): Allgeyer Tobias, Braun Kai, Heller Florian, Mehlhaus Jonas, Meißner Pascal, Qattan Mohamad, Wittenbeck Valerij
autogenerated on Mon Jun 10 2019 12:45:13