COIN-OR::LEMON - Graph Library

source: glemon-0.x/mapstorage.cc @ 174:95872af46fc4

Last change on this file since 174:95872af46fc4 was 174:95872af46fc4, checked in by Alpar Juttner, 18 years ago

Add copyright headers

File size: 12.2 KB
Line 
1/* -*- C++ -*-
2 *
3 * This file is a part of LEMON, a generic C++ optimization library
4 *
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 *
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
12 *
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
15 * purpose.
16 *
17 */
18
19#include "mapstorage.h"
20#include "gui_writer.h"
21#include "gui_reader.h"
22#include <limits>
23#include <cmath>
24#include <gtkmm.h>
25
26MapStorage::MapStorage() : modified(false), file_name(""), arrow_pos_read_ok(false)
27{
28  nodemap_storage["coordinates_x"] = new Graph::NodeMap<double>(graph);
29  coords.setXMap(*nodemap_storage["coordinates_x"]);
30  nodemap_storage["coordinates_y"] = new Graph::NodeMap<double>(graph);
31  coords.setYMap(*nodemap_storage["coordinates_y"]);
32
33  edgemap_storage["arrow_pos_x"] = new Graph::EdgeMap<double>(graph);
34  arrow_pos.setXMap(*edgemap_storage["arrow_pos_x"]);
35  edgemap_storage["arrow_pos_y"] = new Graph::EdgeMap<double>(graph);
36  arrow_pos.setYMap(*edgemap_storage["arrow_pos_y"]);
37
38  nodemap_storage["label"] = new Graph::NodeMap<double>(graph);
39  edgemap_storage["label"] = new Graph::EdgeMap<double>(graph);
40
41  nodemap_default["label"] = 1.0;
42  edgemap_default["label"] = 1.0;
43
44  active_nodemaps.resize(NODE_PROPERTY_NUM);
45  for(int i=0;i<NODE_PROPERTY_NUM;i++)
46    {
47      active_nodemaps[i]="";
48    }
49
50  active_edgemaps.resize(EDGE_PROPERTY_NUM);
51  for(int i=0;i<EDGE_PROPERTY_NUM;i++)
52    {
53      active_edgemaps[i]="";
54    }
55}
56
57MapStorage::~MapStorage()
58{
59  for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
60      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
61  {
62    delete it->second;
63  }
64  for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
65      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
66  {
67    delete it->second;
68  }
69}
70
71int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap<double> *nodemap, double default_value)
72{
73  if( nodemap_storage.find(name) == nodemap_storage.end() )
74    {
75      nodemap_storage[name]=nodemap;
76      // set the maps default value
77      nodemap_default[name] = default_value;
78
79      //announce changement in maps
80      signal_node_map.emit(name);
81      return 0;
82    }
83  return 1;
84}
85
86void MapStorage::changeActiveMap(bool itisedge, int prop, std::string mapname)
87{
88  if(itisedge)
89    {
90      active_edgemaps[prop]=mapname;
91    }
92  else
93    {
94      active_nodemaps[prop]=mapname;
95    }
96  signal_prop.emit(itisedge, prop);
97}
98
99void MapStorage::broadcastActiveMaps()
100{
101  for(int i=0;i<NODE_PROPERTY_NUM;i++)
102    {
103      signal_map_win.emit(false, i, active_nodemaps[i]);
104    }
105 
106  for(int i=0;i<EDGE_PROPERTY_NUM;i++)
107    {
108      signal_map_win.emit(true, i, active_edgemaps[i]);
109    }
110}
111
112
113std::string MapStorage::getActiveEdgeMap(int prop)
114{
115  return active_edgemaps[prop];
116}
117
118std::string MapStorage::getActiveNodeMap(int prop)
119{
120  return active_nodemaps[prop];
121}
122
123std::vector<std::string> MapStorage::getEdgeMapList()
124{
125  std::vector<std::string> eml;
126  eml.resize(edgemap_storage.size());
127  int i=0;
128  std::map< std::string,Graph::EdgeMap<double> * >::iterator emsi=beginOfEdgeMaps();
129  for(;emsi!=endOfEdgeMaps();emsi++)
130    {
131      eml[i]=(emsi->first);
132      i++;
133    }
134  return eml;
135}
136
137std::vector<std::string> MapStorage::getNodeMapList()
138{
139  std::vector<std::string> nml;
140  nml.resize(nodemap_storage.size());
141  int i=0;
142  std::map< std::string,Graph::NodeMap<double> * >::iterator nmsi=beginOfNodeMaps();
143  for(;nmsi!=endOfNodeMaps();nmsi++)
144    {
145      nml[i]=(nmsi->first);
146      i++;
147    }
148  return nml;
149}
150
151MapStorage::Signal_Prop MapStorage::signal_prop_ch()
152{
153  return signal_prop;
154}
155
156int MapStorage::addEdgeMap(const std::string & name, Graph::EdgeMap<double> *edgemap, double default_value)
157{
158  if( edgemap_storage.find(name) == edgemap_storage.end() )
159    {
160      edgemap_storage[name]=edgemap;
161      // set the maps default value
162      edgemap_default[name] = default_value;
163
164      //announce changement in maps
165      signal_edge_map.emit(name);
166      return 0;
167    }
168  return 1;
169}
170
171double MapStorage::maxOfNodeMap(const std::string & name)
172{
173  double max=0;
174  for (NodeIt j(graph); j!=INVALID; ++j)
175  {
176    if( (*nodemap_storage[name])[j]>max )
177    {
178      max=(*nodemap_storage[name])[j];
179    }
180  }
181  return max;
182}
183
184double MapStorage::maxOfEdgeMap(const std::string & name)
185{
186  double max=0;
187  for (EdgeIt j(graph); j!=INVALID; ++j)
188  {
189    if( (*edgemap_storage[name])[j]>max )
190    {
191      max=(*edgemap_storage[name])[j];
192    }
193  }
194  return max;
195}
196
197double MapStorage::minOfNodeMap(const std::string & name)
198{
199  NodeIt j(graph);
200  double min;
201  if(j!=INVALID)
202    {
203      min=(*nodemap_storage[name])[j];
204    }
205  else
206    {
207      min=0;
208    }
209  for (; j!=INVALID; ++j)
210  {
211    if( (*nodemap_storage[name])[j]<min )
212    {
213      min=(*nodemap_storage[name])[j];
214    }
215  }
216  return min;
217}
218
219double MapStorage::minOfEdgeMap(const std::string & name)
220{
221  EdgeIt j(graph);
222  double min;
223  if(j!=INVALID)
224    {
225      min=(*edgemap_storage[name])[j];
226    }
227  else
228    {
229      min=0;
230    }
231  for (EdgeIt j(graph); j!=INVALID; ++j)
232  {
233    if( (*edgemap_storage[name])[j]<min )
234    {
235      min=(*edgemap_storage[name])[j];
236    }
237  }
238  return min;
239}
240
241int MapStorage::readFromFile(const std::string &filename)
242{
243  bool read_x = false;
244  bool read_y = false;
245  bool read_edge_id = false;
246
247  try {
248    LemonReader lreader(filename);
249    ContentReader content(lreader);
250    lreader.run();
251
252    if (content.nodeSetNum() < 1)
253    {
254      Gtk::MessageDialog mdialog("No nodeset found in file.");
255      mdialog.run();
256      clear();
257      return 1;
258    }
259
260    if (content.edgeSetNum() < 1)
261    {
262      Gtk::MessageDialog mdialog("No edgeset found in file.");
263      mdialog.run();
264      clear();
265      return 1;
266    }
267
268    const std::vector<std::string>& nodeMapNames = content.nodeSetMaps(0);
269    const std::vector<std::string>& edgeMapNames = content.edgeSetMaps(0);
270
271    GraphReader<Graph> greader(filename, graph);
272    for (std::vector<std::string>::const_iterator it = nodeMapNames.begin();
273        it != nodeMapNames.end(); ++it)
274    {
275      if (*it == "coordinates_x")
276      {
277        read_x = true;
278        //std::cout << "read X nodemap" << std::endl;
279      }
280      else if (*it == "coordinates_y")
281      {
282        read_y = true;
283        //std::cout << "read Y nodemap" << std::endl;
284      }
285      else if (*it == "label")
286      {
287        //std::cout << "read id nodemap" << std::endl;
288      }
289      else
290      {
291        nodemap_storage[*it] = new Graph::NodeMap<double>(graph);
292        //std::cout << "read " << *it << " nodemap" << std::endl;
293      }
294      greader.readNodeMap(*it, *nodemap_storage[*it]);
295    }
296    for (std::vector<std::string>::const_iterator it = edgeMapNames.begin();
297        it != edgeMapNames.end(); ++it)
298    {
299      if (*it == "label")
300      {
301        //std::cout << "read id edgemap" << std::endl;
302        read_edge_id = true;
303      }
304      else
305      {
306        edgemap_storage[*it] = new Graph::EdgeMap<double>(graph);
307        //std::cout << "read " << *it << " edgemap" << std::endl;
308      }
309      greader.readEdgeMap(*it, *edgemap_storage[*it]);
310    }
311    GuiReader gui_reader(greader, this);
312    greader.run();
313  } catch (Exception& error) {
314    Gtk::MessageDialog mdialog(error.what());
315    mdialog.run();
316    clear();
317    return 1;
318  }
319
320  if (!read_edge_id)
321  {
322    edgemap_storage["label"] = new Graph::EdgeMap<double>(graph);
323    int i = 1;
324    for (EdgeIt e(graph); e != INVALID; ++e)
325    {
326      (*edgemap_storage["label"])[e] = i++;
327    }
328  }
329
330  if (!read_x || !read_y)
331  {
332    int node_num = 0;
333    for (NodeIt n(graph); n != INVALID; ++n)
334    {
335      node_num++;
336    }
337    const double pi = 3.142;
338    double step = 2 * pi / (double) node_num;
339    int i = 0;
340    for (NodeIt n(graph); n != INVALID; ++n)
341    {
342      nodemap_storage["coordinates_x"]->set(n, 250.0 * std::cos(i * step));
343      nodemap_storage["coordinates_y"]->set(n, 250.0 * std::sin(i * step));
344      i++;
345    }
346  }
347
348  if (!arrow_pos_read_ok)
349  {
350    arrow_pos_read_ok = false;
351    for (EdgeIt e(graph); e != INVALID; ++e)
352    {
353      if (graph.source(e) == graph.target(e))
354      {
355        arrow_pos.set(e, coords[graph.source(e)] + XY(0.0, 80.0));
356      }
357      else
358      {
359        arrow_pos.set(e, (coords[graph.source(e)] + coords[graph.target(e)]) / 2.0);
360      }
361    }
362  }
363
364  // fill in the default values for the maps
365  for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
366      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
367  {
368    if ((it->first != "label") &&
369        (it->first != "coordiantes_x") &&
370        (it->first != "coordinates_y"))
371    {
372      nodemap_default[it->first] = 0.0;
373    }
374    else if (it->first == "label")
375    {
376      NodeIt n(graph);
377      double max = (*nodemap_storage["label"])[n];
378      for (; n != INVALID; ++n)
379      {
380        if ((*nodemap_storage["label"])[n] > max)
381          max = (*nodemap_storage["label"])[n];
382      }
383      nodemap_default["label"] = max + 1.0;
384    }
385  }
386  for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
387      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
388  {
389    if (it->first != "label")
390    {
391      edgemap_default[it->first] = 0.0;
392    }
393    else
394    {
395      double max = std::numeric_limits<double>::min();
396      for (EdgeIt e(graph); e != INVALID; ++e)
397      {
398        if ((*edgemap_storage["label"])[e] > max)
399          max = (*edgemap_storage["label"])[e];
400      }
401      if (max > std::numeric_limits<double>::min())
402        edgemap_default["label"] = max + 1.0;
403      else
404        edgemap_default["label"] = 1.0;
405    }
406  }
407
408  return 0;
409}
410
411void MapStorage::writeToFile(const std::string &filename)
412{
413  GraphWriter<Graph> gwriter(filename, graph);
414
415  for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
416      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
417  {
418    gwriter.writeNodeMap(it->first, *(it->second));
419  }
420  for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
421      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
422  {
423    if ((it->first != "arrow_pos_x") &&
424        (it->first != "arrow_pos_y"))
425    {
426      gwriter.writeEdgeMap(it->first, *(it->second));
427    }
428  }
429
430  GuiWriter gui_writer(gwriter, this);
431
432  gwriter.run();
433}
434
435void MapStorage::clear()
436{
437  for (std::map<std::string, Graph::NodeMap<double>*>::iterator it =
438      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
439  {
440    if ((it->first != "coordinates_x") &&
441        (it->first != "coordinates_y") &&
442        (it->first != "label"))
443    {
444      delete it->second;
445      nodemap_storage.erase(it);
446    }
447  }
448  for (std::map<std::string, Graph::EdgeMap<double>*>::iterator it =
449      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
450  {
451    if ((it->first != "label") &&
452        (it->first != "arrow_pos_x") &&
453        (it->first != "arrow_pos_y"))
454    {
455      delete it->second;
456      edgemap_storage.erase(it);
457    }
458  }
459  for (std::map<std::string, double>::iterator it =
460      nodemap_default.begin(); it != nodemap_default.end(); ++it)
461  {
462    if (it->first != "label")
463      nodemap_default.erase(it);
464  }
465  for (std::map<std::string, double>::iterator it =
466      edgemap_default.begin(); it != edgemap_default.end(); ++it)
467  {
468    if (it->first != "label")
469      edgemap_default.erase(it);
470  }
471  graph.clear();
472  file_name = "";
473  modified = false;
474
475  arrow_pos_read_ok = false;
476 
477  for(int i=0;i<NODE_PROPERTY_NUM;i++)
478    {
479      changeActiveMap(false, i, "");
480      signal_map_win.emit(false, i, "");
481    }
482 
483  for(int i=0;i<EDGE_PROPERTY_NUM;i++)
484    {
485      changeActiveMap(true, i, "");
486      signal_map_win.emit(true, i, "");
487    }
488}
489
490void MapStorage::ArrowPosReadOK()
491{
492  arrow_pos_read_ok = true;
493}
494
495void MapStorage::mapChanged(bool itisedge, std::string mapname)
496{
497  if(itisedge)
498    {
499      for(int i=0;i<EDGE_PROPERTY_NUM;i++)
500        {
501          if(active_edgemaps[i]==mapname)
502            {
503              signal_prop.emit(itisedge, i);
504            }
505        }
506    }
507  else
508    {
509      for(int i=0;i<NODE_PROPERTY_NUM;i++)
510        {
511          if(active_nodemaps[i]==mapname)
512            {
513              signal_prop.emit(itisedge, i);
514            }
515        }
516    }
517}
Note: See TracBrowser for help on using the repository browser.