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)
80 min=(*nodemap_storage[name])[j];
86 for (; j!=INVALID; ++j)
88 if( (*nodemap_storage[name])[j]<min )
90 min=(*nodemap_storage[name])[j];
96 double MapStorage::minOfEdgeMap(const std::string & name)
102 min=(*edgemap_storage[name])[j];
108 for (EdgeIt j(graph); j!=INVALID; ++j)
110 if( (*edgemap_storage[name])[j]<min )
112 min=(*edgemap_storage[name])[j];
118 void MapStorage::initMapsForEdge(Graph::Edge e)
120 std::map< std::string,Graph::EdgeMap<double> * >::iterator ems_it;
121 for(ems_it=edgemap_storage.begin();ems_it!=edgemap_storage.end();ems_it++)
123 (*((*ems_it).second))[e]=5;
127 void MapStorage::readFromFile(const std::string &filename)
133 LemonReader lreader(filename);
134 ContentReader content(lreader);
137 const std::vector<std::string>& nodeMapNames = content.nodeSetMaps(0);
138 const std::vector<std::string>& edgeMapNames = content.edgeSetMaps(0);
140 GraphReader<Graph> greader(filename, graph);
141 for (std::vector<std::string>::const_iterator it = nodeMapNames.begin();
142 it != nodeMapNames.end(); ++it)
144 if (*it == "coordinates_x")
147 //std::cout << "read X nodemap" << std::endl;
149 else if (*it == "coordinates_y")
152 //std::cout << "read Y nodemap" << std::endl;
154 else if (*it == "id")
156 //std::cout << "read id nodemap" << std::endl;
160 nodemap_storage[*it] = new Graph::NodeMap<double>(graph);
161 //std::cout << "read " << *it << " nodemap" << std::endl;
163 greader.readNodeMap(*it, *nodemap_storage[*it]);
165 for (std::vector<std::string>::const_iterator it = edgeMapNames.begin();
166 it != edgeMapNames.end(); ++it)
170 //std::cout << "read id edgemap" << std::endl;
174 edgemap_storage[*it] = new Graph::EdgeMap<double>(graph);
175 //std::cout << "read " << *it << " edgemap" << std::endl;
177 greader.readEdgeMap(*it, *edgemap_storage[*it]);
180 } catch (DataFormatError& error) {
182 Gtk::MessageDialog mdialog("Read Error");
183 mdialog.set_message(error.what());
186 // reset graph and mapstorage ?
190 if (!read_x || !read_y)
193 for (NodeIt n(graph); n != INVALID; ++n)
197 const double pi = 3.142;
198 double step = 2 * pi / (double) node_num;
200 for (NodeIt n(graph); n != INVALID; ++n)
202 nodemap_storage["coordinates_x"]->set(n, 250.0 * cos(i * step));
203 nodemap_storage["coordinates_y"]->set(n, 250.0 * sin(i * step));
209 void MapStorage::writeToFile(const std::string &filename)
211 GraphWriter<Graph> gwriter(filename, graph);
213 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
214 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
216 gwriter.writeNodeMap(it->first, *(it->second));
217 //std::cout << "wrote " << it->first << " nodemap" << std::endl;
219 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
220 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
222 gwriter.writeEdgeMap(it->first, *(it->second));
223 //std::cout << "wrote " << it->first << " edgemap" << std::endl;
228 void MapStorage::clear()
230 for (std::map<std::string, Graph::NodeMap<double>*>::iterator it =
231 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
233 if ((it->first != "coordinates_x") &&
234 (it->first != "coordinates_y") &&
238 nodemap_storage.erase(it);
241 for (std::map<std::string, Graph::EdgeMap<double>*>::iterator it =
242 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
244 if (it->first != "id")
247 edgemap_storage.erase(it);