a71197404f36fa3960ea24043d640ebabde6d526
[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 }
213
214 /**
215   *
216   */
217 void RouteDialog::changeEvent(QEvent *e)
218 {
219     QDialog::changeEvent(e);
220     switch (e->type()) {
221     case QEvent::LanguageChange:
222         ui->retranslateUi(this);
223         break;
224     default:
225         break;
226     }
227 }
228
229 /**
230   *
231   */
232 int RouteDialog::getLeft()
233 {
234     return left;
235 }
236
237 /**
238   *
239   */
240 int RouteDialog::getRight()
241 {
242     return right;
243 }
244
245 /**
246   *
247   */
248 int RouteDialog::getTop()
249 {
250     return top;
251 }
252
253 /**
254   *
255   */
256 int RouteDialog::getBottom()
257 {
258     return bottom;
259 }
260
261 /**
262   *
263   */
264 void drawFlag( RouteDialog *rD, QPainter *p, int x, int y, QString startFinish)
265 {
266     /*QPolygon pg;
267
268     pg.setPoint(0,x, y-25);
269     pg.setPoint(1,x+10,y-20);
270     pg.setPoint(2,x, y-15);
271     pg.setPoint(3,x,y-20);*/
272     if (y> (rD->getTop() + 25))
273     {
274         // Upside
275         p->drawLine(x,y,x,y-15);
276         if (x <= (rD->getRight()-20))
277         {
278             // flag right
279             p->drawLine( x,    y-25, x+10, y-20);
280             p->drawLine( x+10, y-20, x,    y-15);
281             p->drawLine( x,    y-15, x,    y-25);
282
283             // Draw start or finish
284             p->drawText(x+10, y, startFinish);
285         }
286         else
287         {
288             // Flag left
289             p->drawLine( x,    y-25, x-10, y-20);
290             p->drawLine( x-10, y-20, x,    y-15);
291             p->drawLine( x,    y-15, x,    y-25);
292
293             // Draw start or finish
294             p->drawText(x+10, y, startFinish);
295         }    
296     }
297     else if (y <= (rD->getTop() + 25))
298     {
299         // downside
300         p->drawLine(x,y,x,y+15);
301         if (x <= (rD->getRight()-20))
302         {
303             // flag right
304             p->drawLine( x,    y+25, x+10, y+20);
305             p->drawLine( x+10, y+20, x,    y+15);
306             p->drawLine( x,    y+15, x,    y+25);
307
308             // Draw start or finish
309             p->drawText(x+10, y+15, startFinish);
310         }
311         else
312         {
313             // Flag left
314             p->drawLine( x,    y+25, x-10, y+20);
315             p->drawLine( x-10, y+20, x,    y+15);
316             p->drawLine( x,    y+15, x,    y+25);
317
318             // Draw start or finish
319             p->drawText(x+10, y+15, startFinish);
320         }
321     }
322     //p->drawPolygon();
323    // p->drawPolygon( pg,Qt::OddEvenFill);
324     //p->drawPolyline( &pg);
325     //p->drawPoints( pg);
326 }
327
328 /**
329   * Draws route to the route dialog.
330   * Type 0 is 2d viewing and type 1 is for 3d viewing
331   * @param QPaintEvent
332  */
333 /* */
334 void RouteDialog::paintEvent(QPaintEvent *)
335 {
336     // Check login
337     checkLogin();
338
339     int type = 0; //  0 for 2d, 1 for 3d
340     int startx, starty; // Starting point of the route
341     int i, maxi;
342     qreal x1, y1, x2, y2;
343     int x1Screen, y1Screen, x2Screen, y2Screen;
344     Vector v1, v2;
345     QPainter painter(this);
346     int startStop = 0;
347
348     painter.setRenderHint(QPainter::Antialiasing, true);
349     painter.setPen(QPen((Qt::white),2));
350     painter.setBrush(QBrush((Qt::yellow), Qt::SolidPattern));
351
352     // Draw route window frame
353     /*painter.drawLine(left,top,right,top);
354     painter.drawLine(right,top,right,bottom);
355     painter.drawLine(left,top,left,bottom);
356     painter.drawLine(left,bottom,right,bottom);*/
357
358     maxi = vertexList.size();
359
360     for (i=0; i<maxi-1; i++)
361     {
362        v1 = vertexList.at(i);
363        v2 = vertexList.at(i+1);
364
365        if (type == 0)
366        {    // 2d
367             x1 = v1.getX(); y1 = v1.getY();
368             x2 = v2.getX(); y2 = v2.getY();
369             //QString jono = QString("x: %1 y: %2").arg(x1).arg(y1);
370             //QMessageBox::about(0,"Tark",jono);
371
372             x1Screen = left + (x1-xmin)/(xmax-xmin)*(right-left);
373             y1Screen = top + (ymax-y1)/(ymax-ymin)*(bottom-top);
374             x2Screen = left + (x2-xmin)/(xmax-xmin)*(right-left);
375             y2Screen = top + (ymax-y2)/(ymax-ymin)*(bottom-top);
376         }
377         else if (type == 1)
378         {   // 3d
379             transformseg( &view3d, &v1,&v2, &x1Screen, &y1Screen, &x2Screen, &y2Screen);
380         }
381
382         // Show with circle if starting point
383         if (i==0)
384         {
385             // Starting point
386             startx = x1Screen; starty = y1Screen;
387            // painter.drawEllipse( x1Screen-5, y1Screen-5, 10, 10);
388            drawFlag( this, &painter,  x1Screen,  y1Screen, "Start" );
389         }
390         painter.drawLine( x1Screen, y1Screen, x2Screen, y2Screen);
391     }
392     // Show the endig point if different than the starting point
393     if (x2Screen != startx || y2Screen != starty)
394     {
395         //painter.drawEllipse( x2Screen-5, y2Screen-5, 10, 10);
396         drawFlag( this, &painter, x2Screen, y2Screen, "Finish" );
397     }
398
399     {
400         qreal maxvx, maxvy; // max speed point coordinates
401         qreal maxv;         // max speed
402         Vector v;
403
404         maxv = 0.0;
405         for (i=0; i<maxi-1; i++)
406         {
407             v = vertexList.at(i);
408             if (v.getV() > maxv)
409             {
410                 maxv = v.getV();
411                 maxvx = v.getX();
412                 maxvy = v.getY();
413             }
414         }
415         // Translate world coordinates to screen coordinates
416         x1Screen = left + (maxvx-xmin)/(xmax-xmin)*(right-left);
417         y1Screen = top + (ymax-maxvy)/(ymax-ymin)*(bottom-top);
418
419         // Show max velocity point by yellow circle
420         painter.drawEllipse( x1Screen-5, y1Screen-5, 10, 10);
421         painter.drawEllipse( ui->maxSpeedLabel->geometry().x()-15, ui->maxSpeedLabel->geometry().y()+15, 10, 10);
422
423         QString jono;
424         //jono = QString("%1 km/h").arg(maxv);
425         jono.sprintf("%.1f km/h", maxv); // Show only 1 decimal
426         ui->speedValueLabel->setText(jono);
427     }
428 }
429
430 /**
431   *
432   */
433 bool RouteDialog::readRouteFromFile( QString &routeFile , CalibrateDialog *calibrateDialog)
434 {
435     QString rFile = routeFile; //Not used
436     Vector temp;
437     QString rivi;
438     QFile file;
439
440     progresbar = calibrateDialog;
441     int progresbarValue = 0;
442     progresbar->setProgressValue(++progresbarValue);
443
444     //QString fileName = QFileDialog::getOpenFileName(this,
445     //     tr("Read Route"), "./", tr("Route Files (*.txt)"));
446
447     //file.setFileName( fileName);
448     file.setFileName( "routetemp.xml");
449     if (!file.open(QIODevice::ReadOnly))
450     {
451         QMessageBox::about(0, "Error", "File not found");
452         progresbar->setProgressValue(100);
453         return false;
454     }
455
456     vertexList.clear();
457
458     while(!file.atEnd())
459     {
460         int count;
461         bool allRead;
462         QString astr1, astr2, astr3, astr4;
463         QString str1, str2, str3, str4;
464         rivi = file.readLine();
465
466         allRead = false;
467         count = 0;
468         while( !allRead)
469         {
470             astr1 = rivi.section(" ", count*4+1, count*4+1); // latitude=""
471             astr2 = rivi.section(" ", count*4+2, count*4+2); // longitude=""
472             astr3 = rivi.section(" ", count*4+3, count*4+3); // altitude=""
473             astr4 = rivi.section(" ", count*4+4, count*4+4); // speed=""
474
475             {
476                 double x, y, z, v;
477                 str1 = astr1.section('"',1,1);
478                 str2 = astr2.section('"',1,1);
479                 str3 = astr3.section('"',1,1);
480                 str4 = astr4.section('"',1,1);
481             //QString str = QString("%1 %2 %3 %4").arg(str1).arg(str2).arg(str3).arg(str4);
482             //QMessageBox::about(0, "LUKEE", str);
483                 /* */
484
485                 if (str1.length() > 0)
486                 {
487                     x = str2.toDouble();// latitude y-value
488                     y = str1.toDouble();// longitude x-value
489                     z = str3.toDouble();// altitude z-value
490                     v = str4.toDouble();// speed km/h
491                // QString str = QString("%1 %2 %3 %4").arg(x).arg(y).arg(z).arg(v);
492                // QMessageBox::about(0, "LUKEE", str);
493                     temp.setX( x); // Longitude
494                     temp.setY( y); // Latitude
495                     temp.setZ( z); // altitude
496                     temp.setV( v);
497
498                     vertexList.append(temp);
499                     count++;
500                 }
501                 else
502                 {
503                     allRead = true;
504                 }
505             }
506         }
507         // Older version
508         /*
509         str1 = rivi.section(" ", 0, 0);
510         if (str1.compare("Start:") != 0 && str1.compare("Stop:") != 0)
511         {
512             str1 = rivi.section(" ", 2, 2); // latitude y-value
513             str2 = rivi.section(" ", 4, 4); // longitude x-value
514             str3 = rivi.section(" ", 6, 6); // altitude z-value
515             str4 = rivi.section(" ", 8, 8); // speed km/h
516             //QString str = QString("la: %1 lo: %2 al: %3").arg(str1).arg(str2).arg(str3);
517             //QMessageBox::about(0, "LUKEE", str);
518
519             if (str1.length() > 0)
520             {
521                 double x, y, z, v;
522                 x = str2.toDouble();
523                 y = str1.toDouble();
524                 z = str3.toDouble();
525                 v = str4.toDouble();
526                 temp.setX( x); // Longitude
527                 temp.setY( y); // Latitude
528                 temp.setZ( z); // altitude
529                 temp.setV( v);
530
531                 vertexList.append(temp);
532             }
533         }
534         */
535     }
536
537     file.close();
538
539      /********  in 3d use only */
540      a = 400/2.;
541      b = 1 - a*(-1);
542      c = -300/2.;
543      d = 300 - c*(-1);
544      //angle = toradians(60);
545
546      view3d.setUp( 1.0, 0.0, 0.0);
547      view3d.setAngle(toradians(60));
548      setAtPoint( &view3d);
549      xmin = objxmin; xmax = objxmax; ymin = objymin; ymax = objymax; // 2d viewing needs this !!!!
550      setFromPoint( &view3d);
551      view3d.setEye();
552      /****** end of 3d *****/
553
554      /*
555      //Testing distance counting
556      Vector a1, a2;
557      qreal dist;
558      //a1.setX( xmin); a1.setY( ymin);
559      //a2.setX( xmax); a2.setY( ymax);
560      a1.setX( 25.483); a1.setY( 65.017); // Oulu
561      a2.setX( 27.767); a2.setY( 64.283); // Kajaani
562      dist = countDistance( &a1, &a2);
563      QString str = QString("Min & Max datan välimatka %1").arg(dist);
564      QMessageBox::about( 0, "Testi", str);
565      */
566
567      return true;
568 }
569
570 /**
571   * Find out data range for x-, y- and z-coordinates
572   */
573 void dataMinMax( void)
574 {
575     int i, maxi;
576     qreal x,y,z;
577     Vector temp;
578
579     temp = vertexList.at(0);
580     objxmax = objxmin = temp.getX();
581     objymax = objymin = temp.getY();
582     objzmax = objzmin = temp.getZ();
583
584     maxi = vertexList.size();
585     for (i=1; i<maxi; i++)
586     {
587         temp = vertexList.at(i);
588         x = temp.getX();
589         y = temp.getY();
590         z = temp.getZ();
591
592         if (x < objxmin)
593         {
594                 objxmin = x;
595         }
596         else if (x > objxmax)
597         {
598                objxmax = x;
599         }
600
601         if (y < objymin)
602         {
603                 objymin = y;
604         }
605         else if (y > objymax)
606         {
607                objymax = y;
608         }
609
610         if (z < objzmin)
611         {
612                 objzmin = z;
613         }
614         else if (z > objzmax)
615         {
616                objzmax = z;
617         }
618     }
619     //QString jono = QString("ojxmin %1 objxmax %2").arg(objxmin).arg(objxmax);
620     //QString jono = QString("ojymin %1 objymax %2").arg(objymin).arg(objymax);
621     //QString jono = QString("ojzmin %1 objzmax %2").arg(objzmin).arg(objzmax);
622     //QMessageBox::about(0,"Tark", jono);
623 }
624
625 /**
626   * Setting the point where the viewed object is. In the middle of datapoints.
627   */
628 void setAtPoint( Viewing *v)
629 {
630     qreal x, y, z;
631     dataMinMax();
632     //Vector test;
633
634     x = (objxmax+objxmin)/2.0;
635     y= (objymax+objymin)/2.0;
636     z= (objzmax+objzmin)/2.0;
637
638     v->setAtPoint( x, y, z);
639     //QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(atPoint.x()).arg(atPoint.y()).arg(atPoint.z());
640     //QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(atPoint.x).arg(atPoint.y).arg(atPoint.z);
641
642     /* *
643     test = v->getAtPoint();
644     QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(test.getX()).arg(test.getY()).arg(test.getZ());
645     QMessageBox::about(0,"At point", jono);
646     * */
647 }
648
649 /**
650   * Setting the point where the object is viewed by eye.
651   */
652 void setFromPoint( Viewing *v)
653 {
654     qreal x, y, z;
655     Vector point;
656     point = v->getAtPoint();
657     //Vector test;
658     //fromPoint.setX( atPoint.getX() + (objxmax-objxmin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objymax-objymin)/2.0));
659     //x = 3.0;
660     //x = point.getX() + (objxmax-objxmin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objymax-objymin)/2.0);
661     x = point.getX();
662     //y = point.getY();
663     y = point.getY() + 40; // + (objymax-objymin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objxmax-objxmin)/2.0);
664     z = point.getZ();
665
666     v->setFromPoint(x,y,z);
667     //QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(fromPoint.x()).arg(fromPoint.y()).arg(fromPoint.z());
668     //QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(fromPoint.x).arg(fromPoint.y).arg(fromPoint.z);
669     /* *
670     test = v->getFromPoint();
671     QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(test.getX()).arg(test.getY()).arg(test.getZ());
672     QMessageBox::about(0,"From point", jono); // (1.9, 0.5, 0.5)
673     * */
674 }
675
676
677 #define NOEDGE     0x00
678 #define LEFTEDGE   0x01
679 #define RIGHTEDGE  0x02
680 #define BOTTOMEDGE 0x04
681 #define TOPEDGE    0x08
682
683 /**
684   * Returns a code specifying which edge in the viewing pyramid was crossed.
685   * There may be more than one.
686   */
687 int code( qreal x, qreal y, qreal z)
688 {
689     int c;
690
691     c = NOEDGE;
692     if (x<-z) c |= LEFTEDGE;
693     if (x>z) c |= RIGHTEDGE;
694     if (y<-z) c |= BOTTOMEDGE;
695     if (y>z) c |= TOPEDGE;
696
697     return c;
698 }
699
700 /**
701   * Converts clipped world coordinates to screen coordinates.
702   */
703 void WORLDtoSCREEN( qreal xWorld, qreal yWorld, int *xScreen, int *yScreen)
704 {
705    *xScreen = (int) (a*xWorld+b);
706    *yScreen = (int) (c*yWorld+d);
707 }
708
709 /**
710   * Clips the line segment in three-dimensional coordinates to the
711   * viewing pyramid.
712   */
713 void clip3d( qreal x1, qreal y1, qreal z1, qreal x2, qreal y2, qreal z2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2)
714 {
715     int c,c1,c2;
716     qreal x,y,z,t;
717
718     c1 = code(x1,y1,z1);
719     c2 = code(x2,y2,z2);
720
721     while (c1!= NOEDGE || c2 != NOEDGE)
722     {
723         if ((c1 & c2 ) != NOEDGE) return;
724         c = c1;
725         if (c == NOEDGE) c = c2;
726         if ((c&LEFTEDGE) == LEFTEDGE)
727         {
728                 // Crosses left edge
729                 t = (z1+x1)/((x1-x2)-(z2-z1));
730                 z = t*(z2-z1)+z1;
731                 x = -z;
732                 y = t*(y2-y1)+y1;
733         }
734         else if ((c&RIGHTEDGE) == RIGHTEDGE)
735         {
736                 // Crosses right edge
737                 t = (z1-x1)/((x2-x1)-(z2-z1));
738                 z = t*(z2-z1)+z1;
739                 x = z;
740                 y = t*(y2-y1)+y1;
741         }
742         else if ((c&BOTTOMEDGE) == BOTTOMEDGE)
743         {
744                 // Crosses bottom edge
745                 t = (z1+y1)/((y1-y2)-(z2-z1));
746                 z = t*(z2-z1)+z1;
747                 x = t*(x2-x1)+x1;
748                 y = -z;
749         }
750         else if ((c&TOPEDGE) == TOPEDGE)
751         {
752                 // Crosses top edge
753                 t = (z1-y1)/((y2-y1)-(z2-z1));
754                 z = t*(z2-z1)+z1;
755                 x = t*(x2-x1)+x1;
756                 y = z;
757         }
758
759         if (c == c1)
760         {
761             x1=x; y1=y; z1=z;
762             c1 = code(x,y,z);
763         }
764         else
765         {
766             x2=x; y2=y; z2=z;
767             c2 = code(x,y,z);
768         }
769     }
770
771     if (z1 != 0)
772     {
773         WORLDtoSCREEN(x1/z1,y1/z1,xscreen1, yscreen1);
774         WORLDtoSCREEN(x2/z2,y2/z2,xscreen2, yscreen2);
775     }
776     else
777     {
778         WORLDtoSCREEN(x1,y1,xscreen1, yscreen1);
779         WORLDtoSCREEN(x2,y2,xscreen2, yscreen2);
780     }
781     //Now ready to draw line( xscreen1, yscreen1, xscreen2, yscreen2);
782 }
783
784 /**
785   * Transform the segment connecting the two vectors into the viewing plane.
786   * clip3d() clips the line if needed.
787   */
788 void transformseg( Viewing *v, Vector *v1, Vector *v2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2)
789
790 {
791     qreal x1, y1, z1, x2, y2, z2;
792     Vector a1, a2, a3;
793
794     a1 = v->getA1();
795     a2 = v->getA2();
796     a3 = v->getA3();
797
798     x1 = (a1.getX()*v1->getX() + a1.getY()*v1->getY() + a1.getZ()*v1->getZ() + v->getOffsx())*v->getDval();
799     y1 = (a2.getX()*v1->getX() + a2.getY()*v1->getY() + a2.getZ()*v1->getZ() + v->getOffsy())*v->getDval();
800     z1 = a3.getX()*v1->getX() + a3.getY()*v1->getY() + a3.getZ()*v1->getZ() + v->getOffsz();
801
802     x2 = (a1.getX()*v2->getX() + a1.getY()*v2->getY() + a1.getZ()*v2->getZ() + v->getOffsx())*v->getDval();
803     y2 = (a2.getX()*v2->getX() + a2.getY()*v2->getY() + a2.getZ()*v2->getZ() + v->getOffsy())*v->getDval();
804     z2 = a3.getX()*v2->getX() + a3.getY()*v2->getY() + a3.getZ()*v2->getZ() + v->getOffsz();
805
806     clip3d(x1,y1,z1,x2,y2,z2, xscreen1, yscreen1, xscreen2, yscreen2 );
807 }
808
809 /**
810   * This slot function is called when ever new push button clicked.
811   */
812 void RouteDialog::on_newPushButton_clicked()
813 {
814     close();    // go back to previous dialog
815 }
816
817 /**
818   * This slot function is called when ever send push button clicked.
819   */
820 void RouteDialog::on_sendPushButton_clicked()
821 {
822     ui->sendPushButton->setEnabled(false);
823     emit sendroute();
824 }
825
826 /**
827   * This function is set info text to user.
828   */
829 void RouteDialog::setLabelInfoToUser(QString infoText)
830 {
831     this->ui->labelInfoToUser->setText(infoText);
832 }
833
834 /**
835   * This function enable send server button.
836   */
837 void RouteDialog::setSendServerButtonEnabled()
838 {
839     ui->sendPushButton->setEnabled(true);
840 }
841
842 /**
843   * This function check login and set send route to server button disabled/enabled.
844   */
845 void RouteDialog::checkLogin()
846 {
847     if (loginSaved())
848     {
849         ui->sendPushButton->setEnabled(true);
850         ui->labelInfoToUser->setText("");
851     }
852     else
853     {
854         ui->sendPushButton->setEnabled(false);
855         ui->labelInfoToUser->setText("You're not logged! Please register or log in.");
856     }
857 }
858
859 /**
860   * This slot function called when ever info button clicked.
861   */
862 void RouteDialog::on_pushButtonInfo_clicked()
863 {    
864     if(!helpRoutingDialog)
865     {
866         helpRoutingDialog = new HelpRoutingDialog;
867     }
868     connect(helpRoutingDialog, SIGNAL(rejected()), this, SLOT(killHelpDialog()));
869     helpRoutingDialog->show();
870 }
871
872 /**
873   * This slot function called when ever dialog rejected.
874   */
875 void RouteDialog::killHelpDialog()
876 {
877     if(helpRoutingDialog)
878     {
879         qDebug() << "__Route kill: helpRoutingDialog";
880         delete helpRoutingDialog;
881         helpRoutingDialog = NULL;
882     }
883 }