graph_displayer_canvas.cc
author hegyi
Tue, 20 Feb 2007 15:46:19 +0000
changeset 189 8b69c54d5bf0
parent 184 4e8704aae278
child 190 2cac5b936a2b
child 204 8fec6a6472fe
permissions -rwxr-xr-x
No segmentation fault will be occured if two nodes are exactly overlap each other, AND they are connected.
alpar@174
     1
/* -*- C++ -*-
alpar@174
     2
 *
alpar@174
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@174
     4
 *
alpar@174
     5
 * Copyright (C) 2003-2006
alpar@174
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@174
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@174
     8
 *
alpar@174
     9
 * Permission to use, modify and distribute this software is granted
alpar@174
    10
 * provided that this copyright notice appears in all copies. For
alpar@174
    11
 * precise terms see the accompanying LICENSE file.
alpar@174
    12
 *
alpar@174
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@174
    14
 * express or implied, and with no claim as to its suitability for any
alpar@174
    15
 * purpose.
alpar@174
    16
 *
alpar@174
    17
 */
alpar@174
    18
ladanyi@53
    19
#include "graph_displayer_canvas.h"
hegyi@167
    20
#include <lemon/random.h>
alpar@59
    21
#include <cmath>
ladanyi@6
    22
hegyi@96
    23
GraphDisplayerCanvas::GraphDisplayerCanvas(NoteBookTab & mainw) :
hegyi@94
    24
  nodesmap(mainw.mapstorage.graph), edgesmap(mainw.mapstorage.graph), edgetextmap(mainw.mapstorage.graph),
hegyi@94
    25
  nodetextmap(mainw.mapstorage.graph), displayed_graph(*(root()), 0, 0),
ladanyi@66
    26
  isbutton(0), active_item(NULL), target_item(NULL), nodemap_to_edit(""),
hegyi@160
    27
  edgemap_to_edit(""), autoscale(true), zoomtrack(false), radius_size(20), edge_width(10),
ladanyi@184
    28
  was_redesigned(false), is_drawn(false), mytab(mainw),
ladanyi@184
    29
  background_set(false)
ladanyi@6
    30
{
hegyi@187
    31
  //add mouse scroll event handler - it won't be changed, it handles zoom
hegyi@187
    32
  signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::scrollEventHandler), false);
hegyi@187
    33
ladanyi@53
    34
  //base event handler is move tool
ladanyi@53
    35
  actual_handler=signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::moveEventHandler), false);
ladanyi@53
    36
  actual_tool=MOVE;
hegyi@34
    37
hegyi@187
    38
ladanyi@53
    39
  active_node=INVALID;
ladanyi@53
    40
  active_edge=INVALID;
ladanyi@53
    41
  forming_edge=INVALID;
ladanyi@184
    42
ladanyi@184
    43
  setBackground();
ladanyi@184
    44
}
ladanyi@184
    45
ladanyi@184
    46
void GraphDisplayerCanvas::setBackground()
ladanyi@184
    47
{
ladanyi@184
    48
  if (background_set)
ladanyi@184
    49
  {
ladanyi@184
    50
    delete background;
ladanyi@184
    51
  }
ladanyi@184
    52
  if (mytab.mapstorage.isBackgroundSet())
ladanyi@184
    53
  {
ladanyi@184
    54
    background_set = true;
ladanyi@184
    55
    refBackground = Gdk::Pixbuf::create_from_file(
ladanyi@184
    56
      mytab.mapstorage.getBackgroundFilename());
ladanyi@184
    57
    background = new Gnome::Canvas::Pixbuf(
ladanyi@184
    58
        *(root()),
ladanyi@184
    59
        0.0 - refBackground->get_width() / 2.0,
ladanyi@184
    60
        0.0 - refBackground->get_height() / 2.0,
ladanyi@184
    61
        refBackground);
ladanyi@184
    62
    background->lower_to_bottom();
ladanyi@184
    63
  }
ladanyi@184
    64
  else
ladanyi@184
    65
  {
ladanyi@184
    66
    background_set = false;
ladanyi@184
    67
  }
ladanyi@53
    68
}
hegyi@9
    69
