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