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