ladanyi@53
    70
GraphDisplayerCanvas::~GraphDisplayerCanvas()
ladanyi@53
    71
{
hegyi@96
    72
  for (NodeIt n((mytab.mapstorage).graph); n != INVALID; ++n)
hegyi@94
    73
    {
hegyi@94
    74
      delete nodesmap[n];
hegyi@94
    75
      delete nodetextmap[n];
hegyi@94
    76
    }
hegyi@94
    77
  
hegyi@96
    78
  for (EdgeIt e((mytab.mapstorage).graph); e != INVALID; ++e)
hegyi@94
    79
    {
hegyi@94
    80
      delete edgesmap[e];
hegyi@94
    81
      delete edgetextmap[e];
hegyi@94
    82
    }
hegyi@94
    83
}
ladanyi@6
    84
hegyi@94
    85
void GraphDisplayerCanvas::propertyChange(bool itisedge, int prop)
hegyi@94
    86
{
hegyi@94
    87
  if(itisedge)
hegyi@94
    88
    {
hegyi@94
    89
      propertyUpdate(Edge(INVALID), prop);
hegyi@94
    90
    }
hegyi@94
    91
  else
hegyi@94
    92
    {
hegyi@94
    93
      propertyUpdate(Node(INVALID), prop);
hegyi@94
    94
    }
hegyi@94
    95
}
hegyi@94
    96
hegyi@94
    97
void GraphDisplayerCanvas::propertyUpdate(Edge edge)
hegyi@94
    98
{
hegyi@94
    99
  for(int i=0;i<EDGE_PROPERTY_NUM;i++)
hegyi@94
   100
    {
hegyi@94
   101
      propertyUpdate(edge, i);
hegyi@94
   102
    }
hegyi@94
   103
}
hegyi@94
   104
hegyi@94
   105
void GraphDisplayerCanvas::propertyUpdate(Node node)
hegyi@94
   106
{
hegyi@94
   107
  for(int i=0;i<NODE_PROPERTY_NUM;i++)
hegyi@94
   108
    {
hegyi@94
   109
      propertyUpdate(node, i);
hegyi@94
   110
    }
hegyi@94
   111
}
hegyi@94
   112
hegyi@118
   113
void GraphDisplayerCanvas::propertyUpdate(Node node, int prop)
hegyi@94
   114
{
hegyi@96
   115
  std::string mapname=mytab.getActiveNodeMap(prop);
hegyi@94
   116
hegyi@172
   117
  if(is_drawn)
hegyi@94
   118
    {
hegyi@172
   119
      if(mapname!="")
hegyi@94
   120
	{
hegyi@172
   121
	  if( ( ((mytab.mapstorage).nodemap_storage).find(mapname) != ((mytab.mapstorage).nodemap_storage).end() ) )
hegyi@172
   122
	    {
hegyi@172
   123
	      switch(prop)
hegyi@172
   124
		{
hegyi@172
   125
		case N_RADIUS:
hegyi@172
   126
		  changeNodeRadius(mapname, node);
hegyi@172
   127
		  break;
hegyi@172
   128
		case N_COLOR:
hegyi@172
   129
		  changeNodeColor(mapname, node);
hegyi@172
   130
		  break;
hegyi@172
   131
		case N_TEXT:
hegyi@172
   132
		  changeNodeText(mapname, node);
hegyi@172
   133
		  break;
hegyi@172
   134
		default:
hegyi@172
   135
		  std::cerr<<"Error\n";
hegyi@172
   136
		}
hegyi@172
   137
	    }
hegyi@172
   138
	}
hegyi@172
   139
      else //mapname==""
hegyi@172
   140
	{
hegyi@172
   141
	  Node node=INVALID;	
hegyi@94
   142
	  switch(prop)
hegyi@94
   143
	    {
hegyi@94
   144
	    case N_RADIUS:
hegyi@172
   145
	      resetNodeRadius(node);
hegyi@94
   146
	      break;
hegyi@94
   147
	    case N_COLOR:
hegyi@172
   148
	      resetNodeColor(node);
hegyi@94
   149
	      break;
hegyi@94
   150
	    case N_TEXT:
hegyi@172
   151
	      resetNodeText(node);
hegyi@94
   152
	      break;
hegyi@94
   153
	    default:
hegyi@94
   154
	      std::cerr<<"Error\n";
hegyi@94
   155
	    }
hegyi@94
   156
	}
hegyi@94
   157
    }
hegyi@94
   158
}
hegyi@94
   159
