gui/gdc-broken_edge.cc
author deba
Mon, 05 Dec 2005 17:03:58 +0000
changeset 1848 291764ce6d91
parent 1819 fd82adfbe905
child 1860 27a9a75b957b
permissions -rw-r--r--
Doc bug fix
hegyi@1819
     1
#include "graph_displayer_canvas.h"
alpar@1632
     2
#include <cmath>
hegyi@1497
     3
hegyi@1819
     4
GraphDisplayerCanvas::BrokenEdge::BrokenEdge(Gnome::Canvas::Group & g, Gnome::Canvas::Points p, GraphDisplayerCanvas & gc) : Line(g), gdc(gc), isbutton(false)
hegyi@1497
     5
{
hegyi@1499
     6
  my_points=new Gnome::Art::Point[3];
hegyi@1499
     7
hegyi@1499
     8
  arrow=new Gnome::Canvas::Polygon(g);
hegyi@1499
     9
  *arrow << Gnome::Canvas::Properties::fill_color("red");
hegyi@1819
    10
  arrow->signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::BrokenEdge::edgeFormerEventHandler));
ladanyi@1645
    11
  arrow->lower_to_bottom();
hegyi@1524
    12
  setPoints(p);
hegyi@1499
    13
}
hegyi@1499
    14
hegyi@1819
    15
GraphDisplayerCanvas::BrokenEdge::~BrokenEdge()
hegyi@1499
    16
{
hegyi@1499
    17
  if(arrow)delete(arrow);
hegyi@1499
    18
}
hegyi@1499
    19
hegyi@1819
    20
