[163] | 1 | #include <dijkstrabox.h> |
---|
| 2 | |
---|
| 3 | enum {INPUT, OUTPUT, MAP_NUM}; |
---|
| 4 | |
---|
| 5 | DijkstraBox::DijkstraBox(std::vector<std::string> t):AlgoBox() |
---|
| 6 | { |
---|
| 7 | init(t); |
---|
| 8 | } |
---|
| 9 | |
---|
| 10 | void DijkstraBox::run() |
---|
| 11 | { |
---|
| 12 | if( |
---|
| 13 | tabcbt.get_active_text()!="" && |
---|
| 14 | (edgemapcbts[INPUT])->get_active_text()!="" && |
---|
| 15 | (edgemapcbts[OUTPUT])->get_active_text()!="" && |
---|
| 16 | source.get_active_text()!="" && |
---|
| 17 | target.get_active_text()!="" |
---|
| 18 | ) |
---|
| 19 | { |
---|
| 20 | const Graph &g=mapstorage->graph; |
---|
| 21 | |
---|
| 22 | Graph::EdgeMap<double> * inputmap= |
---|
| 23 | (mapstorage->edgemap_storage)[(edgemapcbts[INPUT])->get_active_text()]; |
---|
| 24 | Graph::EdgeMap<double> * outputmap= |
---|
| 25 | (mapstorage->edgemap_storage)[(edgemapcbts[OUTPUT])->get_active_text()]; |
---|
| 26 | |
---|
| 27 | Node from, to; |
---|
| 28 | int assigned=0; |
---|
| 29 | std::ostringstream o; |
---|
| 30 | |
---|
| 31 | //zero out output map |
---|
| 32 | for (EdgeIt i(g); i!=INVALID; ++i) |
---|
| 33 | { |
---|
| 34 | (*outputmap)[i]=0; |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | for (NodeIt i(g); (i!=INVALID) && (assigned<2); ++i) |
---|
| 38 | { |
---|
| 39 | std::ostringstream text; |
---|
| 40 | text << (*((mapstorage->nodemap_storage)["label"]))[i]; |
---|
| 41 | if(!(text.str().compare(source.get_active_text()))) |
---|
| 42 | { |
---|
| 43 | from=i; |
---|
| 44 | assigned++; |
---|
| 45 | } |
---|
| 46 | if(!(text.str().compare(target.get_active_text()))) |
---|
| 47 | { |
---|
| 48 | to=i; |
---|
| 49 | assigned++; |
---|
| 50 | } |
---|
| 51 | } |
---|
| 52 | if(!(from==to)) |
---|
| 53 | { |
---|
| 54 | Dijkstra<Graph, Graph::EdgeMap<double> > dijkstra(g, *inputmap); |
---|
| 55 | dijkstra.run(from, to); |
---|
| 56 | |
---|
| 57 | if(dijkstra.reached(to)) |
---|
| 58 | { |
---|
| 59 | Node n=to; |
---|
| 60 | int length=0; |
---|
| 61 | while (n!=INVALID && n!=from) |
---|
| 62 | { |
---|
| 63 | Edge e=dijkstra.predEdge(n); |
---|
| 64 | (*outputmap)[e]=1; |
---|
| 65 | n=dijkstra.predNode(n); |
---|
| 66 | length++; |
---|
| 67 | } |
---|
| 68 | o << "Result: " << length << " long path, with cost " << dijkstra.dist(to); |
---|
| 69 | } |
---|
| 70 | else |
---|
| 71 | { |
---|
| 72 | o << "Result: failed to find shortest path between "; |
---|
| 73 | o << source.get_active_text() << " and " << target.get_active_text(); |
---|
| 74 | } |
---|
| 75 | resultlabel.set_text(o.str()); |
---|
| 76 | |
---|
| 77 | mapstorage->mapChanged(true, (edgemapcbts[OUTPUT])->get_active_text()); |
---|
| 78 | } |
---|
| 79 | // mapstorage->changeActiveMap(true, E_COLOR, |
---|
| 80 | // (edgemapcbts[OUTPUT])->get_active_text()); |
---|
| 81 | // mapstorage->changeActiveMap(true, E_TEXT, |
---|
| 82 | // (edgemapcbts[INPUT])->get_active_text()); |
---|
| 83 | } |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | void DijkstraBox::build_box() |
---|
| 87 | { |
---|
| 88 | //if active tab is changed, labels of graph nodes had to be loaded into comboboxes |
---|
| 89 | //this can be done after the maps are loaded into ComboBoxes |
---|
| 90 | signal_upon_maplist_updated().connect(sigc::mem_fun(*this, &DijkstraBox::maplists_updated)); |
---|
| 91 | |
---|
| 92 | addMapSelector("Cost map: ", true); |
---|
| 93 | addMapSelector("Edges of path here: ", true); |
---|
| 94 | |
---|
| 95 | Gtk::Label * source_label=new Gtk::Label("Source: "); |
---|
| 96 | Gtk::Label * target_label=new Gtk::Label("Target: "); |
---|
| 97 | |
---|
| 98 | table.attach(*source_label, 0,1,0,1); |
---|
| 99 | table.attach(*target_label, 0,1,1,2); |
---|
| 100 | table.attach(source, 1,2,0,1); |
---|
| 101 | table.attach(target, 1,2,1,2); |
---|
| 102 | |
---|
| 103 | |
---|
| 104 | pack_start(table); |
---|
| 105 | |
---|
| 106 | resultlabel.set_text("Result: algorithm is not run yet."); |
---|
| 107 | pack_start(resultlabel); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | void DijkstraBox::maplists_updated() |
---|
| 111 | { |
---|
| 112 | if(tabcbt.get_active_text()!="") |
---|
| 113 | { |
---|
| 114 | source.clear(); |
---|
| 115 | target.clear(); |
---|
| 116 | const Graph &g=mapstorage->graph; |
---|
| 117 | for (NodeIt i(g); i!=INVALID; ++i) |
---|
| 118 | { |
---|
| 119 | std::ostringstream text; |
---|
| 120 | text << (*((mapstorage->nodemap_storage)["label"]))[i]; |
---|
| 121 | source.prepend_text(text.str()); |
---|
| 122 | target.prepend_text(text.str()); |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | } |
---|