gui/mapstorage.cc
author alpar
Fri, 02 Dec 2005 10:02:40 +0000
changeset 1843 1e386f4047c9
parent 1708 8fea0e60f855
child 1860 27a9a75b957b
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
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;
hegyi@1837
    19
hegyi@1837
    20
  active_nodemaps.resize(NODE_PROPERTY_NUM);
hegyi@1837
    21
  for(int i=0;i<NODE_PROPERTY_NUM;i++)
hegyi@1837
    22
    {
hegyi@1837
    23
      active_nodemaps[i]="";
hegyi@1837
    24
    }
hegyi@1837
    25
hegyi@1837
    26
  active_edgemaps.resize(EDGE_PROPERTY_NUM);
hegyi@1837
    27
  for(int i=0;i<EDGE_PROPERTY_NUM;i++)
hegyi@1837
    28
    {
hegyi@1837
    29
      active_edgemaps[i]="";
hegyi@1837
    30
    }
ladanyi@1606
    31
}
ladanyi@1606
    32
ladanyi@1606
    33
MapStorage::~MapStorage()
ladanyi@1606
    34
{
ladanyi@1606
    35
  for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
ladanyi@1606
    36
      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
ladanyi@1606
    37
  {
ladanyi@1606
    38
    delete it->second;
ladanyi@1606
    39
  }
ladanyi@1606
    40
  for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
ladanyi@1606
    41
      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
ladanyi@1606
    42
  {
ladanyi@1606
    43
    delete it->second;
ladanyi@1606
    44
  }
ladanyi@1606
    45
}
ladanyi@1442
    46
ladanyi@1645
    47
int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap<double> *nodemap, double default_value = 0.0)
ladanyi@1442
    48
{
hegyi@1597
    49
  if( nodemap_storage.find(name) == nodemap_storage.end() )
hegyi@1597
    50
    {
hegyi@1597
    51
      nodemap_storage[name]=nodemap;
ladanyi@1645
    52
      // set the maps default value
ladanyi@1645
    53
      nodemap_default[name] = default_value;
hegyi@1597
    54
      return 0;
hegyi@1597
    55
    }
hegyi@1597
    56
  return 1;
ladanyi@1442
    57
}
ladanyi@1442
    58
hegyi@1837
    59
void MapStorage::changeActiveMap(bool itisedge, int prop, std::string mapname)
hegyi@1837
    60
{
hegyi@1837
    61
  if(itisedge)
hegyi@1837
    62
    {
hegyi@1837
    63
      active_edgemaps[prop]=mapname;
hegyi@1837
    64
    }
hegyi@1837
    65
  else
hegyi@1837
    66
    {
hegyi@1837
    67
      active_nodemaps[prop]=mapname;
hegyi@1837
    68
    }
hegyi@1837
    69
  signal_prop.emit(itisedge, prop);
hegyi@1837
    70
}
hegyi@1837
    71
hegyi@1837
    72
std::string MapStorage::getActiveEdgeMap(int prop)
hegyi@1837
    73
{
hegyi@1837
    74
  return active_edgemaps[prop];
hegyi@1837
    75
}
hegyi@1837
    76
hegyi@1837
    77
std::string MapStorage::getActiveNodeMap(int prop)
hegyi@1837
    78
{
hegyi@1837
    79
  return active_nodemaps[prop];
hegyi@1837
    80
}
hegyi@1837
    81
hegyi@1837
    82
std::vector<std::string> MapStorage::getEdgeMapList()
hegyi@1837
    83
{
hegyi@1837
    84
  std::vector<std::string> eml;
hegyi@1837
    85
  eml.resize(edgemap_storage.size());
hegyi@1837
    86
  int i=0;
hegyi@1837
    87
  std::map< std::string,Graph::EdgeMap<double> * >::iterator emsi=beginOfEdgeMaps();
hegyi@1837
    88
  for(;emsi!=endOfEdgeMaps();emsi++)
hegyi@1837
    89
    {
hegyi@1837
    90
      eml[i]=(emsi->first);
hegyi@1837
    91
      i++;
hegyi@1837
    92
    }
hegyi@1837
    93
  return eml;
hegyi@1837
    94
}
hegyi@1837
    95
