gui/mapstorage.cc
author ladanyi
Fri, 26 Aug 2005 10:46:51 +0000
changeset 1649 95c8cd25a456
parent 1646 2dd083dded66
child 1686 a5b8e0fde2be
permissions -rw-r--r--
bugfix
ladanyi@1606
     1
#include "mapstorage.h"
ladanyi@1645
     2
#include <gtkmm.h>
ladanyi@1634
     3
#include <cmath>
ladanyi@1442
     4
ladanyi@1606
     5
MapStorage::MapStorage() : modified(false), file_name("")
ladanyi@1442
     6
{
ladanyi@1606
     7
  nodemap_storage["coordinates_x"] = new Graph::NodeMap<double>(graph);
ladanyi@1606
     8
  coords.setXMap(*nodemap_storage["coordinates_x"]);
ladanyi@1606
     9
  nodemap_storage["coordinates_y"] = new Graph::NodeMap<double>(graph);
ladanyi@1606
    10
  coords.setYMap(*nodemap_storage["coordinates_y"]);
ladanyi@1606
    11
ladanyi@1606
    12
  nodemap_storage["id"] = new Graph::NodeMap<double>(graph);
ladanyi@1606
    13
  edgemap_storage["id"] = new Graph::EdgeMap<double>(graph);
ladanyi@1646
    14
ladanyi@1646
    15
  nodemap_default["id"] = 1.0;
ladanyi@1646
    16
  edgemap_default["id"] = 1.0;
ladanyi@1606
    17
}
ladanyi@1606
    18
ladanyi@1606
    19
MapStorage::~MapStorage()
ladanyi@1606
    20
{
ladanyi@1606
    21
  for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
ladanyi@1606
    22
      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
ladanyi@1606
    23
  {
ladanyi@1606
    24
    delete it->second;
ladanyi@1606
    25
  }
ladanyi@1606
    26
  for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
ladanyi@1606
    27
      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
ladanyi@1606
    28
  {
ladanyi@1606
    29
    delete it->second;
ladanyi@1606
    30
  }
ladanyi@1606
    31
}
ladanyi@1442
    32
ladanyi@1645
    33
int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap<double> *nodemap, double default_value = 0.0)
ladanyi@1442
    34
{
hegyi@1597
    35
  if( nodemap_storage.find(name) == nodemap_storage.end() )
hegyi@1597
    36
    {
hegyi@1597
    37
      nodemap_storage[name]=nodemap;
ladanyi@1645
    38
      // set the maps default value
ladanyi@1645
    39
      nodemap_default[name] = default_value;
hegyi@1597
    40
      return 0;
hegyi@1597
    41
    }
hegyi@1597
    42
  return 1;
ladanyi@1442
    43
}
ladanyi@1442
    44
ladanyi@1645
    45
int MapStorage::addEdgeMap(const std::string & name, Graph::EdgeMap<double> *edgemap, double default_value = 0.0)
ladanyi@1442
    46
{
hegyi@1597
    47
  if( edgemap_storage.find(name) == edgemap_storage.end() )
hegyi@1597
    48
    {
hegyi@1597
    49
      edgemap_storage[name]=edgemap;
ladanyi@1645
    50
      // set the maps default value
ladanyi@1645
    51
      edgemap_default[name] = default_value;
hegyi@1597
    52
      return 0;
hegyi@1597
    53
    }
hegyi@1597
    54
  return 1;
ladanyi@1442
    55
}
ladanyi@1442
    56
ladanyi@1442
    57
double MapStorage::maxOfNodeMap(const std::string & name)
ladanyi@1442
    58
{
ladanyi@1442
    59
  double max=0;
ladanyi@1606
    60
  for (NodeIt j(graph); j!=INVALID; ++j)
ladanyi@1442
    61
  {
ladanyi@1442
    62
    if( (*nodemap_storage[name])[j]>max )
ladanyi@1442
    63
    {
ladanyi@1442
    64
      max=(*nodemap_storage[name])[j];
ladanyi@1442
    65
    }
ladanyi@1442
    66
  }
ladanyi@1442
    67
  return max;
ladanyi@1442
    68
}
ladanyi@1442
    69
ladanyi@1442
    70
double MapStorage::maxOfEdgeMap(const std::string & name)
ladanyi@1442
    71
{
ladanyi@1442
    72
  double max=0;
ladanyi@1606
    73
  for (EdgeIt j(graph); j!=INVALID; ++j)
ladanyi@1442
    74
  {
ladanyi@1442
    75
    if( (*edgemap_storage[name])[j]>max )
ladanyi@1442
    76
    {
ladanyi@1442
    77
      max=(*edgemap_storage[name])[j];
ladanyi@1442
    78
    }
ladanyi@1442
    79
  }
ladanyi@1442
    80
  return max;
ladanyi@1442
    81
}
ladanyi@1442
    82