void GraphDisplayerCanvas::BrokenEdge::setPoints(Gnome::Canvas::Points p, bool move)
hegyi@1499
    21
{
hegyi@1499
    22
  bool set_arrow=false;
hegyi@1601
    23
  //red arrow losts its position-right button
hegyi@1500
    24
  if(!move)
hegyi@1497
    25
    {
hegyi@1500
    26
      if(p.size()==2)
hegyi@1500
    27
	{
hegyi@1500
    28
	  set_arrow=true;
hegyi@1500
    29
	  Gnome::Canvas::Points points_with_center;
hegyi@1500
    30
	  points_with_center.push_back(my_points[0]=p[0]);
hegyi@1500
    31
	  points_with_center.push_back(my_points[1]=Gnome::Art::Point( (p[0].get_x()+p[1].get_x())/2+0 , (p[0].get_y()+p[1].get_y())/2 )+0 );
hegyi@1500
    32
	  points_with_center.push_back(my_points[2]=p[1]);
hegyi@1500
    33
	  property_points().set_value(points_with_center);
hegyi@1500
    34
	}  
hegyi@1500
    35
      if(p.size()==3)
hegyi@1500
    36
	{
hegyi@1500
    37
	  set_arrow=true;
hegyi@1500
    38
	  property_points().set_value(p);
hegyi@1500
    39
	  for(int i=0;i<3;i++)
hegyi@1500
    40
	    {
hegyi@1500
    41
	      my_points[i]=p[i];
hegyi@1500
    42
	    }
hegyi@1500
    43
	}
hegyi@1500
    44
    }
hegyi@1500
    45
  else
hegyi@1499
    46
    {
hegyi@1601
    47
      //arrow keeps its position-left button
hegyi@1601
    48
hegyi@1714
    49
//       if(p.size()==2)
hegyi@1714
    50
//       	{
hegyi@1714
    51
//       	  Gnome::Canvas::Points points;
hegyi@1714
    52
//       	  my_points[0]=p[0];
hegyi@1714
    53
//       	  my_points[2]=p[1];
hegyi@1714
    54
//       	  for(int i=0;i<3;i++)
hegyi@1714
    55
//       	    {
hegyi@1714
    56
//       	      points.push_back(my_points[i]);
hegyi@1714
    57
//       	    }
hegyi@1714
    58
//       	  property_points().set_value(points);
hegyi@1714
    59
//       	}
hegyi@1602
    60
      set_arrow=true;
hegyi@1601
    61
hegyi@1601
    62
      //////////////////////////////////////////////////////////////////////////////////////////////////////
hegyi@1714
    63
      /////////// keeps shape-with scalar multiplication - version 2.
hegyi@1601
    64
      //////////////////////////////////////////////////////////////////////////////////////////////////////
hegyi@1601
    65
hegyi@1714
    66
      if(p.size()==2)
hegyi@1714
    67
      	{
hegyi@1714
    68
	  //old vector from one to the other node - a
hegyi@1714
    69
	  xy<double> a_v(my_points[2].get_x()-my_points[0].get_x(),my_points[2].get_y()-my_points[0].get_y());
hegyi@1714
    70
	  //new vector from one to the other node - b
hegyi@1714
    71
	  xy<double> b_v(p[1].get_x()-p[0].get_x(),p[1].get_y()-p[0].get_y());
hegyi@1601
    72
hegyi@1714
    73
	  double absa=sqrt(a_v.normSquare());
hegyi@1714
    74
	  double absb=sqrt(b_v.normSquare());
hegyi@1601
    75
hegyi@1831
    76
	  if((absa!=0)&&(absb!=0))
hegyi@1831
    77
	    {
hegyi@1831
    78
	      //old vector from one node to the breakpoint - c
hegyi@1831
    79
	      xy<double> c_v(my_points[1].get_x()-my_points[0].get_x(),my_points[1].get_y()-my_points[0].get_y());
hegyi@1601
    80
hegyi@1831
    81
	      //unit vector with the same direction to a_v
hegyi@1831
    82
	      xy<double> a_v_u(a_v.x/absa,a_v.y/absa);
hegyi@1601
    83
hegyi@1831
    84
	      //normal vector of unit vector with the same direction to a_v
hegyi@1831
    85
	      xy<double> a_v_u_n(((-1)*a_v_u.y),a_v_u.x);
hegyi@1601
    86
hegyi@1831
    87
	      //unit vector with the same direction to b_v
hegyi@1831
    88
	      xy<double> b_v_u(b_v.x/absb,b_v.y/absb);
hegyi@1601
    89
hegyi@1831
    90
	      //normal vector of unit vector with the same direction to b_v
hegyi@1831
    91
	      xy<double> b_v_u_n(((-1)*b_v_u.y),b_v_u.x);
hegyi@1601
    92
hegyi@1831
    93
	      //vector c in a_v_u and a_v_u_n co-ordinate system
hegyi@1831
    94
	      xy<double> c_a(c_v*a_v_u,c_v*a_v_u_n);
hegyi@1601
    95
hegyi@1831
    96
	      //new vector from one node to the breakpoint - d - we have to calculate this one
hegyi@1831
    97
	      xy<double> d_v=absb/absa*(c_a.x*b_v_u+c_a.y*b_v_u_n);
hegyi@1601
    98
hegyi@1831
    99
	      my_points[1]=Gnome::Art::Point(d_v.x+p[0].get_x(),d_v.y+p[0].get_y());
hegyi@1601
   100
hegyi@1831
   101
	      my_points[0]=p[0];
hegyi@1831
   102
	      my_points[2]=p[1];
hegyi@1831
   103
	  
hegyi@1831
   104
	      Gnome::Canvas::Points points;
hegyi@1831
   105
	      for(int i=0;i<3;i++)
hegyi@1831
   106
		{
hegyi@1831
   107
		  points.push_back(my_points[i]);
hegyi@1831
   108
		}
hegyi@1831
   109
	      property_points().set_value(points);
hegyi@1831
   110
	    }
hegyi@1831
   111
	  else
hegyi@1714
   112
	    {
hegyi@1831
   113
	      //if distance is 0, segmentation would be occured
hegyi@1831
   114
	      //in calculations, because of division by zero
hegyi@1831
   115
	      //But we have luck: the edge cannot be seen in
hegyi@1831
   116
	      //this case, so no update needed
hegyi@1831
   117
	      set_arrow=false;
hegyi@1714
   118
	    }
hegyi@1714
   119
	}
hegyi@1499
   120
    }
hegyi@1499
   121
  if(set_arrow)
hegyi@1499
   122
    {
hegyi@1499
   123
      //calculating coordinates of the direction indicator arrow
hegyi@1499
   124
hegyi@1499
   125
      xy<gdouble> target( my_points[2].get_x(), my_points[2].get_y() );
hegyi@1499
   126
      xy<gdouble> center( my_points[1].get_x(), my_points[1].get_y() );
hegyi@1499
   127
hegyi@1499
   128
      xy<gdouble> unit_vector_in_dir(target-center);
hegyi@1831
   129
      double length=sqrt( unit_vector_in_dir.normSquare() );
hegyi@1831
   130
hegyi@1499
   131
      //       std::cout << target << " - " << center << " = " << unit_vector_in_dir << "    / " <<unit_vector_in_dir.normSquare() ;
hegyi@1831
   132
      unit_vector_in_dir/=length;
hegyi@1499
   133
      //       std::cout << " = " << unit_vector_in_dir << std::endl;
hegyi@1499
   134
hegyi@1499
   135
      xy<gdouble> unit_norm_vector(0-unit_vector_in_dir.y, unit_vector_in_dir.x);
hegyi@1499
   136
      //       std::cout << unit_norm_vector << std::endl;
hegyi@1499
   137
hegyi@1499
   138
      {      
hegyi@1504
   139
	//       /\       // top
hegyi@1504
   140
	//      /  \      //
hegyi@1504
   141
	//      -  -      // c(enter)l(eft), ccl, ccr, cr
hegyi@1504
   142
	//       ||       //
hegyi@1504
   143
	//       ||       // b(ottom)l, br
hegyi@1499
   144
      }
hegyi@1499
   145
hegyi@1499
   146
      double size=3;
hegyi@1499
   147
hegyi@1499
   148
      xy<gdouble> bl (center - unit_vector_in_dir * 3 * size + unit_norm_vector * size );
hegyi@1499
   149
      xy<gdouble> br (center - unit_vector_in_dir * 3 * size - unit_norm_vector * size );
hegyi@1499
   150
      xy<gdouble> ccl(center + unit_vector_in_dir *  size + unit_norm_vector * size );
hegyi@1499
   151
      xy<gdouble> ccr(center + unit_vector_in_dir *  size - unit_norm_vector * size );
hegyi@1499
   152
      xy<gdouble> cl (center + unit_vector_in_dir *  size + unit_norm_vector * 2 * size );
hegyi@1499
   153
      xy<gdouble> cr (center + unit_vector_in_dir *  size - unit_norm_vector * 2 * size );
hegyi@1499
   154
      xy<gdouble> top(center + unit_vector_in_dir * 3 * size);
hegyi@1499
   155
	 
hegyi@1831
   156
      //std::cout << bl << " " << br << " " << ccl << " "  << ccr << " " << cl << " " << cr << " " << top << std::endl;
hegyi@1499
   157
hegyi@1499
   158
      Gnome::Canvas::Points arrow_points;
hegyi@1499
   159
      arrow_points.push_back(Gnome::Art::Point( bl.x , bl.y  ) );
hegyi@1499
   160
      arrow_points.push_back(Gnome::Art::Point( br.x , br.y  ) );
hegyi@1499
   161
      arrow_points.push_back(Gnome::Art::Point( ccr.x, ccr.y ) );
hegyi@1499
   162
      arrow_points.push_back(Gnome::Art::Point( cr.x , cr.y  ) );
hegyi@1499
   163
      arrow_points.push_back(Gnome::Art::Point( top.x, top.y ) );
hegyi@1499
   164
      arrow_points.push_back(Gnome::Art::Point( cl.x , cl.y  ) );
hegyi@1499
   165
      arrow_points.push_back(Gnome::Art::Point( ccl.x, ccl.y ) );
hegyi@1499
   166
hegyi@1499
   167
      arrow->property_points().set_value(arrow_points);
hegyi@1497
   168
    }
hegyi@1497
   169
}
hegyi@1499
   170
