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