ladanyi@1606: #include "broken_edge.h"
alpar@1632: #include <cmath>
hegyi@1497: 
hegyi@1501: BrokenEdge::BrokenEdge(Gnome::Canvas::Group & g, Gnome::Canvas::Points p, GraphDisplayerCanvas & gc) : Line(g), gdc(gc), isbutton(false)
hegyi@1497: {
hegyi@1499:   my_points=new Gnome::Art::Point[3];
hegyi@1499: 
hegyi@1499:   arrow=new Gnome::Canvas::Polygon(g);
hegyi@1499:   *arrow << Gnome::Canvas::Properties::fill_color("red");
hegyi@1524:   arrow->signal_event().connect(sigc::mem_fun(*this, &BrokenEdge::edgeFormerEventHandler));
ladanyi@1645:   arrow->lower_to_bottom();
hegyi@1524:   setPoints(p);
hegyi@1499: }
hegyi@1499: 
hegyi@1499: BrokenEdge::~BrokenEdge()
hegyi@1499: {
hegyi@1499:   if(arrow)delete(arrow);
hegyi@1499: }
hegyi@1499: 
hegyi@1524: void BrokenEdge::setPoints(Gnome::Canvas::Points p, bool move)
hegyi@1499: {
hegyi@1499:   bool set_arrow=false;
hegyi@1601:   //red arrow losts its position-right button
hegyi@1500:   if(!move)
hegyi@1497:     {
hegyi@1500:       if(p.size()==2)
hegyi@1500: 	{
hegyi@1500: 	  set_arrow=true;
hegyi@1500: 	  Gnome::Canvas::Points points_with_center;
hegyi@1500: 	  points_with_center.push_back(my_points[0]=p[0]);
hegyi@1500: 	  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: 	  points_with_center.push_back(my_points[2]=p[1]);
hegyi@1500: 	  property_points().set_value(points_with_center);
hegyi@1500: 	}  
hegyi@1500:       if(p.size()==3)
hegyi@1500: 	{
hegyi@1500: 	  set_arrow=true;
hegyi@1500: 	  property_points().set_value(p);
hegyi@1500: 	  for(int i=0;i<3;i++)
hegyi@1500: 	    {
hegyi@1500: 	      my_points[i]=p[i];
hegyi@1500: 	    }
hegyi@1500: 	}
hegyi@1500:     }
hegyi@1500:   else
hegyi@1499:     {
hegyi@1601:       //arrow keeps its position-left button
hegyi@1601: 
hegyi@1602:       if(p.size()==2)
hegyi@1602:       	{
hegyi@1602:       	  Gnome::Canvas::Points points;
hegyi@1602:       	  my_points[0]=p[0];
hegyi@1602:       	  my_points[2]=p[1];
hegyi@1602:       	  for(int i=0;i<3;i++)
hegyi@1602:       	    {
hegyi@1602:       	      points.push_back(my_points[i]);
hegyi@1602:       	    }
hegyi@1602:       	  property_points().set_value(points);
hegyi@1602:       	}
hegyi@1602:       set_arrow=true;
hegyi@1601: 
hegyi@1601:       //////////////////////////////////////////////////////////////////////////////////////////////////////
hegyi@1601:       /////////// kepps shape-with angles
hegyi@1601:       //////////////////////////////////////////////////////////////////////////////////////////////////////
hegyi@1601: 
hegyi@1601: 
hegyi@1601: //       //old vector from one to the other node
hegyi@1601: //       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: //       //projection of the old vector to positive x axis
hegyi@1601: //       xy<double> o_x_p2p(fabs(o_p2p.x),0);
hegyi@1601: //       //length of p2p vector
hegyi@1601: //       double o_l_p2p=sqrt( o_p2p.normSquare() );
hegyi@1601: //       if(o_p2p.x<0)
hegyi@1601: // 	{
hegyi@1601: // 	  o_l_p2p*=-1;
hegyi@1601: // 	}
hegyi@1601: //       //length of projection of p2p vector
hegyi@1601: //       double o_l_x_p2p=sqrt( o_x_p2p.normSquare() );
hegyi@1601: //       //old angle of p2p vector to the x axis
hegyi@1601: //       double o_a_p2p=acos(o_l_x_p2p/o_l_p2p);
hegyi@1601: //       if(o_p2p.y>0)
hegyi@1601: // 	{
hegyi@1601: // 	  o_a_p2p=2*M_PI-o_a_p2p;
hegyi@1601: // 	}
hegyi@1601: 
hegyi@1601: //       //old vector from first node to the breakpoint
hegyi@1601: //       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: //       //projection of the old node-breakpoint vector to positive x axis
hegyi@1601: //       xy<double> o_x_1b(fabs(o_1b.x),0);
hegyi@1601: //       //length of 1b vector
hegyi@1601: //       double o_l_1b=sqrt( o_1b.normSquare() );
hegyi@1601: //       if(o_1b.x<0)
hegyi@1601: // 	{
hegyi@1601: // 	  o_l_1b*=-1;
hegyi@1601: // 	}
hegyi@1601: //       //length of projection of 1b vector
hegyi@1601: //       double o_l_x_1b=sqrt( o_x_1b.normSquare() );
hegyi@1601: //       //old angle of 1b vector to the x axis
hegyi@1601: //       double o_a_1b=acos(o_l_x_1b/o_l_1b);
hegyi@1601: //       if(o_1b.y>0)
hegyi@1601: // 	{
hegyi@1601: // 	  o_a_1b=2*M_PI-o_a_1b;
hegyi@1601: // 	}
hegyi@1601: 
hegyi@1601: //       if(p.size()==2)
hegyi@1601: //       	{
hegyi@1601: // 	  set_arrow=true;
hegyi@1601: 
hegyi@1601: //       	  my_points[0]=p[0];
hegyi@1601: //       	  my_points[2]=p[1];
hegyi@1601: 
hegyi@1601: // 	  //new vector from one to the other node
hegyi@1601: // 	  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: // 	  //projection of the new vector to positive x axis
hegyi@1601: // 	  xy<double> n_x_p2p(fabs(n_p2p.x),0);
hegyi@1601: // 	  //length of p2p vector
hegyi@1601: // 	  double n_l_p2p=sqrt( n_p2p.normSquare() );
hegyi@1601: // 	  if(n_p2p.x<0)
hegyi@1601: // 	    {
hegyi@1601: // 	      n_l_p2p*=-1;
hegyi@1601: // 	    }
hegyi@1601: // 	  //length of projection of p2p vector
hegyi@1601: // 	  double n_l_x_p2p=sqrt( n_x_p2p.normSquare() );
hegyi@1601: // 	  //new angle of p2p vector to the x axis
hegyi@1601: // 	  double n_a_p2p=acos(n_l_x_p2p/n_l_p2p);
hegyi@1601: // 	  if(n_p2p.y>0)
hegyi@1601: // 	    {
hegyi@1601: // 	      n_a_p2p=2*M_PI-n_a_p2p;
hegyi@1601: // 	    }
hegyi@1601: 
hegyi@1601: // 	  //new angle of 1b vector to the x axis
hegyi@1601: // 	  double n_a_1b=o_a_1b+n_a_p2p-o_a_p2p;
hegyi@1601: 
hegyi@1601: // 	  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: // 	  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: 
hegyi@1601: // // 	  std::cout << o_p2p << " " << n_p2p << std::endl;
hegyi@1601: 
hegyi@1601: // 	  if((n_a_1b>M_PI*3/2)||(n_a_1b<M_PI/2))
hegyi@1601: // 	    {
hegyi@1601: // 	      std::cout << "jobb terfel" << std::endl;
hegyi@1601: // 	      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: // 	    }
hegyi@1601: // 	  else if((n_a_1b<M_PI*3/2)&&(n_a_1b>M_PI/2))
hegyi@1601: // 	    {
hegyi@1601: // 	      std::cout << "bal terfel" << std::endl;
hegyi@1601: // 	      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: // 	    }
hegyi@1601: // 	  else
hegyi@1601: // 	    {
hegyi@1601: // 	      std::cout << "y tengely" << std::endl;
hegyi@1601: // 	      double new_y=my_points[1].get_y();
hegyi@1601: // 	      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: // 	    }
hegyi@1601: 
hegyi@1601: // 	  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: 
hegyi@1601: 
hegyi@1601: // // 	  if(o_1b.x*o_1b.y>0)
hegyi@1601: // // 	    {
hegyi@1601: // // 	      if(n_p2p.x>0)
hegyi@1601: // // 		{
hegyi@1601: // // 		  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: // // 		}
hegyi@1601: // // 	      else
hegyi@1601: // // 		{
hegyi@1601: // // 		  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: // // 		}
hegyi@1601: // // 	    }
hegyi@1601: // // 	  else if(o_1b.x*o_1b.y<0)
hegyi@1601: // // 	    {
hegyi@1601: // // 	      if(n_p2p.x>0)
hegyi@1601: // // 		{
hegyi@1601: // // 		  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: // // 		}
hegyi@1601: // // 	      else
hegyi@1601: // // 		{
hegyi@1601: // // 		  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: // // 		}
hegyi@1601: // // 	    }
hegyi@1601: // // 	  else
hegyi@1601: // // 	    {
hegyi@1601: // // 	    }
hegyi@1601: 
hegyi@1601:       //////////////////////////////////////////////////////////////////////////////////////////////////////
hegyi@1601:       /////////// kepps shape-with scalar multiplication
hegyi@1601:       //////////////////////////////////////////////////////////////////////////////////////////////////////
hegyi@1601: 
hegyi@1602: //       if(p.size()==2)
hegyi@1602: //       	{
hegyi@1602: // 	  //old vector from one to the other node - a
hegyi@1602: // 	  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: // 	  //new vector from one to the other node - b
hegyi@1602: // 	  xy<double> b_v(p[1].get_x()-p[0].get_x(),p[1].get_y()-p[0].get_y());
hegyi@1601: 
hegyi@1602: // 	  //old vector from one node to the breakpoint - c
hegyi@1602: // 	  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: 
hegyi@1602: // 	  //new vector from one node to the breakpoint - d - we have to calculate this one
hegyi@1602: // 	  xy<double> d_v;
hegyi@1601: 
hegyi@1602: // 	  //scalar product of a and b (old and new vector from first point to the other)
hegyi@1602: // 	  double sab=a_v*b_v;
hegyi@1602: // 	  //scalar product of c and d (old and new vector from first point to breakpoint)
hegyi@1602: // 	  double scd=sab*c_v.normSquare()/a_v.normSquare();
hegyi@1601: 
hegyi@1602: // 	  std::cout<<" a " << a_v<<" b " <<b_v<<" c " <<c_v<<" sab " <<sab<<" scd "<<scd<<std::endl;
hegyi@1601: 
hegyi@1602: // 	  double a=c_v.normSquare();
hegyi@1602: // 	  double b=2*scd*c_v.y;
hegyi@1602: // 	  double c=scd*scd-b_v.normSquare()/a_v.normSquare()*c_v.normSquare()*c_v.x*c_v.x;
hegyi@1601: 
hegyi@1602: // 	  std::cout<<" a " << a<<" b " <<b<<" c " <<c<<std::endl;
hegyi@1601: 
hegyi@1602: // 	  d_v.y=(-b-sqrt(b*b-4*a*c))/2/a;
hegyi@1601: 
hegyi@1602: // 	  if(c_v.x!=0)
hegyi@1602: // 	    {
hegyi@1602: // 	      d_v.x=(scd-c_v.y*d_v.y)/c_v.x;
hegyi@1602: // 	    }
hegyi@1602: // 	  else
hegyi@1602: // 	    {
hegyi@1602: // 	      d_v.x=my_points[1].get_x();
hegyi@1602: // 	    }
hegyi@1601: 
hegyi@1602: // 	  std::cout<<" d " << d_v<<std::endl;
hegyi@1601: 
hegyi@1602: // 	  my_points[1]=Gnome::Art::Point(d_v.x+p[0].get_x(),d_v.y+p[0].get_y());
hegyi@1601: 
hegyi@1602: // 	  Gnome::Canvas::Points points;
hegyi@1602: // 	  for(int i=0;i<3;i++)
hegyi@1602: // 	    {
hegyi@1602: // 	      points.push_back(my_points[i]);
hegyi@1602: // 	    }
hegyi@1602: // 	  property_points().set_value(points);
hegyi@1602: // 	}
hegyi@1499:     }
hegyi@1499:   if(set_arrow)
hegyi@1499:     {
hegyi@1499:       //calculating coordinates of the direction indicator arrow
hegyi@1499: 
hegyi@1499:       xy<gdouble> target( my_points[2].get_x(), my_points[2].get_y() );
hegyi@1499:       xy<gdouble> center( my_points[1].get_x(), my_points[1].get_y() );
hegyi@1499: 
hegyi@1499:       xy<gdouble> unit_vector_in_dir(target-center);
hegyi@1499:       //       std::cout << target << " - " << center << " = " << unit_vector_in_dir << "    / " <<unit_vector_in_dir.normSquare() ;
hegyi@1499:       unit_vector_in_dir/=sqrt( unit_vector_in_dir.normSquare() );
hegyi@1499:       //       std::cout << " = " << unit_vector_in_dir << std::endl;
hegyi@1499: 
hegyi@1499:       xy<gdouble> unit_norm_vector(0-unit_vector_in_dir.y, unit_vector_in_dir.x);
hegyi@1499:       //       std::cout << unit_norm_vector << std::endl;
hegyi@1499: 
hegyi@1499:       {      
hegyi@1504: 	//       /\       // top
hegyi@1504: 	//      /  \      //
hegyi@1504: 	//      -  -      // c(enter)l(eft), ccl, ccr, cr
hegyi@1504: 	//       ||       //
hegyi@1504: 	//       ||       // b(ottom)l, br
hegyi@1499:       }
hegyi@1499: 
hegyi@1499:       double size=3;
hegyi@1499: 
hegyi@1499:       xy<gdouble> bl (center - unit_vector_in_dir * 3 * size + unit_norm_vector * size );
hegyi@1499:       xy<gdouble> br (center - unit_vector_in_dir * 3 * size - unit_norm_vector * size );
hegyi@1499:       xy<gdouble> ccl(center + unit_vector_in_dir *  size + unit_norm_vector * size );
hegyi@1499:       xy<gdouble> ccr(center + unit_vector_in_dir *  size - unit_norm_vector * size );
hegyi@1499:       xy<gdouble> cl (center + unit_vector_in_dir *  size + unit_norm_vector * 2 * size );
hegyi@1499:       xy<gdouble> cr (center + unit_vector_in_dir *  size - unit_norm_vector * 2 * size );
hegyi@1499:       xy<gdouble> top(center + unit_vector_in_dir * 3 * size);
hegyi@1499: 	 
hegyi@1499:       //       std::cout << bl << " " << br << " " << ccl << " "  << ccr << " " << cl << " " << cr << " " << top << std::endl;
hegyi@1499: 
hegyi@1499:       Gnome::Canvas::Points arrow_points;
hegyi@1499:       arrow_points.push_back(Gnome::Art::Point( bl.x , bl.y  ) );
hegyi@1499:       arrow_points.push_back(Gnome::Art::Point( br.x , br.y  ) );
hegyi@1499:       arrow_points.push_back(Gnome::Art::Point( ccr.x, ccr.y ) );
hegyi@1499:       arrow_points.push_back(Gnome::Art::Point( cr.x , cr.y  ) );
hegyi@1499:       arrow_points.push_back(Gnome::Art::Point( top.x, top.y ) );
hegyi@1499:       arrow_points.push_back(Gnome::Art::Point( cl.x , cl.y  ) );
hegyi@1499:       arrow_points.push_back(Gnome::Art::Point( ccl.x, ccl.y ) );
hegyi@1499: 
hegyi@1499:       arrow->property_points().set_value(arrow_points);
hegyi@1497:     }
hegyi@1497: }
hegyi@1499: 
hegyi@1524: bool BrokenEdge::edgeFormerEventHandler(GdkEvent* e)
hegyi@1499: {
hegyi@1499:   switch(e->type)
hegyi@1499:     {
hegyi@1499:     case GDK_BUTTON_PRESS:
hegyi@1499:       //we mark the location of the event to be able to calculate parameters of dragging
hegyi@1524:       if(gdc.getActualTool()!=CREATE_NODE)
hegyi@1501: 	{
hegyi@1524: 	  gdc.toggleEdgeActivity(this, true);
hegyi@1501: 	  clicked_x=e->button.x;
hegyi@1501: 	  clicked_y=e->button.y;
hegyi@1501: 	  isbutton=true;
hegyi@1501: 	}
hegyi@1499:       break;
hegyi@1499:     case GDK_BUTTON_RELEASE:
hegyi@1524:       if(gdc.getActualTool()!=CREATE_NODE)
hegyi@1505: 	{
hegyi@1524: 	  gdc.toggleEdgeActivity(this, false);
hegyi@1505: 	  isbutton=false;
hegyi@1505: 	}
hegyi@1499:       break;
hegyi@1499:     case GDK_MOTION_NOTIFY:
hegyi@1499:       //we only have to do sg. if the mouse button is pressed
hegyi@1499:       if(isbutton)
hegyi@1499: 	{
hegyi@1499: 	  //new coordinates will be the old values,
hegyi@1499: 	  //because the item will be moved to the
hegyi@1499: 	  //new coordinate therefore the new movement
hegyi@1499: 	  //has to be calculated from here
hegyi@1499: 
hegyi@1499: 	  double dx=e->motion.x-clicked_x;
hegyi@1499: 	  double dy=e->motion.y-clicked_y;
hegyi@1499: 
hegyi@1499: 	  Gnome::Canvas::Points points_new;
hegyi@1499: 
hegyi@1499: 	  points_new.push_back(my_points[0]);
hegyi@1499: 	  points_new.push_back(my_points[1]=Gnome::Art::Point(my_points[1].get_x()+dx,my_points[1].get_y()+dy));
hegyi@1499: 	  points_new.push_back(my_points[2]);
hegyi@1499: 
hegyi@1524: 	  setPoints(points_new);
hegyi@1524: 	  gdc.textReposition(xy<double>(my_points[1].get_x(),my_points[1].get_y()));
hegyi@1499: 
hegyi@1499: 	  clicked_x=e->motion.x;
hegyi@1499: 	  clicked_y=e->motion.y;
hegyi@1499: 
hegyi@1499: 	}
hegyi@1499:     default: break;
hegyi@1499:     }
hegyi@1499: 
hegyi@1499:   return true;
hegyi@1499: }
hegyi@1505: 
hegyi@1524: xy<double> BrokenEdge::getArrowPos()
hegyi@1505: {
hegyi@1505:   xy<double> ret_val(my_points[1].get_x(),my_points[1].get_y());
hegyi@1505:   return ret_val;
hegyi@1505: }