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