topological_map.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
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  * Utility
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  * Initialization
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  * Lookup
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  * Basic modification
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 } // namespace


topological_map_2d
Author(s): Bhaskara Marthi
autogenerated on Sun Jan 5 2014 11:39:24