graph_displayer_canvas-node.cc
author Akos Ladanyi <ladanyi@tmit.bme.hu>
Mon, 07 Jul 2008 15:20:43 +0100
changeset 3 2cc5ed6e6255
permissions -rw-r--r--
Use hg changeset hash instead of svn revision.
hegyi@1
     1
/* -*- C++ -*-
hegyi@1
     2
 *
hegyi@1
     3
 * This file is a part of LEMON, a generic C++ optimization library
hegyi@1
     4
 *
hegyi@1
     5
 * Copyright (C) 2003-2006
hegyi@1
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
hegyi@1
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
hegyi@1
     8
 *
hegyi@1
     9
 * Permission to use, modify and distribute this software is granted
hegyi@1
    10
 * provided that this copyright notice appears in all copies. For
hegyi@1
    11
 * precise terms see the accompanying LICENSE file.
hegyi@1
    12
 *
hegyi@1
    13
 * This software is provided "AS IS" with no warranty of any kind,
hegyi@1
    14
 * express or implied, and with no claim as to its suitability for any
hegyi@1
    15
 * purpose.
hegyi@1
    16
 *
hegyi@1
    17
 */
hegyi@1
    18
hegyi@1
    19
#include <graph_displayer_canvas.h>
hegyi@1
    20
#include <mapstorage.h>
hegyi@1
    21
#include <nbtab.h>
hegyi@1
    22
#include <cmath>
hegyi@1
    23
hegyi@1
    24
const int minimum_node_radius=5;
hegyi@1
    25
hegyi@1
    26
int DigraphDisplayerCanvas::changeNodeRadius (std::string mapname, Node node)
hegyi@1
    27
{
hegyi@1
    28
  MapStorage& ms = *mytab.mapstorage;
hegyi@1
    29
hegyi@1
    30
  double min, max;
hegyi@1
    31
hegyi@1
    32
  {
hegyi@1
    33
    NodeIt n(ms.digraph);
hegyi@1
    34
    min = max = ms.get(mapname, n);
hegyi@1
    35
    for (; n != INVALID; ++n)
hegyi@1
    36
    {
hegyi@1
    37
      if (static_cast<double>(ms.get(mapname, n)) > max)
hegyi@1
    38
        max = ms.get(mapname, n);
hegyi@1
    39
      if (static_cast<double>(ms.get(mapname, n)) < min)
hegyi@1
    40
        min = ms.get(mapname, n);
hegyi@1
    41
    }
hegyi@1
    42
  }
hegyi@1
    43
hegyi@1
    44
  if(node==INVALID)
hegyi@1
    45
    {
hegyi@1
    46
      for (NodeIt i(ms.digraph); i!=INVALID; ++i)
hegyi@1
    47
	{
hegyi@1
    48
	  double v=fabs(ms.get(mapname,i));
hegyi@1
    49
	  int w;
hegyi@1
    50
	  if(autoscale)
hegyi@1
    51
	    {
hegyi@1
    52
	      if(min==max)
hegyi@1
    53
		{
hegyi@1
    54
		  w=(int)(node_property_defaults[N_RADIUS]);
hegyi@1
    55
		}
hegyi@1
    56
	      else
hegyi@1
    57
		{
hegyi@1
    58
		  w=(int)(minimum_node_radius+(v-min)/(max-min)*(radius_size-minimum_node_radius));
hegyi@1
    59
		}
hegyi@1
    60
	    }
hegyi@1
    61
	  else
hegyi@1
    62
	    {
hegyi@1
    63
	      w=(int)(v*radius_size);
hegyi@1
    64
	    }
hegyi@1
    65
hegyi@1
    66
	  if(w<minimum_node_radius)
hegyi@1
    67
	    {
hegyi@1
    68
	      w=minimum_node_radius;
hegyi@1
    69
	    }
hegyi@1
    70
hegyi@1
    71
	  if(zoomtrack)
hegyi@1
    72
	    {
hegyi@1
    73
	      double actual_ppu=get_pixels_per_unit();
hegyi@1
    74
	      w=(int)(w/actual_ppu*fixed_zoom_factor);
hegyi@1
    75
	    }
hegyi@1
    76
hegyi@1
    77
	  if(w>=0)
hegyi@1
    78
	    {
hegyi@1
    79
	      double x1, y1, x2, y2;
hegyi@1
    80
	      x1=nodesmap[i]->property_x1().get_value();
hegyi@1
    81
	      x2=nodesmap[i]->property_x2().get_value();
hegyi@1
    82
	      y1=nodesmap[i]->property_y1().get_value();
hegyi@1
    83
	      y2=nodesmap[i]->property_y2().get_value();
hegyi@1
    84
	      nodesmap[i]->property_x1().set_value((x1+x2)/2-w);
hegyi@1
    85
	      nodesmap[i]->property_x2().set_value((x1+x2)/2+w);
hegyi@1
    86
	      nodesmap[i]->property_y1().set_value((y1+y2)/2-w);
hegyi@1
    87
	      nodesmap[i]->property_y2().set_value((y1+y2)/2+w);
hegyi@1
    88
	    }
hegyi@1
    89
	}
hegyi@1
    90
    }
hegyi@1
    91
  else
hegyi@1
    92
    {
hegyi@1
    93
      //I think only new nodes use this case
hegyi@1
    94
      //that has no own value, only the default one
hegyi@1
    95
      //int w=(int)(*actual_map)[node];
hegyi@1
    96
      int w=(int)(node_property_defaults[N_RADIUS]);
hegyi@1
    97
      if(w>=0)
hegyi@1
    98
	{
hegyi@1
    99
	  double x1, y1, x2, y2;
hegyi@1
   100
	  x1=nodesmap[node]->property_x1().get_value();
hegyi@1
   101
	  x2=nodesmap[node]->property_x2().get_value();
hegyi@1
   102
	  y1=nodesmap[node]->property_y1().get_value();
hegyi@1
   103
	  y2=nodesmap[node]->property_y2().get_value();
hegyi@1
   104
	  nodesmap[node]->property_x1().set_value((x1+x2)/2-w);
hegyi@1
   105
	  nodesmap[node]->property_x2().set_value((x1+x2)/2+w);
hegyi@1
   106
	  nodesmap[node]->property_y1().set_value((y1+y2)/2-w);
hegyi@1
   107
	  nodesmap[node]->property_y2().set_value((y1+y2)/2+w);
hegyi@1
   108
	}
hegyi@1
   109
    }
hegyi@1
   110
  return 0;
hegyi@1
   111
};
hegyi@1
   112
