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