hegyi@118
   160
void GraphDisplayerCanvas::propertyUpdate(Edge edge, int prop)
hegyi@94
   161
{
hegyi@96
   162
  std::string mapname=mytab.getActiveEdgeMap(prop);
hegyi@94
   163
hegyi@172
   164
  if(is_drawn)
hegyi@94
   165
    {
hegyi@172
   166
      if(mapname!="")
hegyi@172
   167
	{
hegyi@172
   168
	  if( ( ((mytab.mapstorage).edgemap_storage).find(mapname) != ((mytab.mapstorage).edgemap_storage).end() ) )
hegyi@172
   169
	    {
hegyi@172
   170
	      switch(prop)
hegyi@172
   171
		{
hegyi@172
   172
		case E_WIDTH:
hegyi@172
   173
		  changeEdgeWidth(mapname, edge);
hegyi@172
   174
		  break;
hegyi@172
   175
		case E_COLOR:
hegyi@172
   176
		  changeEdgeColor(mapname, edge);
hegyi@172
   177
		  break;
hegyi@172
   178
		case E_TEXT:
hegyi@172
   179
		  changeEdgeText(mapname, edge);
hegyi@172
   180
		  break;
hegyi@172
   181
		default:
hegyi@172
   182
		  std::cerr<<"Error\n";
hegyi@172
   183
		}
hegyi@172
   184
	    }
hegyi@172
   185
	}
hegyi@172
   186
      else //mapname==""
hegyi@94
   187
	{
hegyi@94
   188
	  switch(prop)
hegyi@94
   189
	    {
hegyi@94
   190
	    case E_WIDTH:
hegyi@172
   191
	      resetEdgeWidth(edge);
hegyi@94
   192
	      break;
hegyi@94
   193
	    case E_COLOR:
hegyi@172
   194
	      resetEdgeColor(edge);
hegyi@94
   195
	      break;
hegyi@94
   196
	    case E_TEXT:
hegyi@172
   197
	      resetEdgeText(edge);
hegyi@94
   198
	      break;
hegyi@94
   199
	    default:
hegyi@94
   200
	      std::cerr<<"Error\n";
hegyi@94
   201
	    }
hegyi@94
   202
	}
hegyi@94
   203
    }
ladanyi@53
   204
}
ladanyi@53
   205
ladanyi@53
   206
void GraphDisplayerCanvas::drawGraph()
ladanyi@53
   207
{
ladanyi@6
   208
  //first edges are drawn, to hide joining with nodes later
ladanyi@6
   209
hegyi@96
   210
  for (EdgeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
ladanyi@6
   211
  {
ladanyi@151
   212
    if (mytab.mapstorage.graph.source(i) == mytab.mapstorage.graph.target(i))
ladanyi@151
   213
    {
ladanyi@151
   214
      edgesmap[i]=new LoopEdge(displayed_graph, i, *this);
ladanyi@151
   215
    }
ladanyi@151
   216
    else
ladanyi@151
   217
    {
ladanyi@151
   218
      edgesmap[i]=new BrokenEdge(displayed_graph, i, *this);
ladanyi@151
   219
    }
ladanyi@6
   220
    //initializing edge-text as well, to empty string
ladanyi@6
   221
ladanyi@98
   222
    XY text_pos=mytab.mapstorage.arrow_pos[i];
ladanyi@98
   223
    text_pos+=(XY(10,10));
hegyi@25
   224
hegyi@25
   225
    edgetextmap[i]=new Gnome::Canvas::Text(displayed_graph, text_pos.x, text_pos.y, "");
hegyi@28
   226
    edgetextmap[i]->property_fill_color().set_value("darkgreen");
hegyi@149
   227
    edgetextmap[i]->signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::mapEditEventHandler), false);
ladanyi@63
   228
    edgetextmap[i]->raise_to_top();
ladanyi@6
   229
  }
