COIN-OR::LEMON - Graph Library

source: glemon-0.x/mapstorage.cc @ 194:6b2b718420eb

Last change on this file since 194:6b2b718420eb was 194:6b2b718420eb, checked in by Hegyi Péter, 13 years ago

Header reorganising

File size: 15.7 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 <limits>
20#include <cmath>
21#include <gtkmm.h>
22
23#include <mapstorage.h>
24#include <graph_displayer_canvas.h> //kivenni
25#include <nbtab.h> //kivenni
26#include <gui_writer.h>
27#include <gui_reader.h>
28#include <lemon/graph_to_eps.h>
29
30const int i_d=20;
31const double a_d=0.05;
32const double p_d=40000;
33
34MapStorage::MapStorage(NoteBookTab& tab) : mytab(tab), modified(false), file_name(""), arrow_pos_read_ok(false), iterations(i_d), attraction(a_d), propulsation(p_d), background_set(false)
35{
36  nodemap_storage["coordinates_x"] = new Graph::NodeMap<double>(graph);
37  coords.setXMap(*nodemap_storage["coordinates_x"]);
38  nodemap_storage["coordinates_y"] = new Graph::NodeMap<double>(graph);
39  coords.setYMap(*nodemap_storage["coordinates_y"]);
40
41  edgemap_storage["arrow_pos_x"] = new Graph::EdgeMap<double>(graph);
42  arrow_pos.setXMap(*edgemap_storage["arrow_pos_x"]);
43  edgemap_storage["arrow_pos_y"] = new Graph::EdgeMap<double>(graph);
44  arrow_pos.setYMap(*edgemap_storage["arrow_pos_y"]);
45
46  nodemap_storage["label"] = new Graph::NodeMap<double>(graph);
47  edgemap_storage["label"] = new Graph::EdgeMap<double>(graph);
48
49  nodemap_default["label"] = 1.0;
50  edgemap_default["label"] = 1.0;
51
52  active_nodemaps.resize(NODE_PROPERTY_NUM);
53  for(int i=0;i<NODE_PROPERTY_NUM;i++)
54    {
55      active_nodemaps[i]="";
56    }
57
58  active_edgemaps.resize(EDGE_PROPERTY_NUM);
59  for(int i=0;i<EDGE_PROPERTY_NUM;i++)
60    {
61      active_edgemaps[i]="";
62    }
63}
64
65MapStorage::~MapStorage()
66{
67  for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
68      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
69  {
70    delete it->second;
71  }
72  for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
73      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
74  {
75    delete it->second;
76  }
77}
78
79int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap<double> *nodemap, double default_value)
80{
81  if( nodemap_storage.find(name) == nodemap_storage.end() )
82    {
83      nodemap_storage[name]=nodemap;
84      // set the maps default value
85      nodemap_default[name] = default_value;
86
87      //announce changement in maps
88      signal_node_map.emit(name);
89      return 0;
90    }
91  return 1;
92}
93
94void MapStorage::changeActiveMap(bool itisedge, int prop, std::string mapname)
95{
96  if(itisedge)
97    {
98      active_edgemaps[prop]=mapname;
99    }
100  else
101    {
102      active_nodemaps[prop]=mapname;
103    }
104  signal_prop.emit(itisedge, prop);
105}
106
107void MapStorage::broadcastActiveMaps()
108{
109  for(int i=0;i<NODE_PROPERTY_NUM;i++)
110    {
111      signal_map_win.emit(false, i, active_nodemaps[i]);
112    }
113 
114  for(int i=0;i<EDGE_PROPERTY_NUM;i++)
115    {
116      signal_map_win.emit(true, i, active_edgemaps[i]);
117    }
118}
119
120
121std::string MapStorage::getActiveEdgeMap(int prop)
122{
123  return active_edgemaps[prop];
124}
125
126std::string MapStorage::getActiveNodeMap(int prop)
127{
128  return active_nodemaps[prop];
129}
130
131std::vector<std::string> MapStorage::getEdgeMapList()
132{
133  std::vector<std::string> eml;
134  eml.resize(edgemap_storage.size());
135  int i=0;
136  std::map< std::string,Graph::EdgeMap<double> * >::iterator emsi=beginOfEdgeMaps();
137  for(;emsi!=endOfEdgeMaps();emsi++)
138    {
139      eml[i]=(emsi->first);
140      i++;
141    }
142  return eml;
143}
144
145std::vector<std::string> MapStorage::getNodeMapList()
146{
147  std::vector<std::string> nml;
148  nml.resize(nodemap_storage.size());
149  int i=0;
150  std::map< std::string,Graph::NodeMap<double> * >::iterator nmsi=beginOfNodeMaps();
151  for(;nmsi!=endOfNodeMaps();nmsi++)
152    {
153      nml[i]=(nmsi->first);
154      i++;
155    }
156  return nml;
157}
158
159sigc::signal<void, bool, int> MapStorage::signal_prop_ch()
160{
161  return signal_prop;
162}
163
164int MapStorage::addEdgeMap(const std::string & name, Graph::EdgeMap<double> *edgemap, double default_value)
165{
166  if( edgemap_storage.find(name) == edgemap_storage.end() )
167    {
168      edgemap_storage[name]=edgemap;
169      // set the maps default value
170      edgemap_default[name] = default_value;
171
172      //announce changement in maps
173      signal_edge_map.emit(name);
174      return 0;
175    }
176  return 1;
177}
178
179double MapStorage::maxOfNodeMap(const std::string & name)
180{
181  double max=0;
182  for (NodeIt j(graph); j!=INVALID; ++j)
183  {
184    if( (*nodemap_storage[name])[j]>max )
185    {
186      max=(*nodemap_storage[name])[j];
187    }
188  }
189  return max;
190}
191
192double MapStorage::maxOfEdgeMap(const std::string & name)
193{
194  double max=0;
195  for (EdgeIt j(graph); j!=INVALID; ++j)
196  {
197    if( (*edgemap_storage[name])[j]>max )
198    {
199      max=(*edgemap_storage[name])[j];
200    }
201  }
202  return max;
203}
204
205double MapStorage::minOfNodeMap(const std::string & name)
206{
207  NodeIt j(graph);
208  double min;
209  if(j!=INVALID)
210    {
211      min=(*nodemap_storage[name])[j];
212    }
213  else
214    {
215      min=0;
216    }
217  for (; j!=INVALID; ++j)
218  {
219    if( (*nodemap_storage[name])[j]<min )
220    {
221      min=(*nodemap_storage[name])[j];
222    }
223  }
224  return min;
225}
226
227double MapStorage::minOfEdgeMap(const std::string & name)
228{
229  EdgeIt j(graph);
230  double min;
231  if(j!=INVALID)
232    {
233      min=(*edgemap_storage[name])[j];
234    }
235  else
236    {
237      min=0;
238    }
239  for (EdgeIt j(graph); j!=INVALID; ++j)
240  {
241    if( (*edgemap_storage[name])[j]<min )
242    {
243      min=(*edgemap_storage[name])[j];
244    }
245  }
246  return min;
247}
248
249int MapStorage::readFromFile(const std::string &filename)
250{
251  bool read_x = false;
252  bool read_y = false;
253  bool read_edge_id = false;
254
255  try {
256    LemonReader lreader(filename);
257    ContentReader content(lreader);
258    lreader.run();
259
260    if (content.nodeSetNum() < 1)
261    {
262      Gtk::MessageDialog mdialog("No nodeset found in file.");
263      mdialog.run();
264      clear();
265      return 1;
266    }
267
268    if (content.edgeSetNum() < 1)
269    {
270      Gtk::MessageDialog mdialog("No edgeset found in file.");
271      mdialog.run();
272      clear();
273      return 1;
274    }
275
276    const std::vector<std::string>& nodeMapNames = content.nodeSetMaps(0);
277    const std::vector<std::string>& edgeMapNames = content.edgeSetMaps(0);
278
279    GraphReader<Graph> greader(filename, graph);
280    for (std::vector<std::string>::const_iterator it = nodeMapNames.begin();
281        it != nodeMapNames.end(); ++it)
282    {
283      if (*it == "coordinates_x")
284      {
285        read_x = true;
286        //std::cout << "read X nodemap" << std::endl;
287      }
288      else if (*it == "coordinates_y")
289      {
290        read_y = true;
291        //std::cout << "read Y nodemap" << std::endl;
292      }
293      else if (*it == "label")
294      {
295        //std::cout << "read id nodemap" << std::endl;
296      }
297      else
298      {
299        nodemap_storage[*it] = new Graph::NodeMap<double>(graph);
300        //std::cout << "read " << *it << " nodemap" << std::endl;
301      }
302      greader.readNodeMap(*it, *nodemap_storage[*it]);
303    }
304    for (std::vector<std::string>::const_iterator it = edgeMapNames.begin();
305        it != edgeMapNames.end(); ++it)
306    {
307      if (*it == "label")
308      {
309        //std::cout << "read id edgemap" << std::endl;
310        read_edge_id = true;
311      }
312      else
313      {
314        edgemap_storage[*it] = new Graph::EdgeMap<double>(graph);
315        //std::cout << "read " << *it << " edgemap" << std::endl;
316      }
317      greader.readEdgeMap(*it, *edgemap_storage[*it]);
318    }
319    GuiReader gui_reader(greader, this);
320    greader.run();
321  } catch (Exception& error) {
322    Gtk::MessageDialog mdialog(error.what());
323    mdialog.run();
324    clear();
325    return 1;
326  }
327
328  if (!read_edge_id)
329  {
330    edgemap_storage["label"] = new Graph::EdgeMap<double>(graph);
331    int i = 1;
332    for (EdgeIt e(graph); e != INVALID; ++e)
333    {
334      (*edgemap_storage["label"])[e] = i++;
335    }
336  }
337
338  if (!read_x || !read_y)
339  {
340    int node_num = 0;
341    for (NodeIt n(graph); n != INVALID; ++n)
342    {
343      node_num++;
344    }
345    const double pi = 3.142;
346    double step = 2 * pi / (double) node_num;
347    int i = 0;
348    for (NodeIt n(graph); n != INVALID; ++n)
349    {
350      nodemap_storage["coordinates_x"]->set(n, 250.0 * std::cos(i * step));
351      nodemap_storage["coordinates_y"]->set(n, 250.0 * std::sin(i * step));
352      i++;
353    }
354  }
355
356  if (!arrow_pos_read_ok)
357  {
358    arrow_pos_read_ok = false;
359    for (EdgeIt e(graph); e != INVALID; ++e)
360    {
361      if (graph.source(e) == graph.target(e))
362      {
363        arrow_pos.set(e, coords[graph.source(e)] + XY(0.0, 80.0));
364      }
365      else
366      {
367        arrow_pos.set(e, (coords[graph.source(e)] + coords[graph.target(e)]) / 2.0);
368      }
369    }
370  }
371
372  // fill in the default values for the maps
373  for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
374      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
375  {
376    if ((it->first != "label") &&
377        (it->first != "coordiantes_x") &&
378        (it->first != "coordinates_y"))
379    {
380      nodemap_default[it->first] = 0.0;
381    }
382    else if (it->first == "label")
383    {
384      NodeIt n(graph);
385      double max = (*nodemap_storage["label"])[n];
386      for (; n != INVALID; ++n)
387      {
388        if ((*nodemap_storage["label"])[n] > max)
389          max = (*nodemap_storage["label"])[n];
390      }
391      nodemap_default["label"] = max + 1.0;
392    }
393  }
394  for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
395      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
396  {
397    if (it->first != "label")
398    {
399      edgemap_default[it->first] = 0.0;
400    }
401    else
402    {
403      double max = std::numeric_limits<double>::min();
404      for (EdgeIt e(graph); e != INVALID; ++e)
405      {
406        if ((*edgemap_storage["label"])[e] > max)
407          max = (*edgemap_storage["label"])[e];
408      }
409      if (max > std::numeric_limits<double>::min())
410        edgemap_default["label"] = max + 1.0;
411      else
412        edgemap_default["label"] = 1.0;
413    }
414  }
415
416  return 0;
417}
418
419void MapStorage::writeToFile(const std::string &filename)
420{
421  GraphWriter<Graph> gwriter(filename, graph);
422
423  for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
424      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
425  {
426    gwriter.writeNodeMap(it->first, *(it->second));
427  }
428  for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
429      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
430  {
431    if ((it->first != "arrow_pos_x") &&
432        (it->first != "arrow_pos_y"))
433    {
434      gwriter.writeEdgeMap(it->first, *(it->second));
435    }
436  }
437
438  GuiWriter gui_writer(gwriter, this);
439
440  gwriter.run();
441}
442
443void MapStorage::clear()
444{
445  for (std::map<std::string, Graph::NodeMap<double>*>::iterator it =
446      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
447  {
448    if ((it->first != "coordinates_x") &&
449        (it->first != "coordinates_y") &&
450        (it->first != "label"))
451    {
452      delete it->second;
453      nodemap_storage.erase(it);
454    }
455  }
456  for (std::map<std::string, Graph::EdgeMap<double>*>::iterator it =
457      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
458  {
459    if ((it->first != "label") &&
460        (it->first != "arrow_pos_x") &&
461        (it->first != "arrow_pos_y"))
462    {
463      delete it->second;
464      edgemap_storage.erase(it);
465    }
466  }
467  for (std::map<std::string, double>::iterator it =
468      nodemap_default.begin(); it != nodemap_default.end(); ++it)
469  {
470    if (it->first != "label")
471      nodemap_default.erase(it);
472  }
473  for (std::map<std::string, double>::iterator it =
474      edgemap_default.begin(); it != edgemap_default.end(); ++it)
475  {
476    if (it->first != "label")
477      edgemap_default.erase(it);
478  }
479  graph.clear();
480  file_name = "";
481  modified = false;
482
483  arrow_pos_read_ok = false;
484 
485  for(int i=0;i<NODE_PROPERTY_NUM;i++)
486    {
487      changeActiveMap(false, i, "");
488      signal_map_win.emit(false, i, "");
489    }
490 
491  for(int i=0;i<EDGE_PROPERTY_NUM;i++)
492    {
493      changeActiveMap(true, i, "");
494      signal_map_win.emit(true, i, "");
495    }
496
497  attraction=a_d;
498  propulsation=p_d;
499  iterations=i_d;
500
501  signal_design_win.emit(attraction, propulsation, iterations);
502}
503
504void MapStorage::ArrowPosReadOK()
505{
506  arrow_pos_read_ok = true;
507}
508
509void MapStorage::mapChanged(bool itisedge, std::string mapname)
510{
511  if(itisedge)
512    {
513      for(int i=0;i<EDGE_PROPERTY_NUM;i++)
514        {
515          if(active_edgemaps[i]==mapname)
516            {
517              signal_prop.emit(itisedge, i);
518            }
519        }
520    }
521  else
522    {
523      for(int i=0;i<NODE_PROPERTY_NUM;i++)
524        {
525          if(active_nodemaps[i]==mapname)
526            {
527              signal_prop.emit(itisedge, i);
528            }
529        }
530    }
531}
532
533void MapStorage::get_design_data(double & attraction_p, double & propulsation_p, int & iterations_p)
534{
535  attraction_p=attraction;
536  propulsation_p=propulsation;
537  iterations_p=iterations;
538}
539
540void MapStorage::set_attraction(double attraction_p)
541{
542  attraction=attraction_p;
543}
544
545void MapStorage::set_propulsation(double propulsation_p)
546{
547  propulsation=propulsation_p;
548}
549
550void MapStorage::set_iteration(int iterations_p)
551{
552  iterations=iterations_p;
553}
554
555void MapStorage::redesign_data_changed()
556{
557  signal_design_win.emit(attraction, propulsation, iterations);
558}
559
560void MapStorage::setBackground(const std::string& file_name)
561{
562  if (file_name == background_file_name) return;
563  if (file_name == "")
564  {
565    background_file_name = "";
566    background_set = false;
567  }
568  else
569  {
570    background_file_name = file_name;
571    background_set = true;
572  }
573  mytab.gd_canvas->setBackground();
574}
575
576const std::string& MapStorage::getBackgroundFilename()
577{
578  return background_file_name;
579}
580
581bool MapStorage::isBackgroundSet()
582{
583  return background_set;
584}
585
586double MapStorage::getBackgroundScaling()
587{
588  return background_scaling;
589}
590
591void MapStorage::setBackgroundScaling(double scaling)
592{
593  background_scaling = scaling;
594}
595
596void MapStorage::exportGraphToEPS(std::vector<bool> options, std::string filename)
597{
598  Graph::NodeMap<int> _nodeColors(graph, 0);
599  Graph::EdgeMap<int> _edgeColors(graph, 0);
600  Graph::NodeMap<double> _nodeSizes(graph, 6.0);
601  Graph::EdgeMap<double> _edgeWidths(graph, 1.0);
602  bool _drawArrows=options[ARROWS];
603  bool _enableParallel=options[PAR];
604
605  std::string emptyString="";
606  Graph::NodeMap<std::string> _nodeTextMap(graph,emptyString);
607
608  //_nodeTextMap=(Graph::NodeMap<void> *)&emptyStringMap;
609
610  if(options[N_MAPS])
611    {
612      if(active_nodemaps[N_RADIUS]!="")
613        {
614          _nodeSizes=*(nodemap_storage[active_nodemaps[N_RADIUS]]);
615        }
616      if(active_nodemaps[N_COLOR]!="")
617        {
618          for(NodeIt ni(graph);ni!=INVALID;++ni)
619            {
620              _nodeColors[ni]=(int)((*(nodemap_storage[active_nodemaps[N_COLOR]]))[ni]);
621            }
622        }
623      if(active_nodemaps[N_TEXT]!="")
624        {
625          for(NodeIt ni(graph);ni!=INVALID;++ni)
626            {
627              std::ostringstream o;
628              o << ((*(nodemap_storage[active_nodemaps[N_TEXT]]))[ni]);
629              _nodeTextMap[ni]=o.str();       
630            }
631        }
632    }
633  if(options[E_MAPS])
634    {
635      if(active_edgemaps[E_WIDTH]!="")
636        {
637          _edgeWidths=*(edgemap_storage[active_edgemaps[E_WIDTH]]);
638        }
639      if(active_edgemaps[E_COLOR]!="")
640        {
641          for(EdgeIt ei(graph);ei!=INVALID;++ei)
642            {
643              _edgeColors[ei]=(int)((*(edgemap_storage[active_edgemaps[E_COLOR]]))[ei]);
644            }
645        }
646    }
647
648  Palette palette;
649  Palette paletteW(true);
650
651  graphToEps(graph,filename).
652    title("Sample .eps figure (fits to A4)").
653    copyright("(C) 2006 LEMON Project").
654    absoluteNodeSizes().absoluteEdgeWidths().
655    nodeScale(2).nodeSizes(_nodeSizes).
656    coords(coords).
657    nodeColors(composeMap(paletteW,_nodeColors)).
658    edgeColors(composeMap(palette,_edgeColors)).
659    edgeWidthScale(0.3).edgeWidths(_edgeWidths).
660    nodeTexts(_nodeTextMap).nodeTextSize(7).
661    enableParallel(_enableParallel).parEdgeDist(4).
662    drawArrows(_drawArrows).arrowWidth(7).arrowLength(7).
663    run();
664
665}
Note: See TracBrowser for help on using the repository browser.