gui/broken_edge.cc
author alpar
Wed, 03 Aug 2005 19:10:12 +0000
changeset 1613 cd237f1936f8
parent 1602 c0d7a9247963
child 1632 93ac8c521fe5
permissions -rw-r--r--
Bugfix due to Gabor.
ladanyi@1606
     1
#include "broken_edge.h"
hegyi@1499
     2
#include <math.h>
hegyi@1497
     3
hegyi@1501
     4
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@1524
    10
  arrow->signal_event().connect(sigc::mem_fun(*this, &BrokenEdge::edgeFormerEventHandler));
hegyi@1524
    11
  setPoints(p);
hegyi@1499
    12
}
hegyi@1499
    13
hegyi@1499
    14
BrokenEdge::~BrokenEdge()
hegyi@1499
    15
{
hegyi@1499
    16
  if(arrow)delete(arrow);
hegyi@1499
    17
}
hegyi@1499
    18
hegyi@1524
    19
void BrokenEdge::setPoints(Gnome::Canvas::Points p, bool move)
hegyi@1499
    20
{
hegyi@1499
    21
  bool set_arrow=false;
hegyi@1601
    22
  //red arrow losts its position-right button
hegyi@1500
    23
  if(!move)
hegyi@1497
    24
    {
hegyi@1500
    25
      if(p.size()==2)
hegyi@1500
    26
	{
hegyi@1500
    27
	  set_arrow=true;
hegyi@1500
    28
	  Gnome::Canvas::Points points_with_center;
hegyi@1500
    29
	  points_with_center.push_back(my_points[0]=p[0]);
hegyi@1500
    30
	  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
    31
	  points_with_center.push_back(my_points[2]=p[1]);
hegyi@1500
    32
	  property_points().set_value(points_with_center);
hegyi@1500
    33
	}  
hegyi@1500
    34
      if(p.size()==3)
hegyi@1500
    35
	{
hegyi@1500
    36
	  set_arrow=true;
hegyi@1500
    37
	  property_points().set_value(p);
hegyi@1500
    38
	  for(int i=0;i<3;i++)
hegyi@1500
    39
	    {
hegyi@1500
    40
	      my_points[i]=p[i];
hegyi@1500
    41
	    }
hegyi@1500
    42
	}
hegyi@1500
    43
    }
hegyi@1500
    44
  else
hegyi@1499
    45
    {
hegyi@1601
    46
      //arrow keeps its position-left button
hegyi@1601
    47
hegyi@1602
    48
      if(p.size()==2)
hegyi@1602
    49
      	{
hegyi@1602
    50
      	  Gnome::Canvas::Points points;
hegyi@1602
    51
      	  my_points[0]=p[0];
hegyi@1602
    52
      	  my_points[2]=p[1];
hegyi@1602
    53
      	  for(int i=0;i<3;i++)
hegyi@1602
    54
      	    {
hegyi@1602
    55
      	      points.push_back(my_points[i]);
hegyi@1602
    56
      	    }
hegyi@1602
    57
      	  property_points().set_value(points);
hegyi@1602
    58
      	}
hegyi@1602
    59
      set_arrow=true;
hegyi@1601
    60
hegyi@1601
    61
      //////////////////////////////////////////////////////////////////////////////////////////////////////
hegyi@1601
    62
      /////////// kepps shape-with angles
hegyi@1601
    63
      //////////////////////////////////////////////////////////////////////////////////////////////////////
hegyi@1601
    64
hegyi@1601
    65
hegyi@1601
    66
//       //old vector from one to the other node
hegyi@1601
    67
//       xy<double> o_p2p(my_points[2].get_x()-my_points[0].get_x(),my_points[2].get_y()-my_points[0].get_y());
hegyi@1601
    68
//       //projection of the old vector to positive x axis
hegyi@1601
    69
//       xy<double> o_x_p2p(fabs(o_p2p.x),0);
hegyi@1601
    70
//       //length of p2p vector
hegyi@1601
    71
//       double o_l_p2p=sqrt( o_p2p.normSquare() );
hegyi@1601
    72
//       if(o_p2p.x<0)
hegyi@1601
    73
// 	{
hegyi@1601
    74
// 	  o_l_p2p*=-1;
hegyi@1601
    75
// 	}
hegyi@1601
    76
//       //length of projection of p2p vector
hegyi@1601
    77
//       double o_l_x_p2p=sqrt( o_x_p2p.normSquare() );
hegyi@1601
    78
//       //old angle of p2p vector to the x axis
hegyi@1601
    79
//       double o_a_p2p=acos(o_l_x_p2p/o_l_p2p);
hegyi@1601
    80
//       if(o_p2p.y>0)
hegyi@1601
    81
// 	{
hegyi@1601
    82
// 	  o_a_p2p=2*M_PI-o_a_p2p;
hegyi@1601
    83
// 	}
hegyi@1601
    84
hegyi@1601
    85
//       //old vector from first node to the breakpoint
hegyi@1601
    86
//       xy<double> o_1b((my_points[1].get_x()-my_points[0].get_x()),(my_points[1].get_y()-my_points[0].get_y()));
hegyi@1601
    87
//       //projection of the old node-breakpoint vector to positive x axis
hegyi@1601
    88
//       xy<double> o_x_1b(fabs(o_1b.x),0);
hegyi@1601
    89
//       //length of 1b vector
hegyi@1601
    90
//       double o_l_1b=sqrt( o_1b.normSquare() );
hegyi@1601
    91
//       if(o_1b.x<0)
hegyi@1601
    92
// 	{
hegyi@1601
    93
// 	  o_l_1b*=-1;
hegyi@1601
    94
// 	}
hegyi@1601
    95
//       //length of projection of 1b vector
hegyi@1601
    96
//       double o_l_x_1b=sqrt( o_x_1b.normSquare() );
hegyi@1601
    97
//       //old angle of 1b vector to the x axis
hegyi@1601
    98
//       double o_a_1b=acos(o_l_x_1b/o_l_1b);
hegyi@1601
    99
//       if(o_1b.y>0)
hegyi@1601
   100
// 	{
hegyi@1601
   101
// 	  o_a_1b=2*M_PI-o_a_1b;
hegyi@1601
   102
// 	}
hegyi@1601
   103
hegyi@1601
   104
//       if(p.size()==2)
hegyi@1601
   105
//       	{
hegyi@1601
   106
// 	  set_arrow=true;
hegyi@1601
   107
hegyi@1601
   108
//       	  my_points[0]=p[0];
hegyi@1601
   109
//       	  my_points[2]=p[1];
hegyi@1601
   110
hegyi@1601
   111
// 	  //new vector from one to the other node
hegyi@1601
   112
// 	  xy<double> n_p2p(my_points[2].get_x()-my_points[0].get_x(),my_points[2].get_y()-my_points[0].get_y());
hegyi@1601
   113
// 	  //projection of the new vector to positive x axis
hegyi@1601
   114
// 	  xy<double> n_x_p2p(fabs(n_p2p.x),0);
hegyi@1601
   115
// 	  //length of p2p vector
hegyi@1601
   116
// 	  double n_l_p2p=sqrt( n_p2p.normSquare() );
hegyi@1601
   117
// 	  if(n_p2p.x<0)
hegyi@1601
   118
// 	    {
hegyi@1601
   119
// 	      n_l_p2p*=-1;
hegyi@1601
   120
// 	    }
hegyi@1601
   121
// 	  //length of projection of p2p vector
hegyi@1601
   122
// 	  double n_l_x_p2p=sqrt( n_x_p2p.normSquare() );
hegyi@1601
   123
// 	  //new angle of p2p vector to the x axis
hegyi@1601
   124
// 	  double n_a_p2p=acos(n_l_x_p2p/n_l_p2p);
hegyi@1601
   125
// 	  if(n_p2p.y>0)
hegyi@1601
   126
// 	    {
hegyi@1601
   127
// 	      n_a_p2p=2*M_PI-n_a_p2p;
hegyi@1601
   128
// 	    }
hegyi@1601
   129
hegyi@1601
   130
// 	  //new angle of 1b vector to the x axis
hegyi@1601
   131
// 	  double n_a_1b=o_a_1b+n_a_p2p-o_a_p2p;
hegyi@1601
   132
hegyi@1601
   133
// 	  std::cout << " p2p regi: " << o_a_p2p/M_PI*180 << " uj: " << n_a_p2p/M_PI*180-(int)n_a_p2p/M_PI*180 << std::endl;
hegyi@1601
   134
// 	  std::cout << " 1b regi: " << o_a_1b/M_PI*180 << " uj: " << n_a_1b/M_PI*180-(int)n_a_1b/M_PI*180 << std::endl;
hegyi@1601
   135
hegyi@1601
   136
// // 	  std::cout << o_p2p << " " << n_p2p << std::endl;
hegyi@1601
   137
hegyi@1601
   138
// 	  if((n_a_1b>M_PI*3/2)||(n_a_1b<M_PI/2))
hegyi@1601
   139
// 	    {
hegyi@1601
   140
// 	      std::cout << "jobb terfel" << std::endl;
hegyi@1601
   141
// 	      my_points[1]=Gnome::Art::Point(p[0].get_x()+cos(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p,p[0].get_y()-sin(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p);
hegyi@1601
   142
// 	    }
hegyi@1601
   143
// 	  else if((n_a_1b<M_PI*3/2)&&(n_a_1b>M_PI/2))
hegyi@1601
   144
// 	    {
hegyi@1601
   145
// 	      std::cout << "bal terfel" << std::endl;
hegyi@1601
   146
// 	      my_points[1]=Gnome::Art::Point(p[0].get_x()-cos(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p,p[0].get_y()+sin(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p);
hegyi@1601
   147
// 	    }
hegyi@1601
   148
// 	  else
hegyi@1601
   149
// 	    {
hegyi@1601
   150
// 	      std::cout << "y tengely" << std::endl;
hegyi@1601
   151
// 	      double new_y=my_points[1].get_y();
hegyi@1601
   152
// 	      my_points[1]=Gnome::Art::Point(p[0].get_x()-cos(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p,new_y);
hegyi@1601
   153
// 	    }
hegyi@1601
   154
hegyi@1601
   155
// 	  std::cout << "alap: " << p[0] << " eredeti hossz: " << o_l_1b << " nagy uj: " << n_l_p2p << " nagy regi: " << o_l_p2p << " a*b/c " << o_l_1b*n_l_p2p/o_l_p2p << " eredmeny: " << my_points[1] << std::endl;
hegyi@1601
   156
hegyi@1601
   157
hegyi@1601
   158
// // 	  if(o_1b.x*o_1b.y>0)
hegyi@1601
   159
// // 	    {
hegyi@1601
   160
// // 	      if(n_p2p.x>0)
hegyi@1601
   161
// // 		{
hegyi@1601
   162
// // 		  my_points[1]=Gnome::Art::Point(p[0].get_x()+cos(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p,p[0].get_y()+sin(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p);
hegyi@1601
   163
// // 		}
hegyi@1601
   164
// // 	      else
hegyi@1601
   165
// // 		{
hegyi@1601
   166
// // 		  my_points[1]=Gnome::Art::Point(p[0].get_x()-cos(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p,p[0].get_y()-sin(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p);
hegyi@1601
   167
// // 		}
hegyi@1601
   168
// // 	    }
hegyi@1601
   169
// // 	  else if(o_1b.x*o_1b.y<0)
hegyi@1601
   170
// // 	    {
hegyi@1601
   171
// // 	      if(n_p2p.x>0)
hegyi@1601
   172
// // 		{
hegyi@1601
   173
// // 		  my_points[1]=Gnome::Art::Point(p[0].get_x()+cos(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p,p[0].get_y()-sin(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p);
hegyi@1601
   174
// // 		}
hegyi@1601
   175
// // 	      else
hegyi@1601
   176
// // 		{
hegyi@1601
   177
// // 		  my_points[1]=Gnome::Art::Point(p[0].get_x()-cos(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p,p[0].get_y()+sin(n_a_1b)*o_l_1b*n_l_p2p/o_l_p2p);
hegyi@1601
   178
// // 		}
hegyi@1601
   179
// // 	    }
hegyi@1601
   180
// // 	  else
hegyi@1601
   181
// // 	    {
hegyi@1601
   182
// // 	    }
hegyi@1601
   183
hegyi@1601
   184
      //////////////////////////////////////////////////////////////////////////////////////////////////////
hegyi@1601
   185
      /////////// kepps shape-with scalar multiplication
hegyi@1601
   186
      //////////////////////////////////////////////////////////////////////////////////////////////////////
hegyi@1601
   187
hegyi@1602
   188
//       if(p.size()==2)
hegyi@1602
   189
//       	{
hegyi@1602
   190
// 	  //old vector from one to the other node - a
hegyi@1602
   191
// 	  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@1602
   192
// 	  //new vector from one to the other node - b
hegyi@1602
   193
// 	  xy<double> b_v(p[1].get_x()-p[0].get_x(),p[1].get_y()-p[0].get_y());
hegyi@1601
   194
hegyi@1602
   195
// 	  //old vector from one node to the breakpoint - c
hegyi@1602
   196
// 	  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
   197
hegyi@1602
   198
// 	  //new vector from one node to the breakpoint - d - we have to calculate this one
hegyi@1602
   199
// 	  xy<double> d_v;
hegyi@1601
   200
hegyi@1602
   201
// 	  //scalar product of a and b (old and new vector from first point to the other)
hegyi@1602
   202
// 	  double sab=a_v*b_v;
hegyi@1602
   203
// 	  //scalar product of c and d (old and new vector from first point to breakpoint)
hegyi@1602
   204
// 	  double scd=sab*c_v.normSquare()/a_v.normSquare();
hegyi@1601
   205
hegyi@1602
   206
// 	  std::cout<<" a " << a_v<<" b " <<b_v<<" c " <<c_v<<" sab " <<sab<<" scd "<<scd<<std::endl;
hegyi@1601
   207
hegyi@1602
   208
// 	  double a=c_v.normSquare();
hegyi@1602
   209
// 	  double b=2*scd*c_v.y;
hegyi@1602
   210
// 	  double c=scd*scd-b_v.normSquare()/a_v.normSquare()*c_v.normSquare()*c_v.x*c_v.x;
hegyi@1601
   211
hegyi@1602
   212
// 	  std::cout<<" a " << a<<" b " <<b<<" c " <<c<<std::endl;
hegyi@1601
   213
hegyi@1602
   214
// 	  d_v.y=(-b-sqrt(b*b-4*a*c))/2/a;
hegyi@1601
   215
hegyi@1602
   216
// 	  if(c_v.x!=0)
hegyi@1602
   217
// 	    {
hegyi@1602
   218
// 	      d_v.x=(scd-c_v.y*d_v.y)/c_v.x;
hegyi@1602
   219
// 	    }
hegyi@1602
   220
// 	  else
hegyi@1602
   221
// 	    {
hegyi@1602
   222
// 	      d_v.x=my_points[1].get_x();
hegyi@1602
   223
// 	    }
hegyi@1601
   224
hegyi@1602
   225
// 	  std::cout<<" d " << d_v<<std::endl;
hegyi@1601
   226
hegyi@1602
   227
// 	  my_points[1]=Gnome::Art::Point(d_v.x+p[0].get_x(),d_v.y+p[0].get_y());
hegyi@1601
   228
hegyi@1602
   229
// 	  Gnome::Canvas::Points points;
hegyi@1602
   230
// 	  for(int i=0;i<3;i++)
hegyi@1602
   231
// 	    {
hegyi@1602
   232
// 	      points.push_back(my_points[i]);
hegyi@1602
   233
// 	    }
hegyi@1602
   234
// 	  property_points().set_value(points);
hegyi@1602
   235
// 	}
hegyi@1499
   236
    }
hegyi@1499
   237
  if(set_arrow)
hegyi@1499
   238
    {
hegyi@1499
   239
      //calculating coordinates of the direction indicator arrow
hegyi@1499
   240
hegyi@1499
   241
      xy<gdouble> target( my_points[2].get_x(), my_points[2].get_y() );
hegyi@1499
   242
      xy<gdouble> center( my_points[1].get_x(), my_points[1].get_y() );
hegyi@1499
   243
hegyi@1499
   244
      xy<gdouble> unit_vector_in_dir(target-center);
hegyi@1499
   245
      //       std::cout << target << " - " << center << " = " << unit_vector_in_dir << "    / " <<unit_vector_in_dir.normSquare() ;
hegyi@1499
   246
      unit_vector_in_dir/=sqrt( unit_vector_in_dir.normSquare() );
hegyi@1499
   247
      //       std::cout << " = " << unit_vector_in_dir << std::endl;
hegyi@1499
   248
hegyi@1499
   249
      xy<gdouble> unit_norm_vector(0-unit_vector_in_dir.y, unit_vector_in_dir.x);
hegyi@1499
   250
      //       std::cout << unit_norm_vector << std::endl;
hegyi@1499
   251
hegyi@1499
   252
      {      
hegyi@1504
   253
	//       /\       // top
hegyi@1504
   254
	//      /  \      //
hegyi@1504
   255
	//      -  -      // c(enter)l(eft), ccl, ccr, cr
hegyi@1504
   256
	//       ||       //
hegyi@1504
   257
	//       ||       // b(ottom)l, br
hegyi@1499
   258
      }
hegyi@1499
   259
hegyi@1499
   260
      double size=3;
hegyi@1499
   261
hegyi@1499
   262
      xy<gdouble> bl (center - unit_vector_in_dir * 3 * size + unit_norm_vector * size );
hegyi@1499
   263
      xy<gdouble> br (center - unit_vector_in_dir * 3 * size - unit_norm_vector * size );
hegyi@1499
   264
      xy<gdouble> ccl(center + unit_vector_in_dir *  size + unit_norm_vector * size );
hegyi@1499
   265
      xy<gdouble> ccr(center + unit_vector_in_dir *  size - unit_norm_vector * size );
hegyi@1499
   266
      xy<gdouble> cl (center + unit_vector_in_dir *  size + unit_norm_vector * 2 * size );
hegyi@1499
   267
      xy<gdouble> cr (center + unit_vector_in_dir *  size - unit_norm_vector * 2 * size );
hegyi@1499
   268
      xy<gdouble> top(center + unit_vector_in_dir * 3 * size);
hegyi@1499
   269
	 
hegyi@1499
   270
      //       std::cout << bl << " " << br << " " << ccl << " "  << ccr << " " << cl << " " << cr << " " << top << std::endl;
hegyi@1499
   271
hegyi@1499
   272
      Gnome::Canvas::Points arrow_points;
hegyi@1499
   273
      arrow_points.push_back(Gnome::Art::Point( bl.x , bl.y  ) );
hegyi@1499
   274
      arrow_points.push_back(Gnome::Art::Point( br.x , br.y  ) );
hegyi@1499
   275
      arrow_points.push_back(Gnome::Art::Point( ccr.x, ccr.y ) );
hegyi@1499
   276
      arrow_points.push_back(Gnome::Art::Point( cr.x , cr.y  ) );
hegyi@1499
   277
      arrow_points.push_back(Gnome::Art::Point( top.x, top.y ) );
hegyi@1499
   278
      arrow_points.push_back(Gnome::Art::Point( cl.x , cl.y  ) );
hegyi@1499
   279
      arrow_points.push_back(Gnome::Art::Point( ccl.x, ccl.y ) );
hegyi@1499
   280
hegyi@1499
   281
      arrow->property_points().set_value(arrow_points);
hegyi@1497
   282
    }
hegyi@1497
   283
}
hegyi@1499
   284
