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