ladanyi@6
   230
ladanyi@6
   231
  //afterwards nodes come to be drawn
ladanyi@6
   232
hegyi@96
   233
  for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
ladanyi@6
   234
  {
ladanyi@6
   235
    //drawing bule nodes, with black line around them
ladanyi@6
   236
ladanyi@53
   237
    nodesmap[i]=new Gnome::Canvas::Ellipse(
ladanyi@53
   238
        displayed_graph,
hegyi@96
   239
        (mytab.mapstorage).coords[i].x-20,
hegyi@96
   240
        (mytab.mapstorage).coords[i].y-20,
hegyi@96
   241
        (mytab.mapstorage).coords[i].x+20,
hegyi@96
   242
        (mytab.mapstorage).coords[i].y+20);
ladanyi@6
   243
    *(nodesmap[i]) << Gnome::Canvas::Properties::fill_color("blue");
ladanyi@6
   244
    *(nodesmap[i]) << Gnome::Canvas::Properties::outline_color("black");
ladanyi@63
   245
    nodesmap[i]->raise_to_top();
hegyi@28
   246
hegyi@28
   247
    //initializing edge-text as well, to empty string
hegyi@28
   248
hegyi@150
   249
    XY text_pos(
hegyi@96
   250
        ((mytab.mapstorage).coords[i].x+node_property_defaults[N_RADIUS]+5),
hegyi@96
   251
        ((mytab.mapstorage).coords[i].y+node_property_defaults[N_RADIUS]+5));
hegyi@28
   252
ladanyi@53
   253
    nodetextmap[i]=new Gnome::Canvas::Text(displayed_graph,
ladanyi@53
   254
        text_pos.x, text_pos.y, "");
hegyi@28
   255
    nodetextmap[i]->property_fill_color().set_value("darkblue");
hegyi@149
   256
    nodetextmap[i]->signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::mapEditEventHandler), false);
ladanyi@63
   257
    nodetextmap[i]->raise_to_top();
ladanyi@6
   258
  }
ladanyi@6
   259
hegyi@172
   260
  is_drawn=true;
hegyi@172
   261
hegyi@172
   262
  //upon drawing graph
hegyi@172
   263
  //properties have to
hegyi@172
   264
  //be set in as well
hegyi@172
   265
  for(int i=0;i<NODE_PROPERTY_NUM;i++)
hegyi@172
   266
    {
hegyi@172
   267
      propertyUpdate(Node(INVALID), i);
hegyi@172
   268
    }
hegyi@172
   269
hegyi@172
   270
  for(int i=0;i<EDGE_PROPERTY_NUM;i++)
hegyi@172
   271
    {
hegyi@172
   272
      propertyUpdate(Edge(INVALID), i);
hegyi@172
   273
    }
hegyi@172
   274
ladanyi@6
   275
  updateScrollRegion();
ladanyi@6
   276
}
ladanyi@6
   277
ladanyi@53
   278
void GraphDisplayerCanvas::clear()
ladanyi@6
   279
{
ladanyi@53
   280
  active_node=INVALID;
ladanyi@53
   281
  active_edge=INVALID;
ladanyi@53
   282
  forming_edge=INVALID;
ladanyi@6
   283
hegyi@96
   284
  for (NodeIt n((mytab.mapstorage).graph); n != INVALID; ++n)
ladanyi@6
   285
  {
ladanyi@53
   286
    delete nodesmap[n];
ladanyi@53
   287
    delete nodetextmap[n];
ladanyi@6
   288
  }
ladanyi@6
   289
hegyi@96
   290
  for (EdgeIt e((mytab.mapstorage).graph); e != INVALID; ++e)
ladanyi@53
   291
  {
ladanyi@53
   292
    delete edgesmap[e];
ladanyi@53
   293
    delete edgetextmap[e];
ladanyi@53
   294
  }
hegyi@172
   295
hegyi@172
   296
  is_drawn=false;
ladanyi@6
   297
}
hegyi@154
   298
hegyi@157
   299
