COIN-OR::LEMON - Graph Library

Changeset 1301:1f3baf3bd1f2 in lemon-0.x


Ignore:
Timestamp:
04/04/05 21:22:04 (19 years ago)
Author:
Hegyi Péter
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@1736
Message:

Graph displayer displays graphs now.

Location:
src/work/peter
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/work/peter/graph-displayer.cc

    r1290 r1301  
    1212#include <lemon/list_graph.h>
    1313#include <lemon/graph_reader.h>
     14#include <lemon/graph_writer.h>
    1415#include <lemon/graph_utils.h>
    1516#include <lemon/maps.h>
     
    3738
    3839  ///Event handler function that handles dragging nodes of displayed_graph
    39   bool event_handler(GdkEvent* e, int b);
    40 
    41   ///Event handler function that handles dragging displayed_graph
    42   bool tri_mover(GdkEvent* e);
    43 
    44   ///Coordinates of Weight Point of tirangle
    45   Gnome::Art::Point * wp;
    46   ///Array of nodes of planefigure
    47   Gnome::Canvas::Ellipse ** nodes;
    48   ///Sides of planefigure
    49   Gnome::Canvas::Polygon * sides;
     40  bool event_handler(GdkEvent* e, Node n);
     41
     42        ///The graph, on which we work
     43        Graph g;
     44  ///Map of nodes of planefigure
     45  Graph::NodeMap<Gnome::Canvas::Ellipse *> nodesmap; 
     46  ///Map of edges of planefigure
     47  Graph::EdgeMap<Gnome::Canvas::Line *> edgesmap; 
    5048  ///Group of graphical elements of displayed_graph
    5149  Gnome::Canvas::Group displayed_graph;
     
    5351  ///Indicates whether the button of mouse is pressed or not
    5452  bool isbutton;
    55 
    56   ///Number Of Elements - the number of nodes
    57   int noe;
    58 
    59   ///Array of coordinates
    60   double * coordinates;
    6153
    6254  ///At this location was the mousebutton pressed.
     
    7365};
    7466
    75 ///When we click on the weight point we can drag the whole planefigure. This function resolves it.
    76 bool GraphDisplayerCanvas::tri_mover(GdkEvent* e)
    77 {
    78   switch(e->type)
    79   {
    80     case GDK_BUTTON_PRESS:
    81       clicked_x=e->button.x;
    82       clicked_y=e->button.y;
    83       isbutton=true;
    84       break;
    85     case GDK_BUTTON_RELEASE:
    86       isbutton=false;
    87       active_item=NULL;
    88       break;
    89     case GDK_MOTION_NOTIFY:
    90       if(isbutton)
    91       {
    92         double dx=e->motion.x-clicked_x;
    93         double dy=e->motion.y-clicked_y;
    94 
    95         Gnome::Canvas::Points coos;
    96 
    97         for(int i=0;i<=noe;i++)
    98         {
    99           nodes[i]->move(dx,dy);
    100 
    101           double x=(coordinates[2*i]+=dx);
    102           double y=(coordinates[2*i+1]+=dy);
    103 
    104           if(i!=noe)coos.push_back(Gnome::Art::Point(x,y));
    105 
    106         }
    107 
    108         clicked_x=e->motion.x;
    109         clicked_y=e->motion.y;
    110 
    111         sides->property_points().set_value(coos);
    112       }
    113     default: break;
    114   }
    115   return true;
    116 }
    11767
    11868///This function moves only one node of displayed_graph,
    11969///but recalculate the location of weight point,
    12070///and also redraw the sides of the planefigure.
    121 bool GraphDisplayerCanvas::event_handler(GdkEvent* e, int b)
     71bool GraphDisplayerCanvas::event_handler(GdkEvent* e, Node n)
    12272{
    12373  switch(e->type)
     
    13686      if(isbutton)
    13787      {
    138         //double x1, y1, x2, y2;
    139         //(get_item_at(e->motion.x, e->motion.y))->get_bounds(x1, y1, x2, y2);
    140         //printf("Item coos: %d %d %d %d\n", (int)x1, (int)y1, (int)x2, (int)y2);
    141         //printf("Mouse is moved! %d %d\n",(int)e->motion.x,(int)e->motion.y);
    14288        double dx=e->motion.x-clicked_x;
    14389        double dy=e->motion.y-clicked_y;
    14490        active_item->move(dx, dy);
    145 
    146         coordinates[2*b]+=dx;
    147         coordinates[2*b+1]+=dy;
    148 
    149         Gnome::Canvas::Points coos;
    150 
    151         double x_wp=0;
    152         double y_wp=0;
    153 
    154         for(int i=0;i<noe;i++)
    155         {
    156           coos.push_back(Gnome::Art::Point(coordinates[2*i], coordinates[2*i+1]));
    157 
    158           x_wp+=coordinates[2*i];
    159           y_wp+=coordinates[2*i+1];
    160         }
    161 
    162         sides->property_points().set_value(coos);
    163 
    164         x_wp/=noe;
    165         y_wp/=noe;
    166 
    167         dx=x_wp-coordinates[noe*2];
    168         dy=y_wp-coordinates[noe*2+1];
    169         nodes[noe]->move(dx, dy);
    170 
    171         coordinates[noe*2]+=dx;
    172         coordinates[noe*2+1]+=dy;
    173 
    17491        clicked_x=e->motion.x;
    17592        clicked_y=e->motion.y;
     93
     94                                EdgeIt e;
     95
     96                                g.firstOut(e,n);
     97                                for(;e!=INVALID;g.nextOut(e))
     98                                {
     99                                                Gnome::Canvas::Points coos;
     100                                                double x1, x2, y1, y2;
     101
     102                                                nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
     103                                                coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
     104
     105                                                nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
     106                                                coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
     107
     108                                                edgesmap[e]->property_points().set_value(coos);
     109                                }
     110
     111                                g.firstIn(e,n);
     112                                for(;e!=INVALID;g.nextIn(e))
     113                                {
     114                                                Gnome::Canvas::Points coos;
     115                                                double x1, x2, y1, y2;
     116
     117                                                nodesmap[g.source(e)]->get_bounds(x1, y1, x2, y2);
     118                                                coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
     119
     120                                                nodesmap[g.target(e)]->get_bounds(x1, y1, x2, y2);
     121                                                coos.push_back(Gnome::Art::Point((x1+x2)/2,(y1+y2)/2));
     122
     123                                                edgesmap[e]->property_points().set_value(coos);
     124                                }
    176125      }
    177126    default: break;
     
    180129}
    181130
    182 GraphDisplayerCanvas::GraphDisplayerCanvas(Graph & g, CoordinatesMap & cm):displayed_graph(*(root()), 0, 0),isbutton(false),active_item(NULL)
    183 {
    184     nodes=new Gnome::Canvas::Ellipse* [countNodes(g)];
    185     int sorszam=0;
    186 
     131GraphDisplayerCanvas::GraphDisplayerCanvas(Graph & gr, CoordinatesMap & cm):g(gr),nodesmap(g),edgesmap(g),displayed_graph(*(root()), 0, 0),isbutton(false),active_item(NULL)
     132{
     133                for (EdgeIt i(g); i!=INVALID; ++i)
     134    {
     135                                Gnome::Canvas::Points coos;
     136                                coos.push_back(Gnome::Art::Point(cm[g.source(i)].x,cm[g.source(i)].y));
     137                                coos.push_back(Gnome::Art::Point(cm[g.target(i)].x,cm[g.target(i)].y));
     138                                edgesmap[i]=new Gnome::Canvas::Line(displayed_graph, coos);
     139                                *(edgesmap[i]) << Gnome::Canvas::Properties::fill_color("green");
     140                                edgesmap[i]->property_width_pixels().set_value(10);
     141    }
    187142    for (NodeIt i(g); i!=INVALID; ++i)
    188143    {
    189         nodes[sorszam]= new Gnome::Canvas::Ellipse(displayed_graph, cm[i].x-20, cm[i].y-20, cm[i].x+20, cm[i].y+20);
    190         *(nodes[sorszam]) << Gnome::Canvas::Properties::fill_color("blue");
    191         *(nodes[sorszam]) << Gnome::Canvas::Properties::outline_color("black");
    192         (nodes[sorszam])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &GraphDisplayerCanvas::event_handler),sorszam));
    193         sorszam++;
    194     }
    195 
    196     for (EdgeIt i(g); i!=INVALID; ++i)
    197     {
    198     }
    199 
     144                                nodesmap[i]=new Gnome::Canvas::Ellipse(displayed_graph, cm[i].x-20, cm[i].y-20, cm[i].x+20, cm[i].y+20);
     145                                *(nodesmap[i]) << Gnome::Canvas::Properties::fill_color("blue");
     146                                *(nodesmap[i]) << Gnome::Canvas::Properties::outline_color("black");
     147                                (nodesmap[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &GraphDisplayerCanvas::event_handler),i));
     148    }
    200149
    201150}
     
    203152GraphDisplayerCanvas::~GraphDisplayerCanvas()
    204153{
    205 }
     154                Graph::NodeMap <int> id(g);
     155                Graph::NodeMap <double> xc(g);
     156                Graph::NodeMap <double> yc(g);
     157
     158                int j=1;
     159
     160    for (NodeIt i(g); i!=INVALID; ++i)
     161    {
     162                                double x1,y1,x2,y2;
     163                                nodesmap[i]->get_bounds(x1, y1, x2, y2);
     164
     165                                id[i]=j++;
     166                                xc[i]=(x1+x2)/2;
     167                                yc[i]=(y1+y2)/2;
     168                }
     169
     170                GraphWriter<Graph> writer(std::cout,g);
     171
     172                writer.addNodeMap("id", id);
     173                writer.addNodeMap("coordinates_x", xc);
     174                writer.addNodeMap("coordinates_y", yc);
     175                writer.run();
     176}
     177
    206178
    207179//MainWin:
    208 
    209180class MainWin : public Gtk::Window
    210181{
     
    291262
    292263  CoordinatesMap cm(g);
     264  Graph::EdgeMap<double> cap(g);
    293265
    294266  //we create one object to read x coordinates
     
    304276  reader.run();
    305277
    306   for (NodeIt i(g); i!=INVALID; ++i)
    307       std::cout << " " << g.id(i) << " " << cm[i];
    308   std::cout << std::endl;
    309 
    310278  Gnome::Canvas::init();
    311279  Gtk::Main app(argc, argv);
  • src/work/peter/graphocska.lgf

    r1290 r1301  
    11@nodeset
    2 id   coordinates_x    coordinates_y
    3 1    300              300
    4 2    -300             -300
    5 3    300              -300
    6 4    -300             300
    7 5    100              100
    8 6    -100             -100
    9 7    -100             100
    10 8    100              -100
     2id      coordinates_x   coordinates_y   
     31       230     -80     
     42       230     100     
     53       120     -80     
     64       120     100     
     75       20      100     
     86       20      -80     
     97       -40     10     
     108       -100    100     
     119       -100    10     
     1210      -100    -80     
     1311      -200    -80     
     1412      -200    10     
     1513      -200    100     
     1614      -300    100     
     1715      -300    -80     
     18
     19@edgeset
     20        cap
     2115 14 1
     2214 13 2
     2313 12 3
     2413 8 4
     2512 11 5
     2612 9 6
     2711 10 7
     2810 9 8
     2910 7 9
     309 8 10
     317 6 11
     326 5 12
     336 3 13
     345 4 14
     354 3 15
     363 2 16
     372 1 17
     38
    1139@end
Note: See TracChangeset for help on using the changeset viewer.