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
10 #include "routedialog.h"
11 #include "ui_routedialog.h"
12 #include "usersettings.h"
16 #include <QMessageBox>
18 #include <QFileDialog>
23 * In starting Qt 4.6 there is QVector3D.
24 * Later (updating Qt version) this class can be removed.
28 qreal x, y, z; // Location
31 Vector() { x=0.; y=0. ; z=0.; };
32 Vector( qreal initX, qreal initY, qreal initZ) { x = initX, y = initY; z = initZ; };
33 void setX( qreal newX) { x = newX; };
34 void setY( qreal newY) { y = newY; };
35 void setZ( qreal newZ) { z = newZ; };
36 void setV( qreal newV) { v = newV; };
37 qreal getX() { return x; };
38 qreal getY() { return y; };
39 qreal getZ() { return z; };
40 qreal getV() { return v; };
41 qreal length() { return sqrt(x*x+y*y+z*z); };
42 Vector operator+(Vector v)
44 x = x + v.x; y = y + v.y; z = z + v.z;
47 Vector operator-(Vector v)
49 x = x - v.x; y = y - v.y; z = z - v.z;
52 Vector operator/(qreal c)
54 x = x/c; y = y/c; z = z/c;
57 Vector crossProduct( Vector a, Vector b)
59 x = a.y*b.z - a.z*b.y;
60 y = a.z*b.x - a.x*b.z;
61 z = a.x*b.y - a.y*b.x;
69 Vector atPoint, fromPoint, up, a1, a2, a3;
70 qreal offsx, offsy, offsz;
74 qreal getOffsx() { return offsx; };
75 qreal getOffsy() { return offsy; };
76 qreal getOffsz() { return offsz; };
77 qreal getDval() { return dval; };
78 void setAngle( qreal newA) { angle = newA; };
79 void setUp( qreal newUpX, qreal newUpY, qreal newUpZ)
81 up.setX(newUpX); up.setY(newUpY); up.setZ(newUpZ);
83 void setAtPoint( qreal newX, qreal newY, qreal newZ)
85 atPoint.setX(newX); atPoint.setY(newY); atPoint.setZ(newZ);
87 void setFromPoint(qreal newX, qreal newY, qreal newZ)
89 fromPoint.setX(newX); fromPoint.setY(newY); fromPoint.setZ(newZ);
93 double amarkmag, tempmag;
96 dval = cos(angle/2.0)/sin(angle/2.0);
97 dist = atPoint-fromPoint;
98 amarkmag = dist.length();
101 temp.crossProduct( dist, up);
102 tempmag = temp.length();
105 temp.crossProduct( a1, a3);
106 tempmag = temp.length();
109 offsx = -a1.getX()*fromPoint.getX() - a1.getY()*fromPoint.getY() - a1.getZ()*fromPoint.getZ();
110 offsy = -a2.getX()*fromPoint.getX() - a2.getY()*fromPoint.getY() - a2.getZ()*fromPoint.getZ();
111 offsz = -a3.getX()*fromPoint.getX() - a3.getY()*fromPoint.getY() - a3.getZ()*fromPoint.getZ();
112 //QString jono2 = QString("offsx %1 offsy %2 offsz %3").arg(offsx).arg(offsy).arg(offsz);
113 //QMessageBox::about(0,"offs x y z", jono2);
115 Vector getAtPoint() { return atPoint; };
116 Vector getFromPoint() { return fromPoint; };
117 Vector getA1() { return a1; };
118 Vector getA2() { return a2; };
119 Vector getA3() { return a3; };
123 qreal xmax, xmin, ymin, ymax; // Limits in world coordinates
125 QList<Vector> vertexList; // Vertecies of route
127 qreal objxmin, objxmax, objymin, objymax, objzmin, objzmax; // data ranges
129 #define maxof(val1,val2) ((val1>val2)?val1:val2)
130 #define toradians( degrees) (degrees*0.017453293)
132 #define WIDTH 1.8 // For 3d viewing only
133 qreal a, b,c,d; // Used for 3d viewing to calculate screen coordinates
135 Viewing view3d; // Viewing settings for 3d
137 // Function prototypes
138 void dataMinMax( void);
139 void setAtPoint( Viewing *v);
140 void setFromPoint( Viewing *v);
141 void transformseg( Viewing *v, Vector *v1, Vector *v2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2 );
143 #define R 6378.140 // The radius of the earth by kilometers
145 * count distance of two points (defined by longitude & latitude)
146 * on the surface of the earth.
148 qreal countDistance(Vector *p1, Vector *p2)
150 qreal dLon, dLat; // delta of longitude & latitude
153 dLon = p2->getX() - p1->getX(); // longitude difference
154 dLat = p2->getY() - p1->getY(); // latitude difference
155 if (dLon <0) dLon = -dLon;
156 if (dLat <0) dLat = -dLat;
158 dLon = dLon*3.14/180;
159 dLat = dLat*3.14/180;
160 a = (sin(dLat/2.))*(sin(dLat/2.)) +
161 (cos(p1->getY())*3.14/180)*(cos(p2->getY())*3.14/180)*(sin(dLon/2.))*(sin(dLon/2.));
162 c = 2.*atan(sqrt(a)/sqrt(1-a)); // c is angle between points p1 & p2 with circel by radius 1.
164 return R*c; // Return distance in kilometers
167 RouteDialog::RouteDialog(QWidget *parent) :
169 ui(new Ui::RouteDialog)
172 this->setWindowTitle("Route");
173 left = 5; top = 5; right = 495; bottom = 295; // Limits in screen coordinates
176 ui->sendPushButton->setAutoFillBackground(true);
177 ui->sendPushButton->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
178 ui->newPushButton->setAutoFillBackground(true);
179 ui->newPushButton->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
181 // Send rout to server button disable/enable.
182 ui->sendPushButton->setEnabled(false);
185 ui->sendPushButton->setEnabled(true);
189 RouteDialog::~RouteDialog()
194 void RouteDialog::changeEvent(QEvent *e)
196 QDialog::changeEvent(e);
198 case QEvent::LanguageChange:
199 ui->retranslateUi(this);
205 int RouteDialog::getLeft()
209 int RouteDialog::getRight()
213 int RouteDialog::getTop()
217 int RouteDialog::getBottom()
222 void drawFlag( RouteDialog *rD, QPainter *p, int x, int y)
226 pg.setPoint(0,x, y-25);
227 pg.setPoint(1,x+10,y-20);
228 pg.setPoint(2,x, y-15);
229 pg.setPoint(3,x,y-20);*/
230 if (y> (rD->getTop() + 25))
233 p->drawLine(x,y,x,y-15);
234 if (x <= (rD->getRight()-20))
237 p->drawLine( x, y-25, x+10, y-20);
238 p->drawLine( x+10, y-20, x, y-15);
239 p->drawLine( x, y-15, x, y-25);
244 p->drawLine( x, y-25, x-10, y-20);
245 p->drawLine( x-10, y-20, x, y-15);
246 p->drawLine( x, y-15, x, y-25);
250 else if (y <= (rD->getTop() + 25))
253 p->drawLine(x,y,x,y+15);
254 if (x <= (rD->getRight()-20))
257 p->drawLine( x, y+25, x+10, y+20);
258 p->drawLine( x+10, y+20, x, y+15);
259 p->drawLine( x, y+15, x, y+25);
264 p->drawLine( x, y+25, x-10, y+20);
265 p->drawLine( x-10, y+20, x, y+15);
266 p->drawLine( x, y+15, x, y+25);
270 // p->drawPolygon( pg,Qt::OddEvenFill);
271 //p->drawPolyline( &pg);
272 //p->drawPoints( pg);
276 * Draws route to the route dialog.
277 * Type 0 is 2d viewing and type 1 is for 3d viewing
281 void RouteDialog::paintEvent(QPaintEvent *)
283 int type = 0; // 0 for 2d, 1 for 3d
284 int startx, starty; // Starting point of the route
286 qreal x1, y1, x2, y2;
287 int x1Screen, y1Screen, x2Screen, y2Screen;
289 QPainter painter(this);
291 painter.setRenderHint(QPainter::Antialiasing, true);
292 painter.setPen(QPen((Qt::white),2));
293 painter.setBrush(QBrush((Qt::yellow), Qt::SolidPattern));
295 // Draw route window frame
296 /*painter.drawLine(left,top,right,top);
297 painter.drawLine(right,top,right,bottom);
298 painter.drawLine(left,top,left,bottom);
299 painter.drawLine(left,bottom,right,bottom);*/
301 maxi = vertexList.size();
303 for (i=0; i<maxi-1; i++)
305 v1 = vertexList.at(i);
306 v2 = vertexList.at(i+1);
310 x1 = v1.getX(); y1 = v1.getY();
311 x2 = v2.getX(); y2 = v2.getY();
312 //QString jono = QString("x: %1 y: %2").arg(x1).arg(y1);
313 //QMessageBox::about(0,"Tark",jono);
315 x1Screen = left + (x1-xmin)/(xmax-xmin)*(right-left);
316 y1Screen = top + (ymax-y1)/(ymax-ymin)*(bottom-top);
317 x2Screen = left + (x2-xmin)/(xmax-xmin)*(right-left);
318 y2Screen = top + (ymax-y2)/(ymax-ymin)*(bottom-top);
322 transformseg( &view3d, &v1,&v2, &x1Screen, &y1Screen, &x2Screen, &y2Screen);
325 // Show with circle if starting point
329 startx = x1Screen; starty = y1Screen;
330 // painter.drawEllipse( x1Screen-5, y1Screen-5, 10, 10);
331 drawFlag( this, &painter, x1Screen , y1Screen);
333 painter.drawLine( x1Screen, y1Screen, x2Screen, y2Screen);
335 // Show the endig point if different than the starting point
336 if (x2Screen != startx || y2Screen != starty)
338 //painter.drawEllipse( x2Screen-5, y2Screen-5, 10, 10);
339 drawFlag( this, &painter,x2Screen, y2Screen );
343 qreal maxvx, maxvy; // max speed point coordinates
344 qreal maxv; // max speed
348 for (i=0; i<maxi-1; i++)
350 v = vertexList.at(i);
358 // Translate world coordinates to screen coordinates
359 x1Screen = left + (maxvx-xmin)/(xmax-xmin)*(right-left);
360 y1Screen = top + (ymax-maxvy)/(ymax-ymin)*(bottom-top);
362 // Show max velocity point by yellow circle
363 painter.drawEllipse( x1Screen-5, y1Screen-5, 10, 10);
366 //jono = QString("%1 km/h").arg(maxv);
367 jono.sprintf("%.1f km/h", maxv);
368 ui->speedValueLabel->setText(jono);
372 bool RouteDialog::readRouteFromFile( QString &routeFile)
378 //QString fileName = QFileDialog::getOpenFileName(this,
379 // tr("Read Route"), "./", tr("Route Files (*.txt)"));
381 //file.setFileName( fileName);
382 file.setFileName( "routetemp.xml");
383 if (!file.open(QIODevice::ReadOnly))
385 QMessageBox::about(0, "Error", "File not found");
395 QString astr1, astr2, astr3, astr4;
396 QString str1, str2, str3, str4;
397 rivi = file.readLine();
403 astr1 = rivi.section(" ", count*4+1, count*4+1); // latitude=""
404 astr2 = rivi.section(" ", count*4+2, count*4+2); // longitude=""
405 astr3 = rivi.section(" ", count*4+3, count*4+3); // altitude=""
406 astr4 = rivi.section(" ", count*4+4, count*4+4); // speed=""
410 str1 = astr1.section('"',1,1);
411 str2 = astr2.section('"',1,1);
412 str3 = astr3.section('"',1,1);
413 str4 = astr4.section('"',1,1);
414 //QString str = QString("%1 %2 %3 %4").arg(str1).arg(str2).arg(str3).arg(str4);
415 //QMessageBox::about(0, "LUKEE", str);
418 if (str1.length() > 0)
420 x = str2.toDouble();// latitude y-value
421 y = str1.toDouble();// longitude x-value
422 z = str3.toDouble();// altitude z-value
423 v = str4.toDouble();// speed km/h
424 // QString str = QString("%1 %2 %3 %4").arg(x).arg(y).arg(z).arg(v);
425 // QMessageBox::about(0, "LUKEE", str);
426 temp.setX( x); // Longitude
427 temp.setY( y); // Latitude
428 temp.setZ( z); // altitude
431 vertexList.append(temp);
442 str1 = rivi.section(" ", 0, 0);
443 if (str1.compare("Start:") != 0 && str1.compare("Stop:") != 0)
445 str1 = rivi.section(" ", 2, 2); // latitude y-value
446 str2 = rivi.section(" ", 4, 4); // longitude x-value
447 str3 = rivi.section(" ", 6, 6); // altitude z-value
448 str4 = rivi.section(" ", 8, 8); // speed km/h
449 //QString str = QString("la: %1 lo: %2 al: %3").arg(str1).arg(str2).arg(str3);
450 //QMessageBox::about(0, "LUKEE", str);
452 if (str1.length() > 0)
459 temp.setX( x); // Longitude
460 temp.setY( y); // Latitude
461 temp.setZ( z); // altitude
464 vertexList.append(temp);
472 /******** in 3d use only */
477 //angle = toradians(60);
479 view3d.setUp( 1.0, 0.0, 0.0);
480 view3d.setAngle(toradians(60));
481 setAtPoint( &view3d);
482 xmin = objxmin; xmax = objxmax; ymin = objymin; ymax = objymax; // 2d viewing needs this !!!!
483 setFromPoint( &view3d);
485 /****** end of 3d *****/
488 //Testing distance counting
491 //a1.setX( xmin); a1.setY( ymin);
492 //a2.setX( xmax); a2.setY( ymax);
493 a1.setX( 25.483); a1.setY( 65.017); // Oulu
494 a2.setX( 27.767); a2.setY( 64.283); // Kajaani
495 dist = countDistance( &a1, &a2);
496 QString str = QString("Min & Max datan välimatka %1").arg(dist);
497 QMessageBox::about( 0, "Testi", str);
504 * Find out data range for x-, y- and z-coordinates
506 void dataMinMax( void)
512 temp = vertexList.at(0);
513 objxmax = objxmin = temp.getX();
514 objymax = objymin = temp.getY();
515 objzmax = objzmin = temp.getZ();
517 maxi = vertexList.size();
518 for (i=1; i<maxi; i++)
520 temp = vertexList.at(i);
529 else if (x > objxmax)
538 else if (y > objymax)
547 else if (z > objzmax)
552 //QString jono = QString("ojxmin %1 objxmax %2").arg(objxmin).arg(objxmax);
553 //QString jono = QString("ojymin %1 objymax %2").arg(objymin).arg(objymax);
554 //QString jono = QString("ojzmin %1 objzmax %2").arg(objzmin).arg(objzmax);
555 //QMessageBox::about(0,"Tark", jono);
559 * Setting the point where the viewed object is. In the middle of datapoints.
561 void setAtPoint( Viewing *v)
567 x = (objxmax+objxmin)/2.0;
568 y= (objymax+objymin)/2.0;
569 z= (objzmax+objzmin)/2.0;
571 v->setAtPoint( x, y, z);
572 //QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(atPoint.x()).arg(atPoint.y()).arg(atPoint.z());
573 //QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(atPoint.x).arg(atPoint.y).arg(atPoint.z);
576 test = v->getAtPoint();
577 QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(test.getX()).arg(test.getY()).arg(test.getZ());
578 QMessageBox::about(0,"At point", jono);
583 * Setting the point where the object is viewed by eye.
585 void setFromPoint( Viewing *v)
589 point = v->getAtPoint();
591 //fromPoint.setX( atPoint.getX() + (objxmax-objxmin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objymax-objymin)/2.0));
593 //x = point.getX() + (objxmax-objxmin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objymax-objymin)/2.0);
596 y = point.getY() + 40; // + (objymax-objymin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objxmax-objxmin)/2.0);
599 v->setFromPoint(x,y,z);
600 //QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(fromPoint.x()).arg(fromPoint.y()).arg(fromPoint.z());
601 //QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(fromPoint.x).arg(fromPoint.y).arg(fromPoint.z);
603 test = v->getFromPoint();
604 QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(test.getX()).arg(test.getY()).arg(test.getZ());
605 QMessageBox::about(0,"From point", jono); // (1.9, 0.5, 0.5)
611 #define LEFTEDGE 0x01
612 #define RIGHTEDGE 0x02
613 #define BOTTOMEDGE 0x04
616 * Returns a code specifying which edge in the viewing pyramid was crossed.
617 * There may be more than one.
619 int code( qreal x, qreal y, qreal z)
624 if (x<-z) c |= LEFTEDGE;
625 if (x>z) c |= RIGHTEDGE;
626 if (y<-z) c |= BOTTOMEDGE;
627 if (y>z) c |= TOPEDGE;
633 * Converts clipped world coordinates to screen coordinates.
635 void WORLDtoSCREEN( qreal xWorld, qreal yWorld, int *xScreen, int *yScreen)
637 *xScreen = (int) (a*xWorld+b);
638 *yScreen = (int) (c*yWorld+d);
642 * Clips the line segment in three-dimensional coordinates to the
645 void clip3d( qreal x1, qreal y1, qreal z1, qreal x2, qreal y2, qreal z2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2)
653 while (c1!= NOEDGE || c2 != NOEDGE)
655 if ((c1 & c2 ) != NOEDGE) return;
657 if (c == NOEDGE) c = c2;
658 if ((c&LEFTEDGE) == LEFTEDGE)
661 t = (z1+x1)/((x1-x2)-(z2-z1));
666 else if ((c&RIGHTEDGE) == RIGHTEDGE)
668 // Crosses right edge
669 t = (z1-x1)/((x2-x1)-(z2-z1));
674 else if ((c&BOTTOMEDGE) == BOTTOMEDGE)
676 // Crosses bottom edge
677 t = (z1+y1)/((y1-y2)-(z2-z1));
682 else if ((c&TOPEDGE) == TOPEDGE)
685 t = (z1-y1)/((y2-y1)-(z2-z1));
705 WORLDtoSCREEN(x1/z1,y1/z1,xscreen1, yscreen1);
706 WORLDtoSCREEN(x2/z2,y2/z2,xscreen2, yscreen2);
710 WORLDtoSCREEN(x1,y1,xscreen1, yscreen1);
711 WORLDtoSCREEN(x2,y2,xscreen2, yscreen2);
713 //Now ready to draw line( xscreen1, yscreen1, xscreen2, yscreen2);
717 * Transform the segment connecting the two vectors into the viewing plane.
718 * clip3d() clips the line if needed.
720 void transformseg( Viewing *v, Vector *v1, Vector *v2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2)
723 qreal x1, y1, z1, x2, y2, z2;
730 x1 = (a1.getX()*v1->getX() + a1.getY()*v1->getY() + a1.getZ()*v1->getZ() + v->getOffsx())*v->getDval();
731 y1 = (a2.getX()*v1->getX() + a2.getY()*v1->getY() + a2.getZ()*v1->getZ() + v->getOffsy())*v->getDval();
732 z1 = a3.getX()*v1->getX() + a3.getY()*v1->getY() + a3.getZ()*v1->getZ() + v->getOffsz();
734 x2 = (a1.getX()*v2->getX() + a1.getY()*v2->getY() + a1.getZ()*v2->getZ() + v->getOffsx())*v->getDval();
735 y2 = (a2.getX()*v2->getX() + a2.getY()*v2->getY() + a2.getZ()*v2->getZ() + v->getOffsy())*v->getDval();
736 z2 = a3.getX()*v2->getX() + a3.getY()*v2->getY() + a3.getZ()*v2->getZ() + v->getOffsz();
738 clip3d(x1,y1,z1,x2,y2,z2, xscreen1, yscreen1, xscreen2, yscreen2 );
741 void RouteDialog::on_newPushButton_clicked()
743 close(); // go back to previous dialog
746 void RouteDialog::on_sendPushButton_clicked()
751 void RouteDialog::setLabelInfoToUser(QString infoText)
753 this->ui->labelInfoToUser->setText(infoText);