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 ui->speedValueLabel->setText(jono);
371 bool RouteDialog::readRouteFromFile( QString &routeFile)
377 //QString fileName = QFileDialog::getOpenFileName(this,
378 // tr("Read Route"), "./", tr("Route Files (*.txt)"));
380 //file.setFileName( fileName);
381 file.setFileName( "routetemp.xml");
382 if (!file.open(QIODevice::ReadOnly))
384 QMessageBox::about(0, "Error", "File not found");
394 QString astr1, astr2, astr3, astr4;
395 QString str1, str2, str3, str4;
396 rivi = file.readLine();
402 astr1 = rivi.section(" ", count*4+1, count*4+1); // latitude=""
403 astr2 = rivi.section(" ", count*4+2, count*4+2); // longitude=""
404 astr3 = rivi.section(" ", count*4+3, count*4+3); // altitude=""
405 astr4 = rivi.section(" ", count*4+4, count*4+4); // speed=""
409 str1 = astr1.section('"',1,1);
410 str2 = astr2.section('"',1,1);
411 str3 = astr3.section('"',1,1);
412 str4 = astr4.section('"',1,1);
413 //QString str = QString("%1 %2 %3 %4").arg(str1).arg(str2).arg(str3).arg(str4);
414 //QMessageBox::about(0, "LUKEE", str);
417 if (str1.length() > 0)
419 x = str2.toDouble();// latitude y-value
420 y = str1.toDouble();// longitude x-value
421 z = str3.toDouble();// altitude z-value
422 v = str4.toDouble();// speed km/h
423 // QString str = QString("%1 %2 %3 %4").arg(x).arg(y).arg(z).arg(v);
424 // QMessageBox::about(0, "LUKEE", str);
425 temp.setX( x); // Longitude
426 temp.setY( y); // Latitude
427 temp.setZ( z); // altitude
430 vertexList.append(temp);
441 str1 = rivi.section(" ", 0, 0);
442 if (str1.compare("Start:") != 0 && str1.compare("Stop:") != 0)
444 str1 = rivi.section(" ", 2, 2); // latitude y-value
445 str2 = rivi.section(" ", 4, 4); // longitude x-value
446 str3 = rivi.section(" ", 6, 6); // altitude z-value
447 str4 = rivi.section(" ", 8, 8); // speed km/h
448 //QString str = QString("la: %1 lo: %2 al: %3").arg(str1).arg(str2).arg(str3);
449 //QMessageBox::about(0, "LUKEE", str);
451 if (str1.length() > 0)
458 temp.setX( x); // Longitude
459 temp.setY( y); // Latitude
460 temp.setZ( z); // altitude
463 vertexList.append(temp);
471 /******** in 3d use only */
476 //angle = toradians(60);
478 view3d.setUp( 1.0, 0.0, 0.0);
479 view3d.setAngle(toradians(60));
480 setAtPoint( &view3d);
481 xmin = objxmin; xmax = objxmax; ymin = objymin; ymax = objymax; // 2d viewing needs this !!!!
482 setFromPoint( &view3d);
484 /****** end of 3d *****/
487 //Testing distance counting
490 //a1.setX( xmin); a1.setY( ymin);
491 //a2.setX( xmax); a2.setY( ymax);
492 a1.setX( 25.483); a1.setY( 65.017); // Oulu
493 a2.setX( 27.767); a2.setY( 64.283); // Kajaani
494 dist = countDistance( &a1, &a2);
495 QString str = QString("Min & Max datan välimatka %1").arg(dist);
496 QMessageBox::about( 0, "Testi", str);
503 * Find out data range for x-, y- and z-coordinates
505 void dataMinMax( void)
511 temp = vertexList.at(0);
512 objxmax = objxmin = temp.getX();
513 objymax = objymin = temp.getY();
514 objzmax = objzmin = temp.getZ();
516 maxi = vertexList.size();
517 for (i=1; i<maxi; i++)
519 temp = vertexList.at(i);
528 else if (x > objxmax)
537 else if (y > objymax)
546 else if (z > objzmax)
551 //QString jono = QString("ojxmin %1 objxmax %2").arg(objxmin).arg(objxmax);
552 //QString jono = QString("ojymin %1 objymax %2").arg(objymin).arg(objymax);
553 //QString jono = QString("ojzmin %1 objzmax %2").arg(objzmin).arg(objzmax);
554 //QMessageBox::about(0,"Tark", jono);
558 * Setting the point where the viewed object is. In the middle of datapoints.
560 void setAtPoint( Viewing *v)
566 x = (objxmax+objxmin)/2.0;
567 y= (objymax+objymin)/2.0;
568 z= (objzmax+objzmin)/2.0;
570 v->setAtPoint( x, y, z);
571 //QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(atPoint.x()).arg(atPoint.y()).arg(atPoint.z());
572 //QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(atPoint.x).arg(atPoint.y).arg(atPoint.z);
575 test = v->getAtPoint();
576 QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(test.getX()).arg(test.getY()).arg(test.getZ());
577 QMessageBox::about(0,"At point", jono);
582 * Setting the point where the object is viewed by eye.
584 void setFromPoint( Viewing *v)
588 point = v->getAtPoint();
590 //fromPoint.setX( atPoint.getX() + (objxmax-objxmin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objymax-objymin)/2.0));
592 //x = point.getX() + (objxmax-objxmin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objymax-objymin)/2.0);
595 y = point.getY() + 40; // + (objymax-objymin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objxmax-objxmin)/2.0);
598 v->setFromPoint(x,y,z);
599 //QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(fromPoint.x()).arg(fromPoint.y()).arg(fromPoint.z());
600 //QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(fromPoint.x).arg(fromPoint.y).arg(fromPoint.z);
602 test = v->getFromPoint();
603 QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(test.getX()).arg(test.getY()).arg(test.getZ());
604 QMessageBox::about(0,"From point", jono); // (1.9, 0.5, 0.5)
610 #define LEFTEDGE 0x01
611 #define RIGHTEDGE 0x02
612 #define BOTTOMEDGE 0x04
615 * Returns a code specifying which edge in the viewing pyramid was crossed.
616 * There may be more than one.
618 int code( qreal x, qreal y, qreal z)
623 if (x<-z) c |= LEFTEDGE;
624 if (x>z) c |= RIGHTEDGE;
625 if (y<-z) c |= BOTTOMEDGE;
626 if (y>z) c |= TOPEDGE;
632 * Converts clipped world coordinates to screen coordinates.
634 void WORLDtoSCREEN( qreal xWorld, qreal yWorld, int *xScreen, int *yScreen)
636 *xScreen = (int) (a*xWorld+b);
637 *yScreen = (int) (c*yWorld+d);
641 * Clips the line segment in three-dimensional coordinates to the
644 void clip3d( qreal x1, qreal y1, qreal z1, qreal x2, qreal y2, qreal z2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2)
652 while (c1!= NOEDGE || c2 != NOEDGE)
654 if ((c1 & c2 ) != NOEDGE) return;
656 if (c == NOEDGE) c = c2;
657 if ((c&LEFTEDGE) == LEFTEDGE)
660 t = (z1+x1)/((x1-x2)-(z2-z1));
665 else if ((c&RIGHTEDGE) == RIGHTEDGE)
667 // Crosses right edge
668 t = (z1-x1)/((x2-x1)-(z2-z1));
673 else if ((c&BOTTOMEDGE) == BOTTOMEDGE)
675 // Crosses bottom edge
676 t = (z1+y1)/((y1-y2)-(z2-z1));
681 else if ((c&TOPEDGE) == TOPEDGE)
684 t = (z1-y1)/((y2-y1)-(z2-z1));
704 WORLDtoSCREEN(x1/z1,y1/z1,xscreen1, yscreen1);
705 WORLDtoSCREEN(x2/z2,y2/z2,xscreen2, yscreen2);
709 WORLDtoSCREEN(x1,y1,xscreen1, yscreen1);
710 WORLDtoSCREEN(x2,y2,xscreen2, yscreen2);
712 //Now ready to draw line( xscreen1, yscreen1, xscreen2, yscreen2);
716 * Transform the segment connecting the two vectors into the viewing plane.
717 * clip3d() clips the line if needed.
719 void transformseg( Viewing *v, Vector *v1, Vector *v2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2)
722 qreal x1, y1, z1, x2, y2, z2;
729 x1 = (a1.getX()*v1->getX() + a1.getY()*v1->getY() + a1.getZ()*v1->getZ() + v->getOffsx())*v->getDval();
730 y1 = (a2.getX()*v1->getX() + a2.getY()*v1->getY() + a2.getZ()*v1->getZ() + v->getOffsy())*v->getDval();
731 z1 = a3.getX()*v1->getX() + a3.getY()*v1->getY() + a3.getZ()*v1->getZ() + v->getOffsz();
733 x2 = (a1.getX()*v2->getX() + a1.getY()*v2->getY() + a1.getZ()*v2->getZ() + v->getOffsx())*v->getDval();
734 y2 = (a2.getX()*v2->getX() + a2.getY()*v2->getY() + a2.getZ()*v2->getZ() + v->getOffsy())*v->getDval();
735 z2 = a3.getX()*v2->getX() + a3.getY()*v2->getY() + a3.getZ()*v2->getZ() + v->getOffsz();
737 clip3d(x1,y1,z1,x2,y2,z2, xscreen1, yscreen1, xscreen2, yscreen2 );
740 void RouteDialog::on_newPushButton_clicked()
742 close(); // go back to previous dialog
745 void RouteDialog::on_sendPushButton_clicked()
750 void RouteDialog::setLabelInfoToUser(QString infoText)
752 this->ui->labelInfoToUser->setText(infoText);