1 #include "mapstorage.h"
3 MapStorage::MapStorage() : modified(false), file_name("")
5 nodemap_storage["coordinates_x"] = new Graph::NodeMap<double>(graph);
6 coords.setXMap(*nodemap_storage["coordinates_x"]);
7 nodemap_storage["coordinates_y"] = new Graph::NodeMap<double>(graph);
8 coords.setYMap(*nodemap_storage["coordinates_y"]);
10 nodemap_storage["id"] = new Graph::NodeMap<double>(graph);
11 edgemap_storage["id"] = new Graph::EdgeMap<double>(graph);
14 MapStorage::~MapStorage()
16 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
17 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
21 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
22 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
28 int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap<double> *nodemap)
30 if( nodemap_storage.find(name) == nodemap_storage.end() )
32 nodemap_storage[name]=nodemap;
38 int MapStorage::addEdgeMap(const std::string & name, Graph::EdgeMap<double> *edgemap)
40 if( edgemap_storage.find(name) == edgemap_storage.end() )
42 edgemap_storage[name]=edgemap;
48 double MapStorage::maxOfNodeMap(const std::string & name)
51 for (NodeIt j(graph); j!=INVALID; ++j)
53 if( (*nodemap_storage[name])[j]>max )
55 max=(*nodemap_storage[name])[j];
61 double MapStorage::maxOfEdgeMap(const std::string & name)
64 for (EdgeIt j(graph); j!=INVALID; ++j)
66 if( (*edgemap_storage[name])[j]>max )
68 max=(*edgemap_storage[name])[j];
74 double MapStorage::minOfNodeMap(const std::string & name)
77 double min=(*nodemap_storage[name])[j];
78 for (; j!=INVALID; ++j)
80 if( (*nodemap_storage[name])[j]<min )
82 min=(*nodemap_storage[name])[j];
88 double MapStorage::minOfEdgeMap(const std::string & name)
91 double min=(*edgemap_storage[name])[j];
92 for (EdgeIt j(graph); j!=INVALID; ++j)
94 if( (*edgemap_storage[name])[j]<min )
96 min=(*edgemap_storage[name])[j];
102 void MapStorage::initMapsForEdge(Graph::Edge e)
104 std::map< std::string,Graph::EdgeMap<double> * >::iterator ems_it;
105 for(ems_it=edgemap_storage.begin();ems_it!=edgemap_storage.end();ems_it++)
107 (*((*ems_it).second))[e]=5;
111 void MapStorage::readFromFile(const std::string &filename)
117 LemonReader lreader(filename);
118 ContentReader content(lreader);
121 const std::vector<std::string>& nodeMapNames = content.nodeSetMaps(0);
122 const std::vector<std::string>& edgeMapNames = content.edgeSetMaps(0);
124 GraphReader<Graph> greader(filename, graph);
125 for (std::vector<std::string>::const_iterator it = nodeMapNames.begin();
126 it != nodeMapNames.end(); ++it)
128 if (*it == "coordinates_x")
131 //std::cout << "read X nodemap" << std::endl;
133 else if (*it == "coordinates_y")
136 //std::cout << "read Y nodemap" << std::endl;
138 else if (*it == "id")
140 //std::cout << "read id nodemap" << std::endl;
144 nodemap_storage[*it] = new Graph::NodeMap<double>(graph);
145 //std::cout << "read " << *it << " nodemap" << std::endl;
147 greader.readNodeMap(*it, *nodemap_storage[*it]);
149 for (std::vector<std::string>::const_iterator it = edgeMapNames.begin();
150 it != edgeMapNames.end(); ++it)
154 //std::cout << "read id edgemap" << std::endl;
158 edgemap_storage[*it] = new Graph::EdgeMap<double>(graph);
159 //std::cout << "read " << *it << " edgemap" << std::endl;
161 greader.readEdgeMap(*it, *edgemap_storage[*it]);
164 } catch (DataFormatError& error) {
166 Gtk::MessageDialog mdialog("Read Error");
167 mdialog.set_message(error.what());
170 // reset graph and mapstorage ?
174 if (!read_x || !read_y)
177 for (NodeIt n(graph); n != INVALID; ++n)
181 const double pi = 3.142;
182 double step = 2 * pi / (double) node_num;
184 for (NodeIt n(graph); n != INVALID; ++n)
186 nodemap_storage["coordinates_x"]->set(n, 250.0 * cos(i * step));
187 nodemap_storage["coordinates_y"]->set(n, 250.0 * sin(i * step));
193 void MapStorage::writeToFile(const std::string &filename)
195 GraphWriter<Graph> gwriter(filename, graph);
197 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
198 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
200 gwriter.writeNodeMap(it->first, *(it->second));
201 //std::cout << "wrote " << it->first << " nodemap" << std::endl;
203 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
204 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
206 gwriter.writeEdgeMap(it->first, *(it->second));
207 //std::cout << "wrote " << it->first << " edgemap" << std::endl;
212 void MapStorage::clear()
214 for (std::map<std::string, Graph::NodeMap<double>*>::iterator it =
215 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
217 if ((it->first != "coordinates_x") &&
218 (it->first != "coordinates_y") &&
222 nodemap_storage.erase(it);
225 for (std::map<std::string, Graph::EdgeMap<double>*>::iterator it =
226 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
228 if (it->first != "id")
231 edgemap_storage.erase(it);