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