35f232f1286236c2365113ce2ff14fc423bbb01d
[speedfreak] / Client / routedialog.cpp
1 /*
2  * RouteDialog class
3  *
4  * @author      Olavi Pulkkinen <olavi.pulkkinen@fudeco.com>
5  * @author      Toni Jussila    <toni.jussila@fudeco.com>
6  * @copyright   (c) 2010 Speed Freak team
7  * @license     http://opensource.org/licenses/gpl-license.php GNU Public License
8  */
9
10 #include "routesavedialog.h"
11 #include "routedialog.h"
12 #include "ui_routedialog.h"
13 #include "usersettings.h"
14 #include <cmath>
15 #include <QPainter>
16 #include <QList>
17 #include <QMessageBox>
18 #include <QFile>
19 #include <QFileDialog>
20 #include <QPolygon>
21 #include <QDebug>
22
23 /**
24   * Vector class.
25   * In starting Qt 4.6 there is QVector3D.
26   * Later (updating Qt version) this class can be removed.
27   */
28 class Vector
29 {
30     qreal x, y, z;      // Location
31     qreal v;            // Velocity
32 public:
33     Vector() { x=0.; y=0. ; z=0.; };
34     Vector( qreal initX, qreal initY, qreal initZ) { x = initX, y = initY; z = initZ; };
35     void setX( qreal newX) { x = newX; };
36     void setY( qreal newY) { y = newY; };
37     void setZ( qreal newZ) { z = newZ; };
38     void setV( qreal newV) { v = newV; };
39     qreal getX() { return x; };
40     qreal getY() { return y; };
41     qreal getZ() { return z; };
42     qreal getV() { return v; };
43     qreal length() { return sqrt(x*x+y*y+z*z); };
44     Vector operator+(Vector v)
45     {
46         x = x + v.x; y = y + v.y; z = z + v.z;
47         return *this;
48     };
49     Vector operator-(Vector v)
50     {
51         x = x - v.x; y = y - v.y; z = z - v.z;
52         return *this;
53     };
54     Vector operator/(qreal c)
55     {
56         x = x/c; y = y/c; z = z/c;
57         return *this;
58     };
59     Vector crossProduct( Vector a, Vector b)
60     {
61         x = a.y*b.z - a.z*b.y;
62         y = a.z*b.x - a.x*b.z;
63         z = a.x*b.y - a.y*b.x;
64         return *this;
65     };
66 };
67
68
69 class Viewing
70 {
71     Vector atPoint, fromPoint, up, a1, a2, a3;
72     qreal offsx, offsy, offsz;
73     qreal dval;
74     qreal angle;
75 public:
76     qreal getOffsx() { return offsx; };
77     qreal getOffsy() { return offsy; };
78     qreal getOffsz() { return offsz; };
79     qreal getDval() { return dval; };
80     void setAngle( qreal newA) { angle = newA; };
81     void setUp( qreal newUpX, qreal newUpY, qreal newUpZ)
82     {
83         up.setX(newUpX); up.setY(newUpY); up.setZ(newUpZ);
84     };
85     void setAtPoint( qreal newX, qreal newY, qreal newZ)
86     {
87         atPoint.setX(newX); atPoint.setY(newY); atPoint.setZ(newZ);
88     };
89     void setFromPoint(qreal newX, qreal newY, qreal newZ)
90     {
91         fromPoint.setX(newX); fromPoint.setY(newY); fromPoint.setZ(newZ);
92     }
93     void setEye()
94     {
95         double amarkmag, tempmag;
96         Vector temp, dist;
97
98         dval = cos(angle/2.0)/sin(angle/2.0);
99         dist = atPoint-fromPoint;
100         amarkmag = dist.length();
101         a3 = dist/amarkmag;
102
103         temp.crossProduct( dist, up);
104         tempmag = temp.length();
105         a1 = temp/tempmag;
106
107         temp.crossProduct( a1, a3);
108         tempmag = temp.length();
109         a2 = temp/tempmag;
110
111         offsx = -a1.getX()*fromPoint.getX() - a1.getY()*fromPoint.getY() - a1.getZ()*fromPoint.getZ();
112         offsy = -a2.getX()*fromPoint.getX() - a2.getY()*fromPoint.getY() - a2.getZ()*fromPoint.getZ();
113         offsz = -a3.getX()*fromPoint.getX() - a3.getY()*fromPoint.getY() - a3.getZ()*fromPoint.getZ();
114         //QString jono2 = QString("offsx %1 offsy %2 offsz %3").arg(offsx).arg(offsy).arg(offsz);
115         //QMessageBox::about(0,"offs x y z", jono2);
116     } ;
117     Vector getAtPoint() { return atPoint; };
118     Vector getFromPoint() { return fromPoint; };
119     Vector getA1() { return a1; };
120     Vector getA2() { return a2; };
121     Vector getA3() { return a3; };
122     Viewing () {};
123 };
124
125 qreal xmax, xmin, ymin, ymax;       // Limits in world coordinates
126
127 QList<Vector> vertexList;           // Vertecies of route
128
129 qreal objxmin, objxmax, objymin, objymax, objzmin, objzmax; // data ranges
130
131 #define maxof(val1,val2)  ((val1>val2)?val1:val2)
132 #define toradians( degrees) (degrees*0.017453293)
133
134 #define WIDTH 1.8   // For 3d viewing only
135 qreal a, b,c,d; // Used for 3d viewing to calculate screen coordinates
136
137 Viewing view3d;   // Viewing settings for 3d
138
139 // Function prototypes
140 void dataMinMax( void);
141 void setAtPoint( Viewing *v);
142 void setFromPoint( Viewing *v);
143 void transformseg( Viewing *v, Vector *v1, Vector *v2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2 );
144
145 #define R 6378.140 // The radius of the earth by kilometers
146
147 /**
148   * count distance of two points (defined by longitude & latitude)
149   * on the surface of the earth.
150   */
151 qreal countDistance(Vector *p1, Vector *p2)
152 {
153     qreal dLon, dLat;   // delta of longitude & latitude
154     qreal a, c;
155
156     dLon = p2->getX() - p1->getX();     // longitude difference
157     dLat = p2->getY() - p1->getY();     // latitude difference
158     if (dLon <0) dLon = -dLon;
159     if (dLat <0) dLat = -dLat;
160
161     dLon = dLon*3.14/180;
162     dLat = dLat*3.14/180;
163     a = (sin(dLat/2.))*(sin(dLat/2.)) +
164         (cos(p1->getY())*3.14/180)*(cos(p2->getY())*3.14/180)*(sin(dLon/2.))*(sin(dLon/2.));
165     c = 2.*atan(sqrt(a)/sqrt(1-a));     // c is angle between points p1 & p2 with circel by radius 1.
166
167     return R*c;   // Return distance in kilometers
168 }
169
170 /**
171   * Constructor of this class.
172   */
173 RouteDialog::RouteDialog(RouteSaveDialog *parent) :
174     QDialog(parent), ui(new Ui::RouteDialog)
175 {
176     qDebug() << "__RouteDialog";
177     ui->setupUi(this);
178
179     this->setWindowTitle("Route");
180     left = 5; top = 5; right = 495; bottom = 295; // Limits in screen coordinates
181
182     helpRoutingDialog = NULL;
183
184     // Button settings
185     ui->sendPushButton->setAutoFillBackground(true);
186     ui->sendPushButton->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
187     ui->newPushButton->setAutoFillBackground(true);
188     ui->newPushButton->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
189
190     // Clear labels
191     ui->labelInfoToUser->setText("");
192     ui->speedValueLabel->setText("");
193     ui->avgSpeedValueLabel->setText("");
194
195     // Check login
196     checkLogin();
197
198     // Set average speed
199     QString average;
200     ui->avgSpeedValueLabel->setText(average.sprintf("%.1f", parent->getAverageSpeed()) + " km/h");
201     ui->distanceValueLabel->setText(parent->getDistanceTraveled() + " km");
202 }
203
204 /**
205   * Destructor of this class.
206   */
207 RouteDialog::~RouteDialog()
208 {
209     qDebug() << "__~RouteDialog";
210     if(ui)
211         delete ui;
212     if(calibrateDialog)
213         delete calibrateDialog;
214 }
215
216 /**
217   *
218   */
219 void RouteDialog::changeEvent(QEvent *e)
220 {
221     QDialog::changeEvent(e);
222     switch (e->type()) {
223     case QEvent::LanguageChange:
224         ui->retranslateUi(this);
225         break;
226     default:
227         break;
228     }
229 }
230
231 /**
232   *
233   */
234 int RouteDialog::getLeft()
235 {
236     return left;
237 }
238
239 /**
240   *
241   */
242 int RouteDialog::getRight()
243 {
244     return right;
245 }
246
247 /**
248   *
249   */
250 int RouteDialog::getTop()
251 {
252     return top;
253 }
254
255 /**
256   *
257   */
258 int RouteDialog::getBottom()
259 {
260     return bottom;
261 }
262
263 /**
264   *
265   */
266 void drawFlag( RouteDialog *rD, QPainter *p, int x, int y, QString startFinish)
267 {
268     /*QPolygon pg;
269
270     pg.setPoint(0,x, y-25);
271     pg.setPoint(1,x+10,y-20);
272     pg.setPoint(2,x, y-15);
273     pg.setPoint(3,x,y-20);*/
274     if (y> (rD->getTop() + 25))
275     {
276         // Upside
277         p->drawLine(x,y,x,y-15);
278         if (x <= (rD->getRight()-20))
279         {
280             // flag right
281             p->drawLine( x,    y-25, x+10, y-20);
282             p->drawLine( x+10, y-20, x,    y-15);
283             p->drawLine( x,    y-15, x,    y-25);
284
285             // Draw start or finish
286             p->drawText(x+10, y, startFinish);
287         }
288         else
289         {
290             // Flag left
291             p->drawLine( x,    y-25, x-10, y-20);
292             p->drawLine( x-10, y-20, x,    y-15);
293             p->drawLine( x,    y-15, x,    y-25);
294
295             // Draw start or finish
296             p->drawText(x+10, y, startFinish);
297         }    
298     }
299     else if (y <= (rD->getTop() + 25))
300     {
301         // downside
302         p->drawLine(x,y,x,y+15);
303         if (x <= (rD->getRight()-20))
304         {
305             // flag right
306             p->drawLine( x,    y+25, x+10, y+20);
307             p->drawLine( x+10, y+20, x,    y+15);
308             p->drawLine( x,    y+15, x,    y+25);
309
310             // Draw start or finish
311             p->drawText(x+10, y+15, startFinish);
312         }
313         else
314         {
315             // Flag left
316             p->drawLine( x,    y+25, x-10, y+20);
317             p->drawLine( x-10, y+20, x,    y+15);
318             p->drawLine( x,    y+15, x,    y+25);
319
320             // Draw start or finish
321             p->drawText(x+10, y+15, startFinish);
322         }
323     }
324     //p->drawPolygon();
325    // p->drawPolygon( pg,Qt::OddEvenFill);
326     //p->drawPolyline( &pg);
327     //p->drawPoints( pg);
328 }
329
330 /**
331   * Draws route to the route dialog.
332   * Type 0 is 2d viewing and type 1 is for 3d viewing
333   * @param QPaintEvent
334  */
335 /* */
336 void RouteDialog::paintEvent(QPaintEvent *)
337 {
338     // Check login
339     checkLogin();
340
341     int type = 0; //  0 for 2d, 1 for 3d
342     int startx, starty; // Starting point of the route
343     int i, maxi;
344     qreal x1, y1, x2, y2;
345     int x1Screen, y1Screen, x2Screen, y2Screen;
346     Vector v1, v2;
347     QPainter painter(this);
348     int startStop = 0;
349
350     painter.setRenderHint(QPainter::Antialiasing, true);
351     painter.setPen(QPen((Qt::white),2));
352     painter.setBrush(QBrush((Qt::yellow), Qt::SolidPattern));
353
354     // Draw route window frame
355     /*painter.drawLine(left,top,right,top);
356     painter.drawLine(right,top,right,bottom);
357     painter.drawLine(left,top,left,bottom);
358     painter.drawLine(left,bottom,right,bottom);*/
359
360     maxi = vertexList.size();
361
362     for (i=0; i<maxi-1; i++)
363     {
364        v1 = vertexList.at(i);
365        v2 = vertexList.at(i+1);
366
367        if (type == 0)
368        {    // 2d
369             x1 = v1.getX(); y1 = v1.getY();
370             x2 = v2.getX(); y2 = v2.getY();
371             //QString jono = QString("x: %1 y: %2").arg(x1).arg(y1);
372             //QMessageBox::about(0,"Tark",jono);
373
374             x1Screen = left + (x1-xmin)/(xmax-xmin)*(right-left);
375             y1Screen = top + (ymax-y1)/(ymax-ymin)*(bottom-top);
376             x2Screen = left + (x2-xmin)/(xmax-xmin)*(right-left);
377             y2Screen = top + (ymax-y2)/(ymax-ymin)*(bottom-top);
378         }
379         else if (type == 1)
380         {   // 3d
381             transformseg( &view3d, &v1,&v2, &x1Screen, &y1Screen, &x2Screen, &y2Screen);
382         }
383
384         // Show with circle if starting point
385         if (i==0)
386         {
387             // Starting point
388             startx = x1Screen; starty = y1Screen;
389            // painter.drawEllipse( x1Screen-5, y1Screen-5, 10, 10);
390            drawFlag( this, &painter,  x1Screen,  y1Screen, "Start" );
391         }
392         painter.drawLine( x1Screen, y1Screen, x2Screen, y2Screen);
393     }
394     // Show the endig point if different than the starting point
395     if (x2Screen != startx || y2Screen != starty)
396     {
397         //painter.drawEllipse( x2Screen-5, y2Screen-5, 10, 10);
398         drawFlag( this, &painter, x2Screen, y2Screen, "Finish" );
399     }
400
401     {
402         qreal maxvx, maxvy; // max speed point coordinates
403         qreal maxv;         // max speed
404         Vector v;
405
406         maxv = 0.0;
407         for (i=0; i<maxi-1; i++)
408         {
409             v = vertexList.at(i);
410             if (v.getV() > maxv)
411             {
412                 maxv = v.getV();
413                 maxvx = v.getX();
414                 maxvy = v.getY();
415             }
416         }
417         // Translate world coordinates to screen coordinates
418         x1Screen = left + (maxvx-xmin)/(xmax-xmin)*(right-left);
419         y1Screen = top + (ymax-maxvy)/(ymax-ymin)*(bottom-top);
420
421         // Show max velocity point by yellow circle
422         painter.drawEllipse( x1Screen-5, y1Screen-5, 10, 10);
423         painter.drawEllipse( ui->maxSpeedLabel->geometry().x()-15, ui->maxSpeedLabel->geometry().y()+15, 10, 10);
424
425         QString jono;
426         //jono = QString("%1 km/h").arg(maxv);
427         jono.sprintf("%.1f km/h", maxv); // Show only 1 decimal
428         ui->speedValueLabel->setText(jono);
429     }
430 }
431
432 /**
433   *
434   */
435 bool RouteDialog::readRouteFromFile( QString &routeFile , CalibrateDialog *calibrateDialog)
436 {
437     QString rFile = routeFile; //Not used
438     Vector temp;
439     QString rivi;
440     QFile file;
441
442     progresbar = calibrateDialog;
443     int progresbarValue = 0;
444     progresbar->setProgressValue(++progresbarValue);
445
446     //QString fileName = QFileDialog::getOpenFileName(this,
447     //     tr("Read Route"), "./", tr("Route Files (*.txt)"));
448
449     //file.setFileName( fileName);
450     file.setFileName( "routetemp.xml");
451     if (!file.open(QIODevice::ReadOnly))
452     {
453         QMessageBox::about(0, "Error", "File not found");
454         progresbar->setProgressValue(100);
455         return false;
456     }
457
458     vertexList.clear();
459
460     while(!file.atEnd())
461     {
462         int count;
463         bool allRead;
464         QString astr1, astr2, astr3, astr4;
465         QString str1, str2, str3, str4;
466         rivi = file.readLine();
467
468         allRead = false;
469         count = 0;
470         while( !allRead)
471         {
472             astr1 = rivi.section(" ", count*4+1, count*4+1); // latitude=""
473             astr2 = rivi.section(" ", count*4+2, count*4+2); // longitude=""
474             astr3 = rivi.section(" ", count*4+3, count*4+3); // altitude=""
475             astr4 = rivi.section(" ", count*4+4, count*4+4); // speed=""
476
477             {
478                 double x, y, z, v;
479                 str1 = astr1.section('"',1,1);
480                 str2 = astr2.section('"',1,1);
481                 str3 = astr3.section('"',1,1);
482                 str4 = astr4.section('"',1,1);
483             //QString str = QString("%1 %2 %3 %4").arg(str1).arg(str2).arg(str3).arg(str4);
484             //QMessageBox::about(0, "LUKEE", str);
485                 /* */
486
487                 if (str1.length() > 0)
488                 {
489                     x = str2.toDouble();// latitude y-value
490                     y = str1.toDouble();// longitude x-value
491                     z = str3.toDouble();// altitude z-value
492                     v = str4.toDouble();// speed km/h
493                // QString str = QString("%1 %2 %3 %4").arg(x).arg(y).arg(z).arg(v);
494                // QMessageBox::about(0, "LUKEE", str);
495                     temp.setX( x); // Longitude
496                     temp.setY( y); // Latitude
497                     temp.setZ( z); // altitude
498                     temp.setV( v);
499
500                     vertexList.append(temp);
501                     count++;
502                 }
503                 else
504                 {
505                     allRead = true;
506                 }
507             }
508         }
509         // Older version
510         /*
511         str1 = rivi.section(" ", 0, 0);
512         if (str1.compare("Start:") != 0 && str1.compare("Stop:") != 0)
513         {
514             str1 = rivi.section(" ", 2, 2); // latitude y-value
515             str2 = rivi.section(" ", 4, 4); // longitude x-value
516             str3 = rivi.section(" ", 6, 6); // altitude z-value
517             str4 = rivi.section(" ", 8, 8); // speed km/h
518             //QString str = QString("la: %1 lo: %2 al: %3").arg(str1).arg(str2).arg(str3);
519             //QMessageBox::about(0, "LUKEE", str);
520
521             if (str1.length() > 0)
522             {
523                 double x, y, z, v;
524                 x = str2.toDouble();
525                 y = str1.toDouble();
526                 z = str3.toDouble();
527                 v = str4.toDouble();
528                 temp.setX( x); // Longitude
529                 temp.setY( y); // Latitude
530                 temp.setZ( z); // altitude
531                 temp.setV( v);
532
533                 vertexList.append(temp);
534             }
535         }
536         */
537     }
538
539     file.close();
540
541      /********  in 3d use only */
542      a = 400/2.;
543      b = 1 - a*(-1);
544      c = -300/2.;
545      d = 300 - c*(-1);
546      //angle = toradians(60);
547
548      view3d.setUp( 1.0, 0.0, 0.0);
549      view3d.setAngle(toradians(60));
550      setAtPoint( &view3d);
551      xmin = objxmin; xmax = objxmax; ymin = objymin; ymax = objymax; // 2d viewing needs this !!!!
552      setFromPoint( &view3d);
553      view3d.setEye();
554      /****** end of 3d *****/
555
556      /*
557      //Testing distance counting
558      Vector a1, a2;
559      qreal dist;
560      //a1.setX( xmin); a1.setY( ymin);
561      //a2.setX( xmax); a2.setY( ymax);
562      a1.setX( 25.483); a1.setY( 65.017); // Oulu
563      a2.setX( 27.767); a2.setY( 64.283); // Kajaani
564      dist = countDistance( &a1, &a2);
565      QString str = QString("Min & Max datan välimatka %1").arg(dist);
566      QMessageBox::about( 0, "Testi", str);
567      */
568
569      return true;
570 }
571
572 /**
573   * Find out data range for x-, y- and z-coordinates
574   */
575 void dataMinMax( void)
576 {
577     int i, maxi;
578     qreal x,y,z;
579     Vector temp;
580
581     temp = vertexList.at(0);
582     objxmax = objxmin = temp.getX();
583     objymax = objymin = temp.getY();
584     objzmax = objzmin = temp.getZ();
585
586     maxi = vertexList.size();
587     for (i=1; i<maxi; i++)
588     {
589         temp = vertexList.at(i);
590         x = temp.getX();
591         y = temp.getY();
592         z = temp.getZ();
593
594         if (x < objxmin)
595         {
596                 objxmin = x;
597         }
598         else if (x > objxmax)
599         {
600                objxmax = x;
601         }
602
603         if (y < objymin)
604         {
605                 objymin = y;
606         }
607         else if (y > objymax)
608         {
609                objymax = y;
610         }
611
612         if (z < objzmin)
613         {
614                 objzmin = z;
615         }
616         else if (z > objzmax)
617         {
618                objzmax = z;
619         }
620     }
621     //QString jono = QString("ojxmin %1 objxmax %2").arg(objxmin).arg(objxmax);
622     //QString jono = QString("ojymin %1 objymax %2").arg(objymin).arg(objymax);
623     //QString jono = QString("ojzmin %1 objzmax %2").arg(objzmin).arg(objzmax);
624     //QMessageBox::about(0,"Tark", jono);
625 }
626
627 /**
628   * Setting the point where the viewed object is. In the middle of datapoints.
629   */
630 void setAtPoint( Viewing *v)
631 {
632     qreal x, y, z;
633     dataMinMax();
634     //Vector test;
635
636     x = (objxmax+objxmin)/2.0;
637     y= (objymax+objymin)/2.0;
638     z= (objzmax+objzmin)/2.0;
639
640     v->setAtPoint( x, y, z);
641     //QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(atPoint.x()).arg(atPoint.y()).arg(atPoint.z());
642     //QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(atPoint.x).arg(atPoint.y).arg(atPoint.z);
643
644     /* *
645     test = v->getAtPoint();
646     QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(test.getX()).arg(test.getY()).arg(test.getZ());
647     QMessageBox::about(0,"At point", jono);
648     * */
649 }
650
651 /**
652   * Setting the point where the object is viewed by eye.
653   */
654 void setFromPoint( Viewing *v)
655 {
656     qreal x, y, z;
657     Vector point;
658     point = v->getAtPoint();
659     //Vector test;
660     //fromPoint.setX( atPoint.getX() + (objxmax-objxmin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objymax-objymin)/2.0));
661     //x = 3.0;
662     //x = point.getX() + (objxmax-objxmin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objymax-objymin)/2.0);
663     x = point.getX();
664     //y = point.getY();
665     y = point.getY() + 40; // + (objymax-objymin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objxmax-objxmin)/2.0);
666     z = point.getZ();
667
668     v->setFromPoint(x,y,z);
669     //QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(fromPoint.x()).arg(fromPoint.y()).arg(fromPoint.z());
670     //QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(fromPoint.x).arg(fromPoint.y).arg(fromPoint.z);
671     /* *
672     test = v->getFromPoint();
673     QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(test.getX()).arg(test.getY()).arg(test.getZ());
674     QMessageBox::about(0,"From point", jono); // (1.9, 0.5, 0.5)
675     * */
676 }
677
678
679 #define NOEDGE     0x00
680 #define LEFTEDGE   0x01
681 #define RIGHTEDGE  0x02
682 #define BOTTOMEDGE 0x04
683 #define TOPEDGE    0x08
684
685 /**
686   * Returns a code specifying which edge in the viewing pyramid was crossed.
687   * There may be more than one.
688   */
689 int code( qreal x, qreal y, qreal z)
690 {
691     int c;
692
693     c = NOEDGE;
694     if (x<-z) c |= LEFTEDGE;
695     if (x>z) c |= RIGHTEDGE;
696     if (y<-z) c |= BOTTOMEDGE;
697     if (y>z) c |= TOPEDGE;
698
699     return c;
700 }
701
702 /**
703   * Converts clipped world coordinates to screen coordinates.
704   */
705 void WORLDtoSCREEN( qreal xWorld, qreal yWorld, int *xScreen, int *yScreen)
706 {
707    *xScreen = (int) (a*xWorld+b);
708    *yScreen = (int) (c*yWorld+d);
709 }
710
711 /**
712   * Clips the line segment in three-dimensional coordinates to the
713   * viewing pyramid.
714   */
715 void clip3d( qreal x1, qreal y1, qreal z1, qreal x2, qreal y2, qreal z2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2)
716 {
717     int c,c1,c2;
718     qreal x,y,z,t;
719
720     c1 = code(x1,y1,z1);
721     c2 = code(x2,y2,z2);
722
723     while (c1!= NOEDGE || c2 != NOEDGE)
724     {
725         if ((c1 & c2 ) != NOEDGE) return;
726         c = c1;
727         if (c == NOEDGE) c = c2;
728         if ((c&LEFTEDGE) == LEFTEDGE)
729         {
730                 // Crosses left edge
731                 t = (z1+x1)/((x1-x2)-(z2-z1));
732                 z = t*(z2-z1)+z1;
733                 x = -z;
734                 y = t*(y2-y1)+y1;
735         }
736         else if ((c&RIGHTEDGE) == RIGHTEDGE)
737         {
738                 // Crosses right edge
739                 t = (z1-x1)/((x2-x1)-(z2-z1));
740                 z = t*(z2-z1)+z1;
741                 x = z;
742                 y = t*(y2-y1)+y1;
743         }
744         else if ((c&BOTTOMEDGE) == BOTTOMEDGE)
745         {
746                 // Crosses bottom edge
747                 t = (z1+y1)/((y1-y2)-(z2-z1));
748                 z = t*(z2-z1)+z1;
749                 x = t*(x2-x1)+x1;
750                 y = -z;
751         }
752         else if ((c&TOPEDGE) == TOPEDGE)
753         {
754                 // Crosses top edge
755                 t = (z1-y1)/((y2-y1)-(z2-z1));
756                 z = t*(z2-z1)+z1;
757                 x = t*(x2-x1)+x1;
758                 y = z;
759         }
760
761         if (c == c1)
762         {
763             x1=x; y1=y; z1=z;
764             c1 = code(x,y,z);
765         }
766         else
767         {
768             x2=x; y2=y; z2=z;
769             c2 = code(x,y,z);
770         }
771     }
772
773     if (z1 != 0)
774     {
775         WORLDtoSCREEN(x1/z1,y1/z1,xscreen1, yscreen1);
776         WORLDtoSCREEN(x2/z2,y2/z2,xscreen2, yscreen2);
777     }
778     else
779     {
780         WORLDtoSCREEN(x1,y1,xscreen1, yscreen1);
781         WORLDtoSCREEN(x2,y2,xscreen2, yscreen2);
782     }
783     //Now ready to draw line( xscreen1, yscreen1, xscreen2, yscreen2);
784 }
785
786 /**
787   * Transform the segment connecting the two vectors into the viewing plane.
788   * clip3d() clips the line if needed.
789   */
790 void transformseg( Viewing *v, Vector *v1, Vector *v2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2)
791
792 {
793     qreal x1, y1, z1, x2, y2, z2;
794     Vector a1, a2, a3;
795
796     a1 = v->getA1();
797     a2 = v->getA2();
798     a3 = v->getA3();
799
800     x1 = (a1.getX()*v1->getX() + a1.getY()*v1->getY() + a1.getZ()*v1->getZ() + v->getOffsx())*v->getDval();
801     y1 = (a2.getX()*v1->getX() + a2.getY()*v1->getY() + a2.getZ()*v1->getZ() + v->getOffsy())*v->getDval();
802     z1 = a3.getX()*v1->getX() + a3.getY()*v1->getY() + a3.getZ()*v1->getZ() + v->getOffsz();
803
804     x2 = (a1.getX()*v2->getX() + a1.getY()*v2->getY() + a1.getZ()*v2->getZ() + v->getOffsx())*v->getDval();
805     y2 = (a2.getX()*v2->getX() + a2.getY()*v2->getY() + a2.getZ()*v2->getZ() + v->getOffsy())*v->getDval();
806     z2 = a3.getX()*v2->getX() + a3.getY()*v2->getY() + a3.getZ()*v2->getZ() + v->getOffsz();
807
808     clip3d(x1,y1,z1,x2,y2,z2, xscreen1, yscreen1, xscreen2, yscreen2 );
809 }
810
811 /**
812   * This slot function is called when ever new push button clicked.
813   */
814 void RouteDialog::on_newPushButton_clicked()
815 {
816     close();    // go back to previous dialog
817 }
818
819 /**
820   * This slot function is called when ever send push button clicked.
821   */
822 void RouteDialog::on_sendPushButton_clicked()
823 {
824     ui->sendPushButton->setEnabled(false);
825     emit sendroute();
826 }
827
828 /**
829   * This function is set info text to user.
830   */
831 void RouteDialog::setLabelInfoToUser(QString infoText)
832 {
833     this->ui->labelInfoToUser->setText(infoText);
834 }
835
836 /**
837   * This function enable send server button.
838   */
839 void RouteDialog::setSendServerButtonEnabled()
840 {
841     ui->sendPushButton->setEnabled(true);
842 }
843
844 /**
845   * This function check login and set send route to server button disabled/enabled.
846   */
847 void RouteDialog::checkLogin()
848 {
849     if (loginSaved())
850     {
851         ui->sendPushButton->setEnabled(true);
852         ui->labelInfoToUser->setText("");
853     }
854     else
855     {
856         ui->sendPushButton->setEnabled(false);
857         ui->labelInfoToUser->setText("You're not logged! Please register or log in.");
858     }
859 }
860
861 /**
862   * This slot function called when ever info button clicked.
863   */
864 void RouteDialog::on_pushButtonInfo_clicked()
865 {    
866     if(!helpRoutingDialog)
867     {
868         helpRoutingDialog = new HelpRoutingDialog;
869     }
870     connect(helpRoutingDialog, SIGNAL(rejected()), this, SLOT(killHelpDialog()));
871     helpRoutingDialog->show();
872 }
873
874 /**
875   * This slot function called when ever dialog rejected.
876   */
877 void RouteDialog::killHelpDialog()
878 {
879     if(helpRoutingDialog)
880     {
881         qDebug() << "__Route kill: helpRoutingDialog";
882         delete helpRoutingDialog;
883         helpRoutingDialog = NULL;
884     }
885 }