An additional simplier interface for static size graphs.
Node operator()(int) for getting node by index
int index(Node node) for getting index by node
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["id"] = new Graph::NodeMap<double>(graph);
21 edgemap_storage["id"] = new Graph::EdgeMap<double>(graph);
23 nodemap_default["id"] = 1.0;
24 edgemap_default["id"] = 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 == "id")
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;
274 edgemap_storage[*it] = new Graph::EdgeMap<double>(graph);
275 //std::cout << "read " << *it << " edgemap" << std::endl;
277 greader.readEdgeMap(*it, *edgemap_storage[*it]);
279 GuiReader gui_reader(greader, this);
281 } catch (Exception& error) {
282 Gtk::MessageDialog mdialog(error.what());
290 edgemap_storage["id"] = new Graph::EdgeMap<double>(graph);
292 for (EdgeIt e(graph); e != INVALID; ++e)
294 (*edgemap_storage["id"])[e] = i++;
298 if (!read_x || !read_y)
301 for (NodeIt n(graph); n != INVALID; ++n)
305 const double pi = 3.142;
306 double step = 2 * pi / (double) node_num;
308 for (NodeIt n(graph); n != INVALID; ++n)
310 nodemap_storage["coordinates_x"]->set(n, 250.0 * std::cos(i * step));
311 nodemap_storage["coordinates_y"]->set(n, 250.0 * std::sin(i * step));
316 if (!arrow_pos_read_ok)
318 arrow_pos_read_ok = false;
319 for (EdgeIt e(graph); e != INVALID; ++e)
321 arrow_pos.set(e, (coords[graph.source(e)] + coords[graph.target(e)]) / 2.0);
325 // fill in the default values for the maps
326 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
327 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
329 if ((it->first != "id") &&
330 (it->first != "coordiantes_x") &&
331 (it->first != "coordinates_y"))
333 nodemap_default[it->first] = 0.0;
335 else if (it->first == "id")
338 double max = (*nodemap_storage["id"])[n];
339 for (; n != INVALID; ++n)
341 if ((*nodemap_storage["id"])[n] > max)
342 max = (*nodemap_storage["id"])[n];
344 nodemap_default["id"] = max + 1.0;
347 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
348 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
350 if (it->first != "id")
352 edgemap_default[it->first] = 0.0;
356 double max = std::numeric_limits<double>::min();
357 for (EdgeIt e(graph); e != INVALID; ++e)
359 if ((*edgemap_storage["id"])[e] > max)
360 max = (*edgemap_storage["id"])[e];
362 if (max > std::numeric_limits<double>::min())
363 edgemap_default["id"] = max + 1.0;
365 edgemap_default["id"] = 1.0;
372 void MapStorage::writeToFile(const std::string &filename)
374 GraphWriter<Graph> gwriter(filename, graph);
376 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
377 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
379 gwriter.writeNodeMap(it->first, *(it->second));
381 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
382 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
384 if ((it->first != "arrow_pos_x") &&
385 (it->first != "arrow_pos_y"))
387 gwriter.writeEdgeMap(it->first, *(it->second));
391 GuiWriter gui_writer(gwriter, this);
396 void MapStorage::clear()
398 for (std::map<std::string, Graph::NodeMap<double>*>::iterator it =
399 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
401 if ((it->first != "coordinates_x") &&
402 (it->first != "coordinates_y") &&
406 nodemap_storage.erase(it);
409 for (std::map<std::string, Graph::EdgeMap<double>*>::iterator it =
410 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
412 if ((it->first != "id") &&
413 (it->first != "arrow_pos_x") &&
414 (it->first != "arrow_pos_y"))
417 edgemap_storage.erase(it);
420 for (std::map<std::string, double>::iterator it =
421 nodemap_default.begin(); it != nodemap_default.end(); ++it)
423 if (it->first != "id")
424 nodemap_default.erase(it);
426 for (std::map<std::string, double>::iterator it =
427 edgemap_default.begin(); it != edgemap_default.end(); ++it)
429 if (it->first != "id")
430 edgemap_default.erase(it);
437 void MapStorage::ArrowPosReadOK()
439 arrow_pos_read_ok = true;
442 void MapStorage::mapChanged(bool itisedge, std::string mapname)
446 for(int i=0;i<EDGE_PROPERTY_NUM;i++)
448 if(active_edgemaps[i]==mapname)
450 signal_prop.emit(itisedge, i);
456 for(int i=0;i<NODE_PROPERTY_NUM;i++)
458 if(active_nodemaps[i]==mapname)
460 signal_prop.emit(itisedge, i);