gui/mapstorage.cc
author alpar
Thu, 18 Aug 2005 13:33:49 +0000
changeset 1643 9285f3777553
parent 1634 910b1bcb7d05
child 1645 4a04bb856ac7
permissions -rw-r--r--
- The number of gcc-4.0 warnings has significantly decreases.
- Some code clean-up in gui
ladanyi@1606
     1
#include "mapstorage.h"
ladanyi@1634
     2
#include <cmath>
ladanyi@1442
     3
ladanyi@1606
     4
MapStorage::MapStorage() : modified(false), file_name("")
ladanyi@1442
     5
{
ladanyi@1606
     6
  nodemap_storage["coordinates_x"] = new Graph::NodeMap<double>(graph);
ladanyi@1606
     7
  coords.setXMap(*nodemap_storage["coordinates_x"]);
ladanyi@1606
     8
  nodemap_storage["coordinates_y"] = new Graph::NodeMap<double>(graph);
ladanyi@1606
     9
  coords.setYMap(*nodemap_storage["coordinates_y"]);
ladanyi@1606
    10
ladanyi@1606
    11
  nodemap_storage["id"] = new Graph::NodeMap<double>(graph);
ladanyi@1606
    12
  edgemap_storage["id"] = new Graph::EdgeMap<double>(graph);
ladanyi@1606
    13
}
ladanyi@1606
    14
ladanyi@1606
    15
MapStorage::~MapStorage()
ladanyi@1606
    16
{
ladanyi@1606
    17
  for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
ladanyi@1606
    18
      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
ladanyi@1606
    19
  {
ladanyi@1606
    20
    delete it->second;
ladanyi@1606
    21
  }
ladanyi@1606
    22
  for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
ladanyi@1606
    23
      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
ladanyi@1606
    24
  {
ladanyi@1606
    25
    delete it->second;
ladanyi@1606
    26
  }
ladanyi@1606
    27
}
ladanyi@1442
    28
ladanyi@1442
    29
int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap<double> *nodemap)
ladanyi@1442
    30
{
hegyi@1597
    31
  if( nodemap_storage.find(name) == nodemap_storage.end() )
hegyi@1597
    32
    {
hegyi@1597
    33
      nodemap_storage[name]=nodemap;
hegyi@1597
    34
      return 0;
hegyi@1597
    35
    }
hegyi@1597
    36
  return 1;
ladanyi@1442
    37
}
ladanyi@1442
    38
ladanyi@1442
    39
int MapStorage::addEdgeMap(const std::string & name, Graph::EdgeMap<double> *edgemap)
ladanyi@1442
    40
{
hegyi@1597
    41
  if( edgemap_storage.find(name) == edgemap_storage.end() )
hegyi@1597
    42
    {
hegyi@1597
    43
      edgemap_storage[name]=edgemap;
hegyi@1597
    44
      return 0;
hegyi@1597
    45
    }
hegyi@1597
    46
  return 1;
ladanyi@1442
    47
}
ladanyi@1442
    48
ladanyi@1442
    49
double MapStorage::maxOfNodeMap(const std::string & name)
ladanyi@1442
    50
{
ladanyi@1442
    51
  double max=0;
ladanyi@1606
    52
  for (NodeIt j(graph); j!=INVALID; ++j)
ladanyi@1442
    53
  {
ladanyi@1442
    54
    if( (*nodemap_storage[name])[j]>max )
ladanyi@1442
    55
    {
ladanyi@1442
    56
      max=(*nodemap_storage[name])[j];
ladanyi@1442
    57
    }
ladanyi@1442
    58
  }
ladanyi@1442
    59
  return max;
ladanyi@1442
    60
}
ladanyi@1442
    61
ladanyi@1442
    62
double MapStorage::maxOfEdgeMap(const std::string & name)
ladanyi@1442
    63
{
ladanyi@1442
    64
  double max=0;
ladanyi@1606
    65
  for (EdgeIt j(graph); j!=INVALID; ++j)
ladanyi@1442
    66
  {
ladanyi@1442
    67
    if( (*edgemap_storage[name])[j]>max )
ladanyi@1442
    68
    {
ladanyi@1442
    69
      max=(*edgemap_storage[name])[j];
ladanyi@1442
    70
    }
ladanyi@1442
    71
  }
ladanyi@1442
    72
  return max;
ladanyi@1442
    73
}
ladanyi@1442
    74
ladanyi@1442
    75
