mapstorage.cc
author ladanyi
Fri, 29 Jul 2005 12:01:37 +0000
branchgui
changeset 53 e73d7540bd24
parent 46 121452cc4096
child 58 a27ab230a178
permissions -rw-r--r--
added support for saving files
     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=(*nodemap_storage[name])[j];
    78   for (; j!=INVALID; ++j)
    79   {
    80     if( (*nodemap_storage[name])[j]<min )
    81     {
    82       min=(*nodemap_storage[name])[j];
    83     }
    84   }
    85   return min;
    86 }
    87 
    88 double MapStorage::minOfEdgeMap(const std::string & name)
    89 {
    90   EdgeIt j(graph);
    91   double min=(*edgemap_storage[name])[j];
    92   for (EdgeIt j(graph); j!=INVALID; ++j)
    93   {
    94     if( (*edgemap_storage[name])[j]<min )
    95     {
    96       min=(*edgemap_storage[name])[j];
    97     }
    98   }
    99   return min;
   100 }
   101 
   102 void MapStorage::initMapsForEdge(Graph::Edge e)
   103 {
   104   std::map< std::string,Graph::EdgeMap<double> * >::iterator ems_it;
   105   for(ems_it=edgemap_storage.begin();ems_it!=edgemap_storage.end();ems_it++)
   106     {
   107       (*((*ems_it).second))[e]=5;
   108     }
   109 }
   110 
   111 void MapStorage::readFromFile(const std::string &filename)
   112 {
   113   bool read_x = false;
   114   bool read_y = false;
   115 
   116   try {
   117     LemonReader lreader(filename);
   118     ContentReader content(lreader);
   119     lreader.run();
   120 
   121     const std::vector<std::string>& nodeMapNames = content.nodeSetMaps(0);
   122     const std::vector<std::string>& edgeMapNames = content.edgeSetMaps(0);
   123 
   124     GraphReader<Graph> greader(filename, graph);
   125     for (std::vector<std::string>::const_iterator it = nodeMapNames.begin();
   126         it != nodeMapNames.end(); ++it)
   127     {
   128       if (*it == "coordinates_x")
   129       {
   130         read_x = true;
   131         //std::cout << "read X nodemap" << std::endl;
   132       }
   133       else if (*it == "coordinates_y")
   134       {
   135         read_y = true;
   136         //std::cout << "read Y nodemap" << std::endl;
   137       }
   138       else if (*it == "id")
   139       {
   140         //std::cout << "read id nodemap" << std::endl;
   141       }
   142       else
   143       {
   144         nodemap_storage[*it] = new Graph::NodeMap<double>(graph);
   145         //std::cout << "read " << *it << " nodemap" << std::endl;
   146       }
   147       greader.readNodeMap(*it, *nodemap_storage[*it]);
   148     }
   149     for (std::vector<std::string>::const_iterator it = edgeMapNames.begin();
   150         it != edgeMapNames.end(); ++it)
   151     {
   152       if (*it == "id")
   153       {
   154         //std::cout << "read id edgemap" << std::endl;
   155       }
   156       else
   157       {
   158         edgemap_storage[*it] = new Graph::EdgeMap<double>(graph);
   159         //std::cout << "read " << *it << " edgemap" << std::endl;
   160       }
   161       greader.readEdgeMap(*it, *edgemap_storage[*it]);
   162     }
   163     greader.run();
   164   } catch (DataFormatError& error) {
   165     /*
   166     Gtk::MessageDialog mdialog("Read Error");
   167     mdialog.set_message(error.what());
   168     mdialog.run();
   169     */
   170     // reset graph and mapstorage ?
   171     return;
   172   }
   173 
   174   if (!read_x || !read_y)
   175   {
   176     int node_num = 0;
   177     for (NodeIt n(graph); n != INVALID; ++n)
   178     {
   179       node_num++;
   180     }
   181     const double pi = 3.142;
   182     double step = 2 * pi / (double) node_num;
   183     int i = 0;
   184     for (NodeIt n(graph); n != INVALID; ++n)
   185     {
   186       nodemap_storage["coordinates_x"]->set(n, 250.0 * cos(i * step));
   187       nodemap_storage["coordinates_y"]->set(n, 250.0 * sin(i * step));
   188       i++;
   189     }
   190   }
   191 }
   192 
   193 void MapStorage::writeToFile(const std::string &filename)
   194 {
   195   GraphWriter<Graph> gwriter(filename, graph);
   196 
   197   for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
   198       nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
   199   {
   200     gwriter.writeNodeMap(it->first, *(it->second));
   201     //std::cout << "wrote " << it->first << " nodemap" << std::endl;
   202   }
   203   for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
   204       edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
   205   {
   206     gwriter.writeEdgeMap(it->first, *(it->second));
   207     //std::cout << "wrote " << it->first << " edgemap" << std::endl;
   208   }
   209   gwriter.run();
   210 }
   211 
   212 void MapStorage::clear()
   213 {
   214   for (std::map<std::string, Graph::NodeMap<double>*>::iterator it =
   215       nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
   216   {
   217     if ((it->first != "coordinates_x") &&
   218         (it->first != "coordinates_y") &&
   219         (it->first != "id"))
   220     {
   221       delete it->second;
   222       nodemap_storage.erase(it);
   223     }
   224   }
   225   for (std::map<std::string, Graph::EdgeMap<double>*>::iterator it =
   226       edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
   227   {
   228     if (it->first != "id")
   229     {
   230       delete it->second;
   231       edgemap_storage.erase(it);
   232     }
   233   }
   234   graph.clear();
   235   file_name = "";
   236   modified = false;
   237 }