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