double MapStorage::minOfNodeMap(const std::string & name)
ladanyi@1442
    76
{
ladanyi@1606
    77
  NodeIt j(graph);
hegyi@1617
    78
  double min;
hegyi@1617
    79
  if(j!=INVALID)
hegyi@1617
    80
    {
hegyi@1617
    81
      min=(*nodemap_storage[name])[j];
hegyi@1617
    82
    }
hegyi@1617
    83
  else
hegyi@1617
    84
    {
hegyi@1617
    85
      min=0;
hegyi@1617
    86
    }
ladanyi@1442
    87
  for (; j!=INVALID; ++j)
ladanyi@1442
    88
  {
ladanyi@1442
    89
    if( (*nodemap_storage[name])[j]<min )
ladanyi@1442
    90
    {
ladanyi@1442
    91
      min=(*nodemap_storage[name])[j];
ladanyi@1442
    92
    }
ladanyi@1442
    93
  }
ladanyi@1442
    94
  return min;
ladanyi@1442
    95
}
ladanyi@1442
    96
ladanyi@1442
    97
double MapStorage::minOfEdgeMap(const std::string & name)
ladanyi@1442
    98
{
ladanyi@1606
    99
  EdgeIt j(graph);
hegyi@1617
   100
  double min;
hegyi@1617
   101
  if(j!=INVALID)
hegyi@1617
   102
    {
hegyi@1617
   103
      min=(*edgemap_storage[name])[j];
hegyi@1617
   104
    }
hegyi@1617
   105
  else
hegyi@1617
   106
    {
hegyi@1617
   107
      min=0;
hegyi@1617
   108
    }
ladanyi@1606
   109
  for (EdgeIt j(graph); j!=INVALID; ++j)
ladanyi@1442
   110
  {
ladanyi@1442
   111
    if( (*edgemap_storage[name])[j]<min )
ladanyi@1442
   112
    {
ladanyi@1442
   113
      min=(*edgemap_storage[name])[j];
ladanyi@1442
   114
    }
ladanyi@1442
   115
  }
ladanyi@1442
   116
  return min;
ladanyi@1442
   117
}
ladanyi@1442
   118
alpar@1643
   119
void MapStorage::initMapsForEdge(Edge e)
hegyi@1509
   120
{
hegyi@1525
   121
  std::map< std::string,Graph::EdgeMap<double> * >::iterator ems_it;
hegyi@1525
   122
  for(ems_it=edgemap_storage.begin();ems_it!=edgemap_storage.end();ems_it++)
hegyi@1525
   123
    {
hegyi@1525
   124
      (*((*ems_it).second))[e]=5;
hegyi@1525
   125
    }
hegyi@1509
   126
}
ladanyi@1606
   127
ladanyi@1606
   128
void MapStorage::readFromFile(const std::string &filename)
ladanyi@1606
   129
{
ladanyi@1606
   130
  bool read_x = false;
ladanyi@1606
   131
  bool read_y = false;
ladanyi@1606
   132
ladanyi@1606
   133
  try {
ladanyi@1606
   134
    LemonReader lreader(filename);
ladanyi@1606
   135
    ContentReader content(lreader);
ladanyi@1606
   136
    lreader.run();
ladanyi@1606
   137
ladanyi@1606
   138
    const std::vector<std::string>& nodeMapNames = content.nodeSetMaps(0);
ladanyi@1606
   139
    const std::vector<std::string>& edgeMapNames = content.edgeSetMaps(0);
ladanyi@1606
   140
ladanyi@1606
   141
    GraphReader<Graph> greader(filename, graph);
ladanyi@1606
   142
    for (std::vector<std::string>::const_iterator it = nodeMapNames.begin();
ladanyi@1606
   143
        it != nodeMapNames.end(); ++it)
ladanyi@1606
   144
    {
ladanyi@1606
   145
      if (*it == "coordinates_x")
ladanyi@1606
   146
      {
ladanyi@1606
   147
        read_x = true;
ladanyi@1606
   148
        //std::cout << "read X nodemap" << std::endl;
ladanyi@1606
   149
      }
ladanyi@1606
   150
      else if (*it == "coordinates_y")
ladanyi@1606
   151
      {
ladanyi@1606
   152
        read_y = true;
ladanyi@1606
   153
        //std::cout << "read Y nodemap" << std::endl;
ladanyi@1606
   154
      }
ladanyi@1606
   155
      else if (*it == "id")
ladanyi@1606
   156
      {
ladanyi@1606
   157
        //std::cout << "read id nodemap" << std::endl;
ladanyi@1606
   158
      }
ladanyi@1606
   159
      else
ladanyi@1606
   160
      {
ladanyi@1606
   161
        nodemap_storage[*it] = new Graph::NodeMap<double>(graph);
ladanyi@1606
   162
        //std::cout << "read " << *it << " nodemap" << std::endl;
ladanyi@1606
   163
      }
ladanyi@1606
   164
      greader.readNodeMap(*it, *nodemap_storage[*it]);
ladanyi@1606
   165
    }
ladanyi@1606
   166
    for (std::vector<std::string>::const_iterator it = edgeMapNames.begin();
ladanyi@1606
   167
        it != edgeMapNames.end(); ++it)
ladanyi@1606
   168
    {
ladanyi@1606
   169
      if (*it == "id")
ladanyi@1606
   170
      {
ladanyi@1606
   171
        //std::cout << "read id edgemap" << std::endl;
ladanyi@1606
   172
      }
ladanyi@1606
   173
      else
ladanyi@1606
   174
      {
ladanyi@1606
   175
        edgemap_storage[*it] = new Graph::EdgeMap<double>(graph);
ladanyi@1606
   176
        //std::cout << "read " << *it << " edgemap" << std::endl;
ladanyi@1606
   177
      }
ladanyi@1606
   178
      greader.readEdgeMap(*it, *edgemap_storage[*it]);
ladanyi@1606
   179
    }
ladanyi@1606
   180
    greader.run();
ladanyi@1606
   181
  } catch (DataFormatError& error) {
ladanyi@1606
   182
    /*
ladanyi@1606
   183
    Gtk::MessageDialog mdialog("Read Error");
ladanyi@1606
   184
    mdialog.set_message(error.what());
ladanyi@1606
   185
    mdialog.run();
ladanyi@1606
   186
    */
ladanyi@1606
   187
    // reset graph and mapstorage ?
ladanyi@1606
   188
    return;
ladanyi@1606
   189
  }
ladanyi@1606
   190
ladanyi@1606
   191
  if (!read_x || !read_y)
ladanyi@1606
   192
  {
ladanyi@1606
   193
    int node_num = 0;
ladanyi@1606
   194
    for (NodeIt n(graph); n != INVALID; ++n)
ladanyi@1606
   195
    {
ladanyi@1606
   196
      node_num++;
ladanyi@1606
   197
    }
ladanyi@1606
   198
    const double pi = 3.142;
ladanyi@1606
   199
    double step = 2 * pi / (double) node_num;
ladanyi@1606
   200
    int i = 0;
ladanyi@1606
   201
    for (NodeIt n(graph); n != INVALID; ++n)
ladanyi@1606
   202
    {
ladanyi@1606
   203
      nodemap_storage["coordinates_x"]->set(n, 250.0 * cos(i * step));
ladanyi@1606
   204
      nodemap_storage["coordinates_y"]->set(n, 250.0 * sin(i * step));
ladanyi@1606
   205
      i++;
ladanyi@1606
   206
    }
ladanyi@1606
   207
  }
ladanyi@1606
   208
}
ladanyi@1606
   209
