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