gui/mapstorage.cc
author deba
Thu, 11 Aug 2005 14:35:22 +0000
changeset 1625 6f7162316c07
parent 1606 dc4ea2010dee
child 1634 910b1bcb7d05
permissions -rw-r--r--
Fix Makefile.am
     1 #include "mapstorage.h"
     2 
     3 MapStorage::MapStorage() : modified(false), file_name("")
     4 {
     5   nodemap_storage["coordinates_x"] = new Graph::NodeMap<double>(graph);
     6   coords.setXMap(*nodemap_storage["coordinates_x"]);
     7   nodemap_storage["coordinates_y"] = new Graph::NodeMap<double>(graph);
     8   coords.setYMap(*nodemap_storage["coordinates_y"]);
     9 
    10   nodemap_storage["id"] = new Graph::NodeMap<double>(graph);
    11   edgemap_storage["id"] = new Graph::EdgeMap<double>(graph);
    12 }
    13 
    14 MapStorage::~MapStorage()
    15 {
    16   for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
    17       nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
    18   {
    19     delete it->second;
    20   }
    21   for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
    22       edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
    23   {
    24     delete it->second;
    25   }
    26 }
    27 
    28 int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap<double> *nodemap)
    29 {
    30   if( nodemap_storage.find(name) == nodemap_storage.end() )
    31     {
    32       nodemap_storage[name]=nodemap;
    33       return 0;
    34     }
    35   return 1;
    36 }
    37 
    38 int MapStorage::addEdgeMap(const std::string & name, Graph::EdgeMap<double> *edgemap)
    39 {
    40   if( edgemap_storage.find(name) == edgemap_storage.end() )
    41     {
    42       edgemap_storage[name]=edgemap;
    43       return 0;
    44     }
    45   return 1;
    46 }
    47 
    48 double MapStorage::maxOfNodeMap(const std::string & name)
    49 {
    50   double max=0;
    51   for (NodeIt j(graph); j!=INVALID; ++j)
    52   {
    53     if( (*nodemap_storage[name])[j]>max )
    54     {
    55       max=(*nodemap_storage[name])[j];
    56     }
    57   }
    58   return max;
    59 }
    60 
    61 double MapStorage::maxOfEdgeMap(const std::string & name)
    62 {
    63   double max=0;
    64   for (EdgeIt j(graph); j!=INVALID; ++j)
    65   {
    66     if( (*edgemap_storage[name])[j]>max )
    67     {
    68       max=(*edgemap_storage[name])[j];
    69     }
    70   }
    71   return max;
    72 }
    73 
    74 double MapStorage::minOfNodeMap(const std::string & name)
    75 {
    76   NodeIt j(graph);
    77   double min;
    78   if(j!=INVALID)
    79     {
    80       min=(*nodemap_storage[name])[j];
    81     }
    82   else
    83     {
    84       min=0;
    85     }
    86   for (; j!=INVALID; ++j)
    87   {
    88     if( (*nodemap_storage[name])[j]<min )
    89     {
    90       min=(*nodemap_storage[name])[j];
    91     }
    92   }
    93   return min;
    94 }
    95 
    96 double MapStorage::minOfEdgeMap(const std::string & name)
    97 {
    98   EdgeIt j(graph);
    99   double min;
   100   if(j!=INVALID)
   101     {
   102       min=(*edgemap_storage[name])[j];
   103     }
   104   else
   105     {
   106       min=0;
   107     }
   108   for (EdgeIt j(graph); j!=INVALID; ++j)
   109   {
   110     if( (*edgemap_storage[name])[j]<min )
   111     {
   112       min=(*edgemap_storage[name])[j];
   113     }
   114   }
   115   return min;
   116 }
   117 
   118 void MapStorage::initMapsForEdge(Graph::Edge e)
   119 {
   120   std::map< std::string,Graph::EdgeMap<double> * >::iterator ems_it;
   121   for(ems_it=edgemap_storage.begin();ems_it!=edgemap_storage.end();ems_it++)
   122     {
   123       (*((*ems_it).second))[e]=5;
   124     }
   125 }
   126 
   127 void MapStorage::readFromFile(const std::string &filename)
   128 {
   129   bool read_x = false;
   130   bool read_y = false;
   131 
   132   try {
   133     LemonReader lreader(filename);
   134     ContentReader content(lreader);
   135     lreader.run();
   136 
   137     const std::vector<std::string>& nodeMapNames = content.nodeSetMaps(0);
   138     const std::vector<std::string>& edgeMapNames = content.edgeSetMaps(0);
   139 
   140     GraphReader<Graph> greader(filename, graph);
   141     for (std::vector<std::string>::const_iterator it = nodeMapNames.begin();
   142         it != nodeMapNames.end(); ++it)
   143     {
   144       if (*it == "coordinates_x")
   145       {
   146         read_x = true;
   147         //std::cout << "read X nodemap" << std::endl;
   148       }
   149       else if (*it == "coordinates_y")
   150       {
   151         read_y = true;
   152         //std::cout << "read Y nodemap" << std::endl;
   153       }
   154       else if (*it == "id")
   155       {
   156         //std::cout << "read id nodemap" << std::endl;
   157       }
   158       else
   159       {
   160         nodemap_storage[*it] = new Graph::NodeMap<double>(graph);
   161         //std::cout << "read " << *it << " nodemap" << std::endl;
   162       }
   163       greader.readNodeMap(*it, *nodemap_storage[*it]);
   164     }
   165     for (std::vector<std::string>::const_iterator it = edgeMapNames.begin();
   166         it != edgeMapNames.end(); ++it)
   167     {
   168       if (*it == "id")
   169       {
   170         //std::cout << "read id edgemap" << std::endl;
   171       }
   172       else
   173       {
   174         edgemap_storage[*it] = new Graph::EdgeMap<double>(graph);
   175         //std::cout << "read " << *it << " edgemap" << std::endl;
   176       }
   177       greader.readEdgeMap(*it, *edgemap_storage[*it]);
   178     }
   179     greader.run();
   180   } catch (DataFormatError& error) {
   181     /*
   182     Gtk::MessageDialog mdialog("Read Error");
   183     mdialog.set_message(error.what());
   184     mdialog.run();
   185     */
   186     // reset graph and mapstorage ?
   187     return;
   188   }
   189 
   190   if (!read_x || !read_y)
   191   {
   192     int node_num = 0;
   193     for (NodeIt n(graph); n != INVALID; ++n)
   194     {
   195       node_num++;
   196     }
   197     const double pi = 3.142;
   198     double step = 2 * pi / (double) node_num;
   199     int i = 0;
   200     for (NodeIt n(graph); n != INVALID; ++n)
   201     {
   202       nodemap_storage["coordinates_x"]->set(n, 250.0 * cos(i * step));
   203       nodemap_storage["coordinates_y"]->set(n, 250.0 * sin(i * step));
   204       i++;
   205     }
   206   }
   207 }
   208 
   209 void MapStorage::writeToFile(const std::string &filename)
   210 {
   211   GraphWriter<Graph> gwriter(filename, graph);
   212 
   213   for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
   214       nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
   215   {
   216     gwriter.writeNodeMap(it->first, *(it->second));
   217     //std::cout << "wrote " << it->first << " nodemap" << std::endl;
   218   }
   219   for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
   220       edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
   221   {
   222     gwriter.writeEdgeMap(it->first, *(it->second));
   223     //std::cout << "wrote " << it->first << " edgemap" << std::endl;
   224   }
   225   gwriter.run();
   226 }
   227 
   228 void MapStorage::clear()
   229 {
   230   for (std::map<std::string, Graph::NodeMap<double>*>::iterator it =
   231       nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
   232   {
   233     if ((it->first != "coordinates_x") &&
   234         (it->first != "coordinates_y") &&
   235         (it->first != "id"))
   236     {
   237       delete it->second;
   238       nodemap_storage.erase(it);
   239     }
   240   }
   241   for (std::map<std::string, Graph::EdgeMap<double>*>::iterator it =
   242       edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
   243   {
   244     if (it->first != "id")
   245     {
   246       delete it->second;
   247       edgemap_storage.erase(it);
   248     }
   249   }
   250   graph.clear();
   251   file_name = "";
   252   modified = false;
   253 }