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