gui/mapstorage.cc
author alpar
Wed, 07 Dec 2005 15:40:27 +0000
changeset 1854 a68d4f93b1fb
parent 1708 8fea0e60f855
child 1860 27a9a75b957b
permissions -rw-r--r--
Bugfix
     1 #include "mapstorage.h"
     2 #include <gtkmm.h>
     3 #include <cmath>
     4 
     5 #include <cmath>
     6 
     7 MapStorage::MapStorage() : modified(false), file_name("")
     8 {
     9   nodemap_storage["coordinates_x"] = new Graph::NodeMap<double>(graph);
    10   coords.setXMap(*nodemap_storage["coordinates_x"]);
    11   nodemap_storage["coordinates_y"] = new Graph::NodeMap<double>(graph);
    12   coords.setYMap(*nodemap_storage["coordinates_y"]);
    13 
    14   nodemap_storage["id"] = new Graph::NodeMap<double>(graph);
    15   edgemap_storage["id"] = new Graph::EdgeMap<double>(graph);
    16 
    17   nodemap_default["id"] = 1.0;
    18   edgemap_default["id"] = 1.0;
    19 
    20   active_nodemaps.resize(NODE_PROPERTY_NUM);
    21   for(int i=0;i<NODE_PROPERTY_NUM;i++)
    22     {
    23       active_nodemaps[i]="";
    24     }
    25 
    26   active_edgemaps.resize(EDGE_PROPERTY_NUM);
    27   for(int i=0;i<EDGE_PROPERTY_NUM;i++)
    28     {
    29       active_edgemaps[i]="";
    30     }
    31 }
    32 
    33 MapStorage::~MapStorage()
    34 {
    35   for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
    36       nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
    37   {
    38     delete it->second;
    39   }
    40   for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
    41       edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
    42   {
    43     delete it->second;
    44   }
    45 }
    46 
    47 int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap<double> *nodemap, double default_value = 0.0)
    48 {
    49   if( nodemap_storage.find(name) == nodemap_storage.end() )
    50     {
    51       nodemap_storage[name]=nodemap;
    52       // set the maps default value
    53       nodemap_default[name] = default_value;
    54       return 0;
    55     }
    56   return 1;
    57 }
    58 
    59 void MapStorage::changeActiveMap(bool itisedge, int prop, std::string mapname)
    60 {
    61   if(itisedge)
    62     {
    63       active_edgemaps[prop]=mapname;
    64     }
    65   else
    66     {
    67       active_nodemaps[prop]=mapname;
    68     }
    69   signal_prop.emit(itisedge, prop);
    70 }
    71 
    72 std::string MapStorage::getActiveEdgeMap(int prop)
    73 {
    74   return active_edgemaps[prop];
    75 }
    76 
    77 std::string MapStorage::getActiveNodeMap(int prop)
    78 {
    79   return active_nodemaps[prop];
    80 }
    81 
    82 std::vector<std::string> MapStorage::getEdgeMapList()
    83 {
    84   std::vector<std::string> eml;
    85   eml.resize(edgemap_storage.size());
    86   int i=0;
    87   std::map< std::string,Graph::EdgeMap<double> * >::iterator emsi=beginOfEdgeMaps();
    88   for(;emsi!=endOfEdgeMaps();emsi++)
    89     {
    90       eml[i]=(emsi->first);
    91       i++;
    92     }
    93   return eml;
    94 }
    95 
    96 std::vector<std::string> MapStorage::getNodeMapList()
    97 {
    98   std::vector<std::string> nml;
    99   nml.resize(nodemap_storage.size());
   100   int i=0;
   101   std::map< std::string,Graph::NodeMap<double> * >::iterator nmsi=beginOfNodeMaps();
   102   for(;nmsi!=endOfNodeMaps();nmsi++)
   103     {
   104       nml[i]=(nmsi->first);
   105       i++;
   106     }
   107   return nml;
   108 }
   109 
   110 MapStorage::Signal_Prop MapStorage::signal_prop_ch()
   111 {
   112   return signal_prop;
   113 }
   114 
   115 int MapStorage::addEdgeMap(const std::string & name, Graph::EdgeMap<double> *edgemap, double default_value = 0.0)
   116 {
   117   if( edgemap_storage.find(name) == edgemap_storage.end() )
   118     {
   119       edgemap_storage[name]=edgemap;
   120       // set the maps default value
   121       edgemap_default[name] = default_value;
   122       return 0;
   123     }
   124   return 1;
   125 }
   126 
   127 double MapStorage::maxOfNodeMap(const std::string & name)
   128 {
   129   double max=0;
   130   for (NodeIt j(graph); j!=INVALID; ++j)
   131   {
   132     if( (*nodemap_storage[name])[j]>max )
   133     {
   134       max=(*nodemap_storage[name])[j];
   135     }
   136   }
   137   return max;
   138 }
   139 
   140 double MapStorage::maxOfEdgeMap(const std::string & name)
   141 {
   142   double max=0;
   143   for (EdgeIt j(graph); j!=INVALID; ++j)
   144   {
   145     if( (*edgemap_storage[name])[j]>max )
   146     {
   147       max=(*edgemap_storage[name])[j];
   148     }
   149   }
   150   return max;
   151 }
   152 
   153 double MapStorage::minOfNodeMap(const std::string & name)
   154 {
   155   NodeIt j(graph);
   156   double min;
   157   if(j!=INVALID)
   158     {
   159       min=(*nodemap_storage[name])[j];
   160     }
   161   else
   162     {
   163       min=0;
   164     }
   165   for (; j!=INVALID; ++j)
   166   {
   167     if( (*nodemap_storage[name])[j]<min )
   168     {
   169       min=(*nodemap_storage[name])[j];
   170     }
   171   }
   172   return min;
   173 }
   174 
   175 double MapStorage::minOfEdgeMap(const std::string & name)
   176 {
   177   EdgeIt j(graph);
   178   double min;
   179   if(j!=INVALID)
   180     {
   181       min=(*edgemap_storage[name])[j];
   182     }
   183   else
   184     {
   185       min=0;
   186     }
   187   for (EdgeIt j(graph); j!=INVALID; ++j)
   188   {
   189     if( (*edgemap_storage[name])[j]<min )
   190     {
   191       min=(*edgemap_storage[name])[j];
   192     }
   193   }
   194   return min;
   195 }
   196 
   197 int MapStorage::readFromFile(const std::string &filename)
   198 {
   199   bool read_x = false;
   200   bool read_y = false;
   201   bool read_edge_id = false;
   202 
   203   try {
   204     LemonReader lreader(filename);
   205     ContentReader content(lreader);
   206     lreader.run();
   207 
   208     const std::vector<std::string>& nodeMapNames = content.nodeSetMaps(0);
   209     const std::vector<std::string>& edgeMapNames = content.edgeSetMaps(0);
   210 
   211     GraphReader<Graph> greader(filename, graph);
   212     for (std::vector<std::string>::const_iterator it = nodeMapNames.begin();
   213         it != nodeMapNames.end(); ++it)
   214     {
   215       if (*it == "coordinates_x")
   216       {
   217         read_x = true;
   218         //std::cout << "read X nodemap" << std::endl;
   219       }
   220       else if (*it == "coordinates_y")
   221       {
   222         read_y = true;
   223         //std::cout << "read Y nodemap" << std::endl;
   224       }
   225       else if (*it == "id")
   226       {
   227         //std::cout << "read id nodemap" << std::endl;
   228       }
   229       else
   230       {
   231         nodemap_storage[*it] = new Graph::NodeMap<double>(graph);
   232         //std::cout << "read " << *it << " nodemap" << std::endl;
   233       }
   234       greader.readNodeMap(*it, *nodemap_storage[*it]);
   235     }
   236     for (std::vector<std::string>::const_iterator it = edgeMapNames.begin();
   237         it != edgeMapNames.end(); ++it)
   238     {
   239       if (*it == "id")
   240       {
   241         //std::cout << "read id edgemap" << std::endl;
   242       }
   243       else
   244       {
   245         edgemap_storage[*it] = new Graph::EdgeMap<double>(graph);
   246         //std::cout << "read " << *it << " edgemap" << std::endl;
   247       }
   248       greader.readEdgeMap(*it, *edgemap_storage[*it]);
   249     }
   250     greader.run();
   251   } catch (DataFormatError& error) {
   252     Gtk::MessageDialog mdialog(error.what());
   253     mdialog.run();
   254     clear();
   255     return 1;
   256   }
   257 
   258   if (!read_edge_id)
   259   {
   260     edgemap_storage["id"] = new Graph::EdgeMap<double>(graph);
   261     int i = 1;
   262     for (EdgeIt e(graph); e != INVALID; ++e)
   263     {
   264       (*edgemap_storage["id"])[e] = i++;
   265     }
   266   }
   267 
   268   if (!read_x || !read_y)
   269   {
   270     int node_num = 0;
   271     for (NodeIt n(graph); n != INVALID; ++n)
   272     {
   273       node_num++;
   274     }
   275     const double pi = 3.142;
   276     double step = 2 * pi / (double) node_num;
   277     int i = 0;
   278     for (NodeIt n(graph); n != INVALID; ++n)
   279     {
   280       nodemap_storage["coordinates_x"]->set(n, 250.0 * std::cos(i * step));
   281       nodemap_storage["coordinates_y"]->set(n, 250.0 * std::sin(i * step));
   282       i++;
   283     }
   284   }
   285 
   286   // fill in the default values for the maps
   287   for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
   288       nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
   289   {
   290     if ((it->first != "id") &&
   291         (it->first != "coordiantes_x") &&
   292         (it->first != "coordinates_y"))
   293     {
   294       nodemap_default[it->first] = 0.0;
   295     }
   296     else if (it->first == "id")
   297     {
   298       NodeIt n(graph);
   299       double max = (*nodemap_storage["id"])[n];
   300       for (; n != INVALID; ++n)
   301       {
   302         if ((*nodemap_storage["id"])[n] > max)
   303           max = (*nodemap_storage["id"])[n];
   304       }
   305       nodemap_default["id"] = max + 1.0;
   306     }
   307   }
   308   for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
   309       edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
   310   {
   311     if (it->first != "id")
   312     {
   313       edgemap_default[it->first] = 0.0;
   314     }
   315     else
   316     {
   317       double max = std::numeric_limits<double>::min();
   318       for (EdgeIt e(graph); e != INVALID; ++e)
   319       {
   320         if ((*edgemap_storage["id"])[e] > max)
   321           max = (*edgemap_storage["id"])[e];
   322       }
   323       if (max > std::numeric_limits<double>::min())
   324         edgemap_default["id"] = max + 1.0;
   325       else
   326         edgemap_default["id"] = 1.0;
   327     }
   328   }
   329 
   330   return 0;
   331 }
   332 
   333 void MapStorage::writeToFile(const std::string &filename)
   334 {
   335   GraphWriter<Graph> gwriter(filename, graph);
   336 
   337   for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
   338       nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
   339   {
   340     gwriter.writeNodeMap(it->first, *(it->second));
   341     //std::cout << "wrote " << it->first << " nodemap" << std::endl;
   342   }
   343   for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
   344       edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
   345   {
   346     gwriter.writeEdgeMap(it->first, *(it->second));
   347     //std::cout << "wrote " << it->first << " edgemap" << std::endl;
   348   }
   349   gwriter.run();
   350 }
   351 
   352 void MapStorage::clear()
   353 {
   354   for (std::map<std::string, Graph::NodeMap<double>*>::iterator it =
   355       nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
   356   {
   357     if ((it->first != "coordinates_x") &&
   358         (it->first != "coordinates_y") &&
   359         (it->first != "id"))
   360     {
   361       delete it->second;
   362       nodemap_storage.erase(it);
   363     }
   364   }
   365   for (std::map<std::string, Graph::EdgeMap<double>*>::iterator it =
   366       edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
   367   {
   368     if (it->first != "id")
   369     {
   370       delete it->second;
   371       edgemap_storage.erase(it);
   372     }
   373   }
   374   for (std::map<std::string, double>::iterator it =
   375       nodemap_default.begin(); it != nodemap_default.end(); ++it)
   376   {
   377     if (it->first != "id")
   378       nodemap_default.erase(it);
   379   }
   380   for (std::map<std::string, double>::iterator it =
   381       edgemap_default.begin(); it != edgemap_default.end(); ++it)
   382   {
   383     if (it->first != "id")
   384       edgemap_default.erase(it);
   385   }
   386   graph.clear();
   387   file_name = "";
   388   modified = false;
   389 }