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 this->setWindowTitle("Route");
171 left = 5; top = 5; right = 495; bottom = 295; // Limits in screen coordinates
174 RouteDialog::~RouteDialog()
179 void RouteDialog::changeEvent(QEvent *e)
181 QDialog::changeEvent(e);
183 case QEvent::LanguageChange:
184 ui->retranslateUi(this);
190 int RouteDialog::getLeft()
194 int RouteDialog::getRight()
198 int RouteDialog::getTop()
202 int RouteDialog::getBottom()
207 void drawFlag( RouteDialog *rD, QPainter *p, int x, int y)
211 pg.setPoint(0,x, y-25);
212 pg.setPoint(1,x+10,y-20);
213 pg.setPoint(2,x, y-15);
214 pg.setPoint(3,x,y-20);*/
215 if (y> (rD->getTop() + 25))
218 p->drawLine(x,y,x,y-15);
219 if (x <= (rD->getRight()-20))
222 p->drawLine( x, y-25, x+10, y-20);
223 p->drawLine( x+10, y-20, x, y-15);
224 p->drawLine( x, y-15, x, y-25);
229 p->drawLine( x, y-25, x-10, y-20);
230 p->drawLine( x-10, y-20, x, y-15);
231 p->drawLine( x, y-15, x, y-25);
235 else if (y <= (rD->getTop() + 25))
238 p->drawLine(x,y,x,y+15);
239 if (x <= (rD->getRight()-20))
242 p->drawLine( x, y+25, x+10, y+20);
243 p->drawLine( x+10, y+20, x, y+15);
244 p->drawLine( x, y+15, x, y+25);
249 p->drawLine( x, y+25, x-10, y+20);
250 p->drawLine( x-10, y+20, x, y+15);
251 p->drawLine( x, y+15, x, y+25);
255 // p->drawPolygon( pg,Qt::OddEvenFill);
256 //p->drawPolyline( &pg);
257 //p->drawPoints( pg);
261 * Draws route to the route dialog.
262 * Type 0 is 2d viewing and type 1 is for 3d viewing
266 void RouteDialog::paintEvent(QPaintEvent *)
268 int type = 0; // 0 for 2d, 1 for 3d
269 int startx, starty; // Starting point of the route
271 qreal x1, y1, x2, y2;
272 int x1Screen, y1Screen, x2Screen, y2Screen;
274 QPainter painter(this);
276 painter.setRenderHint(QPainter::Antialiasing, true);
277 painter.setPen(QPen((Qt::white),2));
278 painter.setBrush(QBrush((Qt::yellow), Qt::SolidPattern));
280 // Draw route window frame
281 /*painter.drawLine(left,top,right,top);
282 painter.drawLine(right,top,right,bottom);
283 painter.drawLine(left,top,left,bottom);
284 painter.drawLine(left,bottom,right,bottom);*/
286 maxi = vertexList.size();
288 for (i=0; i<maxi-1; i++)
290 v1 = vertexList.at(i);
291 v2 = vertexList.at(i+1);
295 x1 = v1.getX(); y1 = v1.getY();
296 x2 = v2.getX(); y2 = v2.getY();
297 //QString jono = QString("x: %1 y: %2").arg(x1).arg(y1);
298 //QMessageBox::about(0,"Tark",jono);
300 x1Screen = left + (x1-xmin)/(xmax-xmin)*(right-left);
301 y1Screen = top + (ymax-y1)/(ymax-ymin)*(bottom-top);
302 x2Screen = left + (x2-xmin)/(xmax-xmin)*(right-left);
303 y2Screen = top + (ymax-y2)/(ymax-ymin)*(bottom-top);
307 transformseg( &view3d, &v1,&v2, &x1Screen, &y1Screen, &x2Screen, &y2Screen);
310 // Show with circle if starting point
314 startx = x1Screen; starty = y1Screen;
315 // painter.drawEllipse( x1Screen-5, y1Screen-5, 10, 10);
316 drawFlag( this, &painter, x1Screen , y1Screen);
318 painter.drawLine( x1Screen, y1Screen, x2Screen, y2Screen);
320 // Show the endig point if different than the starting point
321 if (x2Screen != startx || y2Screen != starty)
323 //painter.drawEllipse( x2Screen-5, y2Screen-5, 10, 10);
324 drawFlag( this, &painter,x2Screen, y2Screen );
328 qreal maxvx, maxvy; // max speed point coordinates
329 qreal maxv; // max speed
333 for (i=0; i<maxi-1; i++)
335 v = vertexList.at(i);
343 // Translate world coordinates to screen coordinates
344 x1Screen = left + (maxvx-xmin)/(xmax-xmin)*(right-left);
345 y1Screen = top + (ymax-maxvy)/(ymax-ymin)*(bottom-top);
347 // Show max velocity point by yellow circle
348 painter.drawEllipse( x1Screen-5, y1Screen-5, 10, 10);
351 jono = QString("%1 m/s").arg(maxv);
352 ui->speedValueLabel->setText(jono);
356 bool RouteDialog::readRouteFromFile( QString &routeFile)
362 QString fileName = QFileDialog::getOpenFileName(this,
363 tr("Read Route"), "./", tr("Route Files (*.txt)"));
365 file.setFileName( fileName);
366 if (!file.open(QIODevice::ReadOnly))
368 QMessageBox::about(0, "Error", "File not found");
375 QString str1, str2, str3, str4;
376 rivi = file.readLine();
378 str1 = rivi.section(" ", 0, 0);
379 if (str1.compare("Start:") != 0 && str1.compare("Stop:") != 0)
381 str1 = rivi.section(" ", 2, 2); // latitude y-value
382 str2 = rivi.section(" ", 4, 4); // longitude x-value
383 str3 = rivi.section(" ", 6, 6); // altitude z-value
384 str4 = rivi.section(" ", 8, 8); // speed m/s
385 //QString str = QString("la: %1 lo: %2 al: %3").arg(str1).arg(str2).arg(str3);
386 //QMessageBox::about(0, "LUKEE", str);
388 if (str1.length() > 0)
395 temp.setX( x); // Longitude
396 temp.setY( y); // Latitude
397 temp.setZ( z); // altitude
400 vertexList.append(temp);
407 /******** in 3d use only */
412 //angle = toradians(60);
414 view3d.setUp( 1.0, 0.0, 0.0);
415 view3d.setAngle(toradians(60));
416 setAtPoint( &view3d);
417 xmin = objxmin; xmax = objxmax; ymin = objymin; ymax = objymax; // 2d viewing needs this !!!!
418 setFromPoint( &view3d);
420 /****** end of 3d *****/
423 //Testing distance counting
426 //a1.setX( xmin); a1.setY( ymin);
427 //a2.setX( xmax); a2.setY( ymax);
428 a1.setX( 25.483); a1.setY( 65.017); // Oulu
429 a2.setX( 27.767); a2.setY( 64.283); // Kajaani
430 dist = countDistance( &a1, &a2);
431 QString str = QString("Min & Max datan välimatka %1").arg(dist);
432 QMessageBox::about( 0, "Testi", str);
439 * Find out data range for x-, y- and z-coordinates
441 void dataMinMax( void)
447 temp = vertexList.at(0);
448 objxmax = objxmin = temp.getX();
449 objymax = objymin = temp.getY();
450 objzmax = objzmin = temp.getZ();
452 maxi = vertexList.size();
453 for (i=1; i<maxi; i++)
455 temp = vertexList.at(i);
464 else if (x > objxmax)
473 else if (y > objymax)
482 else if (z > objzmax)
487 //QString jono = QString("ojxmin %1 objxmax %2").arg(objxmin).arg(objxmax);
488 //QString jono = QString("ojymin %1 objymax %2").arg(objymin).arg(objymax);
489 //QString jono = QString("ojzmin %1 objzmax %2").arg(objzmin).arg(objzmax);
490 //QMessageBox::about(0,"Tark", jono);
494 * Setting the point where the viewed object is. In the middle of datapoints.
496 void setAtPoint( Viewing *v)
502 x = (objxmax+objxmin)/2.0;
503 y= (objymax+objymin)/2.0;
504 z= (objzmax+objzmin)/2.0;
506 v->setAtPoint( x, y, z);
507 //QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(atPoint.x()).arg(atPoint.y()).arg(atPoint.z());
508 //QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(atPoint.x).arg(atPoint.y).arg(atPoint.z);
511 test = v->getAtPoint();
512 QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(test.getX()).arg(test.getY()).arg(test.getZ());
513 QMessageBox::about(0,"At point", jono);
518 * Setting the point where the object is viewed by eye.
520 void setFromPoint( Viewing *v)
524 point = v->getAtPoint();
526 //fromPoint.setX( atPoint.getX() + (objxmax-objxmin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objymax-objymin)/2.0));
528 //x = point.getX() + (objxmax-objxmin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objymax-objymin)/2.0);
531 y = point.getY() + 40; // + (objymax-objymin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objxmax-objxmin)/2.0);
534 v->setFromPoint(x,y,z);
535 //QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(fromPoint.x()).arg(fromPoint.y()).arg(fromPoint.z());
536 //QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(fromPoint.x).arg(fromPoint.y).arg(fromPoint.z);
538 test = v->getFromPoint();
539 QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(test.getX()).arg(test.getY()).arg(test.getZ());
540 QMessageBox::about(0,"From point", jono); // (1.9, 0.5, 0.5)
546 #define LEFTEDGE 0x01
547 #define RIGHTEDGE 0x02
548 #define BOTTOMEDGE 0x04
551 * Returns a code specifying which edge in the viewing pyramid was crossed.
552 * There may be more than one.
554 int code( qreal x, qreal y, qreal z)
559 if (x<-z) c |= LEFTEDGE;
560 if (x>z) c |= RIGHTEDGE;
561 if (y<-z) c |= BOTTOMEDGE;
562 if (y>z) c |= TOPEDGE;
568 * Converts clipped world coordinates to screen coordinates.
570 void WORLDtoSCREEN( qreal xWorld, qreal yWorld, int *xScreen, int *yScreen)
572 *xScreen = (int) (a*xWorld+b);
573 *yScreen = (int) (c*yWorld+d);
577 * Clips the line segment in three-dimensional coordinates to the
580 void clip3d( qreal x1, qreal y1, qreal z1, qreal x2, qreal y2, qreal z2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2)
588 while (c1!= NOEDGE || c2 != NOEDGE)
590 if ((c1 & c2 ) != NOEDGE) return;
592 if (c == NOEDGE) c = c2;
593 if ((c&LEFTEDGE) == LEFTEDGE)
596 t = (z1+x1)/((x1-x2)-(z2-z1));
601 else if ((c&RIGHTEDGE) == RIGHTEDGE)
603 // Crosses right edge
604 t = (z1-x1)/((x2-x1)-(z2-z1));
609 else if ((c&BOTTOMEDGE) == BOTTOMEDGE)
611 // Crosses bottom edge
612 t = (z1+y1)/((y1-y2)-(z2-z1));
617 else if ((c&TOPEDGE) == TOPEDGE)
620 t = (z1-y1)/((y2-y1)-(z2-z1));
640 WORLDtoSCREEN(x1/z1,y1/z1,xscreen1, yscreen1);
641 WORLDtoSCREEN(x2/z2,y2/z2,xscreen2, yscreen2);
645 WORLDtoSCREEN(x1,y1,xscreen1, yscreen1);
646 WORLDtoSCREEN(x2,y2,xscreen2, yscreen2);
648 //Now ready to draw line( xscreen1, yscreen1, xscreen2, yscreen2);
652 * Transform the segment connecting the two vectors into the viewing plane.
653 * clip3d() clips the line if needed.
655 void transformseg( Viewing *v, Vector *v1, Vector *v2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2)
658 qreal x1, y1, z1, x2, y2, z2;
665 x1 = (a1.getX()*v1->getX() + a1.getY()*v1->getY() + a1.getZ()*v1->getZ() + v->getOffsx())*v->getDval();
666 y1 = (a2.getX()*v1->getX() + a2.getY()*v1->getY() + a2.getZ()*v1->getZ() + v->getOffsy())*v->getDval();
667 z1 = a3.getX()*v1->getX() + a3.getY()*v1->getY() + a3.getZ()*v1->getZ() + v->getOffsz();
669 x2 = (a1.getX()*v2->getX() + a1.getY()*v2->getY() + a1.getZ()*v2->getZ() + v->getOffsx())*v->getDval();
670 y2 = (a2.getX()*v2->getX() + a2.getY()*v2->getY() + a2.getZ()*v2->getZ() + v->getOffsy())*v->getDval();
671 z2 = a3.getX()*v2->getX() + a3.getY()*v2->getY() + a3.getZ()*v2->getZ() + v->getOffsz();
673 clip3d(x1,y1,z1,x2,y2,z2, xscreen1, yscreen1, xscreen2, yscreen2 );
676 void RouteDialog::on_newPushButton_clicked()
678 close(); // go back to previous dialog
681 void RouteDialog::on_sendPushButton_clicked()
683 // Send route points file to server