hegyi@1837
    96
std::vector<std::string> MapStorage::getNodeMapList()
hegyi@1837
    97
{
hegyi@1837
    98
  std::vector<std::string> nml;
hegyi@1837
    99
  nml.resize(nodemap_storage.size());
hegyi@1837
   100
  int i=0;
hegyi@1837
   101
  std::map< std::string,Graph::NodeMap<double> * >::iterator nmsi=beginOfNodeMaps();
hegyi@1837
   102
  for(;nmsi!=endOfNodeMaps();nmsi++)
hegyi@1837
   103
    {
hegyi@1837
   104
      nml[i]=(nmsi->first);
hegyi@1837
   105
      i++;
hegyi@1837
   106
    }
hegyi@1837
   107
  return nml;
hegyi@1837
   108
}
hegyi@1837
   109
hegyi@1837
   110
MapStorage::Signal_Prop MapStorage::signal_prop_ch()
hegyi@1837
   111
{
hegyi@1837
   112
  return signal_prop;
hegyi@1837
   113
}
hegyi@1837
   114
ladanyi@1645
   115
int MapStorage::addEdgeMap(const std::string & name, Graph::EdgeMap<double> *edgemap, double default_value = 0.0)
ladanyi@1442
   116
{
hegyi@1597
   117
  if( edgemap_storage.find(name) == edgemap_storage.end() )
hegyi@1597
   118
    {
hegyi@1597
   119
      edgemap_storage[name]=edgemap;
ladanyi@1645
   120
      // set the maps default value
ladanyi@1645
   121
      edgemap_default[name] = default_value;
hegyi@1597
   122
      return 0;
hegyi@1597
   123
    }
hegyi@1597
   124
  return 1;
ladanyi@1442
   125
}
ladanyi@1442
   126
ladanyi@1442
   127
double MapStorage::maxOfNodeMap(const std::string & name)
ladanyi@1442
   128
{
ladanyi@1442
   129
  double max=0;
ladanyi@1606
   130
  for (NodeIt j(graph); j!=INVALID; ++j)
ladanyi@1442
   131
  {
ladanyi@1442
   132
    if( (*nodemap_storage[name])[j]>max )
ladanyi@1442
   133
    {
ladanyi@1442
   134
      max=(*nodemap_storage[name])[j];
ladanyi@1442
   135
    }
ladanyi@1442
   136
  }
ladanyi@1442
   137
  return max;
ladanyi@1442
   138
}
ladanyi@1442
   139
ladanyi@1442
   140
double MapStorage::maxOfEdgeMap(const std::string & name)
ladanyi@1442
   141
{
ladanyi@1442
   142
  double max=0;
ladanyi@1606
   143
  for (EdgeIt j(graph); j!=INVALID; ++j)
ladanyi@1442
   144
  {
ladanyi@1442
   145
    if( (*edgemap_storage[name])[j]>max )
ladanyi@1442
   146
    {
ladanyi@1442
   147
      max=(*edgemap_storage[name])[j];
ladanyi@1442
   148
    }
ladanyi@1442
   149
  }
ladanyi@1442
   150
  return max;
ladanyi@1442
   151
}
ladanyi@1442
   152
ladanyi@1442
   153
double MapStorage::minOfNodeMap(const std::string & name)
ladanyi@1442
   154
{
ladanyi@1606
   155
  NodeIt j(graph);
hegyi@1617
   156
  double min;
hegyi@1617
   157
  if(j!=INVALID)
hegyi@1617
   158
    {
hegyi@1617
   159
      min=(*nodemap_storage[name])[j];
hegyi@1617
   160
    }
hegyi@1617
   161
  else
hegyi@1617
   162
    {
hegyi@1617
   163
      min=0;
hegyi@1617
   164
    }
ladanyi@1442
   165
  for (; j!=INVALID; ++j)
ladanyi@1442
   166
  {
ladanyi@1442
   167
    if( (*nodemap_storage[name])[j]<min )
ladanyi@1442
   168
    {
ladanyi@1442
   169
      min=(*nodemap_storage[name])[j];
ladanyi@1442
   170
    }
ladanyi@1442
   171
  }
ladanyi@1442
   172
  return min;
ladanyi@1442
   173
}
ladanyi@1442
   174
