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