1 #include "mapstorage.h"
7 MapStorage::MapStorage() : modified(false), file_name("")
9 nodemap_storage["coordinates_x"] = new Graph::NodeMap<double>(graph);
10 coords.setXMap(*nodemap_storage["coordinates_x"]);
11 nodemap_storage["coordinates_y"] = new Graph::NodeMap<double>(graph);
12 coords.setYMap(*nodemap_storage["coordinates_y"]);
14 nodemap_storage["id"] = new Graph::NodeMap<double>(graph);
15 edgemap_storage["id"] = new Graph::EdgeMap<double>(graph);
17 nodemap_default["id"] = 1.0;
18 edgemap_default["id"] = 1.0;
21 MapStorage::~MapStorage()
23 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
24 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
28 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
29 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
35 int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap<double> *nodemap, double default_value = 0.0)
37 if( nodemap_storage.find(name) == nodemap_storage.end() )
39 nodemap_storage[name]=nodemap;
40 // set the maps default value
41 nodemap_default[name] = default_value;
47 int MapStorage::addEdgeMap(const std::string & name, Graph::EdgeMap<double> *edgemap, double default_value = 0.0)
49 if( edgemap_storage.find(name) == edgemap_storage.end() )
51 edgemap_storage[name]=edgemap;
52 // set the maps default value
53 edgemap_default[name] = default_value;
59 double MapStorage::maxOfNodeMap(const std::string & name)
62 for (NodeIt j(graph); j!=INVALID; ++j)
64 if( (*nodemap_storage[name])[j]>max )
66 max=(*nodemap_storage[name])[j];
72 double MapStorage::maxOfEdgeMap(const std::string & name)
75 for (EdgeIt j(graph); j!=INVALID; ++j)
77 if( (*edgemap_storage[name])[j]>max )
79 max=(*edgemap_storage[name])[j];
85 double MapStorage::minOfNodeMap(const std::string & name)
91 min=(*nodemap_storage[name])[j];
97 for (; j!=INVALID; ++j)
99 if( (*nodemap_storage[name])[j]<min )
101 min=(*nodemap_storage[name])[j];
107 double MapStorage::minOfEdgeMap(const std::string & name)
113 min=(*edgemap_storage[name])[j];
119 for (EdgeIt j(graph); j!=INVALID; ++j)
121 if( (*edgemap_storage[name])[j]<min )
123 min=(*edgemap_storage[name])[j];
129 int MapStorage::readFromFile(const std::string &filename)
133 bool read_edge_id = false;
136 LemonReader lreader(filename);
137 ContentReader content(lreader);
140 const std::vector<std::string>& nodeMapNames = content.nodeSetMaps(0);
141 const std::vector<std::string>& edgeMapNames = content.edgeSetMaps(0);
143 GraphReader<Graph> greader(filename, graph);
144 for (std::vector<std::string>::const_iterator it = nodeMapNames.begin();
145 it != nodeMapNames.end(); ++it)
147 if (*it == "coordinates_x")
150 //std::cout << "read X nodemap" << std::endl;
152 else if (*it == "coordinates_y")
155 //std::cout << "read Y nodemap" << std::endl;
157 else if (*it == "id")
159 //std::cout << "read id nodemap" << std::endl;
163 nodemap_storage[*it] = new Graph::NodeMap<double>(graph);
164 //std::cout << "read " << *it << " nodemap" << std::endl;
166 greader.readNodeMap(*it, *nodemap_storage[*it]);
168 for (std::vector<std::string>::const_iterator it = edgeMapNames.begin();
169 it != edgeMapNames.end(); ++it)
173 //std::cout << "read id edgemap" << std::endl;
177 edgemap_storage[*it] = new Graph::EdgeMap<double>(graph);
178 //std::cout << "read " << *it << " edgemap" << std::endl;
180 greader.readEdgeMap(*it, *edgemap_storage[*it]);
183 } catch (DataFormatError& error) {
184 Gtk::MessageDialog mdialog("Read Error");
185 mdialog.set_message(error.what());
193 edgemap_storage["id"] = new Graph::EdgeMap<double>(graph);
195 for (EdgeIt e(graph); e != INVALID; ++e)
197 (*edgemap_storage["id"])[e] = i++;
201 if (!read_x || !read_y)
204 for (NodeIt n(graph); n != INVALID; ++n)
208 const double pi = 3.142;
209 double step = 2 * pi / (double) node_num;
211 for (NodeIt n(graph); n != INVALID; ++n)
213 nodemap_storage["coordinates_x"]->set(n, 250.0 * std::cos(i * step));
214 nodemap_storage["coordinates_y"]->set(n, 250.0 * std::sin(i * step));
219 // fill in the default values for the maps
220 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
221 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
223 if ((it->first != "id") &&
224 (it->first != "coordiantes_x") &&
225 (it->first != "coordinates_y"))
227 nodemap_default[it->first] = 0.0;
229 else if (it->first == "id")
232 double max = (*nodemap_storage["id"])[n];
233 for (; n != INVALID; ++n)
235 if ((*nodemap_storage["id"])[n] > max)
236 max = (*nodemap_storage["id"])[n];
238 nodemap_default["id"] = max + 1.0;
241 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
242 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
244 if (it->first != "id")
246 edgemap_default[it->first] = 0.0;
250 double max = std::numeric_limits<double>::min();
251 for (EdgeIt e(graph); e != INVALID; ++e)
253 if ((*edgemap_storage["id"])[e] > max)
254 max = (*edgemap_storage["id"])[e];
256 if (max > std::numeric_limits<double>::min())
257 edgemap_default["id"] = max + 1.0;
259 edgemap_default["id"] = 1.0;
266 void MapStorage::writeToFile(const std::string &filename)
268 GraphWriter<Graph> gwriter(filename, graph);
270 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
271 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
273 gwriter.writeNodeMap(it->first, *(it->second));
274 //std::cout << "wrote " << it->first << " nodemap" << std::endl;
276 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
277 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
279 gwriter.writeEdgeMap(it->first, *(it->second));
280 //std::cout << "wrote " << it->first << " edgemap" << std::endl;
285 void MapStorage::clear()
287 for (std::map<std::string, Graph::NodeMap<double>*>::iterator it =
288 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
290 if ((it->first != "coordinates_x") &&
291 (it->first != "coordinates_y") &&
295 nodemap_storage.erase(it);
298 for (std::map<std::string, Graph::EdgeMap<double>*>::iterator it =
299 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
301 if (it->first != "id")
304 edgemap_storage.erase(it);
307 for (std::map<std::string, double>::iterator it =
308 nodemap_default.begin(); it != nodemap_default.end(); ++it)
310 if (it->first != "id")
311 nodemap_default.erase(it);
313 for (std::map<std::string, double>::iterator it =
314 edgemap_default.begin(); it != edgemap_default.end(); ++it)
316 if (it->first != "id")
317 edgemap_default.erase(it);