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
9 #include "routedialog.h"
10 #include "ui_routedialog.h"
14 #include <QMessageBox>
16 #include <QFileDialog>
21 * In starting Qt 4.6 there is QVector3D.
22 * Later (updating Qt version) this class can be removed.
26 qreal x, y, z; // Location
29 Vector() { x=0.; y=0. ; z=0.; };
30 Vector( qreal initX, qreal initY, qreal initZ) { x = initX, y = initY; z = initZ; };
31 void setX( qreal newX) { x = newX; };
32 void setY( qreal newY) { y = newY; };
33 void setZ( qreal newZ) { z = newZ; };
34 void setV( qreal newV) { v = newV; };
35 qreal getX() { return x; };
36 qreal getY() { return y; };
37 qreal getZ() { return z; };
38 qreal getV() { return v; };
39 qreal length() { return sqrt(x*x+y*y+z*z); };
40 Vector operator+(Vector v)
42 x = x + v.x; y = y + v.y; z = z + v.z;
45 Vector operator-(Vector v)
47 x = x - v.x; y = y - v.y; z = z - v.z;
50 Vector operator/(qreal c)
52 x = x/c; y = y/c; z = z/c;
55 Vector crossProduct( Vector a, Vector b)
57 x = a.y*b.z - a.z*b.y;
58 y = a.z*b.x - a.x*b.z;
59 z = a.x*b.y - a.y*b.x;
67 Vector atPoint, fromPoint, up, a1, a2, a3;
68 qreal offsx, offsy, offsz;
72 qreal getOffsx() { return offsx; };
73 qreal getOffsy() { return offsy; };
74 qreal getOffsz() { return offsz; };
75 qreal getDval() { return dval; };
76 void setAngle( qreal newA) { angle = newA; };
77 void setUp( qreal newUpX, qreal newUpY, qreal newUpZ)
79 up.setX(newUpX); up.setY(newUpY); up.setZ(newUpZ);
81 void setAtPoint( qreal newX, qreal newY, qreal newZ)
83 atPoint.setX(newX); atPoint.setY(newY); atPoint.setZ(newZ);
85 void setFromPoint(qreal newX, qreal newY, qreal newZ)
87 fromPoint.setX(newX); fromPoint.setY(newY); fromPoint.setZ(newZ);
91 double amarkmag, tempmag;
94 dval = cos(angle/2.0)/sin(angle/2.0);
95 dist = atPoint-fromPoint;
96 amarkmag = dist.length();
99 temp.crossProduct( dist, up);
100 tempmag = temp.length();
103 temp.crossProduct( a1, a3);
104 tempmag = temp.length();
107 offsx = -a1.getX()*fromPoint.getX() - a1.getY()*fromPoint.getY() - a1.getZ()*fromPoint.getZ();
108 offsy = -a2.getX()*fromPoint.getX() - a2.getY()*fromPoint.getY() - a2.getZ()*fromPoint.getZ();
109 offsz = -a3.getX()*fromPoint.getX() - a3.getY()*fromPoint.getY() - a3.getZ()*fromPoint.getZ();
110 //QString jono2 = QString("offsx %1 offsy %2 offsz %3").arg(offsx).arg(offsy).arg(offsz);
111 //QMessageBox::about(0,"offs x y z", jono2);
113 Vector getAtPoint() { return atPoint; };
114 Vector getFromPoint() { return fromPoint; };
115 Vector getA1() { return a1; };
116 Vector getA2() { return a2; };
117 Vector getA3() { return a3; };
121 qreal xmax, xmin, ymin, ymax; // Limits in world coordinates
123 QList<Vector> vertexList; // Vertecies of route
125 qreal objxmin, objxmax, objymin, objymax, objzmin, objzmax; // data ranges
127 #define maxof(val1,val2) ((val1>val2)?val1:val2)
128 #define toradians( degrees) (degrees*0.017453293)
130 #define WIDTH 1.8 // For 3d viewing only
131 qreal a, b,c,d; // Used for 3d viewing to calculate screen coordinates
133 Viewing view3d; // Viewing settings for 3d
135 // Function prototypes
136 void dataMinMax( void);
137 void setAtPoint( Viewing *v);
138 void setFromPoint( Viewing *v);
139 void transformseg( Viewing *v, Vector *v1, Vector *v2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2 );
141 #define R 6378.140 // The radius of the earth by kilometers
143 * count distance of two points (defined by longitude & latitude)
144 * on the surface of the earth.
146 qreal countDistance(Vector *p1, Vector *p2)
148 qreal dLon, dLat; // delta of longitude & latitude
151 dLon = p2->getX() - p1->getX(); // longitude difference
152 dLat = p2->getY() - p1->getY(); // latitude difference
153 if (dLon <0) dLon = -dLon;
154 if (dLat <0) dLat = -dLat;
156 dLon = dLon*3.14/180;
157 dLat = dLat*3.14/180;
158 a = (sin(dLat/2.))*(sin(dLat/2.)) +
159 (cos(p1->getY())*3.14/180)*(cos(p2->getY())*3.14/180)*(sin(dLon/2.))*(sin(dLon/2.));
160 c = 2.*atan(sqrt(a)/sqrt(1-a)); // c is angle between points p1 & p2 with circel by radius 1.
162 return R*c; // Return distance in kilometers
165 RouteDialog::RouteDialog(QWidget *parent) :
167 ui(new Ui::RouteDialog)
170 left = 5; top = 5; right = 495; bottom = 295; // Limits in screen coordinates
173 RouteDialog::~RouteDialog()
178 void RouteDialog::changeEvent(QEvent *e)
180 QDialog::changeEvent(e);
182 case QEvent::LanguageChange:
183 ui->retranslateUi(this);
189 int RouteDialog::getLeft()
193 int RouteDialog::getRight()
197 int RouteDialog::getTop()
201 int RouteDialog::getBottom()
206 void drawFlag( RouteDialog *rD, QPainter *p, int x, int y)
210 pg.setPoint(0,x, y-25);
211 pg.setPoint(1,x+10,y-20);
212 pg.setPoint(2,x, y-15);
213 pg.setPoint(3,x,y-20);*/
214 if (y> (rD->getTop() + 25))
217 p->drawLine(x,y,x,y-15);
218 if (x <= (rD->getRight()-20))
221 p->drawLine( x, y-25, x+10, y-20);
222 p->drawLine( x+10, y-20, x, y-15);
223 p->drawLine( x, y-15, x, y-25);
228 p->drawLine( x, y-25, x-10, y-20);
229 p->drawLine( x-10, y-20, x, y-15);
230 p->drawLine( x, y-15, x, y-25);
234 else if (y <= (rD->getTop() + 25))
237 p->drawLine(x,y,x,y+15);
238 if (x <= (rD->getRight()-20))
241 p->drawLine( x, y+25, x+10, y+20);
242 p->drawLine( x+10, y+20, x, y+15);
243 p->drawLine( x, y+15, x, y+25);
248 p->drawLine( x, y+25, x-10, y+20);
249 p->drawLine( x-10, y+20, x, y+15);
250 p->drawLine( x, y+15, x, y+25);
254 // p->drawPolygon( pg,Qt::OddEvenFill);
255 //p->drawPolyline( &pg);
256 //p->drawPoints( pg);
260 * Draws route to the route dialog.
261 * Type 0 is 2d viewing and type 1 is for 3d viewing
265 void RouteDialog::paintEvent(QPaintEvent *)
267 int type = 0; // 0 for 2d, 1 for 3d
268 int startx, starty; // Starting point of the route
270 qreal x1, y1, x2, y2;
271 int x1Screen, y1Screen, x2Screen, y2Screen;
273 QPainter painter(this);
275 painter.setRenderHint(QPainter::Antialiasing, true);
276 painter.setPen(QPen((Qt::white),2));
277 painter.setBrush(QBrush((Qt::yellow), Qt::SolidPattern));
279 // Draw route window frame
280 /*painter.drawLine(left,top,right,top);
281 painter.drawLine(right,top,right,bottom);
282 painter.drawLine(left,top,left,bottom);
283 painter.drawLine(left,bottom,right,bottom);*/
285 maxi = vertexList.size();
287 for (i=0; i<maxi-1; i++)
289 v1 = vertexList.at(i);
290 v2 = vertexList.at(i+1);
294 x1 = v1.getX(); y1 = v1.getY();
295 x2 = v2.getX(); y2 = v2.getY();
296 //QString jono = QString("x: %1 y: %2").arg(x1).arg(y1);
297 //QMessageBox::about(0,"Tark",jono);
299 x1Screen = left + (x1-xmin)/(xmax-xmin)*(right-left);
300 y1Screen = top + (ymax-y1)/(ymax-ymin)*(bottom-top);
301 x2Screen = left + (x2-xmin)/(xmax-xmin)*(right-left);
302 y2Screen = top + (ymax-y2)/(ymax-ymin)*(bottom-top);
306 transformseg( &view3d, &v1,&v2, &x1Screen, &y1Screen, &x2Screen, &y2Screen);
309 // Show with circle if starting point
313 startx = x1Screen; starty = y1Screen;
314 // painter.drawEllipse( x1Screen-5, y1Screen-5, 10, 10);
315 drawFlag( this, &painter, x1Screen , y1Screen);
317 painter.drawLine( x1Screen, y1Screen, x2Screen, y2Screen);
319 // Show the endig point if different than the starting point
320 if (x2Screen != startx || y2Screen != starty)
322 //painter.drawEllipse( x2Screen-5, y2Screen-5, 10, 10);
323 drawFlag( this, &painter,x2Screen, y2Screen );
327 qreal maxvx, maxvy; // max speed point coordinates
328 qreal maxv; // max speed
332 for (i=0; i<maxi-1; i++)
334 v = vertexList.at(i);
342 // Translate world coordinates to screen coordinates
343 x1Screen = left + (maxvx-xmin)/(xmax-xmin)*(right-left);
344 y1Screen = top + (ymax-maxvy)/(ymax-ymin)*(bottom-top);
346 // Show max velocity point by yellow circle
347 painter.drawEllipse( x1Screen-5, y1Screen-5, 10, 10);
350 jono = QString("%1 m/s").arg(maxv);
351 ui->maxSpeedLabel->setText(jono);
355 bool RouteDialog::readRouteFromFile( QString &routeFile)
361 QString fileName = QFileDialog::getOpenFileName(this,
362 tr("Read Route"), "./", tr("Route Files (*.txt)"));
364 file.setFileName( fileName);
365 if (!file.open(QIODevice::ReadOnly))
367 QMessageBox::about(0, "Error", "File not found");
374 QString str1, str2, str3, str4;
375 rivi = file.readLine();
377 str1 = rivi.section(" ", 0, 0);
378 if (str1.compare("Start:") != 0 && str1.compare("Stop:") != 0)
380 str1 = rivi.section(" ", 2, 2); // latitude y-value
381 str2 = rivi.section(" ", 4, 4); // longitude x-value
382 str3 = rivi.section(" ", 6, 6); // altitude z-value
383 str4 = rivi.section(" ", 8, 8); // speed m/s
384 //QString str = QString("la: %1 lo: %2 al: %3").arg(str1).arg(str2).arg(str3);
385 //QMessageBox::about(0, "LUKEE", str);
387 if (str1.length() > 0)
394 temp.setX( x); // Longitude
395 temp.setY( y); // Latitude
396 temp.setZ( z); // altitude
399 vertexList.append(temp);
406 /******** in 3d use only */
411 //angle = toradians(60);
413 view3d.setUp( 1.0, 0.0, 0.0);
414 view3d.setAngle(toradians(60));
415 setAtPoint( &view3d);
416 xmin = objxmin; xmax = objxmax; ymin = objymin; ymax = objymax; // 2d viewing needs this !!!!
417 setFromPoint( &view3d);
419 /****** end of 3d *****/
422 //Testing distance counting
425 //a1.setX( xmin); a1.setY( ymin);
426 //a2.setX( xmax); a2.setY( ymax);
427 a1.setX( 25.483); a1.setY( 65.017); // Oulu
428 a2.setX( 27.767); a2.setY( 64.283); // Kajaani
429 dist = countDistance( &a1, &a2);
430 QString str = QString("Min & Max datan välimatka %1").arg(dist);
431 QMessageBox::about( 0, "Testi", str);
438 * Find out data range for x-, y- and z-coordinates
440 void dataMinMax( void)
446 temp = vertexList.at(0);
447 objxmax = objxmin = temp.getX();
448 objymax = objymin = temp.getY();
449 objzmax = objzmin = temp.getZ();
451 maxi = vertexList.size();
452 for (i=1; i<maxi; i++)
454 temp = vertexList.at(i);
463 else if (x > objxmax)
472 else if (y > objymax)
481 else if (z > objzmax)
486 //QString jono = QString("ojxmin %1 objxmax %2").arg(objxmin).arg(objxmax);
487 //QString jono = QString("ojymin %1 objymax %2").arg(objymin).arg(objymax);
488 //QString jono = QString("ojzmin %1 objzmax %2").arg(objzmin).arg(objzmax);
489 //QMessageBox::about(0,"Tark", jono);
493 * Setting the point where the viewed object is. In the middle of datapoints.
495 void setAtPoint( Viewing *v)
501 x = (objxmax+objxmin)/2.0;
502 y= (objymax+objymin)/2.0;
503 z= (objzmax+objzmin)/2.0;
505 v->setAtPoint( x, y, z);
506 //QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(atPoint.x()).arg(atPoint.y()).arg(atPoint.z());
507 //QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(atPoint.x).arg(atPoint.y).arg(atPoint.z);
510 test = v->getAtPoint();
511 QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(test.getX()).arg(test.getY()).arg(test.getZ());
512 QMessageBox::about(0,"At point", jono);
517 * Setting the point where the object is viewed by eye.
519 void setFromPoint( Viewing *v)
523 point = v->getAtPoint();
525 //fromPoint.setX( atPoint.getX() + (objxmax-objxmin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objymax-objymin)/2.0));
527 //x = point.getX() + (objxmax-objxmin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objymax-objymin)/2.0);
530 y = point.getY() + 40; // + (objymax-objymin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objxmax-objxmin)/2.0);
533 v->setFromPoint(x,y,z);
534 //QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(fromPoint.x()).arg(fromPoint.y()).arg(fromPoint.z());
535 //QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(fromPoint.x).arg(fromPoint.y).arg(fromPoint.z);
537 test = v->getFromPoint();
538 QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(test.getX()).arg(test.getY()).arg(test.getZ());
539 QMessageBox::about(0,"From point", jono); // (1.9, 0.5, 0.5)
545 #define LEFTEDGE 0x01
546 #define RIGHTEDGE 0x02
547 #define BOTTOMEDGE 0x04
550 * Returns a code specifying which edge in the viewing pyramid was crossed.
551 * There may be more than one.
553 int code( qreal x, qreal y, qreal z)
558 if (x<-z) c |= LEFTEDGE;
559 if (x>z) c |= RIGHTEDGE;
560 if (y<-z) c |= BOTTOMEDGE;
561 if (y>z) c |= TOPEDGE;
567 * Converts clipped world coordinates to screen coordinates.
569 void WORLDtoSCREEN( qreal xWorld, qreal yWorld, int *xScreen, int *yScreen)
571 *xScreen = (int) (a*xWorld+b);
572 *yScreen = (int) (c*yWorld+d);
576 * Clips the line segment in three-dimensional coordinates to the
579 void clip3d( qreal x1, qreal y1, qreal z1, qreal x2, qreal y2, qreal z2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2)
587 while (c1!= NOEDGE || c2 != NOEDGE)
589 if ((c1 & c2 ) != NOEDGE) return;
591 if (c == NOEDGE) c = c2;
592 if ((c&LEFTEDGE) == LEFTEDGE)
595 t = (z1+x1)/((x1-x2)-(z2-z1));
600 else if ((c&RIGHTEDGE) == RIGHTEDGE)
602 // Crosses right edge
603 t = (z1-x1)/((x2-x1)-(z2-z1));
608 else if ((c&BOTTOMEDGE) == BOTTOMEDGE)
610 // Crosses bottom edge
611 t = (z1+y1)/((y1-y2)-(z2-z1));
616 else if ((c&TOPEDGE) == TOPEDGE)
619 t = (z1-y1)/((y2-y1)-(z2-z1));
639 WORLDtoSCREEN(x1/z1,y1/z1,xscreen1, yscreen1);
640 WORLDtoSCREEN(x2/z2,y2/z2,xscreen2, yscreen2);
644 WORLDtoSCREEN(x1,y1,xscreen1, yscreen1);
645 WORLDtoSCREEN(x2,y2,xscreen2, yscreen2);
647 //Now ready to draw line( xscreen1, yscreen1, xscreen2, yscreen2);
651 * Transform the segment connecting the two vectors into the viewing plane.
652 * clip3d() clips the line if needed.
654 void transformseg( Viewing *v, Vector *v1, Vector *v2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2)
657 qreal x1, y1, z1, x2, y2, z2;
664 x1 = (a1.getX()*v1->getX() + a1.getY()*v1->getY() + a1.getZ()*v1->getZ() + v->getOffsx())*v->getDval();
665 y1 = (a2.getX()*v1->getX() + a2.getY()*v1->getY() + a2.getZ()*v1->getZ() + v->getOffsy())*v->getDval();
666 z1 = a3.getX()*v1->getX() + a3.getY()*v1->getY() + a3.getZ()*v1->getZ() + v->getOffsz();
668 x2 = (a1.getX()*v2->getX() + a1.getY()*v2->getY() + a1.getZ()*v2->getZ() + v->getOffsx())*v->getDval();
669 y2 = (a2.getX()*v2->getX() + a2.getY()*v2->getY() + a2.getZ()*v2->getZ() + v->getOffsy())*v->getDval();
670 z2 = a3.getX()*v2->getX() + a3.getY()*v2->getY() + a3.getZ()*v2->getZ() + v->getOffsz();
672 clip3d(x1,y1,z1,x2,y2,z2, xscreen1, yscreen1, xscreen2, yscreen2 );
675 void RouteDialog::on_newPushButton_clicked()
677 close(); // go back to previous dialog
680 void RouteDialog::on_sendPushButton_clicked()
682 // Send route points file to server