void GraphDisplayerCanvas::setView(bool autoscale_p, bool zoomtrack_p, double width_p, double radius_p)
hegyi@154
   300
{
hegyi@154
   301
  autoscale=autoscale_p;
hegyi@157
   302
  edge_width=width_p;
hegyi@157
   303
  radius_size=radius_p;
hegyi@156
   304
hegyi@156
   305
  if((!zoomtrack) && zoomtrack_p)
hegyi@156
   306
    {
hegyi@156
   307
      fixed_zoom_factor=get_pixels_per_unit();
hegyi@156
   308
    }
hegyi@156
   309
hegyi@156
   310
  zoomtrack=zoomtrack_p;
hegyi@156
   311
hegyi@154
   312
  propertyChange(false, N_RADIUS);
hegyi@157
   313
  propertyChange(true, E_WIDTH);
hegyi@154
   314
}
hegyi@154
   315
hegyi@157
   316
void GraphDisplayerCanvas::getView(bool & autoscale_p, bool & zoomtrack_p, double& width_p, double& radius_p)
hegyi@154
   317
{
hegyi@154
   318
  autoscale_p=autoscale;
hegyi@156
   319
  zoomtrack_p=zoomtrack;
hegyi@157
   320
  width_p=edge_width;
hegyi@157
   321
  radius_p=radius_size;
hegyi@154
   322
}
hegyi@160
   323
hegyi@160
   324
