COIN-OR::LEMON - Graph Library

source: lemon-0.x/gui/mapstorage.cc @ 1867:15cf1fd6a505

Last change on this file since 1867:15cf1fd6a505 was 1867:15cf1fd6a505, checked in by Akos Ladanyi, 18 years ago

Fix crash when the input file does not contain any nodeset or edgeset.

File size: 10.3 KB
Line 
1#include "mapstorage.h"
2#include "gui_writer.h"
3#include "gui_reader.h"
4#include <gtkmm.h>
5#include <cmath>
6
7#include <cmath>
8
9MapStorage::MapStorage() : modified(false), file_name(""), arrow_pos_read_ok(false)
10{
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
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
21  nodemap_storage["id"] = new Graph::NodeMap<double>(graph);
22  edgemap_storage["id"] = new Graph::EdgeMap<double>(graph);
23
24  nodemap_default["id"] = 1.0;
25  edgemap_default["id"] = 1.0;
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    }
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}
53
54int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap<double> *nodemap, double default_value = 0.0)
55{
56  if( nodemap_storage.find(name) == nodemap_storage.end() )
57    {
58      nodemap_storage[name]=nodemap;
59      // set the maps default value
60      nodemap_default[name] = default_value;
61      return 0;
62    }
63  return 1;
64}
65
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
122int MapStorage::addEdgeMap(const std::string & name, Graph::EdgeMap<double> *edgemap, double default_value = 0.0)
123{
124  if( edgemap_storage.find(name) == edgemap_storage.end() )
125    {
126      edgemap_storage[name]=edgemap;
127      // set the maps default value
128      edgemap_default[name] = default_value;
129      return 0;
130    }
131  return 1;
132}
133
134double MapStorage::maxOfNodeMap(const std::string & name)
135{
136  double max=0;
137  for (NodeIt j(graph); j!=INVALID; ++j)
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;
150  for (EdgeIt j(graph); j!=INVALID; ++j)
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{
162  NodeIt j(graph);
163  double min;
164  if(j!=INVALID)
165    {
166      min=(*nodemap_storage[name])[j];
167    }
168  else
169    {
170      min=0;
171    }
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{
184  EdgeIt j(graph);
185  double min;
186  if(j!=INVALID)
187    {
188      min=(*edgemap_storage[name])[j];
189    }
190  else
191    {
192      min=0;
193    }
194  for (EdgeIt j(graph); j!=INVALID; ++j)
195  {
196    if( (*edgemap_storage[name])[j]<min )
197    {
198      min=(*edgemap_storage[name])[j];
199    }
200  }
201  return min;
202}
203
204int MapStorage::readFromFile(const std::string &filename)
205{
206  bool read_x = false;
207  bool read_y = false;
208  bool read_edge_id = false;
209
210  try {
211    LemonReader lreader(filename);
212    ContentReader content(lreader);
213    lreader.run();
214
215    if (content.nodeSetNum() < 1)
216    {
217      Gtk::MessageDialog mdialog("No nodeset found in file.");
218      mdialog.run();
219      clear();
220      return 1;
221    }
222
223    if (content.edgeSetNum() < 1)
224    {
225      Gtk::MessageDialog mdialog("No edgeset found in file.");
226      mdialog.run();
227      clear();
228      return 1;
229    }
230
231    const std::vector<std::string>& nodeMapNames = content.nodeSetMaps(0);
232    const std::vector<std::string>& edgeMapNames = content.edgeSetMaps(0);
233
234    GraphReader<Graph> greader(filename, graph);
235    for (std::vector<std::string>::const_iterator it = nodeMapNames.begin();
236        it != nodeMapNames.end(); ++it)
237    {
238      if (*it == "coordinates_x")
239      {
240        read_x = true;
241        //std::cout << "read X nodemap" << std::endl;
242      }
243      else if (*it == "coordinates_y")
244      {
245        read_y = true;
246        //std::cout << "read Y nodemap" << std::endl;
247      }
248      else if (*it == "id")
249      {
250        //std::cout << "read id nodemap" << std::endl;
251      }
252      else
253      {
254        nodemap_storage[*it] = new Graph::NodeMap<double>(graph);
255        //std::cout << "read " << *it << " nodemap" << std::endl;
256      }
257      greader.readNodeMap(*it, *nodemap_storage[*it]);
258    }
259    for (std::vector<std::string>::const_iterator it = edgeMapNames.begin();
260        it != edgeMapNames.end(); ++it)
261    {
262      if (*it == "id")
263      {
264        //std::cout << "read id edgemap" << std::endl;
265      }
266      else
267      {
268        edgemap_storage[*it] = new Graph::EdgeMap<double>(graph);
269        //std::cout << "read " << *it << " edgemap" << std::endl;
270      }
271      greader.readEdgeMap(*it, *edgemap_storage[*it]);
272    }
273    GuiReader gui_reader(greader, this);
274    greader.run();
275  } catch (Exception& error) {
276    Gtk::MessageDialog mdialog(error.what());
277    mdialog.run();
278    clear();
279    return 1;
280  }
281
282  if (!read_edge_id)
283  {
284    edgemap_storage["id"] = new Graph::EdgeMap<double>(graph);
285    int i = 1;
286    for (EdgeIt e(graph); e != INVALID; ++e)
287    {
288      (*edgemap_storage["id"])[e] = i++;
289    }
290  }
291
292  if (!read_x || !read_y)
293  {
294    int node_num = 0;
295    for (NodeIt n(graph); n != INVALID; ++n)
296    {
297      node_num++;
298    }
299    const double pi = 3.142;
300    double step = 2 * pi / (double) node_num;
301    int i = 0;
302    for (NodeIt n(graph); n != INVALID; ++n)
303    {
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));
306      i++;
307    }
308  }
309
310  if (!arrow_pos_read_ok)
311  {
312    arrow_pos_read_ok = false;
313    for (EdgeIt e(graph); e != INVALID; ++e)
314    {
315      arrow_pos.set(e, (coords[graph.source(e)] + coords[graph.target(e)]) / 2.0);
316    }
317  }
318
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)
322  {
323    if ((it->first != "id") &&
324        (it->first != "coordiantes_x") &&
325        (it->first != "coordinates_y"))
326    {
327      nodemap_default[it->first] = 0.0;
328    }
329    else if (it->first == "id")
330    {
331      NodeIt n(graph);
332      double max = (*nodemap_storage["id"])[n];
333      for (; n != INVALID; ++n)
334      {
335        if ((*nodemap_storage["id"])[n] > max)
336          max = (*nodemap_storage["id"])[n];
337      }
338      nodemap_default["id"] = max + 1.0;
339    }
340  }
341  for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
342      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
343  {
344    if (it->first != "id")
345    {
346      edgemap_default[it->first] = 0.0;
347    }
348    else
349    {
350      double max = std::numeric_limits<double>::min();
351      for (EdgeIt e(graph); e != INVALID; ++e)
352      {
353        if ((*edgemap_storage["id"])[e] > max)
354          max = (*edgemap_storage["id"])[e];
355      }
356      if (max > std::numeric_limits<double>::min())
357        edgemap_default["id"] = max + 1.0;
358      else
359        edgemap_default["id"] = 1.0;
360    }
361  }
362
363  return 0;
364}
365
366void MapStorage::writeToFile(const std::string &filename)
367{
368  GraphWriter<Graph> gwriter(filename, graph);
369
370  for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
371      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
372  {
373    gwriter.writeNodeMap(it->first, *(it->second));
374  }
375  for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
376      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
377  {
378    if ((it->first != "arrow_pos_x") &&
379        (it->first != "arrow_pos_y"))
380    {
381      gwriter.writeEdgeMap(it->first, *(it->second));
382    }
383  }
384
385  GuiWriter gui_writer(gwriter, this);
386
387  gwriter.run();
388}
389
390void MapStorage::clear()
391{
392  for (std::map<std::string, Graph::NodeMap<double>*>::iterator it =
393      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
394  {
395    if ((it->first != "coordinates_x") &&
396        (it->first != "coordinates_y") &&
397        (it->first != "id"))
398    {
399      delete it->second;
400      nodemap_storage.erase(it);
401    }
402  }
403  for (std::map<std::string, Graph::EdgeMap<double>*>::iterator it =
404      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
405  {
406    if ((it->first != "id") &&
407        (it->first != "arrow_pos_x") &&
408        (it->first != "arrow_pos_y"))
409    {
410      delete it->second;
411      edgemap_storage.erase(it);
412    }
413  }
414  for (std::map<std::string, double>::iterator it =
415      nodemap_default.begin(); it != nodemap_default.end(); ++it)
416  {
417    if (it->first != "id")
418      nodemap_default.erase(it);
419  }
420  for (std::map<std::string, double>::iterator it =
421      edgemap_default.begin(); it != edgemap_default.end(); ++it)
422  {
423    if (it->first != "id")
424      edgemap_default.erase(it);
425  }
426  graph.clear();
427  file_name = "";
428  modified = false;
429}
430
431void MapStorage::ArrowPosReadOK()
432{
433  arrow_pos_read_ok = true;
434}
Note: See TracBrowser for help on using the repository browser.