00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00039 #include <topological_map_2d/topological_map.h>
00040 #include <topological_map_2d/exception.h>
00041 #include <graph_mapping_utils/general.h>
00042 #include <boost/foreach.hpp>
00043 #include <boost/lexical_cast.hpp>
00044
00045 namespace topological_map_2d
00046 {
00047
00048 namespace util=graph_mapping_utils;
00049 typedef std::map<unsigned, GraphVertex> VertexMap;
00050 typedef std::map<unsigned, GraphEdge> EdgeMap;
00051 using std::pair;
00052
00053
00054
00055
00056
00057 string gridFrame (const unsigned g)
00058 {
00059 return "grid" + boost::lexical_cast<string>(g);
00060 }
00061
00062 unsigned frameGrid (const std::string& frame)
00063 {
00064
00065 size_t grid_pos = frame.find("grid");
00066
00067 if (grid_pos == string::npos)
00068 throw GridFrameNameException(frame);
00069
00070 int id = atoi(frame.c_str()+grid_pos+4);
00071 if (id<=0 || id==INT_MAX)
00072 throw GridFrameNameException(frame);
00073
00074 return id;
00075 }
00076
00077
00078
00079
00080
00081 TopologicalMap::TopologicalMap () :
00082 Graph()
00083 {}
00084
00085 TopologicalMap::TopologicalMap (const TopologicalMap& g) :
00086 Graph(g)
00087 {
00088 recomputeIndices();
00089 }
00090
00091 TopologicalMap& TopologicalMap::operator= (const TopologicalMap& g)
00092 {
00093 Graph::operator=(g);
00094 recomputeIndices();
00095 return *this;
00096 }
00097
00098 void TopologicalMap::recomputeIndices ()
00099 {
00100 vertex_map_.clear();
00101 edge_map_.clear();
00102 BOOST_FOREACH (const GraphVertex& v, vertices(*this))
00103 vertex_map_[operator[](v).id] = v;
00104 BOOST_FOREACH (const GraphEdge& e, edges(*this))
00105 edge_map_[operator[](e).id] = e;
00106 }
00107
00108
00109
00110
00111
00112
00113
00114 GraphVertex TopologicalMap::node (const unsigned id) const
00115 {
00116 VertexMap::const_iterator pos = vertex_map_.find(id);
00117 if (pos==vertex_map_.end())
00118 throw UnknownNodeIdException(id);
00119 return pos->second;
00120 }
00121
00122 GraphEdge TopologicalMap::edge (const unsigned id) const
00123 {
00124 EdgeMap::const_iterator pos = edge_map_.find(id);
00125 if (pos==edge_map_.end())
00126 throw UnknownEdgeIdException(id);
00127 return pos->second;
00128 }
00129
00130 TopologicalMap::Node& TopologicalMap::nodeInfo (const unsigned id)
00131 {
00132 return operator[](node(id));
00133 }
00134
00135 TopologicalMap::Edge& TopologicalMap::edgeInfo (const unsigned id)
00136 {
00137 return operator[](edge(id));
00138 }
00139
00140 const TopologicalMap::Node& TopologicalMap::nodeInfo (const unsigned id) const
00141 {
00142 return operator[](node(id));
00143 }
00144
00145 const TopologicalMap::Edge& TopologicalMap::edgeInfo (const unsigned id) const
00146 {
00147 return operator[](edge(id));
00148 }
00149
00150
00151
00152
00153
00154
00155 GraphVertex TopologicalMap::addNode (const Node& info)
00156 {
00157 if (info.id==0)
00158 throw InvalidNodeIdException(info.id);
00159 if (util::contains(vertex_map_, info.id))
00160 throw DuplicateNodeIdException(info.id);
00161 return (vertex_map_[info.id] = add_vertex(info, *this));
00162 }
00163
00164 GraphEdge TopologicalMap::addEdge (const Edge& info)
00165 {
00166 if (info.id==0)
00167 throw InvalidEdgeIdException(info.id);
00168 if (util::contains(edge_map_, info.id))
00169 throw DuplicateEdgeIdException(info.id);
00170 const GraphVertex v1 = node(info.src);
00171 const GraphVertex v2 = node(info.dest);
00172 pair<GraphEdge, bool> res = add_edge(v1, v2, info, *this);
00173 if (!res.second)
00174 throw ParallelEdgeException(info.src, info.dest);
00175 return (edge_map_[info.id] = res.first);
00176 }
00177
00178
00179
00180 }