marker_helper.cpp
Go to the documentation of this file.
00001 
00018 #include "marker_helper.h"
00019 #include <tf/tf.h>
00020 #include <ros/package.h>
00021 #include <boost/algorithm/string.hpp>
00022 #include <rapidxml.hpp>
00023 #include <rapidxml_utils.hpp>
00024 
00025 namespace visualization_server {
00026 
00027 MarkerHelper::MarkerHelper(double marker_lifetime, const std::string &dome_config_path, const std::string &mild_config_path)
00028     : marker_lifetime_(marker_lifetime),
00029       dome_config_path_(dome_config_path),
00030       mild_config_path_(mild_config_path)
00031 {
00032 
00033 }
00034 
00035 
00036 visualization_msgs::MarkerArray MarkerHelper::getAllMarkersDome() {
00037     visualization_msgs::MarkerArray markers_dome = parseXmlFile(dome_config_path_);
00038     return markers_dome;
00039 }
00040 
00041 visualization_msgs::MarkerArray MarkerHelper::getAllMarkersMild() {
00042 
00043     visualization_msgs::MarkerArray markers_mild = parseXmlFile(mild_config_path_);
00044     return markers_mild;
00045 }
00046 
00047 visualization_msgs::MarkerArray MarkerHelper::parseXmlFile(std::string xml_path) {
00048     visualization_msgs::MarkerArray markers;
00049 
00050     std::string abs_xml_path;
00051     if (boost::starts_with(xml_path, ".")) {
00052         abs_xml_path = ros::package::getPath("asr_visualization_server") + xml_path.substr(1);
00053     }
00054     else {
00055         abs_xml_path = xml_path;
00056     }
00057 
00058     try {
00059         rapidxml::file<> xmlFile(abs_xml_path.c_str());
00060         rapidxml::xml_document<> doc;
00061         doc.parse<0>(xmlFile.data());
00062 
00063         rapidxml::xml_node<> *root_node = doc.first_node();
00064         if (root_node) {
00065             rapidxml::xml_node<> *child_node = root_node->first_node();
00066             int i = 0;
00067             while (child_node) {
00068                 rapidxml::xml_attribute<> *name_attribute = child_node->first_attribute("name");
00069                 rapidxml::xml_attribute<> *scale_attribute = child_node->first_attribute("scale");
00070                 rapidxml::xml_attribute<> *mesh_attribute = child_node->first_attribute("mesh");
00071                 rapidxml::xml_attribute<> *mat_attribute = child_node->first_attribute("use_mat");
00072                 if (name_attribute && scale_attribute && mesh_attribute && mat_attribute) {
00073                     std::string name = name_attribute->value();
00074                     std::string mesh = mesh_attribute->value();
00075                     std::string scale = scale_attribute->value();
00076                     std::string pose_string = child_node->value();
00077                     bool use_mat = boost::lexical_cast<bool>(mat_attribute->value());
00078                     std::vector<double> pose_vec;
00079                     std::vector<double> scale_vec;
00080                     if (parseDoubleCsv(pose_string, pose_vec, " ,") && pose_vec.size() == 7) {
00081                         if (parseDoubleCsv(scale, scale_vec, " ,") && scale_vec.size() == 3) {
00082                             markers.markers.push_back(createMarker(name, mesh, pose_vec, scale_vec, i, use_mat));
00083                             i++;
00084                         }
00085                     }
00086 
00087                 }
00088                 child_node = child_node->next_sibling();
00089             }
00090         }
00091     } catch(std::runtime_error err) {
00092         ROS_DEBUG_STREAM("Can't parse xml-file. Runtime error: " << err.what());
00093     } catch (rapidxml::parse_error err) {
00094         ROS_DEBUG_STREAM("Can't parse xml-file Parse error: " << err.what());
00095     } catch (boost::bad_lexical_cast err) {
00096         ROS_DEBUG_STREAM("Can't cast use_mat. Cast error: " << err.what());
00097     }
00098 
00099     return markers;
00100 }
00101 
00102 bool MarkerHelper::parseDoubleCsv(std::string csv_in, std::vector<double> &csv_out, std::string delim) {
00103     std::vector<std::string> strvec;
00104 
00105     boost::algorithm::trim_if(csv_in, boost::algorithm::is_any_of(delim));
00106     boost::algorithm::split(strvec, csv_in, boost::algorithm::is_any_of(delim), boost::algorithm::token_compress_on);
00107     for (std::string str : strvec) {
00108         try {
00109             csv_out.push_back(boost::lexical_cast<double>(str));
00110         } catch (boost::bad_lexical_cast err) {
00111             ROS_DEBUG_STREAM("Can't cast node-value. Cast error: " << err.what());
00112             return false;
00113         }
00114     }
00115     return true;
00116 }
00117 
00118 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) {
00119     visualization_msgs::Marker m;
00120 
00121     m.header.frame_id = "/map";
00122     m.type = visualization_msgs::Marker::MESH_RESOURCE;
00123     m.action = visualization_msgs::Marker::ADD;
00124     m.color.a = 1.0;
00125     m.color.r = 0.65;
00126     m.color.g = 0.65;
00127     m.color.b = 0.65;
00128     m.lifetime = ros::Duration(marker_lifetime_);
00129     m.mesh_resource = mesh;
00130     m.mesh_use_embedded_materials = use_mat;
00131     m.id = id;
00132     m.ns = name;
00133     m.scale.x = scale.at(0);
00134     m.scale.y = scale.at(1);
00135     m.scale.z = scale.at(2);
00136     m.pose.position.x = pose.at(0);
00137     m.pose.position.y = pose.at(1);
00138     m.pose.position.z = pose.at(2);
00139     m.pose.orientation.w = pose.at(3);
00140     m.pose.orientation.x = pose.at(4);
00141     m.pose.orientation.y = pose.at(5);
00142     m.pose.orientation.z = pose.at(6);
00143 
00144     return m;
00145 }
00146 
00147 }


asr_visualization_server
Author(s): Allgeyer Tobias, Braun Kai, Heller Florian, Mehlhaus Jonas, Meißner Pascal, Qattan Mohamad, Wittenbeck Valerij
autogenerated on Thu Jun 6 2019 21:15:28