Distribute xml.h too.
1 #include "mapstorage.h"
2 #include "gui_writer.h"
3 #include "gui_reader.h"
9 MapStorage::MapStorage() : modified(false), file_name(""), arrow_pos_read_ok(false)
11 nodemap_storage["coordinates_x"] = new Graph::NodeMap<double>(graph);
12 coords.setXMap(*nodemap_storage["coordinates_x"]);
13 nodemap_storage["coordinates_y"] = new Graph::NodeMap<double>(graph);
14 coords.setYMap(*nodemap_storage["coordinates_y"]);
16 edgemap_storage["arrow_pos_x"] = new Graph::EdgeMap<double>(graph);
17 arrow_pos.setXMap(*edgemap_storage["arrow_pos_x"]);
18 edgemap_storage["arrow_pos_y"] = new Graph::EdgeMap<double>(graph);
19 arrow_pos.setYMap(*edgemap_storage["arrow_pos_y"]);
21 nodemap_storage["id"] = new Graph::NodeMap<double>(graph);
22 edgemap_storage["id"] = new Graph::EdgeMap<double>(graph);
24 nodemap_default["id"] = 1.0;
25 edgemap_default["id"] = 1.0;
27 active_nodemaps.resize(NODE_PROPERTY_NUM);
28 for(int i=0;i<NODE_PROPERTY_NUM;i++)
30 active_nodemaps[i]="";
33 active_edgemaps.resize(EDGE_PROPERTY_NUM);
34 for(int i=0;i<EDGE_PROPERTY_NUM;i++)
36 active_edgemaps[i]="";
40 MapStorage::~MapStorage()
42 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
43 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
47 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
48 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
54 int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap<double> *nodemap, double default_value = 0.0)
56 if( nodemap_storage.find(name) == nodemap_storage.end() )
58 nodemap_storage[name]=nodemap;
59 // set the maps default value
60 nodemap_default[name] = default_value;
66 void MapStorage::changeActiveMap(bool itisedge, int prop, std::string mapname)
70 active_edgemaps[prop]=mapname;
74 active_nodemaps[prop]=mapname;
76 signal_prop.emit(itisedge, prop);
79 std::string MapStorage::getActiveEdgeMap(int prop)
81 return active_edgemaps[prop];
84 std::string MapStorage::getActiveNodeMap(int prop)
86 return active_nodemaps[prop];
89 std::vector<std::string> MapStorage::getEdgeMapList()
91 std::vector<std::string> eml;
92 eml.resize(edgemap_storage.size());
94 std::map< std::string,Graph::EdgeMap<double> * >::iterator emsi=beginOfEdgeMaps();
95 for(;emsi!=endOfEdgeMaps();emsi++)
103 std::vector<std::string> MapStorage::getNodeMapList()
105 std::vector<std::string> nml;
106 nml.resize(nodemap_storage.size());
108 std::map< std::string,Graph::NodeMap<double> * >::iterator nmsi=beginOfNodeMaps();
109 for(;nmsi!=endOfNodeMaps();nmsi++)
111 nml[i]=(nmsi->first);
117 MapStorage::Signal_Prop MapStorage::signal_prop_ch()
122 int MapStorage::addEdgeMap(const std::string & name, Graph::EdgeMap<double> *edgemap, double default_value = 0.0)
124 if( edgemap_storage.find(name) == edgemap_storage.end() )
126 edgemap_storage[name]=edgemap;
127 // set the maps default value
128 edgemap_default[name] = default_value;
134 double MapStorage::maxOfNodeMap(const std::string & name)
137 for (NodeIt j(graph); j!=INVALID; ++j)
139 if( (*nodemap_storage[name])[j]>max )
141 max=(*nodemap_storage[name])[j];
147 double MapStorage::maxOfEdgeMap(const std::string & name)
150 for (EdgeIt j(graph); j!=INVALID; ++j)
152 if( (*edgemap_storage[name])[j]>max )
154 max=(*edgemap_storage[name])[j];
160 double MapStorage::minOfNodeMap(const std::string & name)
166 min=(*nodemap_storage[name])[j];
172 for (; j!=INVALID; ++j)
174 if( (*nodemap_storage[name])[j]<min )
176 min=(*nodemap_storage[name])[j];
182 double MapStorage::minOfEdgeMap(const std::string & name)
188 min=(*edgemap_storage[name])[j];
194 for (EdgeIt j(graph); j!=INVALID; ++j)
196 if( (*edgemap_storage[name])[j]<min )
198 min=(*edgemap_storage[name])[j];
204 int MapStorage::readFromFile(const std::string &filename)
208 bool read_edge_id = false;
211 LemonReader lreader(filename);
212 ContentReader content(lreader);
215 const std::vector<std::string>& nodeMapNames = content.nodeSetMaps(0);
216 const std::vector<std::string>& edgeMapNames = content.edgeSetMaps(0);
218 GraphReader<Graph> greader(filename, graph);
219 for (std::vector<std::string>::const_iterator it = nodeMapNames.begin();
220 it != nodeMapNames.end(); ++it)
222 if (*it == "coordinates_x")
225 //std::cout << "read X nodemap" << std::endl;
227 else if (*it == "coordinates_y")
230 //std::cout << "read Y nodemap" << std::endl;
232 else if (*it == "id")
234 //std::cout << "read id nodemap" << std::endl;
238 nodemap_storage[*it] = new Graph::NodeMap<double>(graph);
239 //std::cout << "read " << *it << " nodemap" << std::endl;
241 greader.readNodeMap(*it, *nodemap_storage[*it]);
243 for (std::vector<std::string>::const_iterator it = edgeMapNames.begin();
244 it != edgeMapNames.end(); ++it)
248 //std::cout << "read id edgemap" << std::endl;
252 edgemap_storage[*it] = new Graph::EdgeMap<double>(graph);
253 //std::cout << "read " << *it << " edgemap" << std::endl;
255 greader.readEdgeMap(*it, *edgemap_storage[*it]);
257 GuiReader gui_reader(greader, this);
259 } catch (DataFormatError& error) {
260 Gtk::MessageDialog mdialog(error.what());
268 edgemap_storage["id"] = new Graph::EdgeMap<double>(graph);
270 for (EdgeIt e(graph); e != INVALID; ++e)
272 (*edgemap_storage["id"])[e] = i++;
276 if (!read_x || !read_y)
279 for (NodeIt n(graph); n != INVALID; ++n)
283 const double pi = 3.142;
284 double step = 2 * pi / (double) node_num;
286 for (NodeIt n(graph); n != INVALID; ++n)
288 nodemap_storage["coordinates_x"]->set(n, 250.0 * std::cos(i * step));
289 nodemap_storage["coordinates_y"]->set(n, 250.0 * std::sin(i * step));
294 if (!arrow_pos_read_ok)
296 arrow_pos_read_ok = false;
297 for (EdgeIt e(graph); e != INVALID; ++e)
299 arrow_pos.set(e, (coords[graph.source(e)] + coords[graph.target(e)]) / 2.0);
303 // fill in the default values for the maps
304 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
305 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
307 if ((it->first != "id") &&
308 (it->first != "coordiantes_x") &&
309 (it->first != "coordinates_y"))
311 nodemap_default[it->first] = 0.0;
313 else if (it->first == "id")
316 double max = (*nodemap_storage["id"])[n];
317 for (; n != INVALID; ++n)
319 if ((*nodemap_storage["id"])[n] > max)
320 max = (*nodemap_storage["id"])[n];
322 nodemap_default["id"] = max + 1.0;
325 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
326 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
328 if (it->first != "id")
330 edgemap_default[it->first] = 0.0;
334 double max = std::numeric_limits<double>::min();
335 for (EdgeIt e(graph); e != INVALID; ++e)
337 if ((*edgemap_storage["id"])[e] > max)
338 max = (*edgemap_storage["id"])[e];
340 if (max > std::numeric_limits<double>::min())
341 edgemap_default["id"] = max + 1.0;
343 edgemap_default["id"] = 1.0;
350 void MapStorage::writeToFile(const std::string &filename)
352 GraphWriter<Graph> gwriter(filename, graph);
354 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
355 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
357 gwriter.writeNodeMap(it->first, *(it->second));
359 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
360 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
362 if ((it->first != "arrow_pos_x") &&
363 (it->first != "arrow_pos_y"))
365 gwriter.writeEdgeMap(it->first, *(it->second));
369 GuiWriter gui_writer(gwriter, this);
374 void MapStorage::clear()
376 for (std::map<std::string, Graph::NodeMap<double>*>::iterator it =
377 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
379 if ((it->first != "coordinates_x") &&
380 (it->first != "coordinates_y") &&
384 nodemap_storage.erase(it);
387 for (std::map<std::string, Graph::EdgeMap<double>*>::iterator it =
388 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
390 if ((it->first != "id") &&
391 (it->first != "arrow_pos_x") &&
392 (it->first != "arrow_pos_y"))
395 edgemap_storage.erase(it);
398 for (std::map<std::string, double>::iterator it =
399 nodemap_default.begin(); it != nodemap_default.end(); ++it)
401 if (it->first != "id")
402 nodemap_default.erase(it);
404 for (std::map<std::string, double>::iterator it =
405 edgemap_default.begin(); it != edgemap_default.end(); ++it)
407 if (it->first != "id")
408 edgemap_default.erase(it);
415 void MapStorage::ArrowPosReadOK()
417 arrow_pos_read_ok = true;