ladanyi@1442
    83
double MapStorage::minOfNodeMap(const std::string & name)
ladanyi@1442
    84
{
ladanyi@1606
    85
  NodeIt j(graph);
hegyi@1617
    86
  double min;
hegyi@1617
    87
  if(j!=INVALID)
hegyi@1617
    88
    {
hegyi@1617
    89
      min=(*nodemap_storage[name])[j];
hegyi@1617
    90
    }
hegyi@1617
    91
  else
hegyi@1617
    92
    {
hegyi@1617
    93
      min=0;
hegyi@1617
    94
    }
ladanyi@1442
    95
  for (; j!=INVALID; ++j)
ladanyi@1442
    96
  {
ladanyi@1442
    97
    if( (*nodemap_storage[name])[j]<min )
ladanyi@1442
    98
    {
ladanyi@1442
    99
      min=(*nodemap_storage[name])[j];
ladanyi@1442
   100
    }
ladanyi@1442
   101
  }
ladanyi@1442
   102
  return min;
ladanyi@1442
   103
}
ladanyi@1442
   104
ladanyi@1442
   105
double MapStorage::minOfEdgeMap(const std::string & name)
ladanyi@1442
   106
{
ladanyi@1606
   107
  EdgeIt j(graph);
hegyi@1617
   108
  double min;
hegyi@1617
   109
  if(j!=INVALID)
hegyi@1617
   110
    {
hegyi@1617
   111
      min=(*edgemap_storage[name])[j];
hegyi@1617
   112
    }
hegyi@1617
   113
  else
hegyi@1617
   114
    {
hegyi@1617
   115
      min=0;
hegyi@1617
   116
    }
ladanyi@1606
   117
  for (EdgeIt j(graph); j!=INVALID; ++j)
ladanyi@1442
   118
  {
ladanyi@1442
   119
    if( (*edgemap_storage[name])[j]<min )
ladanyi@1442
   120
    {
ladanyi@1442
   121
      min=(*edgemap_storage[name])[j];
ladanyi@1442
   122
    }
ladanyi@1442
   123
  }
ladanyi@1442
   124
  return min;
ladanyi@1442
   125
}
ladanyi@1442
   126
ladanyi@1645
   127
