COIN-OR::LEMON - Graph Library

Changeset 1606:dc4ea2010dee in lemon-0.x for gui/mapstorage.cc


Ignore:
Timestamp:
07/29/05 14:01:37 (18 years ago)
Author:
Akos Ladanyi
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@2111
Message:

added support for saving files

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gui/mapstorage.cc

    r1597 r1606  
    1 #include <mapstorage.h>
    2 
    3 MapStorage::MapStorage(Graph & graph):g(graph)
    4 {
    5 };
     1#include "mapstorage.h"
     2
     3MapStorage::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
     14MapStorage::~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}
    627
    728int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap<double> *nodemap)
     
    2849{
    2950  double max=0;
    30   for (NodeIt j(g); j!=INVALID; ++j)
     51  for (NodeIt j(graph); j!=INVALID; ++j)
    3152  {
    3253    if( (*nodemap_storage[name])[j]>max )
     
    4162{
    4263  double max=0;
    43   for (EdgeIt j(g); j!=INVALID; ++j)
     64  for (EdgeIt j(graph); j!=INVALID; ++j)
    4465  {
    4566    if( (*edgemap_storage[name])[j]>max )
     
    5374double MapStorage::minOfNodeMap(const std::string & name)
    5475{
    55   NodeIt j(g);
     76  NodeIt j(graph);
    5677  double min=(*nodemap_storage[name])[j];
    5778  for (; j!=INVALID; ++j)
     
    6788double MapStorage::minOfEdgeMap(const std::string & name)
    6889{
    69   EdgeIt j(g);
     90  EdgeIt j(graph);
    7091  double min=(*edgemap_storage[name])[j];
    71   for (EdgeIt j(g); j!=INVALID; ++j)
     92  for (EdgeIt j(graph); j!=INVALID; ++j)
    7293  {
    7394    if( (*edgemap_storage[name])[j]<min )
     
    87108    }
    88109}
     110
     111void 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
     193void 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
     212void 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}
Note: See TracChangeset for help on using the changeset viewer.