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