COIN-OR::LEMON - Graph Library

source: lemon-0.x/gui/mapstorage.cc @ 1645:4a04bb856ac7

Last change on this file since 1645:4a04bb856ac7 was 1645:4a04bb856ac7, checked in by Akos Ladanyi, 15 years ago
  • id maps are not editable
  • handle exceptions thrown by the file reader
  • texts are always above the edges
  • store a default value for all maps, so that edges and nodes created after adding a new map receive the default value too
  • create node on button release, not on click (fixes a few oddities)
File size: 6.9 KB
Line 
1#include "mapstorage.h"
2#include <gtkmm.h>
3#include <cmath>
4
5MapStorage::MapStorage() : modified(false), file_name("")
6{
7  nodemap_storage["coordinates_x"] = new Graph::NodeMap<double>(graph);
8  coords.setXMap(*nodemap_storage["coordinates_x"]);
9  nodemap_storage["coordinates_y"] = new Graph::NodeMap<double>(graph);
10  coords.setYMap(*nodemap_storage["coordinates_y"]);
11
12  nodemap_storage["id"] = new Graph::NodeMap<double>(graph);
13  edgemap_storage["id"] = new Graph::EdgeMap<double>(graph);
14}
15
16MapStorage::~MapStorage()
17{
18  for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
19      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
20  {
21    delete it->second;
22  }
23  for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
24      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
25  {
26    delete it->second;
27  }
28}
29
30int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap<double> *nodemap, double default_value = 0.0)
31{
32  if( nodemap_storage.find(name) == nodemap_storage.end() )
33    {
34      nodemap_storage[name]=nodemap;
35      // set the maps default value
36      nodemap_default[name] = default_value;
37      return 0;
38    }
39  return 1;
40}
41
42int MapStorage::addEdgeMap(const std::string & name, Graph::EdgeMap<double> *edgemap, double default_value = 0.0)
43{
44  if( edgemap_storage.find(name) == edgemap_storage.end() )
45    {
46      edgemap_storage[name]=edgemap;
47      // set the maps default value
48      edgemap_default[name] = default_value;
49      return 0;
50    }
51  return 1;
52}
53
54double MapStorage::maxOfNodeMap(const std::string & name)
55{
56  double max=0;
57  for (NodeIt j(graph); j!=INVALID; ++j)
58  {
59    if( (*nodemap_storage[name])[j]>max )
60    {
61      max=(*nodemap_storage[name])[j];
62    }
63  }
64  return max;
65}
66
67double MapStorage::maxOfEdgeMap(const std::string & name)
68{
69  double max=0;
70  for (EdgeIt j(graph); j!=INVALID; ++j)
71  {
72    if( (*edgemap_storage[name])[j]>max )
73    {
74      max=(*edgemap_storage[name])[j];
75    }
76  }
77  return max;
78}
79
80double MapStorage::minOfNodeMap(const std::string & name)
81{
82  NodeIt j(graph);
83  double min;
84  if(j!=INVALID)
85    {
86      min=(*nodemap_storage[name])[j];
87    }
88  else
89    {
90      min=0;
91    }
92  for (; j!=INVALID; ++j)
93  {
94    if( (*nodemap_storage[name])[j]<min )
95    {
96      min=(*nodemap_storage[name])[j];
97    }
98  }
99  return min;
100}
101
102double MapStorage::minOfEdgeMap(const std::string & name)
103{
104  EdgeIt j(graph);
105  double min;
106  if(j!=INVALID)
107    {
108      min=(*edgemap_storage[name])[j];
109    }
110  else
111    {
112      min=0;
113    }
114  for (EdgeIt j(graph); j!=INVALID; ++j)
115  {
116    if( (*edgemap_storage[name])[j]<min )
117    {
118      min=(*edgemap_storage[name])[j];
119    }
120  }
121  return min;
122}
123
124int MapStorage::readFromFile(const std::string &filename)
125{
126  bool read_x = false;
127  bool read_y = false;
128
129  try {
130    LemonReader lreader(filename);
131    ContentReader content(lreader);
132    lreader.run();
133
134    const std::vector<std::string>& nodeMapNames = content.nodeSetMaps(0);
135    const std::vector<std::string>& edgeMapNames = content.edgeSetMaps(0);
136
137    GraphReader<Graph> greader(filename, graph);
138    for (std::vector<std::string>::const_iterator it = nodeMapNames.begin();
139        it != nodeMapNames.end(); ++it)
140    {
141      if (*it == "coordinates_x")
142      {
143        read_x = true;
144        //std::cout << "read X nodemap" << std::endl;
145      }
146      else if (*it == "coordinates_y")
147      {
148        read_y = true;
149        //std::cout << "read Y nodemap" << std::endl;
150      }
151      else if (*it == "id")
152      {
153        //std::cout << "read id nodemap" << std::endl;
154      }
155      else
156      {
157        nodemap_storage[*it] = new Graph::NodeMap<double>(graph);
158        //std::cout << "read " << *it << " nodemap" << std::endl;
159      }
160      greader.readNodeMap(*it, *nodemap_storage[*it]);
161    }
162    for (std::vector<std::string>::const_iterator it = edgeMapNames.begin();
163        it != edgeMapNames.end(); ++it)
164    {
165      if (*it == "id")
166      {
167        //std::cout << "read id edgemap" << std::endl;
168      }
169      else
170      {
171        edgemap_storage[*it] = new Graph::EdgeMap<double>(graph);
172        //std::cout << "read " << *it << " edgemap" << std::endl;
173      }
174      greader.readEdgeMap(*it, *edgemap_storage[*it]);
175    }
176    greader.run();
177  } catch (DataFormatError& error) {
178    Gtk::MessageDialog mdialog("Read Error");
179    mdialog.set_message(error.what());
180    mdialog.run();
181    clear();
182    return 1;
183  }
184
185  if (!read_x || !read_y)
186  {
187    int node_num = 0;
188    for (NodeIt n(graph); n != INVALID; ++n)
189    {
190      node_num++;
191    }
192    const double pi = 3.142;
193    double step = 2 * pi / (double) node_num;
194    int i = 0;
195    for (NodeIt n(graph); n != INVALID; ++n)
196    {
197      nodemap_storage["coordinates_x"]->set(n, 250.0 * cos(i * step));
198      nodemap_storage["coordinates_y"]->set(n, 250.0 * sin(i * step));
199      i++;
200    }
201  }
202
203  // fill in the default values for the maps
204  for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
205      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
206  {
207    if ((it->first != "id") &&
208        (it->first != "coordiantes_x") &&
209        (it->first != "coordinates_y"))
210    {
211      nodemap_default[it->first] = 0.0;
212    }
213  }
214  for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
215      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
216  {
217    if (it->first != "id")
218    {
219      edgemap_default[it->first] = 0.0;
220    }
221  }
222
223  return 0;
224}
225
226void MapStorage::writeToFile(const std::string &filename)
227{
228  GraphWriter<Graph> gwriter(filename, graph);
229
230  for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
231      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
232  {
233    gwriter.writeNodeMap(it->first, *(it->second));
234    //std::cout << "wrote " << it->first << " nodemap" << std::endl;
235  }
236  for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
237      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
238  {
239    gwriter.writeEdgeMap(it->first, *(it->second));
240    //std::cout << "wrote " << it->first << " edgemap" << std::endl;
241  }
242  gwriter.run();
243}
244
245void MapStorage::clear()
246{
247  for (std::map<std::string, Graph::NodeMap<double>*>::iterator it =
248      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
249  {
250    if ((it->first != "coordinates_x") &&
251        (it->first != "coordinates_y") &&
252        (it->first != "id"))
253    {
254      delete it->second;
255      nodemap_storage.erase(it);
256    }
257  }
258  for (std::map<std::string, Graph::EdgeMap<double>*>::iterator it =
259      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
260  {
261    if (it->first != "id")
262    {
263      delete it->second;
264      edgemap_storage.erase(it);
265    }
266  }
267  for (std::map<std::string, double>::iterator it =
268      nodemap_default.begin(); it != nodemap_default.end(); ++it)
269  {
270    nodemap_default.erase(it);
271  }
272  for (std::map<std::string, double>::iterator it =
273      edgemap_default.begin(); it != edgemap_default.end(); ++it)
274  {
275    edgemap_default.erase(it);
276  }
277  graph.clear();
278  file_name = "";
279  modified = false;
280}
Note: See TracBrowser for help on using the repository browser.