COIN-OR::LEMON - Graph Library

source: lemon-0.x/gui/gdc-broken_edge.cc @ 1833:6d107b0b6b46

Last change on this file since 1833:6d107b0b6b46 was 1831:75ab76fc4bf2, checked in by Hegyi Péter, 19 years ago

No segmentation fault caused by zero long edges.

File size: 7.3 KB
RevLine 
[1819]1#include "graph_displayer_canvas.h"
[1632]2#include <cmath>
[1497]3
[1819]4GraphDisplayerCanvas::BrokenEdge::BrokenEdge(Gnome::Canvas::Group & g, Gnome::Canvas::Points p, GraphDisplayerCanvas & gc) : Line(g), gdc(gc), isbutton(false)
[1497]5{
[1499]6  my_points=new Gnome::Art::Point[3];
7
8  arrow=new Gnome::Canvas::Polygon(g);
9  *arrow << Gnome::Canvas::Properties::fill_color("red");
[1819]10  arrow->signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::BrokenEdge::edgeFormerEventHandler));
[1645]11  arrow->lower_to_bottom();
[1524]12  setPoints(p);
[1499]13}
14
[1819]15GraphDisplayerCanvas::BrokenEdge::~BrokenEdge()
[1499]16{
17  if(arrow)delete(arrow);
18}
19
[1819]20void GraphDisplayerCanvas::BrokenEdge::setPoints(Gnome::Canvas::Points p, bool move)
[1499]21{
22  bool set_arrow=false;
[1601]23  //red arrow losts its position-right button
[1500]24  if(!move)
[1497]25    {
[1500]26      if(p.size()==2)
27        {
28          set_arrow=true;
29          Gnome::Canvas::Points points_with_center;
30          points_with_center.push_back(my_points[0]=p[0]);
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 );
32          points_with_center.push_back(my_points[2]=p[1]);
33          property_points().set_value(points_with_center);
34        } 
35      if(p.size()==3)
36        {
37          set_arrow=true;
38          property_points().set_value(p);
39          for(int i=0;i<3;i++)
40            {
41              my_points[i]=p[i];
42            }
43        }
44    }
45  else
[1499]46    {
[1601]47      //arrow keeps its position-left button
48
[1714]49//       if(p.size()==2)
50//              {
51//                Gnome::Canvas::Points points;
52//                my_points[0]=p[0];
53//                my_points[2]=p[1];
54//                for(int i=0;i<3;i++)
55//                  {
56//                    points.push_back(my_points[i]);
57//                  }
58//                property_points().set_value(points);
59//              }
[1602]60      set_arrow=true;
[1601]61
62      //////////////////////////////////////////////////////////////////////////////////////////////////////
[1714]63      /////////// keeps shape-with scalar multiplication - version 2.
[1601]64      //////////////////////////////////////////////////////////////////////////////////////////////////////
65
[1714]66      if(p.size()==2)
67        {
68          //old vector from one to the other node - a
69          xy<double> a_v(my_points[2].get_x()-my_points[0].get_x(),my_points[2].get_y()-my_points[0].get_y());
70          //new vector from one to the other node - b
71          xy<double> b_v(p[1].get_x()-p[0].get_x(),p[1].get_y()-p[0].get_y());
[1601]72
[1714]73          double absa=sqrt(a_v.normSquare());
74          double absb=sqrt(b_v.normSquare());
[1601]75
[1831]76          if((absa!=0)&&(absb!=0))
77            {
78              //old vector from one node to the breakpoint - c
79              xy<double> c_v(my_points[1].get_x()-my_points[0].get_x(),my_points[1].get_y()-my_points[0].get_y());
[1601]80
[1831]81              //unit vector with the same direction to a_v
82              xy<double> a_v_u(a_v.x/absa,a_v.y/absa);
[1601]83
[1831]84              //normal vector of unit vector with the same direction to a_v
85              xy<double> a_v_u_n(((-1)*a_v_u.y),a_v_u.x);
[1601]86
[1831]87              //unit vector with the same direction to b_v
88              xy<double> b_v_u(b_v.x/absb,b_v.y/absb);
[1601]89
[1831]90              //normal vector of unit vector with the same direction to b_v
91              xy<double> b_v_u_n(((-1)*b_v_u.y),b_v_u.x);
[1601]92
[1831]93              //vector c in a_v_u and a_v_u_n co-ordinate system
94              xy<double> c_a(c_v*a_v_u,c_v*a_v_u_n);
[1601]95
[1831]96              //new vector from one node to the breakpoint - d - we have to calculate this one
97              xy<double> d_v=absb/absa*(c_a.x*b_v_u+c_a.y*b_v_u_n);
[1601]98
[1831]99              my_points[1]=Gnome::Art::Point(d_v.x+p[0].get_x(),d_v.y+p[0].get_y());
[1601]100
[1831]101              my_points[0]=p[0];
102              my_points[2]=p[1];
103         
104              Gnome::Canvas::Points points;
105              for(int i=0;i<3;i++)
106                {
107                  points.push_back(my_points[i]);
108                }
109              property_points().set_value(points);
110            }
111          else
[1714]112            {
[1831]113              //if distance is 0, segmentation would be occured
114              //in calculations, because of division by zero
115              //But we have luck: the edge cannot be seen in
116              //this case, so no update needed
117              set_arrow=false;
[1714]118            }
119        }
[1499]120    }
121  if(set_arrow)
122    {
123      //calculating coordinates of the direction indicator arrow
124
125      xy<gdouble> target( my_points[2].get_x(), my_points[2].get_y() );
126      xy<gdouble> center( my_points[1].get_x(), my_points[1].get_y() );
127
128      xy<gdouble> unit_vector_in_dir(target-center);
[1831]129      double length=sqrt( unit_vector_in_dir.normSquare() );
130
[1499]131      //       std::cout << target << " - " << center << " = " << unit_vector_in_dir << "    / " <<unit_vector_in_dir.normSquare() ;
[1831]132      unit_vector_in_dir/=length;
[1499]133      //       std::cout << " = " << unit_vector_in_dir << std::endl;
134
135      xy<gdouble> unit_norm_vector(0-unit_vector_in_dir.y, unit_vector_in_dir.x);
136      //       std::cout << unit_norm_vector << std::endl;
137
138      {     
[1504]139        //       /\       // top
140        //      /  \      //
141        //      -  -      // c(enter)l(eft), ccl, ccr, cr
142        //       ||       //
143        //       ||       // b(ottom)l, br
[1499]144      }
145
146      double size=3;
147
148      xy<gdouble> bl (center - unit_vector_in_dir * 3 * size + unit_norm_vector * size );
149      xy<gdouble> br (center - unit_vector_in_dir * 3 * size - unit_norm_vector * size );
150      xy<gdouble> ccl(center + unit_vector_in_dir *  size + unit_norm_vector * size );
151      xy<gdouble> ccr(center + unit_vector_in_dir *  size - unit_norm_vector * size );
152      xy<gdouble> cl (center + unit_vector_in_dir *  size + unit_norm_vector * 2 * size );
153      xy<gdouble> cr (center + unit_vector_in_dir *  size - unit_norm_vector * 2 * size );
154      xy<gdouble> top(center + unit_vector_in_dir * 3 * size);
155         
[1831]156      //std::cout << bl << " " << br << " " << ccl << " "  << ccr << " " << cl << " " << cr << " " << top << std::endl;
[1499]157
158      Gnome::Canvas::Points arrow_points;
159      arrow_points.push_back(Gnome::Art::Point( bl.x , bl.y  ) );
160      arrow_points.push_back(Gnome::Art::Point( br.x , br.y  ) );
161      arrow_points.push_back(Gnome::Art::Point( ccr.x, ccr.y ) );
162      arrow_points.push_back(Gnome::Art::Point( cr.x , cr.y  ) );
163      arrow_points.push_back(Gnome::Art::Point( top.x, top.y ) );
164      arrow_points.push_back(Gnome::Art::Point( cl.x , cl.y  ) );
165      arrow_points.push_back(Gnome::Art::Point( ccl.x, ccl.y ) );
166
167      arrow->property_points().set_value(arrow_points);
[1497]168    }
169}
[1499]170
[1819]171bool GraphDisplayerCanvas::BrokenEdge::edgeFormerEventHandler(GdkEvent* e)
[1499]172{
173  switch(e->type)
174    {
175    case GDK_BUTTON_PRESS:
176      //we mark the location of the event to be able to calculate parameters of dragging
[1524]177      if(gdc.getActualTool()!=CREATE_NODE)
[1501]178        {
[1524]179          gdc.toggleEdgeActivity(this, true);
[1501]180          clicked_x=e->button.x;
181          clicked_y=e->button.y;
182          isbutton=true;
183        }
[1499]184      break;
185    case GDK_BUTTON_RELEASE:
[1524]186      if(gdc.getActualTool()!=CREATE_NODE)
[1505]187        {
[1524]188          gdc.toggleEdgeActivity(this, false);
[1505]189          isbutton=false;
190        }
[1499]191      break;
192    case GDK_MOTION_NOTIFY:
193      //we only have to do sg. if the mouse button is pressed
194      if(isbutton)
195        {
196          //new coordinates will be the old values,
197          //because the item will be moved to the
198          //new coordinate therefore the new movement
199          //has to be calculated from here
200
201          double dx=e->motion.x-clicked_x;
202          double dy=e->motion.y-clicked_y;
203
204          Gnome::Canvas::Points points_new;
205
206          points_new.push_back(my_points[0]);
207          points_new.push_back(my_points[1]=Gnome::Art::Point(my_points[1].get_x()+dx,my_points[1].get_y()+dy));
208          points_new.push_back(my_points[2]);
209
[1524]210          setPoints(points_new);
211          gdc.textReposition(xy<double>(my_points[1].get_x(),my_points[1].get_y()));
[1499]212
213          clicked_x=e->motion.x;
214          clicked_y=e->motion.y;
215
216        }
217    default: break;
218    }
219
220  return true;
221}
[1505]222
[1819]223xy<double> GraphDisplayerCanvas::BrokenEdge::getArrowPos()
[1505]224{
225  xy<double> ret_val(my_points[1].get_x(),my_points[1].get_y());
226  return ret_val;
227}
Note: See TracBrowser for help on using the repository browser.