hegyi@1
   113
int DigraphDisplayerCanvas::resetNodeRadius (Node node)
hegyi@1
   114
{
hegyi@1
   115
  MapStorage& ms = *mytab.mapstorage;
hegyi@1
   116
hegyi@1
   117
  double min, max;
hegyi@1
   118
  min=node_property_defaults[N_RADIUS];
hegyi@1
   119
  max=node_property_defaults[N_RADIUS];
hegyi@1
   120
  Digraph::NodeMap<double> actual_map(ms.digraph,node_property_defaults[N_RADIUS]);
hegyi@1
   121
  
hegyi@1
   122
  if(node==INVALID)
hegyi@1
   123
    {
hegyi@1
   124
      for (NodeIt i(ms.digraph); i!=INVALID; ++i)
hegyi@1
   125
	{
hegyi@1
   126
	  double v=fabs(actual_map[i]);
hegyi@1
   127
	  int w;
hegyi@1
   128
	  if(min==max)
hegyi@1
   129
	    {
hegyi@1
   130
	      w=(int)(node_property_defaults[N_RADIUS]);
hegyi@1
   131
	    }
hegyi@1
   132
	  else
hegyi@1
   133
	    {
hegyi@1
   134
	      w=(int)(MIN_NODE_RADIUS+(v-min)/(max-min)*(MAX_NODE_RADIUS-MIN_NODE_RADIUS));
hegyi@1
   135
	    }
hegyi@1
   136
	  if(zoomtrack)
hegyi@1
   137
	    {
hegyi@1
   138
	      double actual_ppu=get_pixels_per_unit();
hegyi@1
   139
	      w=(int)(w/actual_ppu*fixed_zoom_factor);
hegyi@1
   140
	    }
hegyi@1
   141
	  if(w>=0)
hegyi@1
   142
	    {
hegyi@1
   143
	      double x1, y1, x2, y2;
hegyi@1
   144
	      x1=nodesmap[i]->property_x1().get_value();
hegyi@1
   145
	      x2=nodesmap[i]->property_x2().get_value();
hegyi@1
   146
	      y1=nodesmap[i]->property_y1().get_value();
hegyi@1
   147
	      y2=nodesmap[i]->property_y2().get_value();
hegyi@1
   148
	      nodesmap[i]->property_x1().set_value((x1+x2)/2-w);
hegyi@1
   149
	      nodesmap[i]->property_x2().set_value((x1+x2)/2+w);
hegyi@1
   150
	      nodesmap[i]->property_y1().set_value((y1+y2)/2-w);
hegyi@1
   151
	      nodesmap[i]->property_y2().set_value((y1+y2)/2+w);
hegyi@1
   152
	    }
hegyi@1
   153
	}
hegyi@1
   154
    }
hegyi@1
   155
  else
hegyi@1
   156
    {
hegyi@1
   157
      //I think only new nodes use this case
hegyi@1
   158
//       int w=(int)actual_map[node];
hegyi@1
   159
      int w=(int)(node_property_defaults[N_RADIUS]);
hegyi@1
   160
      if(w>=0)
hegyi@1
   161
	{
hegyi@1
   162
	  double x1, y1, x2, y2;
hegyi@1
   163
	  x1=nodesmap[node]->property_x1().get_value();
hegyi@1
   164
	  x2=nodesmap[node]->property_x2().get_value();
hegyi@1
   165
	  y1=nodesmap[node]->property_y1().get_value();
hegyi@1
   166
	  y2=nodesmap[node]->property_y2().get_value();
hegyi@1
   167
	  nodesmap[node]->property_x1().set_value((x1+x2)/2-w);
hegyi@1
   168
	  nodesmap[node]->property_x2().set_value((x1+x2)/2+w);
hegyi@1
   169
	  nodesmap[node]->property_y1().set_value((y1+y2)/2-w);
hegyi@1
   170
	  nodesmap[node]->property_y2().set_value((y1+y2)/2+w);
hegyi@1
   171
	}
hegyi@1
   172
    }
hegyi@1
   173
  return 0;
hegyi@1
   174
};
hegyi@1
   175