hegyi@1524
   285
bool BrokenEdge::edgeFormerEventHandler(GdkEvent* e)
hegyi@1499
   286
{
hegyi@1499
   287
  switch(e->type)
hegyi@1499
   288
    {
hegyi@1499
   289
    case GDK_BUTTON_PRESS:
hegyi@1499
   290
      //we mark the location of the event to be able to calculate parameters of dragging
hegyi@1524
   291
      if(gdc.getActualTool()!=CREATE_NODE)
hegyi@1501
   292
	{
hegyi@1524
   293
	  gdc.toggleEdgeActivity(this, true);
hegyi@1501
   294
	  clicked_x=e->button.x;
hegyi@1501
   295
	  clicked_y=e->button.y;
hegyi@1501
   296
	  isbutton=true;
hegyi@1501
   297
	}
hegyi@1499
   298
      break;
hegyi@1499
   299
    case GDK_BUTTON_RELEASE:
hegyi@1524
   300
      if(gdc.getActualTool()!=CREATE_NODE)
hegyi@1505
   301
	{
hegyi@1524
   302
	  gdc.toggleEdgeActivity(this, false);
hegyi@1505
   303
	  isbutton=false;
hegyi@1505
   304
	}
hegyi@1499
   305
      break;
hegyi@1499
   306
    case GDK_MOTION_NOTIFY:
hegyi@1499
   307
      //we only have to do sg. if the mouse button is pressed
hegyi@1499
   308
      if(isbutton)
hegyi@1499
   309
	{
hegyi@1499
   310
	  //new coordinates will be the old values,
hegyi@1499
   311
	  //because the item will be moved to the
hegyi@1499
   312
	  //new coordinate therefore the new movement
hegyi@1499
   313
	  //has to be calculated from here
hegyi@1499
   314
hegyi@1499
   315
	  double dx=e->motion.x-clicked_x;
hegyi@1499
   316
	  double dy=e->motion.y-clicked_y;
hegyi@1499
   317
hegyi@1499
   318
	  Gnome::Canvas::Points points_new;
hegyi@1499
   319
hegyi@1499
   320
	  points_new.push_back(my_points[0]);
hegyi@1499
   321
	  points_new.push_back(my_points[1]=Gnome::Art::Point(my_points[1].get_x()+dx,my_points[1].get_y()+dy));
hegyi@1499
   322
	  points_new.push_back(my_points[2]);
hegyi@1499
   323
hegyi@1524
   324
	  setPoints(points_new);
hegyi@1524
   325
	  gdc.textReposition(xy<double>(my_points[1].get_x(),my_points[1].get_y()));
hegyi@1499
   326
hegyi@1499
   327
	  clicked_x=e->motion.x;
hegyi@1499
   328
	  clicked_y=e->motion.y;
hegyi@1499
   329
hegyi@1499
   330
	}
hegyi@1499
   331
    default: break;
hegyi@1499
   332
    }
hegyi@1499
   333
hegyi@1499
   334
  return true;
hegyi@1499
   335
}
hegyi@1505
   336
hegyi@1524
   337
xy<double> BrokenEdge::getArrowPos()
hegyi@1505
   338
{
hegyi@1505
   339
  xy<double> ret_val(my_points[1].get_x(),my_points[1].get_y());
hegyi@1505
   340
  return ret_val;
hegyi@1505
   341
}