[Lemon-commits] [lemon_svn] hegyi: r2987 - glemon/trunk

Lemon SVN svn at lemon.cs.elte.hu
Mon Nov 6 21:51:39 CET 2006


Author: hegyi
Date: Fri Oct 13 15:53:44 2006
New Revision: 2987

Added:
   glemon/trunk/dijkstrabox.cc
   glemon/trunk/dijkstrabox.h

Log:
Dijkstra in GUI - and the body...

Added: glemon/trunk/dijkstrabox.cc
==============================================================================
--- (empty file)
+++ glemon/trunk/dijkstrabox.cc	Fri Oct 13 15:53:44 2006
@@ -0,0 +1,125 @@
+#include <dijkstrabox.h>
+
+enum {INPUT, OUTPUT, MAP_NUM};
+
+DijkstraBox::DijkstraBox(std::vector<std::string> t):AlgoBox()
+{
+  init(t);
+}
+    
+void DijkstraBox::run()
+{
+  if(
+     tabcbt.get_active_text()!="" &&
+     (edgemapcbts[INPUT])->get_active_text()!="" &&
+     (edgemapcbts[OUTPUT])->get_active_text()!="" &&
+     source.get_active_text()!="" &&
+     target.get_active_text()!=""
+     )
+    {
+      const Graph &g=mapstorage->graph;
+
+      Graph::EdgeMap<double> * inputmap=
+	(mapstorage->edgemap_storage)[(edgemapcbts[INPUT])->get_active_text()];
+      Graph::EdgeMap<double> * outputmap=
+	(mapstorage->edgemap_storage)[(edgemapcbts[OUTPUT])->get_active_text()];
+
+      Node from, to;
+      int assigned=0;
+      std::ostringstream o;
+
+      //zero out output map
+      for (EdgeIt i(g); i!=INVALID; ++i)
+	{
+	  (*outputmap)[i]=0;
+	}
+
+      for (NodeIt i(g); (i!=INVALID) && (assigned<2); ++i)
+	{
+	  std::ostringstream text;
+	  text << (*((mapstorage->nodemap_storage)["label"]))[i];
+	  if(!(text.str().compare(source.get_active_text())))
+	    {
+	      from=i;
+	      assigned++;
+	    }
+	  if(!(text.str().compare(target.get_active_text())))
+	    {
+	      to=i;
+	      assigned++;
+	    }
+	}
+      if(!(from==to))
+	{
+	  Dijkstra<Graph, Graph::EdgeMap<double> > dijkstra(g, *inputmap);
+	  dijkstra.run(from, to);
+
+	  if(dijkstra.reached(to))
+	    {
+	      Node n=to;
+	      int length=0;
+	      while (n!=INVALID && n!=from)
+		{
+		  Edge e=dijkstra.predEdge(n);
+		  (*outputmap)[e]=1;
+		  n=dijkstra.predNode(n);
+		  length++;
+		}
+	      o << "Result: " << length << " long path, with cost " << dijkstra.dist(to);
+	    }
+	  else
+	    {
+	      o << "Result: failed to find shortest path between ";
+	      o << source.get_active_text() << " and " << target.get_active_text();
+	    }
+	  resultlabel.set_text(o.str());
+
+	  mapstorage->mapChanged(true, (edgemapcbts[OUTPUT])->get_active_text());
+	}
+      //   mapstorage->changeActiveMap(true, E_COLOR,
+      // 			      (edgemapcbts[OUTPUT])->get_active_text());
+      //   mapstorage->changeActiveMap(true, E_TEXT,
+      // 			      (edgemapcbts[INPUT])->get_active_text());
+    }
+}
+    
+void DijkstraBox::build_box()
+{
+  //if active tab is changed, labels of graph nodes had to be loaded into comboboxes
+  //this can be done after the maps are loaded into ComboBoxes
+  signal_upon_maplist_updated().connect(sigc::mem_fun(*this, &DijkstraBox::maplists_updated));
+
+  addMapSelector("Cost map: ", true);
+  addMapSelector("Edges of path here: ", true);
+
+  Gtk::Label * source_label=new Gtk::Label("Source: ");
+  Gtk::Label * target_label=new Gtk::Label("Target: ");
+
+  table.attach(*source_label, 0,1,0,1);
+  table.attach(*target_label, 0,1,1,2);
+  table.attach(source, 1,2,0,1);
+  table.attach(target, 1,2,1,2);
+
+
+  pack_start(table);
+
+  resultlabel.set_text("Result: algorithm is not run yet.");
+  pack_start(resultlabel);
+}
+
+void DijkstraBox::maplists_updated()
+{
+  if(tabcbt.get_active_text()!="")
+    {
+      source.clear();
+      target.clear();
+      const Graph &g=mapstorage->graph;
+      for (NodeIt i(g); i!=INVALID; ++i)
+	{
+	  std::ostringstream text;
+	  text << (*((mapstorage->nodemap_storage)["label"]))[i];
+	  source.prepend_text(text.str());
+	  target.prepend_text(text.str());
+	}
+    }
+}

Added: glemon/trunk/dijkstrabox.h
==============================================================================
--- (empty file)
+++ glemon/trunk/dijkstrabox.h	Fri Oct 13 15:53:44 2006
@@ -0,0 +1,56 @@
+// -*- C++ -*- //
+
+#ifndef DIJKSTRABOX_H
+#define DIJKSTRABOX_H
+
+class DijkstraBox;
+
+#include <all_include.h>
+#include <algobox.h>
+#include <lemon/dijkstra.h>
+#include <libgnomecanvasmm.h>
+#include <libgnomecanvasmm/polygon.h>
+
+///Graphical interface to run Dijkstra algorithm.
+
+///Child of \ref AlgoBox,
+///therefore the only task to do at implementation was to
+///
+///-call init function with correct parameters from correctly parametrized constructor
+///
+///-implement \ref build_box function
+///
+///-implement \ref run function
+class DijkstraBox : public AlgoBox
+{
+  ///Shows result of Dijkstra algorithm
+  Gtk::Label resultlabel;
+
+
+  ///Table for nodeselector widgets
+  Gtk::Table table;
+
+  ///Combobox for select source node
+  Gtk::ComboBoxText source;
+
+  ///Combobox for select target node
+  Gtk::ComboBoxText target;
+
+public:
+  ///Calls \ref AlgoBox::init function to initialize class properly, automatically.
+  DijkstraBox(std::vector<std::string> t);
+
+  ///Prepare, run and postprocess Dijkstra algorithm.
+
+  ///\ref glemon works only with maps filled with double values
+  ///at the moment. While Dijkstra nedds a bool map as output.
+  ///As postprocess this bool map should be transformed to
+  ///double map.
+  void run();
+
+  ///Builds the graphical design of the interface.
+  void build_box();
+
+  void maplists_updated();
+};
+#endif //DIJKSTRABOX_H



More information about the Lemon-commits mailing list