Slight changes in doc.
1 #include "mapstorage.h"
2 #include "gui_writer.h"
3 #include "gui_reader.h"
8 MapStorage::MapStorage() : modified(false), file_name(""), arrow_pos_read_ok(false)
10 nodemap_storage["coordinates_x"] = new Graph::NodeMap<double>(graph);
11 coords.setXMap(*nodemap_storage["coordinates_x"]);
12 nodemap_storage["coordinates_y"] = new Graph::NodeMap<double>(graph);
13 coords.setYMap(*nodemap_storage["coordinates_y"]);
15 edgemap_storage["arrow_pos_x"] = new Graph::EdgeMap<double>(graph);
16 arrow_pos.setXMap(*edgemap_storage["arrow_pos_x"]);
17 edgemap_storage["arrow_pos_y"] = new Graph::EdgeMap<double>(graph);
18 arrow_pos.setYMap(*edgemap_storage["arrow_pos_y"]);
20 nodemap_storage["label"] = new Graph::NodeMap<double>(graph);
21 edgemap_storage["label"] = new Graph::EdgeMap<double>(graph);
23 nodemap_default["label"] = 1.0;
24 edgemap_default["label"] = 1.0;
26 active_nodemaps.resize(NODE_PROPERTY_NUM);
27 for(int i=0;i<NODE_PROPERTY_NUM;i++)
29 active_nodemaps[i]="";
32 active_edgemaps.resize(EDGE_PROPERTY_NUM);
33 for(int i=0;i<EDGE_PROPERTY_NUM;i++)
35 active_edgemaps[i]="";
39 MapStorage::~MapStorage()
41 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
42 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
46 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
47 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
53 int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap<double> *nodemap, double default_value)
55 std::cout << default_value << std::endl;
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;
62 //announce changement in maps
63 signal_node_map.emit(name);
69 void MapStorage::changeActiveMap(bool itisedge, int prop, std::string mapname)
73 active_edgemaps[prop]=mapname;
77 active_nodemaps[prop]=mapname;
79 signal_prop.emit(itisedge, prop);
82 std::string MapStorage::getActiveEdgeMap(int prop)
84 return active_edgemaps[prop];
87 std::string MapStorage::getActiveNodeMap(int prop)
89 return active_nodemaps[prop];
92 std::vector<std::string> MapStorage::getEdgeMapList()
94 std::vector<std::string> eml;
95 eml.resize(edgemap_storage.size());
97 std::map< std::string,Graph::EdgeMap<double> * >::iterator emsi=beginOfEdgeMaps();
98 for(;emsi!=endOfEdgeMaps();emsi++)
100 eml[i]=(emsi->first);
106 std::vector<std::string> MapStorage::getNodeMapList()
108 std::vector<std::string> nml;
109 nml.resize(nodemap_storage.size());
111 std::map< std::string,Graph::NodeMap<double> * >::iterator nmsi=beginOfNodeMaps();
112 for(;nmsi!=endOfNodeMaps();nmsi++)
114 nml[i]=(nmsi->first);
120 MapStorage::Signal_Prop MapStorage::signal_prop_ch()
125 int MapStorage::addEdgeMap(const std::string & name, Graph::EdgeMap<double> *edgemap, double default_value)
127 if( edgemap_storage.find(name) == edgemap_storage.end() )
129 edgemap_storage[name]=edgemap;
130 // set the maps default value
131 edgemap_default[name] = default_value;
133 //announce changement in maps
134 signal_edge_map.emit(name);
140 double MapStorage::maxOfNodeMap(const std::string & name)
143 for (NodeIt j(graph); j!=INVALID; ++j)
145 if( (*nodemap_storage[name])[j]>max )
147 max=(*nodemap_storage[name])[j];
153 double MapStorage::maxOfEdgeMap(const std::string & name)
156 for (EdgeIt j(graph); j!=INVALID; ++j)
158 if( (*edgemap_storage[name])[j]>max )
160 max=(*edgemap_storage[name])[j];
166 double MapStorage::minOfNodeMap(const std::string & name)
172 min=(*nodemap_storage[name])[j];
178 for (; j!=INVALID; ++j)
180 if( (*nodemap_storage[name])[j]<min )
182 min=(*nodemap_storage[name])[j];
188 double MapStorage::minOfEdgeMap(const std::string & name)
194 min=(*edgemap_storage[name])[j];
200 for (EdgeIt j(graph); j!=INVALID; ++j)
202 if( (*edgemap_storage[name])[j]<min )
204 min=(*edgemap_storage[name])[j];
210 int MapStorage::readFromFile(const std::string &filename)
214 bool read_edge_id = false;
217 LemonReader lreader(filename);
218 ContentReader content(lreader);
221 if (content.nodeSetNum() < 1)
223 Gtk::MessageDialog mdialog("No nodeset found in file.");
229 if (content.edgeSetNum() < 1)
231 Gtk::MessageDialog mdialog("No edgeset found in file.");
237 const std::vector<std::string>& nodeMapNames = content.nodeSetMaps(0);
238 const std::vector<std::string>& edgeMapNames = content.edgeSetMaps(0);
240 GraphReader<Graph> greader(filename, graph);
241 for (std::vector<std::string>::const_iterator it = nodeMapNames.begin();
242 it != nodeMapNames.end(); ++it)
244 if (*it == "coordinates_x")
247 //std::cout << "read X nodemap" << std::endl;
249 else if (*it == "coordinates_y")
252 //std::cout << "read Y nodemap" << std::endl;
254 else if (*it == "label")
256 //std::cout << "read id nodemap" << std::endl;
260 nodemap_storage[*it] = new Graph::NodeMap<double>(graph);
261 //std::cout << "read " << *it << " nodemap" << std::endl;
263 greader.readNodeMap(*it, *nodemap_storage[*it]);
265 for (std::vector<std::string>::const_iterator it = edgeMapNames.begin();
266 it != edgeMapNames.end(); ++it)
270 //std::cout << "read id edgemap" << std::endl;
275 edgemap_storage[*it] = new Graph::EdgeMap<double>(graph);
276 //std::cout << "read " << *it << " edgemap" << std::endl;
278 greader.readEdgeMap(*it, *edgemap_storage[*it]);
280 GuiReader gui_reader(greader, this);
282 } catch (Exception& error) {
283 Gtk::MessageDialog mdialog(error.what());
291 edgemap_storage["label"] = new Graph::EdgeMap<double>(graph);
293 for (EdgeIt e(graph); e != INVALID; ++e)
295 (*edgemap_storage["label"])[e] = i++;
299 if (!read_x || !read_y)
302 for (NodeIt n(graph); n != INVALID; ++n)
306 const double pi = 3.142;
307 double step = 2 * pi / (double) node_num;
309 for (NodeIt n(graph); n != INVALID; ++n)
311 nodemap_storage["coordinates_x"]->set(n, 250.0 * std::cos(i * step));
312 nodemap_storage["coordinates_y"]->set(n, 250.0 * std::sin(i * step));
317 if (!arrow_pos_read_ok)
319 arrow_pos_read_ok = false;
320 for (EdgeIt e(graph); e != INVALID; ++e)
322 arrow_pos.set(e, (coords[graph.source(e)] + coords[graph.target(e)]) / 2.0);
326 // fill in the default values for the maps
327 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
328 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
330 if ((it->first != "label") &&
331 (it->first != "coordiantes_x") &&
332 (it->first != "coordinates_y"))
334 nodemap_default[it->first] = 0.0;
336 else if (it->first == "label")
339 double max = (*nodemap_storage["label"])[n];
340 for (; n != INVALID; ++n)
342 if ((*nodemap_storage["label"])[n] > max)
343 max = (*nodemap_storage["label"])[n];
345 nodemap_default["label"] = max + 1.0;
348 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
349 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
351 if (it->first != "label")
353 edgemap_default[it->first] = 0.0;
357 double max = std::numeric_limits<double>::min();
358 for (EdgeIt e(graph); e != INVALID; ++e)
360 if ((*edgemap_storage["label"])[e] > max)
361 max = (*edgemap_storage["label"])[e];
363 if (max > std::numeric_limits<double>::min())
364 edgemap_default["label"] = max + 1.0;
366 edgemap_default["label"] = 1.0;
371 for (EdgeIt e(graph); e != INVALID; ++e)
373 if (graph.source(e) == graph.target(e))
375 std::cerr << "Removed loop edge " << (*edgemap_storage["label"])[e]
376 << " (from " << (*nodemap_storage["label"])[graph.source(e)]
377 << ", to " << (*nodemap_storage["label"])[graph.target(e)] << ")."
387 void MapStorage::writeToFile(const std::string &filename)
389 GraphWriter<Graph> gwriter(filename, graph);
391 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
392 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
394 gwriter.writeNodeMap(it->first, *(it->second));
396 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
397 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
399 if ((it->first != "arrow_pos_x") &&
400 (it->first != "arrow_pos_y"))
402 gwriter.writeEdgeMap(it->first, *(it->second));
406 GuiWriter gui_writer(gwriter, this);
411 void MapStorage::clear()
413 for (std::map<std::string, Graph::NodeMap<double>*>::iterator it =
414 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
416 if ((it->first != "coordinates_x") &&
417 (it->first != "coordinates_y") &&
418 (it->first != "label"))
421 nodemap_storage.erase(it);
424 for (std::map<std::string, Graph::EdgeMap<double>*>::iterator it =
425 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
427 if ((it->first != "label") &&
428 (it->first != "arrow_pos_x") &&
429 (it->first != "arrow_pos_y"))
432 edgemap_storage.erase(it);
435 for (std::map<std::string, double>::iterator it =
436 nodemap_default.begin(); it != nodemap_default.end(); ++it)
438 if (it->first != "label")
439 nodemap_default.erase(it);
441 for (std::map<std::string, double>::iterator it =
442 edgemap_default.begin(); it != edgemap_default.end(); ++it)
444 if (it->first != "label")
445 edgemap_default.erase(it);
452 void MapStorage::ArrowPosReadOK()
454 arrow_pos_read_ok = true;
457 void MapStorage::mapChanged(bool itisedge, std::string mapname)
461 for(int i=0;i<EDGE_PROPERTY_NUM;i++)
463 if(active_edgemaps[i]==mapname)
465 signal_prop.emit(itisedge, i);
471 for(int i=0;i<NODE_PROPERTY_NUM;i++)
473 if(active_nodemaps[i]==mapname)
475 signal_prop.emit(itisedge, i);