Go to the documentation of this file.00001
00026 #include <topological_tools/topological_map.h>
00027
00028 #include <boost/bind.hpp>
00029 #include <boost/property_tree/ptree.hpp>
00030 #include <boost/property_tree/xml_parser.hpp>
00031 #include <boost/foreach.hpp>
00032 #define foreach BOOST_FOREACH
00033
00034
00035
00036 using namespace std;
00037 using namespace topological_tools;
00038 using namespace geometry_msgs;
00039
00040
00041
00042 TopologicalMap::TopologicalMap ( const string& map_file )
00043 {
00044 using boost::property_tree::ptree;
00045
00046 ptree p;
00047 try
00048 {
00049 read_xml ( map_file, p );
00050
00051 foreach ( ptree::value_type & n, p.get_child ( "TopologicalMap" ) )
00052 {
00053 string name = n.second.get<string> ( "<xmlattr>.name" );
00054 string data = n.second.get<string> ( "Goal" );
00055 Pose goal;
00056
00057 float x, y, th;
00058 sscanf ( data.c_str(), "%f %f %f", &x, &y, &th );
00059 goal.position.x = x;
00060 goal.position.y = y;
00061 goal.orientation.z = sin ( th / 2.0 );
00062 goal.orientation.w = cos ( th / 2.0 );
00063 addNode ( goal, name );
00064 }
00065
00066 foreach ( ptree::value_type & n, p.get_child ( "TopologicalMap" ) )
00067 {
00068 string name = n.second.get<string> ( "<xmlattr>.name" );
00069
00070 foreach ( ptree::value_type & c, n.second.get_child ( "Connections" ) )
00071 {
00072 string connection_label = c.second.get<string> ( "<xmlattr>.label" );
00073 string connection_name = c.second.data();
00074 node_map_[name]->connect ( getNodeByName ( connection_name ), connection_label );
00075 }
00076 }
00077 }
00078 catch ( exception& e )
00079 {
00080 ROS_ERROR_STREAM ( "TopologicalMap:: Exception Thrown: " << e.what() );
00081 }
00082 }
00083
00084
00085
00086 bool TopologicalMap::hasNode ( const string& node_name )
00087 {
00088 return node_map_.count ( node_name );
00089 }
00090
00091
00092
00093 boost::shared_ptr<TopologicalNode> TopologicalMap::getNodeByName ( const string& node_name )
00094 {
00095 if ( !node_map_.count ( node_name ) )
00096 {
00097 ROS_ERROR_STREAM ( "TopologicalMap:: Node " << node_name << " not present in map." );
00098 return boost::shared_ptr<TopologicalNode>();
00099 }
00100
00101 return node_map_[node_name];
00102 }
00103
00104
00105
00106 void TopologicalMap::addNode ( Pose goal, const string& node_name )
00107 {
00108 if ( node_map_.count ( node_name ) )
00109 {
00110 ROS_WARN_STREAM ( "TopologicalMap:: Node " << node_name << " already present in map. It is being replaced." );
00111 }
00112
00113
00114 node_map_[node_name] = boost::shared_ptr<TopologicalNode> ( new TopologicalNode ( goal, node_name ) );
00115 }