Dijkstra in GUI - and the body...
authorhegyi
Fri, 13 Oct 2006 13:53:44 +0000
changeset 163443bc769b344
parent 162 aaa517c9dc23
child 164 70e3c3646283
Dijkstra in GUI - and the body...
dijkstrabox.cc
dijkstrabox.h
     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 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/dijkstrabox.h	Fri Oct 13 13:53:44 2006 +0000
     2.3 @@ -0,0 +1,56 @@
     2.4 +// -*- C++ -*- //
     2.5 +
     2.6 +#ifndef DIJKSTRABOX_H
     2.7 +#define DIJKSTRABOX_H
     2.8 +
     2.9 +class DijkstraBox;
    2.10 +
    2.11 +#include <all_include.h>
    2.12 +#include <algobox.h>
    2.13 +#include <lemon/dijkstra.h>
    2.14 +#include <libgnomecanvasmm.h>
    2.15 +#include <libgnomecanvasmm/polygon.h>
    2.16 +
    2.17 +///Graphical interface to run Dijkstra algorithm.
    2.18 +
    2.19 +///Child of \ref AlgoBox,
    2.20 +///therefore the only task to do at implementation was to
    2.21 +///
    2.22 +///-call init function with correct parameters from correctly parametrized constructor
    2.23 +///
    2.24 +///-implement \ref build_box function
    2.25 +///
    2.26 +///-implement \ref run function
    2.27 +class DijkstraBox : public AlgoBox
    2.28 +{
    2.29 +  ///Shows result of Dijkstra algorithm
    2.30 +  Gtk::Label resultlabel;
    2.31 +
    2.32 +
    2.33 +  ///Table for nodeselector widgets
    2.34 +  Gtk::Table table;
    2.35 +
    2.36 +  ///Combobox for select source node
    2.37 +  Gtk::ComboBoxText source;
    2.38 +
    2.39 +  ///Combobox for select target node
    2.40 +  Gtk::ComboBoxText target;
    2.41 +
    2.42 +public:
    2.43 +  ///Calls \ref AlgoBox::init function to initialize class properly, automatically.
    2.44 +  DijkstraBox(std::vector<std::string> t);
    2.45 +
    2.46 +  ///Prepare, run and postprocess Dijkstra algorithm.
    2.47 +
    2.48 +  ///\ref glemon works only with maps filled with double values
    2.49 +  ///at the moment. While Dijkstra nedds a bool map as output.
    2.50 +  ///As postprocess this bool map should be transformed to
    2.51 +  ///double map.
    2.52 +  void run();
    2.53 +
    2.54 +  ///Builds the graphical design of the interface.
    2.55 +  void build_box();
    2.56 +
    2.57 +  void maplists_updated();
    2.58 +};
    2.59 +#endif //DIJKSTRABOX_H