ladanyi@1442
   175
double MapStorage::minOfEdgeMap(const std::string & name)
ladanyi@1442
   176
{
ladanyi@1606
   177
  EdgeIt j(graph);
hegyi@1617
   178
  double min;
hegyi@1617
   179
  if(j!=INVALID)
hegyi@1617
   180
    {
hegyi@1617
   181
      min=(*edgemap_storage[name])[j];
hegyi@1617
   182
    }
hegyi@1617
   183
  else
hegyi@1617
   184
    {
hegyi@1617
   185
      min=0;
hegyi@1617
   186
    }
ladanyi@1606
   187
  for (EdgeIt j(graph); j!=INVALID; ++j)
ladanyi@1442
   188
  {
ladanyi@1442
   189
    if( (*edgemap_storage[name])[j]<min )
ladanyi@1442
   190
    {
ladanyi@1442
   191
      min=(*edgemap_storage[name])[j];
ladanyi@1442
   192
    }
ladanyi@1442
   193
  }
ladanyi@1442
   194
  return min;
ladanyi@1442
   195
}
ladanyi@1442
   196
ladanyi@1645
   197
int MapStorage::readFromFile(const std::string &filename)
ladanyi@1606
   198
{
ladanyi@1606
   199
  bool read_x = false;
ladanyi@1606
   200
  bool read_y = false;
ladanyi@1646
   201
  bool read_edge_id = false;
ladanyi@1606
   202
ladanyi@1606
   203
  try {
ladanyi@1606
   204
    LemonReader lreader(filename);
ladanyi@1606
   205
    ContentReader content(lreader);
ladanyi@1606
   206
    lreader.run();
ladanyi@1606
   207
ladanyi@1606
   208
    const std::vector<std::string>& nodeMapNames = content.nodeSetMaps(0);
ladanyi@1606
   209
    const std::vector<std::string>& edgeMapNames = content.edgeSetMaps(0);
ladanyi@1606
   210
ladanyi@1606
   211
    GraphReader<Graph> greader(filename, graph);
ladanyi@1606
   212
    for (std::vector<std::string>::const_iterator it = nodeMapNames.begin();
ladanyi@1606
   213
        it != nodeMapNames.end(); ++it)
ladanyi@1606
   214
    {
ladanyi@1606
   215
      if (*it == "coordinates_x")
ladanyi@1606
   216
      {
ladanyi@1606
   217
        read_x = true;
ladanyi@1606
   218
        //std::cout << "read X nodemap" << std::endl;
ladanyi@1606
   219
      }
ladanyi@1606
   220
      else if (*it == "coordinates_y")
ladanyi@1606
   221
      {
ladanyi@1606
   222
        read_y = true;
ladanyi@1606
   223
        //std::cout << "read Y nodemap" << std::endl;
ladanyi@1606
   224
      }
ladanyi@1606
   225
      else if (*it == "id")
ladanyi@1606
   226
      {
ladanyi@1606
   227
        //std::cout << "read id nodemap" << std::endl;
ladanyi@1606
   228
      }
ladanyi@1606
   229
      else
ladanyi@1606
   230
      {
ladanyi@1606
   231
        nodemap_storage[*it] = new Graph::NodeMap<double>(graph);
ladanyi@1606
   232
        //std::cout << "read " << *it << " nodemap" << std::endl;
ladanyi@1606
   233
      }
ladanyi@1606
   234
      greader.readNodeMap(*it, *nodemap_storage[*it]);
ladanyi@1606
   235
    }
ladanyi@1606
   236
    for (std::vector<std::string>::const_iterator it = edgeMapNames.begin();
ladanyi@1606
   237
        it != edgeMapNames.end(); ++it)
ladanyi@1606
   238
    {
ladanyi@1606
   239
      if (*it == "id")
ladanyi@1606
   240
      {
ladanyi@1606
   241
        //std::cout << "read id edgemap" << std::endl;
ladanyi@1606
   242
      }
ladanyi@1606
   243
      else
ladanyi@1606
   244
      {
ladanyi@1606
   245
        edgemap_storage[*it] = new Graph::EdgeMap<double>(graph);
ladanyi@1606
   246
        //std::cout << "read " << *it << " edgemap" << std::endl;
ladanyi@1606
   247
      }
ladanyi@1606
   248
      greader.readEdgeMap(*it, *edgemap_storage[*it]);
ladanyi@1606
   249
    }
ladanyi@1606
   250
    greader.run();
ladanyi@1606
   251
  } catch (DataFormatError& error) {
ladanyi@1708
   252
    Gtk::MessageDialog mdialog(error.what());
ladanyi@1606
   253
    mdialog.run();
ladanyi@1645
   254
    clear();
ladanyi@1645
   255
    return 1;
ladanyi@1606
   256
  }
ladanyi@1606
   257
ladanyi@1646
   258
  if (!read_edge_id)
ladanyi@1646
   259
  {
ladanyi@1646
   260
    edgemap_storage["id"] = new Graph::EdgeMap<double>(graph);
ladanyi@1646
   261
    int i = 1;
ladanyi@1646
   262
    for (EdgeIt e(graph); e != INVALID; ++e)
ladanyi@1646
   263
    {
ladanyi@1646
   264
      (*edgemap_storage["id"])[e] = i++;
ladanyi@1646
   265
    }
ladanyi@1646
   266
  }
ladanyi@1646
   267
ladanyi@1606
   268
  if (!read_x || !read_y)
ladanyi@1606
   269
  {
ladanyi@1606
   270
    int node_num = 0;
ladanyi@1606
   271
    for (NodeIt n(graph); n != INVALID; ++n)
ladanyi@1606
   272
    {
ladanyi@1606
   273
      node_num++;
ladanyi@1606
   274
    }
ladanyi@1606
   275
    const double pi = 3.142;
ladanyi@1606
   276
    double step = 2 * pi / (double) node_num;
ladanyi@1606
   277
    int i = 0;
ladanyi@1606
   278
    for (NodeIt n(graph); n != INVALID; ++n)
ladanyi@1606
   279
    {
deba@1686
   280
      nodemap_storage["coordinates_x"]->set(n, 250.0 * std::cos(i * step));
deba@1686
   281
      nodemap_storage["coordinates_y"]->set(n, 250.0 * std::sin(i * step));
ladanyi@1606
   282
      i++;
ladanyi@1606
   283
    }
ladanyi@1606
   284
  }
ladanyi@1645
   285
ladanyi@1645
   286
  // fill in the default values for the maps
ladanyi@1645
   287
  for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
ladanyi@1645
   288
      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
ladanyi@1645
   289
  {
ladanyi@1645
   290
    if ((it->first != "id") &&
ladanyi@1645
   291
        (it->first != "coordiantes_x") &&
ladanyi@1645
   292
        (it->first != "coordinates_y"))
ladanyi@1645
   293
    {
ladanyi@1645
   294
      nodemap_default[it->first] = 0.0;
ladanyi@1645
   295
    }
ladanyi@1646
   296
    else if (it->first == "id")
ladanyi@1646
   297
    {
ladanyi@1646
   298
      NodeIt n(graph);
ladanyi@1646
   299
      double max = (*nodemap_storage["id"])[n];
ladanyi@1646
   300
      for (; n != INVALID; ++n)
ladanyi@1646
   301
      {
ladanyi@1646
   302
        if ((*nodemap_storage["id"])[n] > max)
ladanyi@1646
   303
          max = (*nodemap_storage["id"])[n];
ladanyi@1646
   304
      }
ladanyi@1646
   305
      nodemap_default["id"] = max + 1.0;
ladanyi@1646
   306
    }
ladanyi@1645
   307
  }
ladanyi@1645
   308
  for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
ladanyi@1645
   309
      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
ladanyi@1645
   310
  {
ladanyi@1645
   311
    if (it->first != "id")
ladanyi@1645
   312
    {
ladanyi@1645
   313
      edgemap_default[it->first] = 0.0;
ladanyi@1645
   314
    }
ladanyi@1646
   315
    else
ladanyi@1646
   316
    {
ladanyi@1649
   317
      double max = std::numeric_limits<double>::min();
ladanyi@1649
   318
      for (EdgeIt e(graph); e != INVALID; ++e)
ladanyi@1646
   319
      {
ladanyi@1646
   320
        if ((*edgemap_storage["id"])[e] > max)
ladanyi@1646
   321
          max = (*edgemap_storage["id"])[e];
ladanyi@1646
   322
      }
ladanyi@1649
   323
      if (max > std::numeric_limits<double>::min())
ladanyi@1649
   324
        edgemap_default["id"] = max + 1.0;
ladanyi@1649
   325
      else
ladanyi@1649
   326
        edgemap_default["id"] = 1.0;
ladanyi@1646
   327
    }
ladanyi@1645
   328
  }
ladanyi@1645
   329
ladanyi@1645
   330
  return 0;
ladanyi@1606
   331
}
ladanyi@1606
   332
