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