gui/mapstorage.cc
author ladanyi
Sat, 17 Dec 2005 21:51:10 +0000
changeset 1861 5f204c7a1318
parent 1837 8dd6160ff699
child 1867 15cf1fd6a505
permissions -rw-r--r--
Distribute xml.h too.
     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 
     9 MapStorage::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 
    40 MapStorage::~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 
    54 int 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 
    66 void 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 
    79 std::string MapStorage::getActiveEdgeMap(int prop)
    80 {
    81   return active_edgemaps[prop];
    82 }
    83 
    84 std::string MapStorage::getActiveNodeMap(int prop)
    85 {
    86   return active_nodemaps[prop];
    87 }
    88 
    89 std::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 
   103 std::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 
   117 MapStorage::Signal_Prop MapStorage::signal_prop_ch()
   118 {
   119   return signal_prop;
   120 }
   121 
   122 int 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 
   134 double 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 
   147 double 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 
   160 double 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 
   182 double 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 
   204 int 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     const std::vector<std::string>& nodeMapNames = content.nodeSetMaps(0);
   216     const std::vector<std::string>& edgeMapNames = content.edgeSetMaps(0);
   217 
   218     GraphReader<Graph> greader(filename, graph);
   219     for (std::vector<std::string>::const_iterator it = nodeMapNames.begin();
   220         it != nodeMapNames.end(); ++it)
   221     {
   222       if (*it == "coordinates_x")
   223       {
   224         read_x = true;
   225         //std::cout << "read X nodemap" << std::endl;
   226       }
   227       else if (*it == "coordinates_y")
   228       {
   229         read_y = true;
   230         //std::cout << "read Y nodemap" << std::endl;
   231       }
   232       else if (*it == "id")
   233       {
   234         //std::cout << "read id nodemap" << std::endl;
   235       }
   236       else
   237       {
   238         nodemap_storage[*it] = new Graph::NodeMap<double>(graph);
   239         //std::cout << "read " << *it << " nodemap" << std::endl;
   240       }
   241       greader.readNodeMap(*it, *nodemap_storage[*it]);
   242     }
   243     for (std::vector<std::string>::const_iterator it = edgeMapNames.begin();
   244         it != edgeMapNames.end(); ++it)
   245     {
   246       if (*it == "id")
   247       {
   248         //std::cout << "read id edgemap" << std::endl;
   249       }
   250       else
   251       {
   252         edgemap_storage[*it] = new Graph::EdgeMap<double>(graph);
   253         //std::cout << "read " << *it << " edgemap" << std::endl;
   254       }
   255       greader.readEdgeMap(*it, *edgemap_storage[*it]);
   256     }
   257     GuiReader gui_reader(greader, this);
   258     greader.run();
   259   } catch (DataFormatError& error) {
   260     Gtk::MessageDialog mdialog(error.what());
   261     mdialog.run();
   262     clear();
   263     return 1;
   264   }
   265 
   266   if (!read_edge_id)
   267   {
   268     edgemap_storage["id"] = new Graph::EdgeMap<double>(graph);
   269     int i = 1;
   270     for (EdgeIt e(graph); e != INVALID; ++e)
   271     {
   272       (*edgemap_storage["id"])[e] = i++;
   273     }
   274   }
   275 
   276   if (!read_x || !read_y)
   277   {
   278     int node_num = 0;
   279     for (NodeIt n(graph); n != INVALID; ++n)
   280     {
   281       node_num++;
   282     }
   283     const double pi = 3.142;
   284     double step = 2 * pi / (double) node_num;
   285     int i = 0;
   286     for (NodeIt n(graph); n != INVALID; ++n)
   287     {
   288       nodemap_storage["coordinates_x"]->set(n, 250.0 * std::cos(i * step));
   289       nodemap_storage["coordinates_y"]->set(n, 250.0 * std::sin(i * step));
   290       i++;
   291     }
   292   }
   293 
   294   if (!arrow_pos_read_ok)
   295   {
   296     arrow_pos_read_ok = false;
   297     for (EdgeIt e(graph); e != INVALID; ++e)
   298     {
   299       arrow_pos.set(e, (coords[graph.source(e)] + coords[graph.target(e)]) / 2.0);
   300     }
   301   }
   302 
   303   // fill in the default values for the maps
   304   for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
   305       nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
   306   {
   307     if ((it->first != "id") &&
   308         (it->first != "coordiantes_x") &&
   309         (it->first != "coordinates_y"))
   310     {
   311       nodemap_default[it->first] = 0.0;
   312     }
   313     else if (it->first == "id")
   314     {
   315       NodeIt n(graph);
   316       double max = (*nodemap_storage["id"])[n];
   317       for (; n != INVALID; ++n)
   318       {
   319         if ((*nodemap_storage["id"])[n] > max)
   320           max = (*nodemap_storage["id"])[n];
   321       }
   322       nodemap_default["id"] = max + 1.0;
   323     }
   324   }
   325   for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
   326       edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
   327   {
   328     if (it->first != "id")
   329     {
   330       edgemap_default[it->first] = 0.0;
   331     }
   332     else
   333     {
   334       double max = std::numeric_limits<double>::min();
   335       for (EdgeIt e(graph); e != INVALID; ++e)
   336       {
   337         if ((*edgemap_storage["id"])[e] > max)
   338           max = (*edgemap_storage["id"])[e];
   339       }
   340       if (max > std::numeric_limits<double>::min())
   341         edgemap_default["id"] = max + 1.0;
   342       else
   343         edgemap_default["id"] = 1.0;
   344     }
   345   }
   346 
   347   return 0;
   348 }
   349 
   350 void MapStorage::writeToFile(const std::string &filename)
   351 {
   352   GraphWriter<Graph> gwriter(filename, graph);
   353 
   354   for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
   355       nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
   356   {
   357     gwriter.writeNodeMap(it->first, *(it->second));
   358   }
   359   for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
   360       edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
   361   {
   362     if ((it->first != "arrow_pos_x") &&
   363         (it->first != "arrow_pos_y"))
   364     {
   365       gwriter.writeEdgeMap(it->first, *(it->second));
   366     }
   367   }
   368 
   369   GuiWriter gui_writer(gwriter, this);
   370 
   371   gwriter.run();
   372 }
   373 
   374 void MapStorage::clear()
   375 {
   376   for (std::map<std::string, Graph::NodeMap<double>*>::iterator it =
   377       nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
   378   {
   379     if ((it->first != "coordinates_x") &&
   380         (it->first != "coordinates_y") &&
   381         (it->first != "id"))
   382     {
   383       delete it->second;
   384       nodemap_storage.erase(it);
   385     }
   386   }
   387   for (std::map<std::string, Graph::EdgeMap<double>*>::iterator it =
   388       edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
   389   {
   390     if ((it->first != "id") &&
   391         (it->first != "arrow_pos_x") &&
   392         (it->first != "arrow_pos_y"))
   393     {
   394       delete it->second;
   395       edgemap_storage.erase(it);
   396     }
   397   }
   398   for (std::map<std::string, double>::iterator it =
   399       nodemap_default.begin(); it != nodemap_default.end(); ++it)
   400   {
   401     if (it->first != "id")
   402       nodemap_default.erase(it);
   403   }
   404   for (std::map<std::string, double>::iterator it =
   405       edgemap_default.begin(); it != edgemap_default.end(); ++it)
   406   {
   407     if (it->first != "id")
   408       edgemap_default.erase(it);
   409   }
   410   graph.clear();
   411   file_name = "";
   412   modified = false;
   413 }
   414 
   415 void MapStorage::ArrowPosReadOK()
   416 {
   417   arrow_pos_read_ok = true;
   418 }