dijkstrabox.cc
changeset 163 443bc769b344
child 165 2cd447b0bd3a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dijkstrabox.cc	Fri Oct 13 13:53:44 2006 +0000
     1.3 @@ -0,0 +1,125 @@
     1.4 +#include <dijkstrabox.h>
     1.5 +
     1.6 +enum {INPUT, OUTPUT, MAP_NUM};
     1.7 +
     1.8 +DijkstraBox::DijkstraBox(std::vector<std::string> t):AlgoBox()
     1.9 +{
    1.10 +  init(t);
    1.11 +}
    1.12 +    
    1.13 +void DijkstraBox::run()
    1.14 +{
    1.15 +  if(
    1.16 +     tabcbt.get_active_text()!="" &&
    1.17 +     (edgemapcbts[INPUT])->get_active_text()!="" &&
    1.18 +     (edgemapcbts[OUTPUT])->get_active_text()!="" &&
    1.19 +     source.get_active_text()!="" &&
    1.20 +     target.get_active_text()!=""
    1.21 +     )
    1.22 +    {
    1.23 +      const Graph &g=mapstorage->graph;
    1.24 +
    1.25 +      Graph::EdgeMap<double> * inputmap=
    1.26 +	(mapstorage->edgemap_storage)[(edgemapcbts[INPUT])->get_active_text()];
    1.27 +      Graph::EdgeMap<double> * outputmap=
    1.28 +	(mapstorage->edgemap_storage)[(edgemapcbts[OUTPUT])->get_active_text()];
    1.29 +
    1.30 +      Node from, to;
    1.31 +      int assigned=0;
    1.32 +      std::ostringstream o;
    1.33 +
    1.34 +      //zero out output map
    1.35 +      for (EdgeIt i(g); i!=INVALID; ++i)
    1.36 +	{
    1.37 +	  (*outputmap)[i]=0;
    1.38 +	}
    1.39 +
    1.40 +      for (NodeIt i(g); (i!=INVALID) && (assigned<2); ++i)
    1.41 +	{
    1.42 +	  std::ostringstream text;
    1.43 +	  text << (*((mapstorage->nodemap_storage)["label"]))[i];
    1.44 +	  if(!(text.str().compare(source.get_active_text())))
    1.45 +	    {
    1.46 +	      from=i;
    1.47 +	      assigned++;
    1.48 +	    }
    1.49 +	  if(!(text.str().compare(target.get_active_text())))
    1.50 +	    {
    1.51 +	      to=i;
    1.52 +	      assigned++;
    1.53 +	    }
    1.54 +	}
    1.55 +      if(!(from==to))
    1.56 +	{
    1.57 +	  Dijkstra<Graph, Graph::EdgeMap<double> > dijkstra(g, *inputmap);
    1.58 +	  dijkstra.run(from, to);
    1.59 +
    1.60 +	  if(dijkstra.reached(to))
    1.61 +	    {
    1.62 +	      Node n=to;
    1.63 +	      int length=0;
    1.64 +	      while (n!=INVALID && n!=from)
    1.65 +		{
    1.66 +		  Edge e=dijkstra.predEdge(n);
    1.67 +		  (*outputmap)[e]=1;
    1.68 +		  n=dijkstra.predNode(n);
    1.69 +		  length++;
    1.70 +		}
    1.71 +	      o << "Result: " << length << " long path, with cost " << dijkstra.dist(to);
    1.72 +	    }
    1.73 +	  else
    1.74 +	    {
    1.75 +	      o << "Result: failed to find shortest path between ";
    1.76 +	      o << source.get_active_text() << " and " << target.get_active_text();
    1.77 +	    }
    1.78 +	  resultlabel.set_text(o.str());
    1.79 +
    1.80 +	  mapstorage->mapChanged(true, (edgemapcbts[OUTPUT])->get_active_text());
    1.81 +	}
    1.82 +      //   mapstorage->changeActiveMap(true, E_COLOR,
    1.83 +      // 			      (edgemapcbts[OUTPUT])->get_active_text());
    1.84 +      //   mapstorage->changeActiveMap(true, E_TEXT,
    1.85 +      // 			      (edgemapcbts[INPUT])->get_active_text());
    1.86 +    }
    1.87 +}
    1.88 +    
    1.89 +void DijkstraBox::build_box()
    1.90 +{
    1.91 +  //if active tab is changed, labels of graph nodes had to be loaded into comboboxes
    1.92 +  //this can be done after the maps are loaded into ComboBoxes
    1.93 +  signal_upon_maplist_updated().connect(sigc::mem_fun(*this, &DijkstraBox::maplists_updated));
    1.94 +
    1.95 +  addMapSelector("Cost map: ", true);
    1.96 +  addMapSelector("Edges of path here: ", true);
    1.97 +
    1.98 +  Gtk::Label * source_label=new Gtk::Label("Source: ");
    1.99 +  Gtk::Label * target_label=new Gtk::Label("Target: ");
   1.100 +
   1.101 +  table.attach(*source_label, 0,1,0,1);
   1.102 +  table.attach(*target_label, 0,1,1,2);
   1.103 +  table.attach(source, 1,2,0,1);
   1.104 +  table.attach(target, 1,2,1,2);
   1.105 +
   1.106 +
   1.107 +  pack_start(table);
   1.108 +
   1.109 +  resultlabel.set_text("Result: algorithm is not run yet.");
   1.110 +  pack_start(resultlabel);
   1.111 +}
   1.112 +
   1.113 +void DijkstraBox::maplists_updated()
   1.114 +{
   1.115 +  if(tabcbt.get_active_text()!="")
   1.116 +    {
   1.117 +      source.clear();
   1.118 +      target.clear();
   1.119 +      const Graph &g=mapstorage->graph;
   1.120 +      for (NodeIt i(g); i!=INVALID; ++i)
   1.121 +	{
   1.122 +	  std::ostringstream text;
   1.123 +	  text << (*((mapstorage->nodemap_storage)["label"]))[i];
   1.124 +	  source.prepend_text(text.str());
   1.125 +	  target.prepend_text(text.str());
   1.126 +	}
   1.127 +    }
   1.128 +}