mapstorage.cc
branchgui
changeset 54 9128e364fb19
parent 46 121452cc4096
child 58 a27ab230a178
equal deleted inserted replaced
7:9f039be3b387 8:0ac567e9a3d8
     1 #include <mapstorage.h>
     1 #include "mapstorage.h"
     2 
     2 
     3 MapStorage::MapStorage(Graph & graph):g(graph)
     3 MapStorage::MapStorage() : modified(false), file_name("")
     4 {
     4 {
     5 };
     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 }
     6 
    27 
     7 int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap<double> *nodemap)
    28 int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap<double> *nodemap)
     8 {
    29 {
     9   if( nodemap_storage.find(name) == nodemap_storage.end() )
    30   if( nodemap_storage.find(name) == nodemap_storage.end() )
    10     {
    31     {
    25 }
    46 }
    26 
    47 
    27 double MapStorage::maxOfNodeMap(const std::string & name)
    48 double MapStorage::maxOfNodeMap(const std::string & name)
    28 {
    49 {
    29   double max=0;
    50   double max=0;
    30   for (NodeIt j(g); j!=INVALID; ++j)
    51   for (NodeIt j(graph); j!=INVALID; ++j)
    31   {
    52   {
    32     if( (*nodemap_storage[name])[j]>max )
    53     if( (*nodemap_storage[name])[j]>max )
    33     {
    54     {
    34       max=(*nodemap_storage[name])[j];
    55       max=(*nodemap_storage[name])[j];
    35     }
    56     }
    38 }
    59 }
    39 
    60 
    40 double MapStorage::maxOfEdgeMap(const std::string & name)
    61 double MapStorage::maxOfEdgeMap(const std::string & name)
    41 {
    62 {
    42   double max=0;
    63   double max=0;
    43   for (EdgeIt j(g); j!=INVALID; ++j)
    64   for (EdgeIt j(graph); j!=INVALID; ++j)
    44   {
    65   {
    45     if( (*edgemap_storage[name])[j]>max )
    66     if( (*edgemap_storage[name])[j]>max )
    46     {
    67     {
    47       max=(*edgemap_storage[name])[j];
    68       max=(*edgemap_storage[name])[j];
    48     }
    69     }
    50   return max;
    71   return max;
    51 }
    72 }
    52 
    73 
    53 double MapStorage::minOfNodeMap(const std::string & name)
    74 double MapStorage::minOfNodeMap(const std::string & name)
    54 {
    75 {
    55   NodeIt j(g);
    76   NodeIt j(graph);
    56   double min=(*nodemap_storage[name])[j];
    77   double min=(*nodemap_storage[name])[j];
    57   for (; j!=INVALID; ++j)
    78   for (; j!=INVALID; ++j)
    58   {
    79   {
    59     if( (*nodemap_storage[name])[j]<min )
    80     if( (*nodemap_storage[name])[j]<min )
    60     {
    81     {
    64   return min;
    85   return min;
    65 }
    86 }
    66 
    87 
    67 double MapStorage::minOfEdgeMap(const std::string & name)
    88 double MapStorage::minOfEdgeMap(const std::string & name)
    68 {
    89 {
    69   EdgeIt j(g);
    90   EdgeIt j(graph);
    70   double min=(*edgemap_storage[name])[j];
    91   double min=(*edgemap_storage[name])[j];
    71   for (EdgeIt j(g); j!=INVALID; ++j)
    92   for (EdgeIt j(graph); j!=INVALID; ++j)
    72   {
    93   {
    73     if( (*edgemap_storage[name])[j]<min )
    94     if( (*edgemap_storage[name])[j]<min )
    74     {
    95     {
    75       min=(*edgemap_storage[name])[j];
    96       min=(*edgemap_storage[name])[j];
    76     }
    97     }
    84   for(ems_it=edgemap_storage.begin();ems_it!=edgemap_storage.end();ems_it++)
   105   for(ems_it=edgemap_storage.begin();ems_it!=edgemap_storage.end();ems_it++)
    85     {
   106     {
    86       (*((*ems_it).second))[e]=5;
   107       (*((*ems_it).second))[e]=5;
    87     }
   108     }
    88 }
   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 }