LEMUR Packages: ompl_lemur or_lemur pr_bgl prpy_lemur
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Groups Pages
graph_io.h
Go to the documentation of this file.
1 
10 namespace pr_bgl
11 {
12 
19 template<typename Graph, typename VertexIndexMap, typename EdgeIndexMap>
20 void
21 write_graphio_graph(std::ostream & out, const Graph & g,
22  VertexIndexMap vertex_index_map, EdgeIndexMap edge_index_map)
23 {
24  // write all vertices
25  typename boost::graph_traits<Graph>::vertex_iterator vi, vi_end;
26  for (boost::tie(vi,vi_end)=vertices(g); vi!=vi_end; ++vi)
27  {
28  std::size_t index = get(vertex_index_map, *vi);
29  out << "vertex " << index << "\n";
30  }
31 
32  // write all edges
33  typename boost::graph_traits<Graph>::edge_iterator ei, ei_end;
34  for (boost::tie(ei,ei_end)=edges(g); ei!=ei_end; ++ei)
35  {
36  std::size_t index = get(edge_index_map, *ei);
37  out << "edge " << index
38  << " source " << boost::source(*ei, g)
39  << " target " << boost::target(*ei, g)
40  << "\n";
41  }
42 }
43 
50 template<typename Graph, typename VertexIndexMap, typename EdgeIndexMap>
51 void
52 write_graphio_properties(std::ostream & out, const Graph & g,
53  VertexIndexMap vertex_index_map, EdgeIndexMap edge_index_map,
54  boost::dynamic_properties & properties)
55 {
56  boost::dynamic_properties::iterator it;
57  for (it=properties.begin(); it!=properties.end(); it++)
58  {
59  if (it->second->value() != typeid(std::string))
60  throw std::runtime_error("a property map is not string-valued!");
61  if (it->second->key() == typeid(typename boost::graph_traits<Graph>::vertex_descriptor))
62  {
63  typename boost::graph_traits<Graph>::vertex_iterator vi, vi_end;
64  for (boost::tie(vi,vi_end)=vertices(g); vi!=vi_end; ++vi)
65  {
66  std::size_t index = get(vertex_index_map, *vi);
67  std::string value = boost::any_cast<std::string>(it->second->get(*vi));
68  out << "vprop " << index << " " << it->first << " " << value << "\n";
69  }
70  }
71  else if (it->second->key() == typeid(typename boost::graph_traits<Graph>::edge_descriptor))
72  {
73  typename boost::graph_traits<Graph>::edge_iterator ei, ei_end;
74  for (boost::tie(ei,ei_end)=edges(g); ei!=ei_end; ++ei)
75  {
76  std::size_t index = get(edge_index_map, *ei);
77  std::string value = boost::any_cast<std::string>(it->second->get(*ei));
78  out << "eprop " << index << " " << it->first << " " << value << "\n";
79  }
80  }
81  else
82  throw std::runtime_error("a property map is not vertex-keyed or edge-keyed!");
83  }
84 }
85 
86 #if 0
87 void load_properties(std::istream & is)
88 {
89  std::string str;
90  while (std::getline(is,str))
91  {
92  std::stringstream ss;
93  ss << str;
94  // get line tag
95  std::string tag;
96  ss >> tag;
97  if (tag == "eprop")
98  {
99  // get edge index
100  std::size_t index;
101  ss >> index;
102  // get potential property name
103  ss >> tag;
104  typename std::map<std::string, IOBaseEdge>::iterator found = edge_serializers.find(tag);
105  if (found == edge_serializers.end())
106  continue;
107  // delegate to iobase!
108  found->second->deserialize(edge_vector_map[index], ss);
109  }
110  }
111 }
112 #endif
113 
114 } // namespace pr_bgl