ladanyi@1606
   210
void MapStorage::writeToFile(const std::string &filename)
ladanyi@1606
   211
{
ladanyi@1606
   212
  GraphWriter<Graph> gwriter(filename, graph);
ladanyi@1606
   213
ladanyi@1606
   214
  for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
ladanyi@1606
   215
      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
ladanyi@1606
   216
  {
ladanyi@1606
   217
    gwriter.writeNodeMap(it->first, *(it->second));
ladanyi@1606
   218
    //std::cout << "wrote " << it->first << " nodemap" << std::endl;
ladanyi@1606
   219
  }
ladanyi@1606
   220
  for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
ladanyi@1606
   221
      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
ladanyi@1606
   222
  {
ladanyi@1606
   223
    gwriter.writeEdgeMap(it->first, *(it->second));
ladanyi@1606
   224
    //std::cout << "wrote " << it->first << " edgemap" << std::endl;
ladanyi@1606
   225
  }
ladanyi@1606
   226
  gwriter.run();
ladanyi@1606
   227
}
ladanyi@1606
   228
ladanyi@1606
   229
void MapStorage::clear()
ladanyi@1606
   230
{
ladanyi@1606
   231
  for (std::map<std::string, Graph::NodeMap<double>*>::iterator it =
ladanyi@1606
   232
      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
ladanyi@1606
   233
  {
ladanyi@1606
   234
    if ((it->first != "coordinates_x") &&
ladanyi@1606
   235
        (it->first != "coordinates_y") &&
ladanyi@1606
   236
        (it->first != "id"))
ladanyi@1606
   237
    {
ladanyi@1606
   238
      delete it->second;
ladanyi@1606
   239
      nodemap_storage.erase(it);
ladanyi@1606
   240
    }
ladanyi@1606
   241
  }
ladanyi@1606
   242
  for (std::map<std::string, Graph::EdgeMap<double>*>::iterator it =
ladanyi@1606
   243
      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
ladanyi@1606
   244
  {
ladanyi@1606
   245
    if (it->first != "id")
ladanyi@1606
   246
    {
ladanyi@1606
   247
      delete it->second;
ladanyi@1606
   248
      edgemap_storage.erase(it);
ladanyi@1606
   249
    }
ladanyi@1606
   250
  }
ladanyi@1606
   251
  graph.clear();
ladanyi@1606
   252
  file_name = "";
ladanyi@1606
   253
  modified = false;
ladanyi@1606
   254
}