int MapStorage::readFromFile(const std::string &filename)
ladanyi@1606
   128
{
ladanyi@1606
   129
  bool read_x = false;
ladanyi@1606
   130
  bool read_y = false;
ladanyi@1646
   131
  bool read_edge_id = false;
ladanyi@1606
   132
ladanyi@1606
   133
  try {
ladanyi@1606
   134
    LemonReader lreader(filename);
ladanyi@1606
   135
    ContentReader content(lreader);
ladanyi@1606
   136
    lreader.run();
ladanyi@1606
   137
ladanyi@1606
   138
    const std::vector<std::string>& nodeMapNames = content.nodeSetMaps(0);
ladanyi@1606
   139
    const std::vector<std::string>& edgeMapNames = content.edgeSetMaps(0);
ladanyi@1606
   140
ladanyi@1606
   141
    GraphReader<Graph> greader(filename, graph);
ladanyi@1606
   142
    for (std::vector<std::string>::const_iterator it = nodeMapNames.begin();
ladanyi@1606
   143
        it != nodeMapNames.end(); ++it)
ladanyi@1606
   144
    {
ladanyi@1606
   145
      if (*it == "coordinates_x")
ladanyi@1606
   146
      {
ladanyi@1606
   147
        read_x = true;
ladanyi@1606
   148
        //std::cout << "read X nodemap" << std::endl;
ladanyi@1606
   149
      }
ladanyi@1606
   150
      else if (*it == "coordinates_y")
ladanyi@1606
   151
      {
ladanyi@1606
   152
        read_y = true;
ladanyi@1606
   153
        //std::cout << "read Y nodemap" << std::endl;
ladanyi@1606
   154
      }
ladanyi@1606
   155
      else if (*it == "id")
ladanyi@1606
   156
      {
ladanyi@1606
   157
        //std::cout << "read id nodemap" << std::endl;
ladanyi@1606
   158
      }
ladanyi@1606
   159
      else
ladanyi@1606
   160
      {
ladanyi@1606
   161
        nodemap_storage[*it] = new Graph::NodeMap<double>(graph);
ladanyi@1606
   162
        //std::cout << "read " << *it << " nodemap" << std::endl;
ladanyi@1606
   163
      }
ladanyi@1606
   164
      greader.readNodeMap(*it, *nodemap_storage[*it]);
ladanyi@1606
   165
    }
ladanyi@1606
   166
    for (std::vector<std::string>::const_iterator it = edgeMapNames.begin();
ladanyi@1606
   167
        it != edgeMapNames.end(); ++it)
ladanyi@1606
   168
    {
ladanyi@1606
   169
      if (*it == "id")
ladanyi@1606
   170
      {
ladanyi@1606
   171
        //std::cout << "read id edgemap" << std::endl;
ladanyi@1606
   172
      }
ladanyi@1606
   173
      else
ladanyi@1606
   174
      {
ladanyi@1606
   175
        edgemap_storage[*it] = new Graph::EdgeMap<double>(graph);
ladanyi@1606
   176
        //std::cout << "read " << *it << " edgemap" << std::endl;
ladanyi@1606
   177
      }
ladanyi@1606
   178
      greader.readEdgeMap(*it, *edgemap_storage[*it]);
ladanyi@1606
   179
    }
ladanyi@1606
   180
    greader.run();
ladanyi@1606
   181
  } catch (DataFormatError& error) {
ladanyi@1606
   182
    Gtk::MessageDialog mdialog("Read Error");
ladanyi@1606
   183
    mdialog.set_message(error.what());
ladanyi@1606
   184
    mdialog.run();
ladanyi@1645
   185
    clear();
ladanyi@1645
   186
    return 1;
ladanyi@1606
   187
  }
ladanyi@1606
   188
ladanyi@1646
   189
  if (!read_edge_id)
ladanyi@1646
   190
  {
ladanyi@1646
   191
    edgemap_storage["id"] = new Graph::EdgeMap<double>(graph);
ladanyi@1646
   192
    int i = 1;
ladanyi@1646
   193
    for (EdgeIt e(graph); e != INVALID; ++e)
ladanyi@1646
   194
    {
ladanyi@1646
   195
      (*edgemap_storage["id"])[e] = i++;
ladanyi@1646
   196
    }
ladanyi@1646
   197
  }
ladanyi@1646
   198
ladanyi@1606
   199
  if (!read_x || !read_y)
ladanyi@1606
   200
  {
ladanyi@1606
   201
    int node_num = 0;
ladanyi@1606
   202
    for (NodeIt n(graph); n != INVALID; ++n)
ladanyi@1606
   203
    {
ladanyi@1606
   204
      node_num++;
ladanyi@1606
   205
    }
ladanyi@1606
   206
    const double pi = 3.142;
ladanyi@1606
   207
    double step = 2 * pi / (double) node_num;
ladanyi@1606
   208
    int i = 0;
ladanyi@1606
   209
    for (NodeIt n(graph); n != INVALID; ++n)
ladanyi@1606
   210
    {
ladanyi@1606
   211
      nodemap_storage["coordinates_x"]->set(n, 250.0 * cos(i * step));
ladanyi@1606
   212
      nodemap_storage["coordinates_y"]->set(n, 250.0 * sin(i * step));
ladanyi@1606
   213
      i++;
ladanyi@1606
   214
    }
ladanyi@1606
   215
  }
ladanyi@1645
   216
ladanyi@1645
   217
  // fill in the default values for the maps
ladanyi@1645
   218
  for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
ladanyi@1645
   219
      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
ladanyi@1645
   220
  {
ladanyi@1645
   221
    if ((it->first != "id") &&
ladanyi@1645
   222
        (it->first != "coordiantes_x") &&
ladanyi@1645
   223
        (it->first != "coordinates_y"))
ladanyi@1645
   224
    {
ladanyi@1645
   225
      nodemap_default[it->first] = 0.0;
ladanyi@1645
   226
    }
ladanyi@1646
   227
    else if (it->first == "id")
ladanyi@1646
   228
    {
ladanyi@1646
   229
      NodeIt n(graph);
ladanyi@1646
   230
      double max = (*nodemap_storage["id"])[n];
ladanyi@1646
   231
      for (; n != INVALID; ++n)
ladanyi@1646
   232
      {
ladanyi@1646
   233
        if ((*nodemap_storage["id"])[n] > max)
ladanyi@1646
   234
          max = (*nodemap_storage["id"])[n];
ladanyi@1646
   235
      }
ladanyi@1646
   236
      nodemap_default["id"] = max + 1.0;
ladanyi@1646
   237
    }
ladanyi@1645
   238
  }
ladanyi@1645
   239
  for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
ladanyi@1645
   240
      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
ladanyi@1645
   241
  {
ladanyi@1645
   242
    if (it->first != "id")
ladanyi@1645
   243
    {
ladanyi@1645
   244
      edgemap_default[it->first] = 0.0;
ladanyi@1645
   245
    }
ladanyi@1646
   246
    else
ladanyi@1646
   247
    {
ladanyi@1649
   248
      double max = std::numeric_limits<double>::min();
ladanyi@1649
   249
      for (EdgeIt e(graph); e != INVALID; ++e)
ladanyi@1646
   250
      {
ladanyi@1646
   251
        if ((*edgemap_storage["id"])[e] > max)
ladanyi@1646
   252
          max = (*edgemap_storage["id"])[e];
ladanyi@1646
   253
      }
ladanyi@1649
   254
      if (max > std::numeric_limits<double>::min())
ladanyi@1649
   255
        edgemap_default["id"] = max + 1.0;
ladanyi@1649
   256
      else
ladanyi@1649
   257
        edgemap_default["id"] = 1.0;
ladanyi@1646
   258
    }
ladanyi@1645
   259
  }
ladanyi@1645
   260
ladanyi@1645
   261
  return 0;
ladanyi@1606
   262
}
ladanyi@1606
   263
