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