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