hegyi@1819
   171
bool GraphDisplayerCanvas::BrokenEdge::edgeFormerEventHandler(GdkEvent* e)
hegyi@1499
   172
{
hegyi@1499
   173
  switch(e->type)
hegyi@1499
   174
    {
hegyi@1499
   175
    case GDK_BUTTON_PRESS:
hegyi@1499
   176
      //we mark the location of the event to be able to calculate parameters of dragging
hegyi@1524
   177
      if(gdc.getActualTool()!=CREATE_NODE)
hegyi@1501
   178
	{
hegyi@1524
   179
	  gdc.toggleEdgeActivity(this, true);
hegyi@1501
   180
	  clicked_x=e->button.x;
hegyi@1501
   181
	  clicked_y=e->button.y;
hegyi@1501
   182
	  isbutton=true;
hegyi@1501
   183
	}
hegyi@1499
   184
      break;
hegyi@1499
   185
    case GDK_BUTTON_RELEASE:
hegyi@1524
   186
      if(gdc.getActualTool()!=CREATE_NODE)
hegyi@1505
   187
	{
hegyi@1524
   188
	  gdc.toggleEdgeActivity(this, false);
hegyi@1505
   189
	  isbutton=false;
hegyi@1505
   190
	}
hegyi@1499
   191
      break;
hegyi@1499
   192
    case GDK_MOTION_NOTIFY:
hegyi@1499
   193
      //we only have to do sg. if the mouse button is pressed
hegyi@1499
   194
      if(isbutton)
hegyi@1499
   195
	{
hegyi@1499
   196
	  //new coordinates will be the old values,
hegyi@1499
   197
	  //because the item will be moved to the
hegyi@1499
   198
	  //new coordinate therefore the new movement
hegyi@1499
   199
	  //has to be calculated from here
hegyi@1499
   200
hegyi@1499
   201
	  double dx=e->motion.x-clicked_x;
hegyi@1499
   202
	  double dy=e->motion.y-clicked_y;
hegyi@1499
   203
hegyi@1499
   204
	  Gnome::Canvas::Points points_new;
hegyi@1499
   205
hegyi@1499
   206
	  points_new.push_back(my_points[0]);
hegyi@1499
   207
	  points_new.push_back(my_points[1]=Gnome::Art::Point(my_points[1].get_x()+dx,my_points[1].get_y()+dy));
hegyi@1499
   208
	  points_new.push_back(my_points[2]);
hegyi@1499
   209
hegyi@1524
   210
	  setPoints(points_new);
hegyi@1524
   211
	  gdc.textReposition(xy<double>(my_points[1].get_x(),my_points[1].get_y()));
hegyi@1499
   212
hegyi@1499
   213
	  clicked_x=e->motion.x;
hegyi@1499
   214
	  clicked_y=e->motion.y;
hegyi@1499
   215
hegyi@1499
   216
	}
hegyi@1499
   217
    default: break;
hegyi@1499
   218
    }
hegyi@1499
   219
hegyi@1499
   220
  return true;
hegyi@1499
   221
}
hegyi@1505
   222
hegyi@1819
   223
xy<double> GraphDisplayerCanvas::BrokenEdge::getArrowPos()
hegyi@1505
   224
{
hegyi@1505
   225
  xy<double> ret_val(my_points[1].get_x(),my_points[1].get_y());
hegyi@1505
   226
  return ret_val;
hegyi@1505
   227
}