ladanyi@1606
   333
void MapStorage::writeToFile(const std::string &filename)
ladanyi@1606
   334
{
ladanyi@1606
   335
  GraphWriter<Graph> gwriter(filename, graph);
ladanyi@1606
   336
ladanyi@1606
   337
  for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
ladanyi@1606
   338
      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
ladanyi@1606
   339
  {
ladanyi@1606
   340
    gwriter.writeNodeMap(it->first, *(it->second));
ladanyi@1606
   341
    //std::cout << "wrote " << it->first << " nodemap" << std::endl;
ladanyi@1606
   342
  }
ladanyi@1606
   343
  for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
ladanyi@1606
   344
      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
ladanyi@1606
   345
  {
ladanyi@1606
   346
    gwriter.writeEdgeMap(it->first, *(it->second));
ladanyi@1606
   347
    //std::cout << "wrote " << it->first << " edgemap" << std::endl;
ladanyi@1606
   348
  }
ladanyi@1606
   349
  gwriter.run();
ladanyi@1606
   350
}
ladanyi@1606
   351
ladanyi@1606
   352
void MapStorage::clear()
ladanyi@1606
   353
{
ladanyi@1606
   354
  for (std::map<std::string, Graph::NodeMap<double>*>::iterator it =
ladanyi@1606
   355
      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
ladanyi@1606
   356
  {
ladanyi@1606
   357
    if ((it->first != "coordinates_x") &&
ladanyi@1606
   358
        (it->first != "coordinates_y") &&
ladanyi@1606
   359
        (it->first != "id"))
ladanyi@1606
   360
    {
ladanyi@1606
   361
      delete it->second;
ladanyi@1606
   362
      nodemap_storage.erase(it);
ladanyi@1606
   363
    }
ladanyi@1606
   364
  }
ladanyi@1606
   365
  for (std::map<std::string, Graph::EdgeMap<double>*>::iterator it =
ladanyi@1606
   366
      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
ladanyi@1606
   367
  {
ladanyi@1606
   368
    if (it->first != "id")
ladanyi@1606
   369
    {
ladanyi@1606
   370
      delete it->second;
ladanyi@1606
   371
      edgemap_storage.erase(it);
ladanyi@1606
   372
    }
ladanyi@1606
   373
  }
ladanyi@1645
   374
  for (std::map<std::string, double>::iterator it =
ladanyi@1645
   375
      nodemap_default.begin(); it != nodemap_default.end(); ++it)
ladanyi@1645
   376
  {
ladanyi@1646
   377
    if (it->first != "id")
ladanyi@1646
   378
      nodemap_default.erase(it);
ladanyi@1645
   379
  }
ladanyi@1645
   380
  for (std::map<std::string, double>::iterator it =
ladanyi@1645
   381
      edgemap_default.begin(); it != edgemap_default.end(); ++it)
ladanyi@1645
   382
  {
ladanyi@1646
   383
    if (it->first != "id")
ladanyi@1646
   384
      edgemap_default.erase(it);
ladanyi@1645
   385
  }
ladanyi@1606
   386
  graph.clear();
ladanyi@1606
   387
  file_name = "";
ladanyi@1606
   388
  modified = false;
ladanyi@1606
   389
}