gui/gdc-broken_edge.cc
changeset 1860 27a9a75b957b
parent 1831 75ab76fc4bf2
     1.1 --- a/gui/gdc-broken_edge.cc	Wed Dec 14 18:11:03 2005 +0000
     1.2 +++ b/gui/gdc-broken_edge.cc	Sat Dec 17 20:55:41 2005 +0000
     1.3 @@ -1,15 +1,13 @@
     1.4  #include "graph_displayer_canvas.h"
     1.5  #include <cmath>
     1.6  
     1.7 -GraphDisplayerCanvas::BrokenEdge::BrokenEdge(Gnome::Canvas::Group & g, Gnome::Canvas::Points p, GraphDisplayerCanvas & gc) : Line(g), gdc(gc), isbutton(false)
     1.8 +GraphDisplayerCanvas::BrokenEdge::BrokenEdge(Gnome::Canvas::Group & g, Edge _edge, GraphDisplayerCanvas & gc) : Line(g), edge(_edge), gdc(gc), isbutton(false)
     1.9  {
    1.10 -  my_points=new Gnome::Art::Point[3];
    1.11 -
    1.12    arrow=new Gnome::Canvas::Polygon(g);
    1.13    *arrow << Gnome::Canvas::Properties::fill_color("red");
    1.14    arrow->signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::BrokenEdge::edgeFormerEventHandler));
    1.15    arrow->lower_to_bottom();
    1.16 -  setPoints(p);
    1.17 +  draw();
    1.18  }
    1.19  
    1.20  GraphDisplayerCanvas::BrokenEdge::~BrokenEdge()
    1.21 @@ -17,155 +15,71 @@
    1.22    if(arrow)delete(arrow);
    1.23  }
    1.24  
    1.25 -void GraphDisplayerCanvas::BrokenEdge::setPoints(Gnome::Canvas::Points p, bool move)
    1.26 +void GraphDisplayerCanvas::BrokenEdge::draw()
    1.27  {
    1.28 -  bool set_arrow=false;
    1.29 -  //red arrow losts its position-right button
    1.30 -  if(!move)
    1.31 -    {
    1.32 -      if(p.size()==2)
    1.33 -	{
    1.34 -	  set_arrow=true;
    1.35 -	  Gnome::Canvas::Points points_with_center;
    1.36 -	  points_with_center.push_back(my_points[0]=p[0]);
    1.37 -	  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 );
    1.38 -	  points_with_center.push_back(my_points[2]=p[1]);
    1.39 -	  property_points().set_value(points_with_center);
    1.40 -	}  
    1.41 -      if(p.size()==3)
    1.42 -	{
    1.43 -	  set_arrow=true;
    1.44 -	  property_points().set_value(p);
    1.45 -	  for(int i=0;i<3;i++)
    1.46 -	    {
    1.47 -	      my_points[i]=p[i];
    1.48 -	    }
    1.49 -	}
    1.50 +  MapStorage& ms = gdc.mytab.mapstorage;
    1.51 +
    1.52 +  // update the edge
    1.53 +  {
    1.54 +    Gnome::Canvas::Points points;
    1.55 +    Node source = ms.graph.source(edge);
    1.56 +    Node target = ms.graph.target(edge);
    1.57 +    points.push_back(Gnome::Art::Point(ms.coords[source].x,
    1.58 +          ms.coords[source].y));
    1.59 +    points.push_back(Gnome::Art::Point(ms.arrow_pos[edge].x,
    1.60 +          ms.arrow_pos[edge].y));
    1.61 +    points.push_back(Gnome::Art::Point(ms.coords[target].x,
    1.62 +          ms.coords[target].y));
    1.63 +    property_points().set_value(points);
    1.64 +  }
    1.65 +
    1.66 +  // update the arrow
    1.67 +  {
    1.68 +    //calculating coordinates of the direction indicator arrow
    1.69 +    XY target(ms.coords[ms.graph.target(edge)]);
    1.70 +    XY center(ms.arrow_pos[edge]);
    1.71 +
    1.72 +    XY unit_vector_in_dir(target-center);
    1.73 +    double length=sqrt( unit_vector_in_dir.normSquare() );
    1.74 +
    1.75 +    //       std::cout << target << " - " << center << " = " << unit_vector_in_dir << "    / " <<unit_vector_in_dir.normSquare() ;
    1.76 +    unit_vector_in_dir/=length;
    1.77 +    //       std::cout << " = " << unit_vector_in_dir << std::endl;
    1.78 +
    1.79 +    XY unit_norm_vector(0-unit_vector_in_dir.y, unit_vector_in_dir.x);
    1.80 +    //       std::cout << unit_norm_vector << std::endl;
    1.81 +
    1.82 +    {      
    1.83 +      //       /\       // top
    1.84 +      //      /  \      //
    1.85 +      //      -  -      // c(enter)l(eft), ccl, ccr, cr
    1.86 +      //       ||       //
    1.87 +      //       ||       // b(ottom)l, br
    1.88      }
    1.89 -  else
    1.90 -    {
    1.91 -      //arrow keeps its position-left button
    1.92  
    1.93 -//       if(p.size()==2)
    1.94 -//       	{
    1.95 -//       	  Gnome::Canvas::Points points;
    1.96 -//       	  my_points[0]=p[0];
    1.97 -//       	  my_points[2]=p[1];
    1.98 -//       	  for(int i=0;i<3;i++)
    1.99 -//       	    {
   1.100 -//       	      points.push_back(my_points[i]);
   1.101 -//       	    }
   1.102 -//       	  property_points().set_value(points);
   1.103 -//       	}
   1.104 -      set_arrow=true;
   1.105 +    double size=3;
   1.106  
   1.107 -      //////////////////////////////////////////////////////////////////////////////////////////////////////
   1.108 -      /////////// keeps shape-with scalar multiplication - version 2.
   1.109 -      //////////////////////////////////////////////////////////////////////////////////////////////////////
   1.110 +    XY bl (center - unit_vector_in_dir * 3 * size + unit_norm_vector * size );
   1.111 +    XY br (center - unit_vector_in_dir * 3 * size - unit_norm_vector * size );
   1.112 +    XY ccl(center + unit_vector_in_dir *  size + unit_norm_vector * size );
   1.113 +    XY ccr(center + unit_vector_in_dir *  size - unit_norm_vector * size );
   1.114 +    XY cl (center + unit_vector_in_dir *  size + unit_norm_vector * 2 * size );
   1.115 +    XY cr (center + unit_vector_in_dir *  size - unit_norm_vector * 2 * size );
   1.116 +    XY top(center + unit_vector_in_dir * 3 * size);
   1.117  
   1.118 -      if(p.size()==2)
   1.119 -      	{
   1.120 -	  //old vector from one to the other node - a
   1.121 -	  xy<double> a_v(my_points[2].get_x()-my_points[0].get_x(),my_points[2].get_y()-my_points[0].get_y());
   1.122 -	  //new vector from one to the other node - b
   1.123 -	  xy<double> b_v(p[1].get_x()-p[0].get_x(),p[1].get_y()-p[0].get_y());
   1.124 +    //std::cout << bl << " " << br << " " << ccl << " "  << ccr << " " << cl << " " << cr << " " << top << std::endl;
   1.125  
   1.126 -	  double absa=sqrt(a_v.normSquare());
   1.127 -	  double absb=sqrt(b_v.normSquare());
   1.128 +    Gnome::Canvas::Points arrow_points;
   1.129 +    arrow_points.push_back(Gnome::Art::Point( bl.x , bl.y  ) );
   1.130 +    arrow_points.push_back(Gnome::Art::Point( br.x , br.y  ) );
   1.131 +    arrow_points.push_back(Gnome::Art::Point( ccr.x, ccr.y ) );
   1.132 +    arrow_points.push_back(Gnome::Art::Point( cr.x , cr.y  ) );
   1.133 +    arrow_points.push_back(Gnome::Art::Point( top.x, top.y ) );
   1.134 +    arrow_points.push_back(Gnome::Art::Point( cl.x , cl.y  ) );
   1.135 +    arrow_points.push_back(Gnome::Art::Point( ccl.x, ccl.y ) );
   1.136  
   1.137 -	  if((absa!=0)&&(absb!=0))
   1.138 -	    {
   1.139 -	      //old vector from one node to the breakpoint - c
   1.140 -	      xy<double> c_v(my_points[1].get_x()-my_points[0].get_x(),my_points[1].get_y()-my_points[0].get_y());
   1.141 -
   1.142 -	      //unit vector with the same direction to a_v
   1.143 -	      xy<double> a_v_u(a_v.x/absa,a_v.y/absa);
   1.144 -
   1.145 -	      //normal vector of unit vector with the same direction to a_v
   1.146 -	      xy<double> a_v_u_n(((-1)*a_v_u.y),a_v_u.x);
   1.147 -
   1.148 -	      //unit vector with the same direction to b_v
   1.149 -	      xy<double> b_v_u(b_v.x/absb,b_v.y/absb);
   1.150 -
   1.151 -	      //normal vector of unit vector with the same direction to b_v
   1.152 -	      xy<double> b_v_u_n(((-1)*b_v_u.y),b_v_u.x);
   1.153 -
   1.154 -	      //vector c in a_v_u and a_v_u_n co-ordinate system
   1.155 -	      xy<double> c_a(c_v*a_v_u,c_v*a_v_u_n);
   1.156 -
   1.157 -	      //new vector from one node to the breakpoint - d - we have to calculate this one
   1.158 -	      xy<double> d_v=absb/absa*(c_a.x*b_v_u+c_a.y*b_v_u_n);
   1.159 -
   1.160 -	      my_points[1]=Gnome::Art::Point(d_v.x+p[0].get_x(),d_v.y+p[0].get_y());
   1.161 -
   1.162 -	      my_points[0]=p[0];
   1.163 -	      my_points[2]=p[1];
   1.164 -	  
   1.165 -	      Gnome::Canvas::Points points;
   1.166 -	      for(int i=0;i<3;i++)
   1.167 -		{
   1.168 -		  points.push_back(my_points[i]);
   1.169 -		}
   1.170 -	      property_points().set_value(points);
   1.171 -	    }
   1.172 -	  else
   1.173 -	    {
   1.174 -	      //if distance is 0, segmentation would be occured
   1.175 -	      //in calculations, because of division by zero
   1.176 -	      //But we have luck: the edge cannot be seen in
   1.177 -	      //this case, so no update needed
   1.178 -	      set_arrow=false;
   1.179 -	    }
   1.180 -	}
   1.181 -    }
   1.182 -  if(set_arrow)
   1.183 -    {
   1.184 -      //calculating coordinates of the direction indicator arrow
   1.185 -
   1.186 -      xy<gdouble> target( my_points[2].get_x(), my_points[2].get_y() );
   1.187 -      xy<gdouble> center( my_points[1].get_x(), my_points[1].get_y() );
   1.188 -
   1.189 -      xy<gdouble> unit_vector_in_dir(target-center);
   1.190 -      double length=sqrt( unit_vector_in_dir.normSquare() );
   1.191 -
   1.192 -      //       std::cout << target << " - " << center << " = " << unit_vector_in_dir << "    / " <<unit_vector_in_dir.normSquare() ;
   1.193 -      unit_vector_in_dir/=length;
   1.194 -      //       std::cout << " = " << unit_vector_in_dir << std::endl;
   1.195 -
   1.196 -      xy<gdouble> unit_norm_vector(0-unit_vector_in_dir.y, unit_vector_in_dir.x);
   1.197 -      //       std::cout << unit_norm_vector << std::endl;
   1.198 -
   1.199 -      {      
   1.200 -	//       /\       // top
   1.201 -	//      /  \      //
   1.202 -	//      -  -      // c(enter)l(eft), ccl, ccr, cr
   1.203 -	//       ||       //
   1.204 -	//       ||       // b(ottom)l, br
   1.205 -      }
   1.206 -
   1.207 -      double size=3;
   1.208 -
   1.209 -      xy<gdouble> bl (center - unit_vector_in_dir * 3 * size + unit_norm_vector * size );
   1.210 -      xy<gdouble> br (center - unit_vector_in_dir * 3 * size - unit_norm_vector * size );
   1.211 -      xy<gdouble> ccl(center + unit_vector_in_dir *  size + unit_norm_vector * size );
   1.212 -      xy<gdouble> ccr(center + unit_vector_in_dir *  size - unit_norm_vector * size );
   1.213 -      xy<gdouble> cl (center + unit_vector_in_dir *  size + unit_norm_vector * 2 * size );
   1.214 -      xy<gdouble> cr (center + unit_vector_in_dir *  size - unit_norm_vector * 2 * size );
   1.215 -      xy<gdouble> top(center + unit_vector_in_dir * 3 * size);
   1.216 -	 
   1.217 -      //std::cout << bl << " " << br << " " << ccl << " "  << ccr << " " << cl << " " << cr << " " << top << std::endl;
   1.218 -
   1.219 -      Gnome::Canvas::Points arrow_points;
   1.220 -      arrow_points.push_back(Gnome::Art::Point( bl.x , bl.y  ) );
   1.221 -      arrow_points.push_back(Gnome::Art::Point( br.x , br.y  ) );
   1.222 -      arrow_points.push_back(Gnome::Art::Point( ccr.x, ccr.y ) );
   1.223 -      arrow_points.push_back(Gnome::Art::Point( cr.x , cr.y  ) );
   1.224 -      arrow_points.push_back(Gnome::Art::Point( top.x, top.y ) );
   1.225 -      arrow_points.push_back(Gnome::Art::Point( cl.x , cl.y  ) );
   1.226 -      arrow_points.push_back(Gnome::Art::Point( ccl.x, ccl.y ) );
   1.227 -
   1.228 -      arrow->property_points().set_value(arrow_points);
   1.229 -    }
   1.230 +    arrow->property_points().set_value(arrow_points);
   1.231 +  }
   1.232  }
   1.233  
   1.234  bool GraphDisplayerCanvas::BrokenEdge::edgeFormerEventHandler(GdkEvent* e)
   1.235 @@ -203,12 +117,10 @@
   1.236  
   1.237  	  Gnome::Canvas::Points points_new;
   1.238  
   1.239 -	  points_new.push_back(my_points[0]);
   1.240 -	  points_new.push_back(my_points[1]=Gnome::Art::Point(my_points[1].get_x()+dx,my_points[1].get_y()+dy));
   1.241 -	  points_new.push_back(my_points[2]);
   1.242 +          gdc.mytab.mapstorage.arrow_pos.set(edge, gdc.mytab.mapstorage.arrow_pos[edge] + XY(dx, dy));
   1.243  
   1.244 -	  setPoints(points_new);
   1.245 -	  gdc.textReposition(xy<double>(my_points[1].get_x(),my_points[1].get_y()));
   1.246 +	  draw();
   1.247 +	  gdc.textReposition(gdc.mytab.mapstorage.arrow_pos[edge]);
   1.248  
   1.249  	  clicked_x=e->motion.x;
   1.250  	  clicked_y=e->motion.y;
   1.251 @@ -219,9 +131,3 @@
   1.252  
   1.253    return true;
   1.254  }
   1.255 -
   1.256 -xy<double> GraphDisplayerCanvas::BrokenEdge::getArrowPos()
   1.257 -{
   1.258 -  xy<double> ret_val(my_points[1].get_x(),my_points[1].get_y());
   1.259 -  return ret_val;
   1.260 -}