[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 | |
---|
[1645] | 54 | int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap<double> *nodemap, double default_value = 0.0) |
---|
[1442] | 55 | { |
---|
[1597] | 56 | if( nodemap_storage.find(name) == nodemap_storage.end() ) |
---|
| 57 | { |
---|
| 58 | nodemap_storage[name]=nodemap; |
---|
[1645] | 59 | // set the maps default value |
---|
| 60 | nodemap_default[name] = default_value; |
---|
[1878] | 61 | |
---|
| 62 | //announce changement in maps |
---|
| 63 | signal_node_map.emit(name); |
---|
[1597] | 64 | return 0; |
---|
| 65 | } |
---|
| 66 | return 1; |
---|
[1442] | 67 | } |
---|
| 68 | |
---|
[1837] | 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 | |
---|
[1645] | 125 | int MapStorage::addEdgeMap(const std::string & name, Graph::EdgeMap<double> *edgemap, double default_value = 0.0) |
---|
[1442] | 126 | { |
---|
[1597] | 127 | if( edgemap_storage.find(name) == edgemap_storage.end() ) |
---|
| 128 | { |
---|
| 129 | edgemap_storage[name]=edgemap; |
---|
[1645] | 130 | // set the maps default value |
---|
| 131 | edgemap_default[name] = default_value; |
---|
[1878] | 132 | |
---|
| 133 | //announce changement in maps |
---|
| 134 | signal_edge_map.emit(name); |
---|
[1597] | 135 | return 0; |
---|
| 136 | } |
---|
| 137 | return 1; |
---|
[1442] | 138 | } |
---|
| 139 | |
---|
| 140 | double MapStorage::maxOfNodeMap(const std::string & name) |
---|
| 141 | { |
---|
| 142 | double max=0; |
---|
[1606] | 143 | for (NodeIt j(graph); j!=INVALID; ++j) |
---|
[1442] | 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; |
---|
[1606] | 156 | for (EdgeIt j(graph); j!=INVALID; ++j) |
---|
[1442] | 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 | { |
---|
[1606] | 168 | NodeIt j(graph); |
---|
[1617] | 169 | double min; |
---|
| 170 | if(j!=INVALID) |
---|
| 171 | { |
---|
| 172 | min=(*nodemap_storage[name])[j]; |
---|
| 173 | } |
---|
| 174 | else |
---|
| 175 | { |
---|
| 176 | min=0; |
---|
| 177 | } |
---|
[1442] | 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 | { |
---|
[1606] | 190 | EdgeIt j(graph); |
---|
[1617] | 191 | double min; |
---|
| 192 | if(j!=INVALID) |
---|
| 193 | { |
---|
| 194 | min=(*edgemap_storage[name])[j]; |
---|
| 195 | } |
---|
| 196 | else |
---|
| 197 | { |
---|
| 198 | min=0; |
---|
| 199 | } |
---|
[1606] | 200 | for (EdgeIt j(graph); j!=INVALID; ++j) |
---|
[1442] | 201 | { |
---|
| 202 | if( (*edgemap_storage[name])[j]<min ) |
---|
| 203 | { |
---|
| 204 | min=(*edgemap_storage[name])[j]; |
---|
| 205 | } |
---|
| 206 | } |
---|
| 207 | return min; |
---|
| 208 | } |
---|
| 209 | |
---|
[1645] | 210 | int MapStorage::readFromFile(const std::string &filename) |
---|
[1606] | 211 | { |
---|
| 212 | bool read_x = false; |
---|
| 213 | bool read_y = false; |
---|
[1646] | 214 | bool read_edge_id = false; |
---|
[1606] | 215 | |
---|
| 216 | try { |
---|
| 217 | LemonReader lreader(filename); |
---|
| 218 | ContentReader content(lreader); |
---|
| 219 | lreader.run(); |
---|
| 220 | |
---|
[1867] | 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 | |
---|
[1606] | 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 | } |
---|
| 254 | else if (*it == "id") |
---|
| 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 | { |
---|
| 268 | if (*it == "id") |
---|
| 269 | { |
---|
| 270 | //std::cout << "read id edgemap" << std::endl; |
---|
| 271 | } |
---|
| 272 | else |
---|
| 273 | { |
---|
| 274 | edgemap_storage[*it] = new Graph::EdgeMap<double>(graph); |
---|
| 275 | //std::cout << "read " << *it << " edgemap" << std::endl; |
---|
| 276 | } |
---|
| 277 | greader.readEdgeMap(*it, *edgemap_storage[*it]); |
---|
| 278 | } |
---|
[1860] | 279 | GuiReader gui_reader(greader, this); |
---|
[1606] | 280 | greader.run(); |
---|
[1867] | 281 | } catch (Exception& error) { |
---|
[1708] | 282 | Gtk::MessageDialog mdialog(error.what()); |
---|
[1606] | 283 | mdialog.run(); |
---|
[1645] | 284 | clear(); |
---|
| 285 | return 1; |
---|
[1606] | 286 | } |
---|
| 287 | |
---|
[1646] | 288 | if (!read_edge_id) |
---|
| 289 | { |
---|
| 290 | edgemap_storage["id"] = new Graph::EdgeMap<double>(graph); |
---|
| 291 | int i = 1; |
---|
| 292 | for (EdgeIt e(graph); e != INVALID; ++e) |
---|
| 293 | { |
---|
| 294 | (*edgemap_storage["id"])[e] = i++; |
---|
| 295 | } |
---|
| 296 | } |
---|
| 297 | |
---|
[1606] | 298 | if (!read_x || !read_y) |
---|
| 299 | { |
---|
| 300 | int node_num = 0; |
---|
| 301 | for (NodeIt n(graph); n != INVALID; ++n) |
---|
| 302 | { |
---|
| 303 | node_num++; |
---|
| 304 | } |
---|
| 305 | const double pi = 3.142; |
---|
| 306 | double step = 2 * pi / (double) node_num; |
---|
| 307 | int i = 0; |
---|
| 308 | for (NodeIt n(graph); n != INVALID; ++n) |
---|
| 309 | { |
---|
[1686] | 310 | nodemap_storage["coordinates_x"]->set(n, 250.0 * std::cos(i * step)); |
---|
| 311 | nodemap_storage["coordinates_y"]->set(n, 250.0 * std::sin(i * step)); |
---|
[1606] | 312 | i++; |
---|
| 313 | } |
---|
| 314 | } |
---|
[1645] | 315 | |
---|
[1860] | 316 | if (!arrow_pos_read_ok) |
---|
| 317 | { |
---|
| 318 | arrow_pos_read_ok = false; |
---|
| 319 | for (EdgeIt e(graph); e != INVALID; ++e) |
---|
| 320 | { |
---|
| 321 | arrow_pos.set(e, (coords[graph.source(e)] + coords[graph.target(e)]) / 2.0); |
---|
| 322 | } |
---|
| 323 | } |
---|
| 324 | |
---|
[1645] | 325 | // fill in the default values for the maps |
---|
| 326 | for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it = |
---|
| 327 | nodemap_storage.begin(); it != nodemap_storage.end(); ++it) |
---|
| 328 | { |
---|
| 329 | if ((it->first != "id") && |
---|
| 330 | (it->first != "coordiantes_x") && |
---|
| 331 | (it->first != "coordinates_y")) |
---|
| 332 | { |
---|
| 333 | nodemap_default[it->first] = 0.0; |
---|
| 334 | } |
---|
[1646] | 335 | else if (it->first == "id") |
---|
| 336 | { |
---|
| 337 | NodeIt n(graph); |
---|
| 338 | double max = (*nodemap_storage["id"])[n]; |
---|
| 339 | for (; n != INVALID; ++n) |
---|
| 340 | { |
---|
| 341 | if ((*nodemap_storage["id"])[n] > max) |
---|
| 342 | max = (*nodemap_storage["id"])[n]; |
---|
| 343 | } |
---|
| 344 | nodemap_default["id"] = max + 1.0; |
---|
| 345 | } |
---|
[1645] | 346 | } |
---|
| 347 | for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it = |
---|
| 348 | edgemap_storage.begin(); it != edgemap_storage.end(); ++it) |
---|
| 349 | { |
---|
| 350 | if (it->first != "id") |
---|
| 351 | { |
---|
| 352 | edgemap_default[it->first] = 0.0; |
---|
| 353 | } |
---|
[1646] | 354 | else |
---|
| 355 | { |
---|
[1649] | 356 | double max = std::numeric_limits<double>::min(); |
---|
| 357 | for (EdgeIt e(graph); e != INVALID; ++e) |
---|
[1646] | 358 | { |
---|
| 359 | if ((*edgemap_storage["id"])[e] > max) |
---|
| 360 | max = (*edgemap_storage["id"])[e]; |
---|
| 361 | } |
---|
[1649] | 362 | if (max > std::numeric_limits<double>::min()) |
---|
| 363 | edgemap_default["id"] = max + 1.0; |
---|
| 364 | else |
---|
| 365 | edgemap_default["id"] = 1.0; |
---|
[1646] | 366 | } |
---|
[1645] | 367 | } |
---|
| 368 | |
---|
| 369 | return 0; |
---|
[1606] | 370 | } |
---|
| 371 | |
---|
| 372 | void MapStorage::writeToFile(const std::string &filename) |
---|
| 373 | { |
---|
| 374 | GraphWriter<Graph> gwriter(filename, graph); |
---|
| 375 | |
---|
| 376 | for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it = |
---|
| 377 | nodemap_storage.begin(); it != nodemap_storage.end(); ++it) |
---|
| 378 | { |
---|
| 379 | gwriter.writeNodeMap(it->first, *(it->second)); |
---|
| 380 | } |
---|
| 381 | for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it = |
---|
| 382 | edgemap_storage.begin(); it != edgemap_storage.end(); ++it) |
---|
| 383 | { |
---|
[1860] | 384 | if ((it->first != "arrow_pos_x") && |
---|
| 385 | (it->first != "arrow_pos_y")) |
---|
| 386 | { |
---|
| 387 | gwriter.writeEdgeMap(it->first, *(it->second)); |
---|
| 388 | } |
---|
[1606] | 389 | } |
---|
[1860] | 390 | |
---|
| 391 | GuiWriter gui_writer(gwriter, this); |
---|
| 392 | |
---|
[1606] | 393 | gwriter.run(); |
---|
| 394 | } |
---|
| 395 | |
---|
| 396 | void MapStorage::clear() |
---|
| 397 | { |
---|
| 398 | for (std::map<std::string, Graph::NodeMap<double>*>::iterator it = |
---|
| 399 | nodemap_storage.begin(); it != nodemap_storage.end(); ++it) |
---|
| 400 | { |
---|
| 401 | if ((it->first != "coordinates_x") && |
---|
| 402 | (it->first != "coordinates_y") && |
---|
| 403 | (it->first != "id")) |
---|
| 404 | { |
---|
| 405 | delete it->second; |
---|
| 406 | nodemap_storage.erase(it); |
---|
| 407 | } |
---|
| 408 | } |
---|
| 409 | for (std::map<std::string, Graph::EdgeMap<double>*>::iterator it = |
---|
| 410 | edgemap_storage.begin(); it != edgemap_storage.end(); ++it) |
---|
| 411 | { |
---|
[1860] | 412 | if ((it->first != "id") && |
---|
| 413 | (it->first != "arrow_pos_x") && |
---|
| 414 | (it->first != "arrow_pos_y")) |
---|
[1606] | 415 | { |
---|
| 416 | delete it->second; |
---|
| 417 | edgemap_storage.erase(it); |
---|
| 418 | } |
---|
| 419 | } |
---|
[1645] | 420 | for (std::map<std::string, double>::iterator it = |
---|
| 421 | nodemap_default.begin(); it != nodemap_default.end(); ++it) |
---|
| 422 | { |
---|
[1646] | 423 | if (it->first != "id") |
---|
| 424 | nodemap_default.erase(it); |
---|
[1645] | 425 | } |
---|
| 426 | for (std::map<std::string, double>::iterator it = |
---|
| 427 | edgemap_default.begin(); it != edgemap_default.end(); ++it) |
---|
| 428 | { |
---|
[1646] | 429 | if (it->first != "id") |
---|
| 430 | edgemap_default.erase(it); |
---|
[1645] | 431 | } |
---|
[1606] | 432 | graph.clear(); |
---|
| 433 | file_name = ""; |
---|
| 434 | modified = false; |
---|
| 435 | } |
---|
[1860] | 436 | |
---|
| 437 | void MapStorage::ArrowPosReadOK() |
---|
| 438 | { |
---|
| 439 | arrow_pos_read_ok = true; |
---|
| 440 | } |
---|