Communication with algorithm window is developed.
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 if (content.nodeSetNum() < 1)
217 Gtk::MessageDialog mdialog("No nodeset found in file.");
223 if (content.edgeSetNum() < 1)
225 Gtk::MessageDialog mdialog("No edgeset found in file.");
231 const std::vector<std::string>& nodeMapNames = content.nodeSetMaps(0);
232 const std::vector<std::string>& edgeMapNames = content.edgeSetMaps(0);
234 GraphReader<Graph> greader(filename, graph);
235 for (std::vector<std::string>::const_iterator it = nodeMapNames.begin();
236 it != nodeMapNames.end(); ++it)
238 if (*it == "coordinates_x")
241 //std::cout << "read X nodemap" << std::endl;
243 else if (*it == "coordinates_y")
246 //std::cout << "read Y nodemap" << std::endl;
248 else if (*it == "id")
250 //std::cout << "read id nodemap" << std::endl;
254 nodemap_storage[*it] = new Graph::NodeMap<double>(graph);
255 //std::cout << "read " << *it << " nodemap" << std::endl;
257 greader.readNodeMap(*it, *nodemap_storage[*it]);
259 for (std::vector<std::string>::const_iterator it = edgeMapNames.begin();
260 it != edgeMapNames.end(); ++it)
264 //std::cout << "read id edgemap" << std::endl;
268 edgemap_storage[*it] = new Graph::EdgeMap<double>(graph);
269 //std::cout << "read " << *it << " edgemap" << std::endl;
271 greader.readEdgeMap(*it, *edgemap_storage[*it]);
273 GuiReader gui_reader(greader, this);
275 } catch (Exception& error) {
276 Gtk::MessageDialog mdialog(error.what());
284 edgemap_storage["id"] = new Graph::EdgeMap<double>(graph);
286 for (EdgeIt e(graph); e != INVALID; ++e)
288 (*edgemap_storage["id"])[e] = i++;
292 if (!read_x || !read_y)
295 for (NodeIt n(graph); n != INVALID; ++n)
299 const double pi = 3.142;
300 double step = 2 * pi / (double) node_num;
302 for (NodeIt n(graph); n != INVALID; ++n)
304 nodemap_storage["coordinates_x"]->set(n, 250.0 * std::cos(i * step));
305 nodemap_storage["coordinates_y"]->set(n, 250.0 * std::sin(i * step));
310 if (!arrow_pos_read_ok)
312 arrow_pos_read_ok = false;
313 for (EdgeIt e(graph); e != INVALID; ++e)
315 arrow_pos.set(e, (coords[graph.source(e)] + coords[graph.target(e)]) / 2.0);
319 // fill in the default values for the maps
320 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
321 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
323 if ((it->first != "id") &&
324 (it->first != "coordiantes_x") &&
325 (it->first != "coordinates_y"))
327 nodemap_default[it->first] = 0.0;
329 else if (it->first == "id")
332 double max = (*nodemap_storage["id"])[n];
333 for (; n != INVALID; ++n)
335 if ((*nodemap_storage["id"])[n] > max)
336 max = (*nodemap_storage["id"])[n];
338 nodemap_default["id"] = max + 1.0;
341 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
342 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
344 if (it->first != "id")
346 edgemap_default[it->first] = 0.0;
350 double max = std::numeric_limits<double>::min();
351 for (EdgeIt e(graph); e != INVALID; ++e)
353 if ((*edgemap_storage["id"])[e] > max)
354 max = (*edgemap_storage["id"])[e];
356 if (max > std::numeric_limits<double>::min())
357 edgemap_default["id"] = max + 1.0;
359 edgemap_default["id"] = 1.0;
366 void MapStorage::writeToFile(const std::string &filename)
368 GraphWriter<Graph> gwriter(filename, graph);
370 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
371 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
373 gwriter.writeNodeMap(it->first, *(it->second));
375 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
376 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
378 if ((it->first != "arrow_pos_x") &&
379 (it->first != "arrow_pos_y"))
381 gwriter.writeEdgeMap(it->first, *(it->second));
385 GuiWriter gui_writer(gwriter, this);
390 void MapStorage::clear()
392 for (std::map<std::string, Graph::NodeMap<double>*>::iterator it =
393 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
395 if ((it->first != "coordinates_x") &&
396 (it->first != "coordinates_y") &&
400 nodemap_storage.erase(it);
403 for (std::map<std::string, Graph::EdgeMap<double>*>::iterator it =
404 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
406 if ((it->first != "id") &&
407 (it->first != "arrow_pos_x") &&
408 (it->first != "arrow_pos_y"))
411 edgemap_storage.erase(it);
414 for (std::map<std::string, double>::iterator it =
415 nodemap_default.begin(); it != nodemap_default.end(); ++it)
417 if (it->first != "id")
418 nodemap_default.erase(it);
420 for (std::map<std::string, double>::iterator it =
421 edgemap_default.begin(); it != edgemap_default.end(); ++it)
423 if (it->first != "id")
424 edgemap_default.erase(it);
431 void MapStorage::ArrowPosReadOK()
433 arrow_pos_read_ok = true;