hegyi@1
   176
int DigraphDisplayerCanvas::changeNodeColor (std::string mapname, Node node)
hegyi@1
   177
{  
hegyi@1
   178
  MapStorage& ms = *mytab.mapstorage;
hegyi@1
   179
hegyi@1
   180
  //function maps the range of the maximum and
hegyi@1
   181
  //the minimum of the nodemap to the range of
hegyi@1
   182
  //green in RGB
hegyi@1
   183
hegyi@1
   184
  double max, min;
hegyi@1
   185
hegyi@1
   186
  {
hegyi@1
   187
    NodeIt n(ms.digraph);
hegyi@1
   188
    min = max = ms.get(mapname, n);
hegyi@1
   189
    for (; n != INVALID; ++n)
hegyi@1
   190
    {
hegyi@1
   191
      if (static_cast<double>(ms.get(mapname, n)) > max)
hegyi@1
   192
        max = ms.get(mapname, n);
hegyi@1
   193
      if (static_cast<double>(ms.get(mapname, n)) < min)
hegyi@1
   194
        min = ms.get(mapname, n);
hegyi@1
   195
    }
hegyi@1
   196
  }
hegyi@1
   197
hegyi@1
   198
  if(node==INVALID)
hegyi@1
   199
    {
hegyi@1
   200
hegyi@1
   201
      for (NodeIt i(ms.digraph); i!=INVALID; ++i)
hegyi@1
   202
	{
hegyi@1
   203
	  Gdk::Color color;
hegyi@1
   204
hegyi@1
   205
	  double w=ms.get(mapname, i);
hegyi@1
   206
hegyi@1
   207
	  if(max!=min)
hegyi@1
   208
	    {
hegyi@1
   209
	      color=rainbowColorCounter(min, max, w);
hegyi@1
   210
	    }
hegyi@1
   211
	  else
hegyi@1
   212
	    {
hegyi@1
   213
	      color.set_rgb_p (0, 0, 1);
hegyi@1
   214
	    }
hegyi@1
   215
hegyi@1
   216
	  nodesmap[i]->property_fill_color_gdk().set_value(color);
hegyi@1
   217
	}
hegyi@1
   218
    }
hegyi@1
   219
  else
hegyi@1
   220
    {
hegyi@1
   221
      Gdk::Color color;
hegyi@1
   222
hegyi@1
   223
      double w=ms.get(mapname, node);
hegyi@1
   224
hegyi@1
   225
      if(max!=min)
hegyi@1
   226
	{
hegyi@1
   227
	  color=rainbowColorCounter(min, max, w);
hegyi@1
   228
	}
hegyi@1
   229
      else
hegyi@1
   230
	{
hegyi@1
   231
	  color.set_rgb_p (0, 0, 1);
hegyi@1
   232
	}
hegyi@1
   233
hegyi@1
   234
      nodesmap[node]->property_fill_color_gdk().set_value(color);
hegyi@1
   235
    }
hegyi@1
   236
  return 0;
hegyi@1
   237
};
hegyi@1
   238
hegyi@1
   239
