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