ladanyi@1606
   264
void MapStorage::writeToFile(const std::string &filename)
ladanyi@1606
   265
{
ladanyi@1606
   266
  GraphWriter<Graph> gwriter(filename, graph);
ladanyi@1606
   267
ladanyi@1606
   268
  for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
ladanyi@1606
   269
      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
ladanyi@1606
   270
  {
ladanyi@1606
   271
    gwriter.writeNodeMap(it->first, *(it->second));
ladanyi@1606
   272
    //std::cout << "wrote " << it->first << " nodemap" << std::endl;
ladanyi@1606
   273
  }
ladanyi@1606
   274
  for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
ladanyi@1606
   275
      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
ladanyi@1606
   276
  {
ladanyi@1606
   277
    gwriter.writeEdgeMap(it->first, *(it->second));
ladanyi@1606
   278
    //std::cout << "wrote " << it->first << " edgemap" << std::endl;
ladanyi@1606
   279
  }
ladanyi@1606
   280
  gwriter.run();
ladanyi@1606
   281
}
ladanyi@1606
   282
ladanyi@1606
   283
void MapStorage::clear()
ladanyi@1606
   284
{
ladanyi@1606
   285
  for (std::map<std::string, Graph::NodeMap<double>*>::iterator it =
ladanyi@1606
   286
      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
ladanyi@1606
   287
  {
ladanyi@1606
   288
    if ((it->first != "coordinates_x") &&
ladanyi@1606
   289
        (it->first != "coordinates_y") &&
ladanyi@1606
   290
        (it->first != "id"))
ladanyi@1606
   291
    {
ladanyi@1606
   292
      delete it->second;
ladanyi@1606
   293
      nodemap_storage.erase(it);
ladanyi@1606
   294
    }
ladanyi@1606
   295
  }
ladanyi@1606
   296
  for (std::map<std::string, Graph::EdgeMap<double>*>::iterator it =
ladanyi@1606
   297
      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
ladanyi@1606
   298
  {
ladanyi@1606
   299
    if (it->first != "id")
ladanyi@1606
   300
    {
ladanyi@1606
   301
      delete it->second;
ladanyi@1606
   302
      edgemap_storage.erase(it);
ladanyi@1606
   303
    }
ladanyi@1606
   304
  }
ladanyi@1645
   305
  for (std::map<std::string, double>::iterator it =
ladanyi@1645
   306
      nodemap_default.begin(); it != nodemap_default.end(); ++it)
ladanyi@1645
   307
  {
ladanyi@1646
   308
    if (it->first != "id")
ladanyi@1646
   309
      nodemap_default.erase(it);
ladanyi@1645
   310
  }
ladanyi@1645
   311
  for (std::map<std::string, double>::iterator it =
ladanyi@1645
   312
      edgemap_default.begin(); it != edgemap_default.end(); ++it)
ladanyi@1645
   313
  {
ladanyi@1646
   314
    if (it->first != "id")
ladanyi@1646
   315
      edgemap_default.erase(it);
ladanyi@1645
   316
  }
ladanyi@1606
   317
  graph.clear();
ladanyi@1606
   318
  file_name = "";
ladanyi@1606
   319
  modified = false;
ladanyi@1606
   320
}