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