gui/mapstorage.cc
author deba
Wed, 26 Oct 2005 10:50:47 +0000
changeset 1741 7a98fe2ed989
parent 1686 a5b8e0fde2be
child 1837 8dd6160ff699
permissions -rw-r--r--
Some modifications on shortest path algoritms:
- heap traits
- checked execution
     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 
    21 MapStorage::~MapStorage()
    22 {
    23   for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
    24       nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
    25   {
    26     delete it->second;
    27   }
    28   for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
    29       edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
    30   {
    31     delete it->second;
    32   }
    33 }
    34 
    35 int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap<double> *nodemap, double default_value = 0.0)
    36 {
    37   if( nodemap_storage.find(name) == nodemap_storage.end() )
    38     {
    39       nodemap_storage[name]=nodemap;
    40       // set the maps default value
    41       nodemap_default[name] = default_value;
    42       return 0;
    43     }
    44   return 1;
    45 }
    46 
    47 int MapStorage::addEdgeMap(const std::string & name, Graph::EdgeMap<double> *edgemap, double default_value = 0.0)
    48 {
    49   if( edgemap_storage.find(name) == edgemap_storage.end() )
    50     {
    51       edgemap_storage[name]=edgemap;
    52       // set the maps default value
    53       edgemap_default[name] = default_value;
    54       return 0;
    55     }
    56   return 1;
    57 }
    58 
    59 double MapStorage::maxOfNodeMap(const std::string & name)
    60 {
    61   double max=0;
    62   for (NodeIt j(graph); j!=INVALID; ++j)
    63   {
    64     if( (*nodemap_storage[name])[j]>max )
    65     {
    66       max=(*nodemap_storage[name])[j];
    67     }
    68   }
    69   return max;
    70 }
    71 
    72 double MapStorage::maxOfEdgeMap(const std::string & name)
    73 {
    74   double max=0;
    75   for (EdgeIt j(graph); j!=INVALID; ++j)
    76   {
    77     if( (*edgemap_storage[name])[j]>max )
    78     {
    79       max=(*edgemap_storage[name])[j];
    80     }
    81   }
    82   return max;
    83 }
    84 
    85 double MapStorage::minOfNodeMap(const std::string & name)
    86 {
    87   NodeIt j(graph);
    88   double min;
    89   if(j!=INVALID)
    90     {
    91       min=(*nodemap_storage[name])[j];
    92     }
    93   else
    94     {
    95       min=0;
    96     }
    97   for (; j!=INVALID; ++j)
    98   {
    99     if( (*nodemap_storage[name])[j]<min )
   100     {
   101       min=(*nodemap_storage[name])[j];
   102     }
   103   }
   104   return min;
   105 }
   106 
   107 double MapStorage::minOfEdgeMap(const std::string & name)
   108 {
   109   EdgeIt j(graph);
   110   double min;
   111   if(j!=INVALID)
   112     {
   113       min=(*edgemap_storage[name])[j];
   114     }
   115   else
   116     {
   117       min=0;
   118     }
   119   for (EdgeIt j(graph); j!=INVALID; ++j)
   120   {
   121     if( (*edgemap_storage[name])[j]<min )
   122     {
   123       min=(*edgemap_storage[name])[j];
   124     }
   125   }
   126   return min;
   127 }
   128 
   129 int MapStorage::readFromFile(const std::string &filename)
   130 {
   131   bool read_x = false;
   132   bool read_y = false;
   133   bool read_edge_id = false;
   134 
   135   try {
   136     LemonReader lreader(filename);
   137     ContentReader content(lreader);
   138     lreader.run();
   139 
   140     const std::vector<std::string>& nodeMapNames = content.nodeSetMaps(0);
   141     const std::vector<std::string>& edgeMapNames = content.edgeSetMaps(0);
   142 
   143     GraphReader<Graph> greader(filename, graph);
   144     for (std::vector<std::string>::const_iterator it = nodeMapNames.begin();
   145         it != nodeMapNames.end(); ++it)
   146     {
   147       if (*it == "coordinates_x")
   148       {
   149         read_x = true;
   150         //std::cout << "read X nodemap" << std::endl;
   151       }
   152       else if (*it == "coordinates_y")
   153       {
   154         read_y = true;
   155         //std::cout << "read Y nodemap" << std::endl;
   156       }
   157       else if (*it == "id")
   158       {
   159         //std::cout << "read id nodemap" << std::endl;
   160       }
   161       else
   162       {
   163         nodemap_storage[*it] = new Graph::NodeMap<double>(graph);
   164         //std::cout << "read " << *it << " nodemap" << std::endl;
   165       }
   166       greader.readNodeMap(*it, *nodemap_storage[*it]);
   167     }
   168     for (std::vector<std::string>::const_iterator it = edgeMapNames.begin();
   169         it != edgeMapNames.end(); ++it)
   170     {
   171       if (*it == "id")
   172       {
   173         //std::cout << "read id edgemap" << std::endl;
   174       }
   175       else
   176       {
   177         edgemap_storage[*it] = new Graph::EdgeMap<double>(graph);
   178         //std::cout << "read " << *it << " edgemap" << std::endl;
   179       }
   180       greader.readEdgeMap(*it, *edgemap_storage[*it]);
   181     }
   182     greader.run();
   183   } catch (DataFormatError& error) {
   184     Gtk::MessageDialog mdialog(error.what());
   185     mdialog.run();
   186     clear();
   187     return 1;
   188   }
   189 
   190   if (!read_edge_id)
   191   {
   192     edgemap_storage["id"] = new Graph::EdgeMap<double>(graph);
   193     int i = 1;
   194     for (EdgeIt e(graph); e != INVALID; ++e)
   195     {
   196       (*edgemap_storage["id"])[e] = i++;
   197     }
   198   }
   199 
   200   if (!read_x || !read_y)
   201   {
   202     int node_num = 0;
   203     for (NodeIt n(graph); n != INVALID; ++n)
   204     {
   205       node_num++;
   206     }
   207     const double pi = 3.142;
   208     double step = 2 * pi / (double) node_num;
   209     int i = 0;
   210     for (NodeIt n(graph); n != INVALID; ++n)
   211     {
   212       nodemap_storage["coordinates_x"]->set(n, 250.0 * std::cos(i * step));
   213       nodemap_storage["coordinates_y"]->set(n, 250.0 * std::sin(i * step));
   214       i++;
   215     }
   216   }
   217 
   218   // fill in the default values for the maps
   219   for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
   220       nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
   221   {
   222     if ((it->first != "id") &&
   223         (it->first != "coordiantes_x") &&
   224         (it->first != "coordinates_y"))
   225     {
   226       nodemap_default[it->first] = 0.0;
   227     }
   228     else if (it->first == "id")
   229     {
   230       NodeIt n(graph);
   231       double max = (*nodemap_storage["id"])[n];
   232       for (; n != INVALID; ++n)
   233       {
   234         if ((*nodemap_storage["id"])[n] > max)
   235           max = (*nodemap_storage["id"])[n];
   236       }
   237       nodemap_default["id"] = max + 1.0;
   238     }
   239   }
   240   for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
   241       edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
   242   {
   243     if (it->first != "id")
   244     {
   245       edgemap_default[it->first] = 0.0;
   246     }
   247     else
   248     {
   249       double max = std::numeric_limits<double>::min();
   250       for (EdgeIt e(graph); e != INVALID; ++e)
   251       {
   252         if ((*edgemap_storage["id"])[e] > max)
   253           max = (*edgemap_storage["id"])[e];
   254       }
   255       if (max > std::numeric_limits<double>::min())
   256         edgemap_default["id"] = max + 1.0;
   257       else
   258         edgemap_default["id"] = 1.0;
   259     }
   260   }
   261 
   262   return 0;
   263 }
   264 
   265 void MapStorage::writeToFile(const std::string &filename)
   266 {
   267   GraphWriter<Graph> gwriter(filename, graph);
   268 
   269   for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
   270       nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
   271   {
   272     gwriter.writeNodeMap(it->first, *(it->second));
   273     //std::cout << "wrote " << it->first << " nodemap" << std::endl;
   274   }
   275   for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
   276       edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
   277   {
   278     gwriter.writeEdgeMap(it->first, *(it->second));
   279     //std::cout << "wrote " << it->first << " edgemap" << std::endl;
   280   }
   281   gwriter.run();
   282 }
   283 
   284 void MapStorage::clear()
   285 {
   286   for (std::map<std::string, Graph::NodeMap<double>*>::iterator it =
   287       nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
   288   {
   289     if ((it->first != "coordinates_x") &&
   290         (it->first != "coordinates_y") &&
   291         (it->first != "id"))
   292     {
   293       delete it->second;
   294       nodemap_storage.erase(it);
   295     }
   296   }
   297   for (std::map<std::string, Graph::EdgeMap<double>*>::iterator it =
   298       edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
   299   {
   300     if (it->first != "id")
   301     {
   302       delete it->second;
   303       edgemap_storage.erase(it);
   304     }
   305   }
   306   for (std::map<std::string, double>::iterator it =
   307       nodemap_default.begin(); it != nodemap_default.end(); ++it)
   308   {
   309     if (it->first != "id")
   310       nodemap_default.erase(it);
   311   }
   312   for (std::map<std::string, double>::iterator it =
   313       edgemap_default.begin(); it != edgemap_default.end(); ++it)
   314   {
   315     if (it->first != "id")
   316       edgemap_default.erase(it);
   317   }
   318   graph.clear();
   319   file_name = "";
   320   modified = false;
   321 }