Added button for sending route to server.
[speedfreak] / Client / routedialog.cpp
1 /*
2  * RouteDialog class
3  *
4  * @author     Olavi Pulkkinen <olavi.pulkkinen@fudeco.com>
5  * @copyright  (c) 2010 Speed Freak team
6  * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
7  */
8
9 #include "routedialog.h"
10 #include "ui_routedialog.h"
11 #include <cmath>
12 #include <QPainter>
13 #include <QList>
14 #include <QMessageBox>
15 #include <QFile>
16 #include <QFileDialog>
17
18 /*
19   * Vector class.
20   * In starting Qt 4.6 there is QVector3D.
21   * Later (updating Qt version) this class can be removed.
22   */
23 class Vector
24 {
25     qreal x, y, z;
26 public:
27     Vector() { x=0.; y=0. ; z=0.; };
28     Vector( qreal initX, qreal initY, qreal initZ) { x = initX, y = initY; z = initZ; };
29     void setX( qreal newX) { x = newX; };
30     void setY( qreal newY) { y = newY; };
31     void setZ( qreal newZ) { z = newZ; };
32     qreal getX() { return x; };
33     qreal getY() { return y; };
34     qreal getZ() { return z; };
35     qreal length() { return sqrt(x*x+y*y+z*z); };
36     Vector operator+(Vector v)
37     {
38         x = x + v.x; y = y + v.y; z = z + v.z;
39         return *this;
40     };
41     Vector operator-(Vector v)
42     {
43         x = x - v.x; y = y - v.y; z = z - v.z;
44         return *this;
45     };
46     Vector operator/(qreal c)
47     {
48         x = x/c; y = y/c; z = z/c;
49         return *this;
50     };
51     Vector crossProduct( Vector a, Vector b)
52     {
53         x = a.y*b.z - a.z*b.y;
54         y = a.z*b.x - a.x*b.z;
55         z = a.x*b.y - a.y*b.x;
56         return *this;
57     };
58 };
59
60
61 class Viewing
62 {
63     Vector atPoint, fromPoint, up, a1, a2, a3;
64     qreal offsx, offsy, offsz;
65     qreal dval;
66     qreal angle;
67 public:
68     qreal getOffsx() { return offsx; };
69     qreal getOffsy() { return offsy; };
70     qreal getOffsz() { return offsz; };
71     qreal getDval() { return dval; };
72     void setAngle( qreal newA) { angle = newA; };
73     void setUp( qreal newUpX, qreal newUpY, qreal newUpZ)
74     {
75         up.setX(newUpX); up.setY(newUpY); up.setZ(newUpZ);
76     };
77     void setAtPoint( qreal newX, qreal newY, qreal newZ)
78     {
79         atPoint.setX(newX); atPoint.setY(newY); atPoint.setZ(newZ);
80     };
81     void setFromPoint(qreal newX, qreal newY, qreal newZ)
82     {
83         fromPoint.setX(newX); fromPoint.setY(newY); fromPoint.setZ(newZ);
84     }
85     void setEye()
86     {
87         double amarkmag, tempmag;
88         Vector temp, dist;
89
90         dval = cos(angle/2.0)/sin(angle/2.0);
91         dist = atPoint-fromPoint;
92         amarkmag = dist.length();
93         a3 = dist/amarkmag;
94
95         temp.crossProduct( dist, up);
96         tempmag = temp.length();
97         a1 = temp/tempmag;
98
99         temp.crossProduct( a1, a3);
100         tempmag = temp.length();
101         a2 = temp/tempmag;
102
103         offsx = -a1.getX()*fromPoint.getX() - a1.getY()*fromPoint.getY() - a1.getZ()*fromPoint.getZ();
104         offsy = -a2.getX()*fromPoint.getX() - a2.getY()*fromPoint.getY() - a2.getZ()*fromPoint.getZ();
105         offsz = -a3.getX()*fromPoint.getX() - a3.getY()*fromPoint.getY() - a3.getZ()*fromPoint.getZ();
106         //QString jono2 = QString("offsx %1 offsy %2 offsz %3").arg(offsx).arg(offsy).arg(offsz);
107         //QMessageBox::about(0,"offs x y z", jono2);
108     } ;
109     Vector getAtPoint() { return atPoint; };
110     Vector getFromPoint() { return fromPoint; };
111     Vector getA1() { return a1; };
112     Vector getA2() { return a2; };
113     Vector getA3() { return a3; };
114     Viewing () {};
115 };
116
117 qreal xmax, xmin, ymin, ymax;       // Limits in world coordinates
118
119 QList<Vector> vertexList;           // Vertecies of route
120
121 qreal objxmin, objxmax, objymin, objymax, objzmin, objzmax; // data ranges
122
123 #define maxof(val1,val2)  ((val1>val2)?val1:val2)
124 #define toradians( degrees) (degrees*0.017453293)
125
126 #define WIDTH 1.8   // For 3d viewing only
127 qreal a, b,c,d; // Used for 3d viewing to calculate screen coordinates
128
129 Viewing view3d;   // Viewing settings for 3d
130
131 // Function prototypes
132 void dataMinMax( void);
133 void setAtPoint( Viewing *v);
134 void setFromPoint( Viewing *v);
135 void transformseg( Viewing *v, Vector *v1, Vector *v2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2 );
136
137 #define R 6378.140 // The radius of the earth by kilometers
138 /*
139   * count distance of two points (defined by longitude & latitude)
140   * on the surface of the earth.
141   */
142 qreal countDistance(Vector *p1, Vector *p2)
143 {
144     qreal dLon, dLat;   // delta of longitude & latitude
145     qreal a, c;
146
147     dLon = p2->getX() - p1->getX();     // longitude difference
148     dLat = p2->getY() - p1->getY();     // latitude difference
149     if (dLon <0) dLon = -dLon;
150     if (dLat <0) dLat = -dLat;
151
152     dLon = dLon*3.14/180;
153     dLat = dLat*3.14/180;
154     a = (sin(dLat/2.))*(sin(dLat/2.)) +
155         (cos(p1->getY())*3.14/180)*(cos(p2->getY())*3.14/180)*(sin(dLon/2.))*(sin(dLon/2.));
156     c = 2.*atan(sqrt(a)/sqrt(1-a));     // c is angle between points p1 & p2 with circel by radius 1.
157
158     return R*c;   // Return distance in kilometers
159 }
160
161 RouteDialog::RouteDialog(QWidget *parent) :
162     QDialog(parent),
163     ui(new Ui::RouteDialog)
164 {
165     ui->setupUi(this);
166     left = 5; top = 5; right = 395; bottom = 195; // Limits in screen coordinates
167 }
168
169 RouteDialog::~RouteDialog()
170 {
171     delete ui;
172 }
173
174 void RouteDialog::changeEvent(QEvent *e)
175 {
176     QDialog::changeEvent(e);
177     switch (e->type()) {
178     case QEvent::LanguageChange:
179         ui->retranslateUi(this);
180         break;
181     default:
182         break;
183     }
184 }
185
186
187 /**
188   * Draws route to the route dialog.
189   * Type 0 is 2d viewing and type 1 is for 3d viewing
190   * @param QPaintEvent
191  */
192 /* */
193 void RouteDialog::paintEvent(QPaintEvent *)
194 {
195     int type = 0; //  0 for 2d, 1 for 3d
196     int startx, starty; // Starting point of the route
197     int i, maxi;
198     qreal x1, y1, x2, y2;
199     int x1Screen, y1Screen, x2Screen, y2Screen;
200     Vector v1, v2;
201     QPainter painter(this);
202
203     painter.setRenderHint(QPainter::Antialiasing, true);
204     painter.setPen(QPen((Qt::black),2));
205     painter.setBrush(QBrush((Qt::yellow), Qt::SolidPattern));
206
207     // Draw route window frame
208     /*painter.drawLine(left,top,right,top);
209     painter.drawLine(right,top,right,bottom);
210     painter.drawLine(left,top,left,bottom);
211     painter.drawLine(left,bottom,right,bottom);
212     */
213     maxi = vertexList.size();
214
215     for (i=0; i<maxi-1; i++)
216     {
217        v1 = vertexList.at(i);
218        v2 = vertexList.at(i+1);
219
220        if (type == 0)
221        {    // 2d
222             x1 = v1.getX(); y1 = v1.getY();
223             x2 = v2.getX(); y2 = v2.getY();
224             //QString jono = QString("x: %1 y: %2").arg(x1).arg(y1);
225             //QMessageBox::about(0,"Tark",jono);
226
227             x1Screen = left + (x1-xmin)/(xmax-xmin)*(right-left);
228             y1Screen = top + (ymax-y1)/(ymax-ymin)*(bottom-top);
229             x2Screen = left + (x2-xmin)/(xmax-xmin)*(right-left);
230             y2Screen = top + (ymax-y2)/(ymax-ymin)*(bottom-top);
231         }
232         else if (type == 1)
233         {   // 3d
234             transformseg( &view3d, &v1,&v2, &x1Screen, &y1Screen, &x2Screen, &y2Screen);
235         }
236
237         // Show with circle if starting point
238         if (i==0)
239         {
240             // Starting point
241             startx = x1Screen; starty = y1Screen;
242             painter.drawEllipse( x1Screen-5, y1Screen-5, 10, 10);
243         }
244         painter.drawLine( x1Screen, y1Screen, x2Screen, y2Screen);
245     }
246     // Show the endig point if different than the starting point
247     if (x2Screen != startx || y2Screen != starty)
248     {
249         painter.drawEllipse( x2Screen-5, y2Screen-5, 10, 10);
250     }
251 }
252
253 void RouteDialog::on_closePushButton_clicked()
254 {
255     close();
256 }
257
258 /*
259   * Read route coordinates (latitude, longitude, altitude) from file.
260   */
261 bool RouteDialog::readRouteFromFile( QString &filename)
262 {
263     Vector temp;
264     QString rivi;
265     QFile file;
266
267     QString fileName = QFileDialog::getOpenFileName(this,
268          tr("Read Route"), "./", tr("Route Files (*.txt)"));
269
270     file.setFileName( fileName);
271     if (!file.open(QIODevice::ReadOnly))
272     {
273         QMessageBox::about(0, "Error", "File not found");
274         return false;
275     }
276
277     vertexList.clear();
278     while(!file.atEnd())
279     {
280         QString str1, str2, str3;
281         rivi = file.readLine();
282
283         str1 = rivi.section(" ", 0, 0);
284         if (str1.compare("Start:") != 0 && str1.compare("Stop:") != 0)
285         {
286             str1 = rivi.section(" ",  2, 2); // latitude y-value
287             str2 = rivi.section(" ",  4, 4); // longitude x-value
288             str3 = rivi.section(" ",  6, 6); // altitude z-value
289             //QString str = QString("la: %1 lo: %2 al: %3").arg(str1).arg(str2).arg(str3);
290             //QMessageBox::about(0, "LUKEE", str);
291
292             if (str1.length() > 0)
293             {
294                 double x, y, z;
295                 x = str2.toDouble();
296                 y = str1.toDouble();
297                 z = str3.toDouble();
298                 temp.setX( x); // Longitude
299                 temp.setY( y); // Latitude
300                 temp.setZ( z); // altitude
301
302                 vertexList.append(temp);
303             }
304         }
305     }
306
307     file.close();
308
309      /********  in 3d use only */
310      a = 400/2.;
311      b = 1 - a*(-1);
312      c = -300/2.;
313      d = 300 - c*(-1);
314      //angle = toradians(60);
315
316      view3d.setUp( 1.0, 0.0, 0.0);
317      view3d.setAngle(toradians(60));
318      setAtPoint( &view3d);
319      xmin = objxmin; xmax = objxmax; ymin = objymin; ymax = objymax; // 2d viewing needs this !!!!
320      setFromPoint( &view3d);
321      view3d.setEye();
322      /****** end of 3d *****/
323
324      /*
325      //Testing distance counting
326      Vector a1, a2;
327      qreal dist;
328      //a1.setX( xmin); a1.setY( ymin);
329      //a2.setX( xmax); a2.setY( ymax);
330      a1.setX( 25.483); a1.setY( 65.017); // Oulu
331      a2.setX( 27.767); a2.setY( 64.283); // Kajaani
332      dist = countDistance( &a1, &a2);
333      QString str = QString("Min & Max datan välimatka %1").arg(dist);
334      QMessageBox::about( 0, "Testi", str);
335      */
336
337      return true;
338 }
339
340 /*
341   * Find out data range for x-, y- and z-coordinates
342   */
343 void dataMinMax( void)
344 {
345     int i, maxi;
346     qreal x,y,z;
347     Vector temp;
348
349     temp = vertexList.at(0);
350     objxmax = objxmin = temp.getX();
351     objymax = objymin = temp.getY();
352     objzmax = objzmin = temp.getZ();
353
354     maxi = vertexList.size();
355     for (i=1; i<maxi; i++)
356     {
357         temp = vertexList.at(i);
358         x = temp.getX();
359         y = temp.getY();
360         z = temp.getZ();
361
362         if (x < objxmin)
363         {
364                 objxmin = x;
365         }
366         else if (x > objxmax)
367         {
368                objxmax = x;
369         }
370
371         if (y < objymin)
372         {
373                 objymin = y;
374         }
375         else if (y > objymax)
376         {
377                objymax = y;
378         }
379
380         if (z < objzmin)
381         {
382                 objzmin = z;
383         }
384         else if (z > objzmax)
385         {
386                objzmax = z;
387         }
388     }
389     //QString jono = QString("ojxmin %1 objxmax %2").arg(objxmin).arg(objxmax);
390     //QString jono = QString("ojymin %1 objymax %2").arg(objymin).arg(objymax);
391     //QString jono = QString("ojzmin %1 objzmax %2").arg(objzmin).arg(objzmax);
392     //QMessageBox::about(0,"Tark", jono);
393 }
394
395 /*
396   * Setting the point where the viewed object is. In the middle of datapoints.
397   */
398 void setAtPoint( Viewing *v)
399 {
400     qreal x, y, z;
401     dataMinMax();
402     //Vector test;
403
404     x = (objxmax+objxmin)/2.0;
405     y= (objymax+objymin)/2.0;
406     z= (objzmax+objzmin)/2.0;
407
408     v->setAtPoint( x, y, z);
409     //QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(atPoint.x()).arg(atPoint.y()).arg(atPoint.z());
410     //QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(atPoint.x).arg(atPoint.y).arg(atPoint.z);
411
412     /* *
413     test = v->getAtPoint();
414     QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(test.getX()).arg(test.getY()).arg(test.getZ());
415     QMessageBox::about(0,"At point", jono);
416     * */
417 }
418
419 /*
420   * Setting the point where the object is viewed by eye.
421   */
422 void setFromPoint( Viewing *v)
423 {
424     qreal x, y, z;
425     Vector point;
426     point = v->getAtPoint();
427     //Vector test;
428     //fromPoint.setX( atPoint.getX() + (objxmax-objxmin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objymax-objymin)/2.0));
429     //x = 3.0;
430     //x = point.getX() + (objxmax-objxmin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objymax-objymin)/2.0);
431     x = point.getX();
432     //y = point.getY();
433     y = point.getY() + 40; // + (objymax-objymin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objxmax-objxmin)/2.0);
434     z = point.getZ();
435
436     v->setFromPoint(x,y,z);
437     //QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(fromPoint.x()).arg(fromPoint.y()).arg(fromPoint.z());
438     //QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(fromPoint.x).arg(fromPoint.y).arg(fromPoint.z);
439     /* *
440     test = v->getFromPoint();
441     QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(test.getX()).arg(test.getY()).arg(test.getZ());
442     QMessageBox::about(0,"From point", jono); // (1.9, 0.5, 0.5)
443     * */
444 }
445
446
447 #define NOEDGE     0x00
448 #define LEFTEDGE   0x01
449 #define RIGHTEDGE  0x02
450 #define BOTTOMEDGE 0x04
451 #define TOPEDGE    0x08
452 /*
453   * Returns a code specifying which edge in the viewing pyramid was crossed.
454   * There may be more than one.
455   */
456 int code( qreal x, qreal y, qreal z)
457 {
458     int c;
459
460     c = NOEDGE;
461     if (x<-z) c |= LEFTEDGE;
462     if (x>z) c |= RIGHTEDGE;
463     if (y<-z) c |= BOTTOMEDGE;
464     if (y>z) c |= TOPEDGE;
465
466     return c;
467 }
468
469 /*
470   * Converts clipped world coordinates to screen coordinates.
471   */
472 void WORLDtoSCREEN( qreal xWorld, qreal yWorld, int *xScreen, int *yScreen)
473 {
474    *xScreen = (int) (a*xWorld+b);
475    *yScreen = (int) (c*yWorld+d);
476 }
477
478 /*
479   * Clips the line segment in three-dimensional coordinates to the
480   * viewing pyramid.
481   */
482 void clip3d( qreal x1, qreal y1, qreal z1, qreal x2, qreal y2, qreal z2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2)
483 {
484     int c,c1,c2;
485     qreal x,y,z,t;
486
487     c1 = code(x1,y1,z1);
488     c2 = code(x2,y2,z2);
489
490     while (c1!= NOEDGE || c2 != NOEDGE)
491     {
492         if ((c1 & c2 ) != NOEDGE) return;
493         c = c1;
494         if (c == NOEDGE) c = c2;
495         if ((c&LEFTEDGE) == LEFTEDGE)
496         {
497                 // Crosses left edge
498                 t = (z1+x1)/((x1-x2)-(z2-z1));
499                 z = t*(z2-z1)+z1;
500                 x = -z;
501                 y = t*(y2-y1)+y1;
502         }
503         else if ((c&RIGHTEDGE) == RIGHTEDGE)
504         {
505                 // Crosses right edge
506                 t = (z1-x1)/((x2-x1)-(z2-z1));
507                 z = t*(z2-z1)+z1;
508                 x = z;
509                 y = t*(y2-y1)+y1;
510         }
511         else if ((c&BOTTOMEDGE) == BOTTOMEDGE)
512         {
513                 // Crosses bottom edge
514                 t = (z1+y1)/((y1-y2)-(z2-z1));
515                 z = t*(z2-z1)+z1;
516                 x = t*(x2-x1)+x1;
517                 y = -z;
518         }
519         else if ((c&TOPEDGE) == TOPEDGE)
520         {
521                 // Crosses top edge
522                 t = (z1-y1)/((y2-y1)-(z2-z1));
523                 z = t*(z2-z1)+z1;
524                 x = t*(x2-x1)+x1;
525                 y = z;
526         }
527
528         if (c == c1)
529         {
530             x1=x; y1=y; z1=z;
531             c1 = code(x,y,z);
532         }
533         else
534         {
535             x2=x; y2=y; z2=z;
536             c2 = code(x,y,z);
537         }
538     }
539
540     if (z1 != 0)
541     {
542         WORLDtoSCREEN(x1/z1,y1/z1,xscreen1, yscreen1);
543         WORLDtoSCREEN(x2/z2,y2/z2,xscreen2, yscreen2);
544     }
545     else
546     {
547         WORLDtoSCREEN(x1,y1,xscreen1, yscreen1);
548         WORLDtoSCREEN(x2,y2,xscreen2, yscreen2);
549     }
550     //Now ready to draw line( xscreen1, yscreen1, xscreen2, yscreen2);
551 }
552
553 /*
554   * Transform the segment connecting the two vectors into the viewing plane.
555   * clip3d() clips the line if needed.
556   */
557 void transformseg( Viewing *v, Vector *v1, Vector *v2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2)
558
559 {
560     qreal x1, y1, z1, x2, y2, z2;
561     Vector a1, a2, a3;
562
563     a1 = v->getA1();
564     a2 = v->getA2();
565     a3 = v->getA3();
566
567     x1 = (a1.getX()*v1->getX() + a1.getY()*v1->getY() + a1.getZ()*v1->getZ() + v->getOffsx())*v->getDval();
568     y1 = (a2.getX()*v1->getX() + a2.getY()*v1->getY() + a2.getZ()*v1->getZ() + v->getOffsy())*v->getDval();
569     z1 = a3.getX()*v1->getX() + a3.getY()*v1->getY() + a3.getZ()*v1->getZ() + v->getOffsz();
570
571     x2 = (a1.getX()*v2->getX() + a1.getY()*v2->getY() + a1.getZ()*v2->getZ() + v->getOffsx())*v->getDval();
572     y2 = (a2.getX()*v2->getX() + a2.getY()*v2->getY() + a2.getZ()*v2->getZ() + v->getOffsy())*v->getDval();
573     z2 = a3.getX()*v2->getX() + a3.getY()*v2->getY() + a3.getZ()*v2->getZ() + v->getOffsz();
574
575     clip3d(x1,y1,z1,x2,y2,z2, xscreen1, yscreen1, xscreen2, yscreen2 );
576 }