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