COIN-OR::LEMON - Graph Library

source: lemon-0.x/gui/mapstorage.cc @ 1860:27a9a75b957b

Last change on this file since 1860:27a9a75b957b was 1860:27a9a75b957b, checked in by Akos Ladanyi, 14 years ago

Save and load the coordinates of the arrows on the edges.

File size: 10.0 KB
RevLine 
[1606]1#include "mapstorage.h"
[1860]2#include "gui_writer.h"
3#include "gui_reader.h"
[1645]4#include <gtkmm.h>
[1634]5#include <cmath>
[1442]6
[1686]7#include <cmath>
8
[1860]9MapStorage::MapStorage() : modified(false), file_name(""), arrow_pos_read_ok(false)
[1442]10{
[1606]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"]);
15
[1860]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"]);
20
[1606]21  nodemap_storage["id"] = new Graph::NodeMap<double>(graph);
22  edgemap_storage["id"] = new Graph::EdgeMap<double>(graph);
[1646]23
24  nodemap_default["id"] = 1.0;
25  edgemap_default["id"] = 1.0;
[1837]26
27  active_nodemaps.resize(NODE_PROPERTY_NUM);
28  for(int i=0;i<NODE_PROPERTY_NUM;i++)
29    {
30      active_nodemaps[i]="";
31    }
32
33  active_edgemaps.resize(EDGE_PROPERTY_NUM);
34  for(int i=0;i<EDGE_PROPERTY_NUM;i++)
35    {
36      active_edgemaps[i]="";
37    }
[1606]38}
39
40MapStorage::~MapStorage()
41{
42  for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
43      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
44  {
45    delete it->second;
46  }
47  for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
48      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
49  {
50    delete it->second;
51  }
52}
[1442]53
[1645]54int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap<double> *nodemap, double default_value = 0.0)
[1442]55{
[1597]56  if( nodemap_storage.find(name) == nodemap_storage.end() )
57    {
58      nodemap_storage[name]=nodemap;
[1645]59      // set the maps default value
60      nodemap_default[name] = default_value;
[1597]61      return 0;
62    }
63  return 1;
[1442]64}
65
[1837]66void MapStorage::changeActiveMap(bool itisedge, int prop, std::string mapname)
67{
68  if(itisedge)
69    {
70      active_edgemaps[prop]=mapname;
71    }
72  else
73    {
74      active_nodemaps[prop]=mapname;
75    }
76  signal_prop.emit(itisedge, prop);
77}
78
79std::string MapStorage::getActiveEdgeMap(int prop)
80{
81  return active_edgemaps[prop];
82}
83
84std::string MapStorage::getActiveNodeMap(int prop)
85{
86  return active_nodemaps[prop];
87}
88
89std::vector<std::string> MapStorage::getEdgeMapList()
90{
91  std::vector<std::string> eml;
92  eml.resize(edgemap_storage.size());
93  int i=0;
94  std::map< std::string,Graph::EdgeMap<double> * >::iterator emsi=beginOfEdgeMaps();
95  for(;emsi!=endOfEdgeMaps();emsi++)
96    {
97      eml[i]=(emsi->first);
98      i++;
99    }
100  return eml;
101}
102
103std::vector<std::string> MapStorage::getNodeMapList()
104{
105  std::vector<std::string> nml;
106  nml.resize(nodemap_storage.size());
107  int i=0;
108  std::map< std::string,Graph::NodeMap<double> * >::iterator nmsi=beginOfNodeMaps();
109  for(;nmsi!=endOfNodeMaps();nmsi++)
110    {
111      nml[i]=(nmsi->first);
112      i++;
113    }
114  return nml;
115}
116
117MapStorage::Signal_Prop MapStorage::signal_prop_ch()
118{
119  return signal_prop;
120}
121
[1645]122int MapStorage::addEdgeMap(const std::string & name, Graph::EdgeMap<double> *edgemap, double default_value = 0.0)
[1442]123{
[1597]124  if( edgemap_storage.find(name) == edgemap_storage.end() )
125    {
126      edgemap_storage[name]=edgemap;
[1645]127      // set the maps default value
128      edgemap_default[name] = default_value;
[1597]129      return 0;
130    }
131  return 1;
[1442]132}
133
134double MapStorage::maxOfNodeMap(const std::string & name)
135{
136  double max=0;
[1606]137  for (NodeIt j(graph); j!=INVALID; ++j)
[1442]138  {
139    if( (*nodemap_storage[name])[j]>max )
140    {
141      max=(*nodemap_storage[name])[j];
142    }
143  }
144  return max;
145}
146
147double MapStorage::maxOfEdgeMap(const std::string & name)
148{
149  double max=0;
[1606]150  for (EdgeIt j(graph); j!=INVALID; ++j)
[1442]151  {
152    if( (*edgemap_storage[name])[j]>max )
153    {
154      max=(*edgemap_storage[name])[j];
155    }
156  }
157  return max;
158}
159
160double MapStorage::minOfNodeMap(const std::string & name)
161{
[1606]162  NodeIt j(graph);
[1617]163  double min;
164  if(j!=INVALID)
165    {
166      min=(*nodemap_storage[name])[j];
167    }
168  else
169    {
170      min=0;
171    }
[1442]172  for (; j!=INVALID; ++j)
173  {
174    if( (*nodemap_storage[name])[j]<min )
175    {
176      min=(*nodemap_storage[name])[j];
177    }
178  }
179  return min;
180}
181
182double MapStorage::minOfEdgeMap(const std::string & name)
183{
[1606]184  EdgeIt j(graph);
[1617]185  double min;
186  if(j!=INVALID)
187    {
188      min=(*edgemap_storage[name])[j];
189    }
190  else
191    {
192      min=0;
193    }
[1606]194  for (EdgeIt j(graph); j!=INVALID; ++j)
[1442]195  {
196    if( (*edgemap_storage[name])[j]<min )
197    {
198      min=(*edgemap_storage[name])[j];
199    }
200  }
201  return min;
202}
203
[1645]204int MapStorage::readFromFile(const std::string &filename)
[1606]205{
206  bool read_x = false;
207  bool read_y = false;
[1646]208  bool read_edge_id = false;
[1606]209
210  try {
211    LemonReader lreader(filename);
212    ContentReader content(lreader);
213    lreader.run();
214
215    const std::vector<std::string>& nodeMapNames = content.nodeSetMaps(0);
216    const std::vector<std::string>& edgeMapNames = content.edgeSetMaps(0);
217
218    GraphReader<Graph> greader(filename, graph);
219    for (std::vector<std::string>::const_iterator it = nodeMapNames.begin();
220        it != nodeMapNames.end(); ++it)
221    {
222      if (*it == "coordinates_x")
223      {
224        read_x = true;
225        //std::cout << "read X nodemap" << std::endl;
226      }
227      else if (*it == "coordinates_y")
228      {
229        read_y = true;
230        //std::cout << "read Y nodemap" << std::endl;
231      }
232      else if (*it == "id")
233      {
234        //std::cout << "read id nodemap" << std::endl;
235      }
236      else
237      {
238        nodemap_storage[*it] = new Graph::NodeMap<double>(graph);
239        //std::cout << "read " << *it << " nodemap" << std::endl;
240      }
241      greader.readNodeMap(*it, *nodemap_storage[*it]);
242    }
243    for (std::vector<std::string>::const_iterator it = edgeMapNames.begin();
244        it != edgeMapNames.end(); ++it)
245    {
246      if (*it == "id")
247      {
248        //std::cout << "read id edgemap" << std::endl;
249      }
250      else
251      {
252        edgemap_storage[*it] = new Graph::EdgeMap<double>(graph);
253        //std::cout << "read " << *it << " edgemap" << std::endl;
254      }
255      greader.readEdgeMap(*it, *edgemap_storage[*it]);
256    }
[1860]257    GuiReader gui_reader(greader, this);
[1606]258    greader.run();
259  } catch (DataFormatError& error) {
[1708]260    Gtk::MessageDialog mdialog(error.what());
[1606]261    mdialog.run();
[1645]262    clear();
263    return 1;
[1606]264  }
265
[1646]266  if (!read_edge_id)
267  {
268    edgemap_storage["id"] = new Graph::EdgeMap<double>(graph);
269    int i = 1;
270    for (EdgeIt e(graph); e != INVALID; ++e)
271    {
272      (*edgemap_storage["id"])[e] = i++;
273    }
274  }
275
[1606]276  if (!read_x || !read_y)
277  {
278    int node_num = 0;
279    for (NodeIt n(graph); n != INVALID; ++n)
280    {
281      node_num++;
282    }
283    const double pi = 3.142;
284    double step = 2 * pi / (double) node_num;
285    int i = 0;
286    for (NodeIt n(graph); n != INVALID; ++n)
287    {
[1686]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));
[1606]290      i++;
291    }
292  }
[1645]293
[1860]294  if (!arrow_pos_read_ok)
295  {
296    arrow_pos_read_ok = false;
297    for (EdgeIt e(graph); e != INVALID; ++e)
298    {
299      arrow_pos.set(e, (coords[graph.source(e)] + coords[graph.target(e)]) / 2.0);
300    }
301  }
302
[1645]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)
306  {
307    if ((it->first != "id") &&
308        (it->first != "coordiantes_x") &&
309        (it->first != "coordinates_y"))
310    {
311      nodemap_default[it->first] = 0.0;
312    }
[1646]313    else if (it->first == "id")
314    {
315      NodeIt n(graph);
316      double max = (*nodemap_storage["id"])[n];
317      for (; n != INVALID; ++n)
318      {
319        if ((*nodemap_storage["id"])[n] > max)
320          max = (*nodemap_storage["id"])[n];
321      }
322      nodemap_default["id"] = max + 1.0;
323    }
[1645]324  }
325  for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
326      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
327  {
328    if (it->first != "id")
329    {
330      edgemap_default[it->first] = 0.0;
331    }
[1646]332    else
333    {
[1649]334      double max = std::numeric_limits<double>::min();
335      for (EdgeIt e(graph); e != INVALID; ++e)
[1646]336      {
337        if ((*edgemap_storage["id"])[e] > max)
338          max = (*edgemap_storage["id"])[e];
339      }
[1649]340      if (max > std::numeric_limits<double>::min())
341        edgemap_default["id"] = max + 1.0;
342      else
343        edgemap_default["id"] = 1.0;
[1646]344    }
[1645]345  }
346
347  return 0;
[1606]348}
349
350void MapStorage::writeToFile(const std::string &filename)
351{
352  GraphWriter<Graph> gwriter(filename, graph);
353
354  for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
355      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
356  {
357    gwriter.writeNodeMap(it->first, *(it->second));
358  }
359  for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
360      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
361  {
[1860]362    if ((it->first != "arrow_pos_x") &&
363        (it->first != "arrow_pos_y"))
364    {
365      gwriter.writeEdgeMap(it->first, *(it->second));
366    }
[1606]367  }
[1860]368
369  GuiWriter gui_writer(gwriter, this);
370
[1606]371  gwriter.run();
372}
373
374void MapStorage::clear()
375{
376  for (std::map<std::string, Graph::NodeMap<double>*>::iterator it =
377      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
378  {
379    if ((it->first != "coordinates_x") &&
380        (it->first != "coordinates_y") &&
381        (it->first != "id"))
382    {
383      delete it->second;
384      nodemap_storage.erase(it);
385    }
386  }
387  for (std::map<std::string, Graph::EdgeMap<double>*>::iterator it =
388      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
389  {
[1860]390    if ((it->first != "id") &&
391        (it->first != "arrow_pos_x") &&
392        (it->first != "arrow_pos_y"))
[1606]393    {
394      delete it->second;
395      edgemap_storage.erase(it);
396    }
397  }
[1645]398  for (std::map<std::string, double>::iterator it =
399      nodemap_default.begin(); it != nodemap_default.end(); ++it)
400  {
[1646]401    if (it->first != "id")
402      nodemap_default.erase(it);
[1645]403  }
404  for (std::map<std::string, double>::iterator it =
405      edgemap_default.begin(); it != edgemap_default.end(); ++it)
406  {
[1646]407    if (it->first != "id")
408      edgemap_default.erase(it);
[1645]409  }
[1606]410  graph.clear();
411  file_name = "";
412  modified = false;
413}
[1860]414
415void MapStorage::ArrowPosReadOK()
416{
417  arrow_pos_read_ok = true;
418}
Note: See TracBrowser for help on using the repository browser.