int DigraphDisplayerCanvas::resetNodeColor (Node node)
hegyi@1
   240
{  
hegyi@1
   241
  MapStorage& ms = *mytab.mapstorage;
hegyi@1
   242
hegyi@1
   243
  //function maps the range of the maximum and
hegyi@1
   244
  //the minimum of the nodemap to the range of
hegyi@1
   245
  //green in RGB
hegyi@1
   246
hegyi@1
   247
  Digraph::NodeMap<double> actual_map(ms.digraph,node_property_defaults[N_COLOR]);
hegyi@1
   248
hegyi@1
   249
  double max, min;
hegyi@1
   250
hegyi@1
   251
  max=node_property_defaults[N_COLOR];
hegyi@1
   252
  min=node_property_defaults[N_COLOR];
hegyi@1
   253
hegyi@1
   254
  if(node==INVALID)
hegyi@1
   255
    {
hegyi@1
   256
hegyi@1
   257
      for (NodeIt i(ms.digraph); i!=INVALID; ++i)
hegyi@1
   258
	{
hegyi@1
   259
	  Gdk::Color color;
hegyi@1
   260
hegyi@1
   261
	  double w=actual_map[i];
hegyi@1
   262
hegyi@1
   263
	  if(max!=min)
hegyi@1
   264
	    {
hegyi@1
   265
	      color.set_rgb_p (0, 0, 100*(w-min)/(max-min));
hegyi@1
   266
	    }
hegyi@1
   267
	  else
hegyi@1
   268
	    {
hegyi@1
   269
	      color.set_rgb_p (0, 0, 100);
hegyi@1
   270
	    }
hegyi@1
   271
hegyi@1
   272
	  nodesmap[i]->property_fill_color_gdk().set_value(color);
hegyi@1
   273
	}
hegyi@1
   274
    }
hegyi@1
   275
  else
hegyi@1
   276
    {
hegyi@1
   277
      Gdk::Color color;
hegyi@1
   278
hegyi@1
   279
      double w=actual_map[node];
hegyi@1
   280
hegyi@1
   281
      if(max!=min)
hegyi@1
   282
	{
hegyi@1
   283
	  color.set_rgb_p (0, 0, 100*(w-min)/(max-min));
hegyi@1
   284
	}
hegyi@1
   285
      else
hegyi@1
   286
	{
hegyi@1
   287
	  color.set_rgb_p (0, 0, 100);
hegyi@1
   288
	}
hegyi@1
   289
hegyi@1
   290
      nodesmap[node]->property_fill_color_gdk().set_value(color);
hegyi@1
   291
    }
hegyi@1
   292
  return 0;
hegyi@1
   293
};
hegyi@1
   294
hegyi@1
   295
int DigraphDisplayerCanvas::changeNodeText (std::string mapname, Node node)
hegyi@1
   296
{
hegyi@1
   297
  MapStorage& ms = *mytab.mapstorage;
hegyi@1
   298
hegyi@1
   299
  //the number in the map will be written on the node
hegyi@1
   300
  //EXCEPT when the name of the map is Text, because
hegyi@1
   301
  //in that case empty string will be written, because
hegyi@1
   302
  //that is the deleter map
hegyi@1
   303
hegyi@1
   304
  if(node==INVALID)
hegyi@1
   305
    {
hegyi@1
   306
      for (NodeIt i(ms.digraph); i!=INVALID; ++i)
hegyi@1
   307
	{
hegyi@1
   308
	  nodemap_to_edit=mapname;
hegyi@1
   309
hegyi@1
   310
	  nodetextmap[i]->property_text().set_value(
hegyi@1
   311
              static_cast<std::string>(ms.get(mapname, i)));
hegyi@1
   312
	}
hegyi@1
   313
    }
hegyi@1
   314
  else
hegyi@1
   315
    {
hegyi@1
   316
      nodetextmap[node]->property_text().set_value(
hegyi@1
   317
          static_cast<std::string>(ms.get(mapname, node)));
hegyi@1
   318
    }
hegyi@1
   319
  return 0;
hegyi@1
   320
};
hegyi@1
   321
hegyi@1
   322
int DigraphDisplayerCanvas::resetNodeText (Node node)
hegyi@1
   323
{
hegyi@1
   324
  MapStorage& ms = *mytab.mapstorage;
hegyi@1
   325
hegyi@1
   326
  //the number in the map will be written on the node
hegyi@1
   327
  //EXCEPT when the name of the map is Text, because
hegyi@1
   328
  //in that case empty string will be written, because
hegyi@1
   329
  //that is the deleter map
hegyi@1
   330
hegyi@1
   331
  if(node==INVALID)
hegyi@1
   332
    {
hegyi@1
   333
      for (NodeIt i(ms.digraph); i!=INVALID; ++i)
hegyi@1
   334
	{
hegyi@1
   335
	  nodemap_to_edit="";
hegyi@1
   336
	  nodetextmap[i]->property_text().set_value("");
hegyi@1
   337
	}
hegyi@1
   338
    }
hegyi@1
   339
  else
hegyi@1
   340
    {
hegyi@1
   341
      nodetextmap[node]->property_text().set_value("");
hegyi@1
   342
    }
hegyi@1
   343
  return 0;
hegyi@1
   344
};