void GraphDisplayerCanvas::reDesignGraph()
hegyi@160
   325
{
hegyi@187
   326
  double max_coord=50000;
hegyi@166
   327
  double min_dist=20;
hegyi@166
   328
  double init_vector_length=25;
hegyi@166
   329
hegyi@166
   330
  if(!was_redesigned)
hegyi@166
   331
    {
hegyi@166
   332
      NodeIt i((mytab.mapstorage).graph);
hegyi@167
   333
alpar@168
   334
      dim2::Point<double> init(init_vector_length*rnd(),
alpar@168
   335
			       init_vector_length*rnd());
hegyi@166
   336
      moveNode(init.x, init.y, nodesmap[i], i);
hegyi@166
   337
      was_redesigned=true;
hegyi@166
   338
    }
hegyi@177
   339
hegyi@177
   340
  double attraction;
hegyi@177
   341
  double propulsation;
hegyi@177
   342
  int iterations;
hegyi@177
   343
hegyi@177
   344
  (mytab.mapstorage).get_design_data(attraction, propulsation, iterations);
hegyi@160
   345
hegyi@160
   346
  //iteration counter
hegyi@160
   347
  for(int l=0;l<iterations;l++)
hegyi@160
   348
    {
hegyi@160
   349
      Graph::NodeMap<double> x(mytab.mapstorage.graph);
hegyi@160
   350
      Graph::NodeMap<double> y(mytab.mapstorage.graph);
hegyi@160
   351
      XYMap<Graph::NodeMap<double> > actual_forces;
hegyi@160
   352
      actual_forces.setXMap(x);
hegyi@160
   353
      actual_forces.setYMap(y);
hegyi@160
   354
hegyi@160
   355
      //count actual force for each nodes
hegyi@160
   356
      for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
hegyi@160
   357
	{
hegyi@160
   358
	  //propulsation of nodes
hegyi@160
   359
	  for (NodeIt j((mytab.mapstorage).graph); j!=INVALID; ++j)
hegyi@160
   360
	    {
hegyi@160
   361
	      if(i!=j)
hegyi@160
   362
		{
alpar@180
   363
		  lemon::dim2::Point<double> delta =
alpar@180
   364
		    ((mytab.mapstorage).coords[i]-
alpar@180
   365
		     (mytab.mapstorage).coords[j]);
hegyi@160
   366
alpar@180
   367
		  const double length_sqr=std::max(delta.normSquare(),min_dist);
hegyi@160
   368
hegyi@160
   369
		  //normalize vector
alpar@180
   370
		  delta/=sqrt(length_sqr);
hegyi@160
   371
hegyi@160
   372
		  //calculating propulsation strength
hegyi@160
   373
		  //greater distance menas smaller propulsation strength
hegyi@160
   374
		  delta*=propulsation/length_sqr;
hegyi@160
   375
		    
hegyi@160
   376
		  actual_forces.set(i,(actual_forces[i]+delta));
hegyi@160
   377
		}
hegyi@160
   378
	    }
hegyi@160
   379
	  //attraction of nodes, to which actual node is bound
hegyi@160
   380
	  for(OutEdgeIt ei((mytab.mapstorage).graph,i);ei!=INVALID;++ei)
hegyi@160
   381
	    {
alpar@180
   382
	      lemon::dim2::Point<double> delta =
alpar@180
   383
		((mytab.mapstorage).coords[i]-
alpar@180
   384
		 (mytab.mapstorage).coords[mytab.mapstorage.
alpar@180
   385
					   graph.target(ei)]);
alpar@180
   386
		
alpar@180
   387
		//calculating attraction strength
alpar@180
   388
		//greater distance means greater strength
alpar@180
   389
		delta*=attraction;
alpar@180
   390
		
alpar@180
   391
		actual_forces.set(i,actual_forces[i]-delta);
hegyi@160
   392
	    }
hegyi@160
   393
	  for(InEdgeIt ei((mytab.mapstorage).graph,i);ei!=INVALID;++ei)
hegyi@160
   394
	    {
alpar@180
   395
	      lemon::dim2::Point<double> delta =
alpar@180
   396
		((mytab.mapstorage).coords[i]-
alpar@180
   397
		 (mytab.mapstorage).coords[mytab.mapstorage.
alpar@180
   398
					   graph.source(ei)]);
alpar@180
   399
		
alpar@180
   400
		//calculating attraction strength
alpar@180
   401
		//greater distance means greater strength
alpar@180
   402
		delta*=attraction;
alpar@180
   403
		
alpar@180
   404
		actual_forces.set(i,actual_forces[i]-delta);
hegyi@160
   405
	    }
hegyi@160
   406
	}
hegyi@160
   407
      for (NodeIt i((mytab.mapstorage).graph); i!=INVALID; ++i)
hegyi@160
   408
	{
hegyi@187
   409
	  if(((mytab.mapstorage).coords[i].x)+actual_forces[i].x>max_coord)
hegyi@187
   410
	    {
hegyi@187
   411
	      actual_forces[i].x=max_coord-((mytab.mapstorage).coords[i].x);
hegyi@187
   412
	      std::cout << "Correction! " << (((mytab.mapstorage).coords[i].x)+actual_forces[i].x) << std::endl;
hegyi@187
   413
	    }
hegyi@187
   414
	  else if(((mytab.mapstorage).coords[i].x)+actual_forces[i].x<(0-max_coord))
hegyi@187
   415
	    {
hegyi@187
   416
	      actual_forces[i].x=0-max_coord-((mytab.mapstorage).coords[i].x);
hegyi@187
   417
	      std::cout << "Correction! " << (((mytab.mapstorage).coords[i].x)+actual_forces[i].x) << std::endl;
hegyi@187
   418
	    }
hegyi@187
   419
	  if(((mytab.mapstorage).coords[i].y)+actual_forces[i].y>max_coord)
hegyi@187
   420
	    {
hegyi@187
   421
	      actual_forces[i].y=max_coord-((mytab.mapstorage).coords[i].y);
hegyi@187
   422
	      std::cout << "Correction! " << (((mytab.mapstorage).coords[i].y)+actual_forces[i].y) << std::endl;
hegyi@187
   423
	    }
hegyi@187
   424
	  else if(((mytab.mapstorage).coords[i].y)+actual_forces[i].y<(0-max_coord))
hegyi@187
   425
	    {
hegyi@187
   426
	      actual_forces[i].y=0-max_coord-((mytab.mapstorage).coords[i].y);
hegyi@187
   427
	      std::cout << "Correction! " << (((mytab.mapstorage).coords[i].y)+actual_forces[i].y) << std::endl;
hegyi@187
   428
	    }
hegyi@160
   429
	  moveNode(actual_forces[i].x, actual_forces[i].y, nodesmap[i], i);
hegyi@160
   430
	}
hegyi@160
   431
    }
hegyi@160
   432
}
hegyi@160
   433